-
Notifications
You must be signed in to change notification settings - Fork 13
/
telnet.go
477 lines (404 loc) · 10.5 KB
/
telnet.go
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package telnet
import (
"fmt"
"net"
"strconv"
"time"
)
// RFC 854: http://tools.ietf.org/html/rfc854, http://support.microsoft.com/kb/231866
var byteToCode map[byte]TelnetCode
var codeToByte map[TelnetCode]byte
type TelnetCode int
func init() {
byteToCode = map[byte]TelnetCode{}
codeToByte = map[TelnetCode]byte{}
codeToByte[NUL] = '\x00'
codeToByte[ECHO] = '\x01'
codeToByte[SGA] = '\x03'
codeToByte[ST] = '\x05'
codeToByte[TM] = '\x06'
codeToByte[BEL] = '\x07'
codeToByte[BS] = '\x08'
codeToByte[HT] = '\x09'
codeToByte[LF] = '\x0a'
codeToByte[FF] = '\x0c'
codeToByte[CR] = '\x0d'
codeToByte[TT] = '\x18'
codeToByte[WS] = '\x1F'
codeToByte[TS] = '\x20'
codeToByte[RFC] = '\x21'
codeToByte[LM] = '\x22'
codeToByte[EV] = '\x24'
codeToByte[SE] = '\xf0'
codeToByte[NOP] = '\xf1'
codeToByte[DM] = '\xf2'
codeToByte[BRK] = '\xf3'
codeToByte[IP] = '\xf4'
codeToByte[AO] = '\xf5'
codeToByte[AYT] = '\xf6'
codeToByte[EC] = '\xf7'
codeToByte[EL] = '\xf8'
codeToByte[GA] = '\xf9'
codeToByte[SB] = '\xfa'
codeToByte[WILL] = '\xfb'
codeToByte[WONT] = '\xfc'
codeToByte[DO] = '\xfd'
codeToByte[DONT] = '\xfe'
codeToByte[IAC] = '\xff'
codeToByte[CMP1] = '\x55'
codeToByte[CMP2] = '\x56'
codeToByte[AARD] = '\x66'
codeToByte[ATCP] = '\xc8'
codeToByte[GMCP] = '\xc9'
for enum, code := range codeToByte {
byteToCode[code] = enum
}
}
// Telnet wraps the given connection object, processing telnet codes from its byte
// stream and interpreting them as necessary, making it possible to hand the connection
// object off to other code so that it doesn't have to worry about telnet escape sequences
// being found in its data.
type Telnet struct {
conn net.Conn
err error
processor *telnetProcessor
}
func NewTelnet(conn net.Conn) *Telnet {
var t Telnet
t.conn = conn
t.processor = newTelnetProcessor()
return &t
}
func (t *Telnet) Write(p []byte) (int, error) {
return t.conn.Write(p)
}
func (t *Telnet) Read(p []byte) (int, error) {
for {
t.fill()
if t.err != nil {
return 0, t.err
}
n, err := t.processor.Read(p)
if n > 0 {
return n, err
}
}
}
func (t *Telnet) Data(code TelnetCode) []byte {
return t.processor.subdata[code]
}
func (t *Telnet) Listen(listenFunc func(TelnetCode, []byte)) {
t.processor.listenFunc = listenFunc
}
// Idea/name for this function shamelessly stolen from bufio
func (t *Telnet) fill() {
buf := make([]byte, 1024)
n, err := t.conn.Read(buf)
t.err = err
t.processor.addBytes(buf[:n])
}
func (t *Telnet) Close() error {
return t.conn.Close()
}
func (t *Telnet) LocalAddr() net.Addr {
return t.conn.LocalAddr()
}
func (t *Telnet) RemoteAddr() net.Addr {
return t.conn.RemoteAddr()
}
func (t *Telnet) SetDeadline(dl time.Time) error {
return t.conn.SetDeadline(dl)
}
func (t *Telnet) SetReadDeadline(dl time.Time) error {
return t.conn.SetReadDeadline(dl)
}
func (t *Telnet) SetWriteDeadline(dl time.Time) error {
return t.conn.SetWriteDeadline(dl)
}
func (t *Telnet) WillEcho() {
t.SendCommand(WILL, ECHO)
}
func (t *Telnet) WontEcho() {
t.SendCommand(WONT, ECHO)
}
func (t *Telnet) DoWindowSize() {
t.SendCommand(DO, WS)
}
func (t *Telnet) DoTerminalType() {
// This is really supposed to be two commands, one to ask if they'll send a
// terminal type, and another to indicate that they should send it if
// they've expressed a "willingness" to send it. For the time being this
// works well enough.
// See http://tools.ietf.org/html/rfc884
t.SendCommand(DO, TT, IAC, SB, TT, 1, IAC, SE) // 1 = SEND
}
func (t *Telnet) SendCommand(codes ...TelnetCode) {
t.conn.Write(BuildCommand(codes...))
}
func BuildCommand(codes ...TelnetCode) []byte {
command := make([]byte, len(codes)+1)
command[0] = codeToByte[IAC]
for i, code := range codes {
command[i+1] = codeToByte[code]
}
return command
}
const (
NUL TelnetCode = iota // NULL, no operation
ECHO TelnetCode = iota // Echo
SGA TelnetCode = iota // Suppress go ahead
ST TelnetCode = iota // Status
TM TelnetCode = iota // Timing mark
BEL TelnetCode = iota // Bell
BS TelnetCode = iota // Backspace
HT TelnetCode = iota // Horizontal tab
LF TelnetCode = iota // Line feed
FF TelnetCode = iota // Form feed
CR TelnetCode = iota // Carriage return
TT TelnetCode = iota // Terminal type
WS TelnetCode = iota // Window size
TS TelnetCode = iota // Terminal speed
RFC TelnetCode = iota // Remote flow control
LM TelnetCode = iota // Line mode
EV TelnetCode = iota // Environment variables
SE TelnetCode = iota // End of subnegotiation parameters.
NOP TelnetCode = iota // No operation.
DM TelnetCode = iota // Data Mark. The data stream portion of a Synch. This should always be accompanied by a TCP Urgent notification.
BRK TelnetCode = iota // Break. NVT character BRK.
IP TelnetCode = iota // Interrupt Process
AO TelnetCode = iota // Abort output
AYT TelnetCode = iota // Are you there
EC TelnetCode = iota // Erase character
EL TelnetCode = iota // Erase line
GA TelnetCode = iota // Go ahead signal
SB TelnetCode = iota // Indicates that what follows is subnegotiation of the indicated option.
WILL TelnetCode = iota // Indicates the desire to begin performing, or confirmation that you are now performing, the indicated option.
WONT TelnetCode = iota // Indicates the refusal to perform, or continue performing, the indicated option.
DO TelnetCode = iota // Indicates the request that the other party perform, or confirmation that you are expecting the other party to perform, the indicated option.
DONT TelnetCode = iota // Indicates the demand that the other party stop performing, or confirmation that you are no longer expecting the other party to perform, the indicated option.
IAC TelnetCode = iota // Interpret as command
// Non-standard codes:
CMP1 TelnetCode = iota // MCCP Compress
CMP2 TelnetCode = iota // MCCP Compress2
AARD TelnetCode = iota // Aardwolf MUD out of band communication, http://www.aardwolf.com/blog/2008/07/10/telnet-negotiation-control-mud-client-interaction/
ATCP TelnetCode = iota // Achaea Telnet Client Protocol, http://www.ironrealms.com/rapture/manual/files/FeatATCP-txt.html
GMCP TelnetCode = iota // Generic Mud Communication Protocol
)
type processorState int
const (
stateBase processorState = iota
stateInIAC processorState = iota
stateInSB processorState = iota
stateCapSB processorState = iota
stateEscIAC processorState = iota
)
// telnetProcessor implements a state machine that reads input one byte at a time
// and processes it according to the telnet spec. It is designed to read a raw telnet
// stream, from which it will extract telnet escape codes and subnegotiation data.
// The processor can then be read from with all of the telnet codes removed, leaving
// the pure user input stream.
type telnetProcessor struct {
state processorState
currentSB TelnetCode
capturedBytes []byte
subdata map[TelnetCode][]byte
cleanData string
listenFunc func(TelnetCode, []byte)
debug bool
}
func newTelnetProcessor() *telnetProcessor {
var tp telnetProcessor
tp.state = stateBase
tp.debug = false
tp.currentSB = NUL
return &tp
}
func (self *telnetProcessor) Read(p []byte) (int, error) {
maxLen := len(p)
n := 0
if maxLen >= len(self.cleanData) {
n = len(self.cleanData)
} else {
n = maxLen
}
for i := 0; i < n; i++ {
p[i] = self.cleanData[i]
}
self.cleanData = self.cleanData[n:] // TODO: Memory leak?
return n, nil
}
func (self *telnetProcessor) capture(b byte) {
if self.debug {
fmt.Println("Captured:", ByteToCodeString(b))
}
self.capturedBytes = append(self.capturedBytes, b)
}
func (self *telnetProcessor) dontCapture(b byte) {
self.cleanData = self.cleanData + string(b)
}
func (self *telnetProcessor) resetSubDataField(code TelnetCode) {
if self.subdata == nil {
self.subdata = map[TelnetCode][]byte{}
}
self.subdata[code] = []byte{}
}
func (self *telnetProcessor) captureSubData(code TelnetCode, b byte) {
if self.debug {
fmt.Println("Captured subdata:", CodeToString(code), b)
}
if self.subdata == nil {
self.subdata = map[TelnetCode][]byte{}
}
self.subdata[code] = append(self.subdata[code], b)
}
func (self *telnetProcessor) addBytes(bytes []byte) {
for _, b := range bytes {
self.addByte(b)
}
}
func (self *telnetProcessor) addByte(b byte) {
code := byteToCode[b]
switch self.state {
case stateBase:
if code == IAC {
self.state = stateInIAC
self.capture(b)
} else {
self.dontCapture(b)
}
case stateInIAC:
if code == WILL || code == WONT || code == DO || code == DONT {
// Stay in this state
} else if code == SB {
self.state = stateInSB
} else {
self.state = stateBase
}
self.capture(b)
case stateInSB:
self.capture(b)
self.currentSB = code
self.state = stateCapSB
self.resetSubDataField(code)
case stateCapSB:
if code == IAC {
self.state = stateEscIAC
} else {
self.captureSubData(self.currentSB, b)
}
case stateEscIAC:
if code == IAC {
self.state = stateCapSB
self.captureSubData(self.currentSB, b)
} else {
self.subDataFinished(self.currentSB)
self.currentSB = NUL
self.state = stateBase
self.addByte(codeToByte[IAC])
self.addByte(b)
}
}
}
func (self *telnetProcessor) subDataFinished(code TelnetCode) {
if self.listenFunc != nil {
self.listenFunc(code, self.subdata[code])
}
}
func ToString(bytes []byte) string {
str := ""
for _, b := range bytes {
if str != "" {
str = str + " "
}
str = str + ByteToCodeString(b)
}
return str
}
func ByteToCodeString(b byte) string {
code, found := byteToCode[b]
if !found {
return "??(" + strconv.Itoa(int(b)) + ")"
}
return CodeToString(code)
}
func CodeToString(code TelnetCode) string {
switch code {
case NUL:
return "NUL"
case ECHO:
return "ECHO"
case SGA:
return "SGA"
case ST:
return "ST"
case TM:
return "TM"
case BEL:
return "BEL"
case BS:
return "BS"
case HT:
return "HT"
case LF:
return "LF"
case FF:
return "FF"
case CR:
return "CR"
case TT:
return "TT"
case WS:
return "WS"
case TS:
return "TS"
case RFC:
return "RFC"
case LM:
return "LM"
case EV:
return "EV"
case SE:
return "SE"
case NOP:
return "NOP"
case DM:
return "DM"
case BRK:
return "BRK"
case IP:
return "IP"
case AO:
return "AO"
case AYT:
return "AYT"
case EC:
return "EC"
case EL:
return "EL"
case GA:
return "GA"
case SB:
return "SB"
case WILL:
return "WILL"
case WONT:
return "WONT"
case DO:
return "DO"
case DONT:
return "DONT"
case IAC:
return "IAC"
case CMP1:
return "CMP1"
case CMP2:
return "CMP2"
case AARD:
return "AARD"
case ATCP:
return "ATCP"
case GMCP:
return "GMCP"
}
return ""
}