-
Notifications
You must be signed in to change notification settings - Fork 0
/
single.go
376 lines (352 loc) · 11.8 KB
/
single.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
// Copyright 2022~2024 wangqi. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
// func runWorker(conf *Config, exChan chan string, whChan chan workhorse) (err error) {
// defer func() {
// // notify this worker is done
// exChan <- conf.desiredPort
// }()
//
// /*
// If this variable is set to a positive integer number, it specifies how
// long (in seconds) apshd will wait to receive an update from the
// client before exiting. Since aprilsh is very useful for mobile
// clients with intermittent operation and connectivity, we suggest
// setting this variable to a high value, such as 604800 (one week) or
// 2592000 (30 days). Otherwise, apshd will wait indefinitely for a
// client to reappear. This variable is somewhat similar to the TMOUT
// variable found in many Bourne shells. However, it is not a login-session
// inactivity timeout; it only applies to network connectivity.
// */
// networkTimeout := getTimeFrom("APRILSH_SERVER_NETWORK_TMOUT", 0)
//
// /*
// If this variable is set to a positive integer number, it specifies how
// long (in seconds) apshd will ignore SIGUSR1 while waiting to receive
// an update from the client. Otherwise, SIGUSR1 will always terminate
// apshd. Users and administrators may implement scripts to clean up
// disconnected aprilsh sessions. With this variable set, a user or
// administrator can issue
//
// $ pkill -SIGUSR1 aprilsh-server
//
// to kill disconnected sessions without killing connected login
// sessions.
// */
// networkSignaledTimeout := getTimeFrom("APRILSH_SERVER_SIGNAL_TMOUT", 0)
//
// // util.Log.Debug("runWorker", "networkTimeout", networkTimeout,
// // "networkSignaledTimeout", networkSignaledTimeout)
//
// // get initial window size
// var windowSize *unix.Winsize
// windowSize, err = unix.IoctlGetWinsize(int(os.Stdin.Fd()), unix.TIOCGWINSZ)
// // windowSize, err := pty.GetsizeFull(os.Stdin)
// if err != nil || windowSize.Col == 0 || windowSize.Row == 0 {
// // Fill in sensible defaults. */
// // They will be overwritten by client on first connection.
// windowSize.Col = 80
// windowSize.Row = 24
// }
// // util.Log.Debug("init terminal size", "cols", windowSize.Col, "rows", windowSize.Row)
//
// // open parser and terminal
// savedLines := terminal.SaveLinesRowsOption
// terminal, err := statesync.NewComplete(int(windowSize.Col), int(windowSize.Row), savedLines)
//
// // open network
// blank := &statesync.UserStream{}
// server := network.NewTransportServer(terminal, blank, conf.desiredIP, conf.desiredPort)
// server.SetVerbose(uint(conf.verbose))
// // defer server.Close()
//
// /*
// // If server is run on a pty, then typeahead may echo and break mosh.pl's
// // detection of the CONNECT message. Print it on a new line to bodge
// // around that.
//
// if term.IsTerminal(int(os.Stdin.Fd())) {
// fmt.Printf("\r\n")
// }
// */
//
// exChan <- server.GetKey() // send the key to run()
//
// // in mosh: the parent print this to stderr.
// // fmt.Printf("#runWorker %s CONNECT %s %s\n", COMMAND_NAME, network.Port(), network.GetKey())
// // printWelcome(os.Stdout, os.Getpid(), os.Stdin)
//
// // prepare for openPTS fail
// if conf.verbose == _VERBOSE_OPEN_PTS_FAIL {
// windowSize = nil
// }
//
// ptmx, pts, err := openPTS(windowSize)
// if err != nil {
// util.Log.Warn("openPTS fail", "error", err)
// whChan <- workhorse{}
// return err
// }
// defer func() {
// ptmx.Close()
// // pts.Close()
// }() // Best effort.
// // fmt.Printf("#runWorker openPTS successfully.\n")
//
// // use pipe to signal when to start shell
// // pw and pr is close inside serve() and startShell()
// pr, pw := io.Pipe()
//
// // prepare host field for utmp record
// utmpHost := fmt.Sprintf("%s:%s", frontend.CommandServerName, server.GetServerPort())
//
// // add utmp entry
// if utmpSupport {
// ok := util.AddUtmpx(pts, utmpHost)
// if !ok {
// utmpSupport = false
// util.Log.Warn("#runWorker can't update utmp")
// }
// }
//
// // start the udp server, serve the udp request
// var wg sync.WaitGroup
// wg.Add(1)
// // waitChan := make(chan bool)
// // go conf.serve(ptmx, pw, terminal, waitChan, network, networkTimeout, networkSignaledTimeout)
// go func() {
// conf.serve(ptmx, pts, pw, terminal, server, networkTimeout, networkSignaledTimeout, conf.user)
// exChan <- fmt.Sprintf("%s:shutdown", conf.desiredPort)
// wg.Done()
// }()
//
// // TODO update last log ?
// // util.UpdateLastLog(ptmxName, getCurrentUser(), utmpHost)
//
// defer func() { // clear utmp entry
// if utmpSupport {
// util.ClearUtmpx(pts)
// }
// }()
//
// util.Log.Info("start listening on", "port", conf.desiredPort, "clientTERM", conf.term)
//
// // start the shell with pts
// shell, err := startShell(pts, pr, utmpHost, conf)
// pts.Close() // it's copied by shell process, it's safe to close it here.
// if err != nil {
// util.Log.Warn("startShell fail", "error", err)
// whChan <- workhorse{}
// } else {
//
// whChan <- workhorse{shell, 0}
// // wait for the shell to finish.
// var state *os.ProcessState
// state, err = shell.Wait()
// if err != nil || state.Exited() {
// if err != nil {
// util.Log.Warn("shell.Wait fail", "error", err, "state", state)
// // } else {
// // util.Log.Debug("shell.Wait quit", "state.exited", state.Exited())
// }
// }
// }
//
// // wait serve to finish
// wg.Wait()
// util.Log.Info("stop listening on", "port", conf.desiredPort)
//
// // fmt.Printf("[%s is exiting.]\n", frontend.COMMAND_SERVER_NAME)
// // https://www.dolthub.com/blog/2022-11-28-go-os-exec-patterns/
// // https://www.prakharsrivastav.com/posts/golang-context-and-cancellation/
//
// // util.Log.Debug("runWorker quit", "port", conf.desiredPort)
// return err
// }
/*
func (m *mainSrv) start(conf *Config) {
// listen the port
if err := m.listen(conf); err != nil {
util.Log.Warn("listen failed", "error", err)
return
}
// start main server waiting for open/close message.
m.wg.Add(1)
go func() {
m.run(conf)
m.wg.Done()
}()
// shutdown if the auto stop flag is set
if conf.autoStop > 0 {
time.AfterFunc(time.Duration(conf.autoStop)*time.Second, func() {
m.downChan <- true
})
}
}
func (m *mainSrv) run(conf *Config) {
if m.conn == nil {
return
}
// prepare to receive the signal
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGINT)
// clean up
defer func() {
signal.Stop(sig)
if syslogSupport {
syslogWriter.Info(fmt.Sprintf("stop listening on %s.", m.conn.LocalAddr()))
}
m.conn.Close()
util.Log.Info("stop listening on", "port", m.port)
}()
buf := make([]byte, 128)
shutdown := false
if syslogSupport {
syslogWriter.Info(fmt.Sprintf("start listening on %s.", m.conn.LocalAddr()))
}
printWelcome(os.Getpid(), m.port, nil)
for {
select {
case portStr := <-m.exChan:
m.cleanWorkers(portStr)
// util.Log.Info("run some worker is done","port", portStr)
case ss := <-sig:
switch ss {
case syscall.SIGHUP: // TODO:reload the config?
util.Log.Info("got signal: SIGHUP")
case syscall.SIGTERM, syscall.SIGINT:
util.Log.Info("got signal: SIGTERM or SIGINT")
shutdown = true
}
case <-m.downChan:
// another way to shutdown besides signal
shutdown = true
default:
}
if shutdown {
// util.Log.Debug("run","shutdown", shutdown)
if len(m.workers) == 0 {
return
} else {
// send kill message to the workers
for i := range m.workers {
m.workers[i].child.Kill()
// util.Log.Debug("stop shell","port", i)
}
// wait for workers to finish, set time out to prevent dead lock
timeout := time.NewTimer(time.Duration(200) * time.Millisecond)
for len(m.workers) > 0 {
select {
case portStr := <-m.exChan: // some worker is done
m.cleanWorkers(portStr)
case t := <-timeout.C:
util.Log.Warn("run quit with timeout", "timeout", t)
return
default:
}
}
return
}
}
// set read time out: 200ms
m.conn.SetDeadline(time.Now().Add(time.Millisecond * time.Duration(m.timeout)))
n, addr, err := m.conn.ReadFromUDP(buf)
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
// fmt.Printf("#run read time out, workers=%d, shutdown=%t, err=%s\n", len(m.workers), shutdown, err)
continue
} else {
// take a break in case reading error
time.Sleep(time.Duration(5) * time.Millisecond)
// fmt.Println("#run read error: ", err)
continue
}
}
req := strings.TrimSpace(string(buf[0:n]))
if strings.HasPrefix(req, frontend.AprilshMsgOpen) { // 'open aprilsh:'
if len(m.workers) >= maxPortLimit {
resp := m.writeRespTo(addr, frontend.AprishMsgClose, "over max port limit")
util.Log.Warn("over max port limit", "request", req, "response", resp)
continue
}
// prepare next port
p := m.getAvailabePort()
// open aprilsh:TERM,user@server.domain
// prepare configuration
conf2 := *conf
conf2.desiredPort = fmt.Sprintf("%d", p)
body := strings.Split(req, ":")
content := strings.Split(body[1], ",")
if len(content) != 2 {
resp := m.writeRespTo(addr, frontend.AprilshMsgOpen, "malform request")
util.Log.Warn("malform request", "request", req, "response", resp)
continue
}
conf2.term = content[0]
conf2.destination = content[1]
// parse user and host from destination
idx := strings.Index(content[1], "@")
if idx > 0 && idx < len(content[1])-1 {
conf2.host = content[1][idx+1:]
conf2.user = content[1][:idx]
} else {
// return "target parameter should be in the form of User@Server", false
resp := m.writeRespTo(addr, frontend.AprilshMsgOpen, "malform destination")
util.Log.Warn("malform destination", "destination", content[1], "response", resp)
continue
}
// we don't need to check if user exist, ssh already done that before
// For security, make sure we don't dump core
encrypt.DisableDumpingCore()
// start the worker
m.wg.Add(1)
go func(conf *Config, exChan chan string, whChan chan workhorse) {
m.runWorker(conf, exChan, whChan)
m.wg.Done()
}(&conf2, m.exChan, m.whChan)
// blocking read the key from worker
key := <-m.exChan
// response session key and udp port to client
msg := fmt.Sprintf("%d,%s", p, key)
m.writeRespTo(addr, frontend.AprilshMsgOpen, msg)
// blocking read the workhorse from runWorker
wh := <-m.whChan
if wh.child != nil {
m.workers[p] = &wh
}
} else if strings.HasPrefix(req, frontend.AprishMsgClose) { // 'close aprilsh:[port]'
pstr := strings.TrimPrefix(req, frontend.AprishMsgClose)
port, err := strconv.Atoi(pstr)
if err == nil {
// find workhorse
if wh, ok := m.workers[port]; ok {
// kill the process, TODO SIGKILL or SIGTERM?
wh.child.Kill()
m.writeRespTo(addr, frontend.AprishMsgClose, "done")
} else {
resp := m.writeRespTo(addr, frontend.AprishMsgClose, "port does not exist")
util.Log.Warn("port does not exist", "request", req, "response", resp)
}
} else {
resp := m.writeRespTo(addr, frontend.AprishMsgClose, "wrong port number")
util.Log.Warn("wrong port number", "request", req, "response", resp)
}
} else {
resp := m.writeRespTo(addr, frontend.AprishMsgClose, "unknow request")
util.Log.Warn("unknow request", "request", req, "response", resp)
}
}
// just for test purpose:
//
// in aprilsh: we can use nc client to get the key and send it back to client.
// we don't print it to the stdout as mosh did.
//
// send udp request and read reply
// % echo "open aprilsh:" | nc localhost 6000 -u -w 1
// % echo "close aprilsh:6001" | nc localhost 6000 -u -w 1
//
// send udp request to remote host
// % ssh ide@localhost "echo 'open aprilsh:' | nc localhost 6000 -u -w 1"
}
*/