forked from jumpserver/koko
/
conn_telnet.go
231 lines (201 loc) · 4.79 KB
/
conn_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
package srvconn
import (
"io"
"net"
"regexp"
"strconv"
"sync"
"time"
"github.com/LeeEirc/tclientlib"
"golang.org/x/text/transform"
"github.com/meowgen/koko/pkg/common"
)
func NewTelnetConnection(opts ...TelnetOption) (*TelnetConnection, error) {
cfg := &TelnetConfig{
Host: "127.0.0.1",
Port: "23",
Term: "xterm",
Timeout: 15,
win: Windows{
Width: 80,
Height: 120,
},
CustomSuccessPattern: tclientlib.DefaultLoginSuccessPattern,
}
for _, setter := range opts {
setter(cfg)
}
var (
conn net.Conn
err error
proxyClient *SSHClient
client *tclientlib.Client
)
dstAddr := net.JoinHostPort(cfg.Host, cfg.Port)
if cfg.proxySSHClientOptions != nil {
if proxyClient, err = getAvailableProxyClient(cfg.proxySSHClientOptions...); err != nil {
return nil, err
}
if conn, err = proxyClient.Dial("tcp", dstAddr); err != nil {
_ = proxyClient.Close()
return nil, err
}
} else {
if conn, err = net.DialTimeout("tcp", dstAddr, cfg.Timeout); err != nil {
return nil, err
}
}
client, err = newTelnetClient(conn, cfg)
if err != nil {
if proxyClient != nil {
_ = proxyClient.Close()
}
return nil, err
}
var (
transformReader io.Reader
transformWriter io.WriteCloser
)
transformReader = client
transformWriter = client
if cfg.Charset != common.UTF8 {
if readDecode := common.LookupCharsetDecode(cfg.Charset); readDecode != nil {
transformReader = transform.NewReader(client, readDecode)
}
if writerEncode := common.LookupCharsetEncode(cfg.Charset); writerEncode != nil {
transformWriter = transform.NewWriter(client, writerEncode)
}
}
return &TelnetConnection{
cfg: cfg,
conn: client,
proxyConn: proxyClient,
transformReader: transformReader,
transformWriter: transformWriter,
}, nil
}
func newTelnetClient(conn net.Conn, cfg *TelnetConfig) (*tclientlib.Client, error) {
// 修复未登录成功,但是telnet连接不断的问题。
// todo:检测超时,直接断开连接,后续通过更新 telnet 包解决
done := make(chan struct{})
go func() {
t := time.NewTicker(cfg.Timeout)
defer t.Stop()
select {
case <-t.C:
select {
case <-done:
default:
_ = conn.Close()
}
case <-done:
return
}
}()
client, err := tclientlib.NewClientConn(conn, &tclientlib.Config{
Username: cfg.Username,
Password: cfg.Password,
Timeout: cfg.Timeout,
TTYOptions: &tclientlib.TerminalOptions{
Wide: cfg.win.Width,
High: cfg.win.Height,
TermType: cfg.Term,
},
LoginSuccessRegex: cfg.CustomSuccessPattern,
})
close(done)
if err != nil {
return nil, err
}
return client, nil
}
type TelnetConnection struct {
cfg *TelnetConfig
conn *tclientlib.Client
proxyConn *SSHClient
transformReader io.Reader
transformWriter io.Writer
once sync.Once
}
func (tc *TelnetConnection) Protocol() string {
return "telnet"
}
func (tc *TelnetConnection) KeepAlive() error {
return nil
}
func (tc *TelnetConnection) SetWinSize(w, h int) error {
return tc.conn.WindowChange(w, h)
}
func (tc *TelnetConnection) Read(p []byte) (n int, err error) {
return tc.transformReader.Read(p)
}
func (tc *TelnetConnection) Write(p []byte) (n int, err error) {
return tc.transformWriter.Write(p)
}
func (tc *TelnetConnection) Close() (err error) {
tc.once.Do(func() {
if tc.proxyConn != nil {
_ = tc.proxyConn.Close()
}
err = tc.conn.Close()
})
return
}
type TelnetOption func(*TelnetConfig)
type TelnetConfig struct {
Host string
Port string
Username string
Password string
Term string
Charset string
Timeout time.Duration
win Windows
CustomSuccessPattern *regexp.Regexp
proxySSHClientOptions []SSHClientOptions
}
func TelnetHost(host string) TelnetOption {
return func(opt *TelnetConfig) {
opt.Host = host
}
}
func TelnetPort(port int) TelnetOption {
return func(opt *TelnetConfig) {
opt.Port = strconv.Itoa(port)
}
}
func TelnetUsername(username string) TelnetOption {
return func(opt *TelnetConfig) {
opt.Username = username
}
}
func TelnetUPassword(password string) TelnetOption {
return func(opt *TelnetConfig) {
opt.Password = password
}
}
func TelnetUTimeout(timeout int) TelnetOption {
return func(opt *TelnetConfig) {
opt.Timeout = time.Duration(timeout) * time.Second
}
}
func TelnetProxyOptions(proxyOpts []SSHClientOptions) TelnetOption {
return func(opt *TelnetConfig) {
opt.proxySSHClientOptions = proxyOpts
}
}
func TelnetPtyWin(win Windows) TelnetOption {
return func(opt *TelnetConfig) {
opt.win = win
}
}
func TelnetCharset(charset string) TelnetOption {
return func(opt *TelnetConfig) {
opt.Charset = charset
}
}
func TelnetCustomSuccessPattern(successPattern *regexp.Regexp) TelnetOption {
return func(opt *TelnetConfig) {
opt.CustomSuccessPattern = successPattern
}
}