-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistener.go
132 lines (116 loc) · 2.62 KB
/
listener.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
package telly
import (
"net"
"time"
)
const (
READ_STATE_NORMAL = 1
READ_STATE_COMMAND = 2
READ_STATE_SUBNEG = 3
TN_INTERPRET_AS_COMMAND = 255
TN_ARE_YOU_THERE = 246
TN_WILL = 251
TN_WONT = 252
TN_DO = 253
TN_DONT = 254
TN_SUBNEGOTIATION_START = 250
TN_SUBNEGOTIATION_END = 240
)
// A Listener is a telnet server listener.
type Listener struct {
listener net.Listener
timeout time.Duration
}
// Listen starts a new telnet listener that can accept connections.
func Listen(bind string) (*Listener, error) {
listener, err := net.Listen("tcp4", bind)
if err != nil {
return nil, err
}
return &Listener{
listener: listener,
timeout: 0,
}, nil
}
// SetTimeout set's the duration that the server will wait for data before closing
// the connection. If you don't set a timeout it's easy to get overflowed by idle
// connections.
func (l *Listener) SetTimeout(dur time.Duration) {
l.timeout = dur
}
// Accept waits for and returns the next connection to the listener.
func (l *Listener) Accept() (*Conn, error) {
conn, err := l.listener.Accept()
if err != nil {
return nil, err
}
telCon := &Conn{
conn: conn,
}
go func() {
state := READ_STATE_NORMAL
buf := make([]byte, 2048)
curMsg := ""
for {
if int64(l.timeout) > 0 {
if err := conn.SetReadDeadline(time.Now().Add(l.timeout)); err != nil {
break
}
}
read, err := conn.Read(buf)
if err != nil {
break
}
state = READ_STATE_NORMAL
for i := 0; i < read; i++ {
switch state {
case READ_STATE_NORMAL:
if buf[i] == TN_INTERPRET_AS_COMMAND {
state = READ_STATE_COMMAND
} else if buf[i] == '\n' {
telCon.Lock()
if telCon.msgHandler != nil {
telCon.msgHandler(telCon, curMsg)
}
telCon.Unlock()
curMsg = ""
} else if buf[i] == '\x08' {
if len(curMsg) > 0 {
curMsg = curMsg[:len(curMsg)-1]
}
} else {
curMsg += string(buf[i])
}
case READ_STATE_COMMAND:
if buf[i] == TN_SUBNEGOTIATION_START {
state = READ_STATE_SUBNEG
} else {
switch buf[i] {
case TN_WILL:
fallthrough
case TN_WONT:
fallthrough
case TN_DO:
fallthrough
case TN_DONT:
state = READ_STATE_COMMAND
default:
state = READ_STATE_NORMAL
}
}
case READ_STATE_SUBNEG:
if buf[i] == TN_SUBNEGOTIATION_END {
state = READ_STATE_NORMAL
}
}
}
}
_ = conn.Close()
telCon.Lock()
if telCon.disHandler != nil {
telCon.disHandler(telCon)
}
telCon.Unlock()
}()
return telCon, err
}