-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathSSHPTYAttachReconnectInputFilter.swift
More file actions
244 lines (221 loc) · 7.93 KB
/
Copy pathSSHPTYAttachReconnectInputFilter.swift
File metadata and controls
244 lines (221 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import Darwin
import Foundation
final class SSHPTYAttachReconnectInputFilter {
private enum SequenceMatch {
case strip(length: Int)
case incomplete
case passThrough
}
private static let escape: UInt8 = 0x1B
private static let bell: UInt8 = 0x07
private static let leftBracket: UInt8 = 0x5B
private static let rightBracket: UInt8 = 0x5D
private static let backslash: UInt8 = 0x5C
private static let semicolon: UInt8 = 0x3B
private static let questionMark: UInt8 = 0x3F
private static let dollar: UInt8 = 0x24
private static let maxPendingProbeBytes = 512
private var isFiltering: Bool
private var pending = [UInt8]()
init(enabled: Bool) {
isFiltering = enabled
}
static func startStdinPump(fd: Int32, filterEnabled: Bool) {
DispatchQueue.global(qos: .userInteractive).async {
let reconnectInputFilter = SSHPTYAttachReconnectInputFilter(enabled: filterEnabled)
var buffer = [UInt8](repeating: 0, count: 8192)
while true {
let count = Darwin.read(STDIN_FILENO, &buffer, buffer.count)
if count > 0 {
let input = reconnectInputFilter.filter(Data(buffer.prefix(count)))
guard !input.isEmpty else {
continue
}
do {
try Self.writeAll(fd: fd, data: input)
} catch {
_ = shutdown(fd, SHUT_WR)
return
}
} else if count == 0 {
let input = reconnectInputFilter.finish()
if !input.isEmpty {
do {
try Self.writeAll(fd: fd, data: input)
} catch {
_ = shutdown(fd, SHUT_WR)
return
}
}
_ = shutdown(fd, SHUT_WR)
return
} else if errno != EINTR {
_ = shutdown(fd, SHUT_WR)
return
}
}
}
}
func filter(_ data: Data) -> Data {
guard isFiltering, !data.isEmpty else {
return data
}
var bytes = pending
pending.removeAll(keepingCapacity: true)
bytes.append(contentsOf: data)
var output = Data()
var index = 0
while index < bytes.count {
guard bytes[index] == Self.escape else {
isFiltering = false
output.append(contentsOf: bytes[index...])
return output
}
switch Self.reconnectProbeReplySequence(in: bytes, at: index) {
case .strip(let length):
index += length
case .incomplete:
let suffix = bytes[index...]
guard suffix.count <= Self.maxPendingProbeBytes else {
isFiltering = false
output.append(contentsOf: suffix)
return output
}
pending.append(contentsOf: suffix)
return output
case .passThrough:
isFiltering = false
output.append(contentsOf: bytes[index...])
return output
}
}
return output
}
func finish() -> Data {
guard !pending.isEmpty else {
return Data()
}
let data = Data(pending)
pending.removeAll(keepingCapacity: false)
return data
}
private static func reconnectProbeReplySequence(in bytes: [UInt8], at start: Int) -> SequenceMatch {
guard start < bytes.count, bytes[start] == escape else {
return .passThrough
}
guard start + 1 < bytes.count else {
return .passThrough
}
switch bytes[start + 1] {
case rightBracket:
return oscColorReplySequence(in: bytes, at: start)
case leftBracket:
return csiProbeReplySequence(in: bytes, at: start)
default:
return .passThrough
}
}
private static func oscColorReplySequence(in bytes: [UInt8], at start: Int) -> SequenceMatch {
var cursor = start + 2
var command = [UInt8]()
while cursor < bytes.count {
let byte = bytes[cursor]
if byte == semicolon {
break
}
if byte < 0x30 || byte > 0x39 || command.count >= 2 {
return .passThrough
}
command.append(byte)
cursor += 1
}
guard cursor < bytes.count else {
return isOSCColorReplyCommandPrefix(command) ? .incomplete : .passThrough
}
guard bytes[cursor] == semicolon else {
return .passThrough
}
guard command == [0x31, 0x30] || command == [0x31, 0x31] else {
return .passThrough
}
cursor += 1
while cursor < bytes.count {
let byte = bytes[cursor]
if byte == bell {
return .strip(length: cursor - start + 1)
}
if byte == escape {
guard cursor + 1 < bytes.count else {
return .incomplete
}
if bytes[cursor + 1] == backslash {
return .strip(length: cursor - start + 2)
}
}
cursor += 1
}
return .incomplete
}
private static func csiProbeReplySequence(in bytes: [UInt8], at start: Int) -> SequenceMatch {
var cursor = start + 2
while cursor < bytes.count {
let byte = bytes[cursor]
if byte >= 0x40, byte <= 0x7E {
return shouldStripCSIReply(bytes: bytes, bodyStart: start + 2, finalIndex: cursor)
? .strip(length: cursor - start + 1)
: .passThrough
}
guard byte >= 0x20, byte <= 0x3F else {
return .passThrough
}
cursor += 1
}
return .incomplete
}
private static func isOSCColorReplyCommandPrefix(_ command: [UInt8]) -> Bool {
command.isEmpty ||
command == [0x31] ||
command == [0x31, 0x30] ||
command == [0x31, 0x31]
}
private static func shouldStripCSIReply(bytes: [UInt8], bodyStart: Int, finalIndex: Int) -> Bool {
var parameterEnd = bodyStart
while parameterEnd < finalIndex, bytes[parameterEnd] >= 0x30, bytes[parameterEnd] <= 0x3F {
parameterEnd += 1
}
guard bytes[parameterEnd..<finalIndex].allSatisfy({ $0 >= 0x20 && $0 <= 0x2F }) else {
return false
}
let parameters = bytes[bodyStart..<parameterEnd]
let intermediates = bytes[parameterEnd..<finalIndex]
let final = bytes[finalIndex]
switch final {
case 0x52, 0x63, 0x6E:
return intermediates.isEmpty
case 0x75:
return intermediates.isEmpty && parameters.first == questionMark
case 0x79:
return intermediates.elementsEqual([dollar])
default:
return false
}
}
private static func writeAll(fd: Int32, data: Data) throws {
try data.withUnsafeBytes { rawBuffer in
guard let base = rawBuffer.bindMemory(to: UInt8.self).baseAddress else { return }
var remaining = rawBuffer.count
var cursor = base
while remaining > 0 {
let written = Darwin.write(fd, cursor, remaining)
if written > 0 {
remaining -= written
cursor = cursor.advanced(by: written)
} else if written < 0 && errno == EINTR {
continue
} else {
throw POSIXError(.EIO)
}
}
}
}
}