-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathserver.go
More file actions
614 lines (533 loc) · 13.1 KB
/
Copy pathserver.go
File metadata and controls
614 lines (533 loc) · 13.1 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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
//go:build !js
package mosh
import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"fmt"
"io"
"net"
"os"
"os/exec"
"sync"
"sync/atomic"
"time"
"github.com/unixshells/vt-go"
"github.com/creack/pty"
)
const (
// Server gives up waiting for client after this.
associationTimeout = 60 * time.Second
// Network timeout for idle sessions.
defaultNetworkTimeout = 24 * time.Hour
// Default terminal size.
defaultCols = 80
defaultRows = 24
)
// Server is a native mosh server. It listens on UDP, runs a shell in a PTY,
// bridges data through the SSP transport, and diffs terminal framebuffers
// to produce HostMessage updates for the client.
type Server struct {
key []byte
ocb *OCB
port int
conn *net.UDPConn
ptmx *os.File
cmd *exec.Cmd
shell string
cols int
rows int
// Transport handles SSP sequencing, fragmentation, and crypto.
transport *Transport
// VT emulator and framebuffer state for CUP-based diffing.
emu *vt.Emulator
baseFB *Framebuffer // what client has (last-acked)
sentFB *Framebuffer // what we last sent (pending ack)
curVisible atomic.Bool
// Remote client address — set on first received datagram.
mu sync.Mutex
clientAddr *net.UDPAddr
started chan struct{} // closed when PTY is running
done chan struct{}
}
// GenerateKey creates a random 128-bit mosh key and returns it as base64.
func GenerateKey() ([]byte, string, error) {
key := make([]byte, 16)
if _, err := rand.Read(key); err != nil {
return nil, "", err
}
return key, base64.StdEncoding.EncodeToString(key), nil
}
// NewServer creates a native mosh server.
// It binds a UDP port, generates a key, and is ready to serve.
func NewServer(shell string, portLow, portHigh int) (*Server, error) {
key, _, err := GenerateKey()
if err != nil {
return nil, err
}
ocb, err := NewOCB(key)
if err != nil {
return nil, err
}
if shell == "" {
shell = os.Getenv("SHELL")
if shell == "" {
shell = "/bin/sh"
}
}
conn, port, err := BindUDP(portLow, portHigh)
if err != nil {
return nil, err
}
return &Server{
key: key,
ocb: ocb,
port: port,
conn: conn,
shell: shell,
cols: defaultCols,
rows: defaultRows,
transport: NewTransport(ocb, true),
started: make(chan struct{}),
done: make(chan struct{}),
}, nil
}
// Port returns the UDP port the server is listening on.
func (s *Server) Port() int {
return s.port
}
// KeyBase64 returns the mosh key as a base64 string (22 chars, no padding).
func (s *Server) KeyBase64() string {
encoded := base64.StdEncoding.EncodeToString(s.key)
for len(encoded) > 0 && encoded[len(encoded)-1] == '=' {
encoded = encoded[:len(encoded)-1]
}
return encoded
}
// ConnectLine returns the MOSH CONNECT line that clients parse.
func (s *Server) ConnectLine() string {
return fmt.Sprintf("MOSH CONNECT %d %s", s.port, s.KeyBase64())
}
// Serve starts the shell and event loop. Blocks until the session ends.
func (s *Server) Serve() error {
s.cmd = exec.Command(s.shell)
s.cmd.Env = append(os.Environ(),
"TERM=xterm-256color",
"MOSH_SERVER_NETWORK_TMOUT=86400",
)
var err error
s.ptmx, err = pty.Start(s.cmd)
if err != nil {
s.conn.Close()
return fmt.Errorf("pty: %w", err)
}
close(s.started)
pty.Setsize(s.ptmx, &pty.Winsize{
Rows: uint16(s.rows),
Cols: uint16(s.cols),
})
s.emu = vt.NewEmulator(s.cols, s.rows)
s.curVisible.Store(true)
s.emu.SetCallbacks(vt.Callbacks{
CursorVisibility: func(visible bool) { s.curVisible.Store(visible) },
})
s.baseFB = NewFramebuffer(s.cols, s.rows)
var wg sync.WaitGroup
wg.Add(3)
// PTY reader: reads shell output into pending buffer.
ptyOutput := make(chan []byte, 64)
go func() {
defer wg.Done()
s.readPTY(ptyOutput)
}()
// UDP receiver: decrypts datagrams, feeds to transport.
userInput := make(chan UserInstruction, 64)
go func() {
defer wg.Done()
s.recvUDP(userInput)
}()
// Main loop: process input, diff framebuffer, send via transport.
go func() {
defer wg.Done()
s.mainLoop(ptyOutput, userInput)
}()
err = s.cmd.Wait()
close(s.done)
s.ptmx.Close()
s.conn.Close()
wg.Wait()
return err
}
// Done returns a channel that is closed when the server shuts down.
func (s *Server) Done() <-chan struct{} {
return s.done
}
// ServeRW runs the event loop using an external io.ReadWriteCloser instead
// of spawning a shell in a PTY. The caller provides terminal I/O through rw
// and a resize callback. When rw is closed or reaches EOF, the server shuts down.
func (s *Server) ServeRW(rw io.ReadWriteCloser, resize func(cols, rows uint16)) error {
close(s.started)
s.emu = vt.NewEmulator(s.cols, s.rows)
s.curVisible.Store(true)
s.emu.SetCallbacks(vt.Callbacks{
CursorVisibility: func(visible bool) { s.curVisible.Store(visible) },
})
s.baseFB = NewFramebuffer(s.cols, s.rows)
var wg sync.WaitGroup
wg.Add(3)
ioOutput := make(chan []byte, 64)
go func() {
defer wg.Done()
s.readIO(rw, ioOutput)
}()
userInput := make(chan UserInstruction, 64)
go func() {
defer wg.Done()
s.recvUDP(userInput)
}()
go func() {
defer wg.Done()
s.mainLoopRW(rw, resize, ioOutput, userInput)
}()
<-s.done
rw.Close()
s.conn.Close()
wg.Wait()
return nil
}
func (s *Server) readIO(r io.Reader, out chan<- []byte) {
buf := make([]byte, 8192)
for {
n, err := r.Read(buf)
if n > 0 {
data := make([]byte, n)
copy(data, buf[:n])
select {
case out <- data:
case <-s.done:
return
}
}
if err != nil {
select {
case <-s.done:
default:
close(s.done)
}
return
}
}
}
func (s *Server) mainLoopRW(rw io.Writer, resize func(cols, rows uint16), ioOutput <-chan []byte, userInput <-chan UserInstruction) {
// Wait for the first client datagram. If no client connects within
// 60 seconds, this was a failed setup — shut down to release resources.
deadline := time.After(associationTimeout)
for {
s.mu.Lock()
addr := s.clientAddr
s.mu.Unlock()
if addr != nil {
break
}
select {
case <-s.done:
return
case <-deadline:
close(s.done)
return
case <-time.After(100 * time.Millisecond):
}
}
ticker := time.NewTicker(tickInterval)
defer ticker.Stop()
dirty := false
for {
select {
case <-s.done:
return
case data, ok := <-ioOutput:
if !ok {
return
}
s.emu.Write(data)
dirty = true
case ui := <-userInput:
if len(ui.Keys) > 0 {
rw.Write(ui.Keys)
}
if ui.Width > 0 && ui.Height > 0 {
s.mu.Lock()
s.cols = int(ui.Width)
s.rows = int(ui.Height)
s.mu.Unlock()
if resize != nil {
resize(uint16(ui.Width), uint16(ui.Height))
}
s.emu.Resize(int(ui.Width), int(ui.Height))
s.baseFB = NewFramebuffer(int(ui.Width), int(ui.Height))
s.sentFB = nil
dirty = true
}
case <-ticker.C:
if s.sentFB != nil && s.transport.AckedByRemote() >= s.transport.SentNum() {
s.baseFB = s.sentFB
s.sentFB = nil
}
if dirty {
currentFB := SnapshotEmulator(s.emu, s.curVisible.Load())
diffBytes := currentFB.Diff(s.baseFB)
if len(diffBytes) > 0 {
hi := HostInstruction{Hoststring: diffBytes, EchoAckNum: -1}
s.transport.SetPending(marshalHostMessage([]HostInstruction{hi}))
s.sentFB = currentFB
}
dirty = false
}
datagrams := s.transport.Tick()
s.sendDatagrams(datagrams)
}
}
}
// Close shuts down the server.
func (s *Server) Close() {
// If Serve() was never called, just close the socket.
select {
case <-s.done:
return
case <-s.started:
// PTY is running — kill it.
default:
// Serve() never called. Close socket and unblock any future Serve().
s.conn.Close()
return
}
if s.cmd.Process != nil {
s.cmd.Process.Kill()
}
s.conn.Close()
if s.ptmx != nil {
s.ptmx.Close()
}
}
// mainLoop is the SSP event loop. It feeds PTY output to a VT emulator,
// snapshots the framebuffer, diffs against the last-acked state, and
// sends CUP-based updates via the transport.
func (s *Server) mainLoop(ptyOutput <-chan []byte, userInput <-chan UserInstruction) {
// Wait for the first client datagram. If no client connects within
// 60 seconds, this was a failed setup — shut down to release resources.
deadline := time.After(associationTimeout)
for {
s.mu.Lock()
addr := s.clientAddr
s.mu.Unlock()
if addr != nil {
break
}
select {
case <-s.done:
return
case <-deadline:
close(s.done)
return
case <-time.After(100 * time.Millisecond):
}
}
ticker := time.NewTicker(tickInterval)
defer ticker.Stop()
dirty := false
for {
select {
case <-s.done:
return
case data, ok := <-ptyOutput:
if !ok {
return
}
s.emu.Write(data)
dirty = true
case ui := <-userInput:
if len(ui.Keys) > 0 {
s.ptmx.Write(ui.Keys)
}
if ui.Width > 0 && ui.Height > 0 {
s.mu.Lock()
s.cols = int(ui.Width)
s.rows = int(ui.Height)
s.mu.Unlock()
pty.Setsize(s.ptmx, &pty.Winsize{
Cols: uint16(ui.Width),
Rows: uint16(ui.Height),
})
s.emu.Resize(int(ui.Width), int(ui.Height))
s.baseFB = NewFramebuffer(int(ui.Width), int(ui.Height))
s.sentFB = nil
dirty = true
}
case <-ticker.C:
// Advance base when client acks all pending.
if s.sentFB != nil && s.transport.AckedByRemote() >= s.transport.SentNum() {
s.baseFB = s.sentFB
s.sentFB = nil
}
if dirty {
currentFB := SnapshotEmulator(s.emu, s.curVisible.Load())
diffBytes := currentFB.Diff(s.baseFB)
if len(diffBytes) > 0 {
hi := HostInstruction{Hoststring: diffBytes, EchoAckNum: -1}
s.transport.SetPending(marshalHostMessage([]HostInstruction{hi}))
s.sentFB = currentFB
}
dirty = false
}
datagrams := s.transport.Tick()
s.sendDatagrams(datagrams)
}
}
}
// readPTY reads from the PTY and sends data to the channel.
func (s *Server) readPTY(out chan<- []byte) {
buf := make([]byte, 8192)
for {
select {
case <-s.done:
return
default:
}
s.ptmx.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n, err := s.ptmx.Read(buf)
if n > 0 {
data := make([]byte, n)
copy(data, buf[:n])
select {
case out <- data:
case <-s.done:
return
}
}
if err != nil && !os.IsTimeout(err) {
return
}
}
}
// recvUDP reads encrypted datagrams, decrypts via transport, parses UserMessage.
func (s *Server) recvUDP(out chan<- UserInstruction) {
buf := make([]byte, maxPayload+64)
for {
select {
case <-s.done:
return
default:
}
s.conn.SetReadDeadline(time.Now().Add(1 * time.Second))
n, addr, err := s.conn.ReadFromUDP(buf)
if err != nil {
if os.IsTimeout(err) {
if time.Since(s.transport.LastRecv()) > defaultNetworkTimeout {
return
}
continue
}
return
}
if n < minDatagram {
continue
}
data := make([]byte, n)
copy(data, buf[:n])
// Feed to transport — only update clientAddr on successful decrypt.
diff := s.transport.Recv(data)
s.mu.Lock()
s.clientAddr = addr
s.mu.Unlock()
if diff == nil {
continue
}
// Parse UserMessage protobuf.
instrs, err := unmarshalUserMessage(diff)
if err != nil {
continue
}
for _, ui := range instrs {
select {
case out <- ui:
case <-s.done:
return
}
}
}
}
// sendDatagrams sends wire datagrams to the client.
func (s *Server) sendDatagrams(datagrams [][]byte) {
s.mu.Lock()
addr := s.clientAddr
s.mu.Unlock()
if addr == nil {
return
}
for _, dg := range datagrams {
s.conn.WriteToUDP(dg, addr)
}
}
// BindUDP binds a UDP socket on the first available port in [low, high].
func BindUDP(low, high int) (*net.UDPConn, int, error) {
if low == 0 && high == 0 {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{IP: net.IPv4zero})
if err != nil {
return nil, 0, err
}
return conn, conn.LocalAddr().(*net.UDPAddr).Port, nil
}
if high < low {
high = low
}
for port := low; port <= high; port++ {
conn, err := net.ListenUDP("udp4", &net.UDPAddr{
IP: net.IPv4zero,
Port: port,
})
if err != nil {
continue
}
return conn, port, nil
}
return nil, 0, fmt.Errorf("no available UDP port in range %d-%d", low, high)
}
// WriteTo implements io.WriterTo for streaming output to an SSH channel.
func (s *Server) WriteTo(w io.Writer) (int64, error) {
line := s.ConnectLine() + "\n"
n, err := w.Write([]byte(line))
return int64(n), err
}
// sendToClient encrypts and sends a raw payload datagram to the client.
// Used by legacy tests that don't go through the Transport.
func (s *Server) sendToClient(payload []byte) {
s.mu.Lock()
addr := s.clientAddr
if addr == nil {
s.mu.Unlock()
return
}
s.transport.mu.Lock()
s.transport.seqOut++
seq := s.transport.seqOut
lastTS := s.transport.lastTS
s.transport.mu.Unlock()
s.mu.Unlock()
dirSeq := dirToClient | (seq & seqMask)
var dirSeqBytes [8]byte
binary.BigEndian.PutUint64(dirSeqBytes[:], dirSeq)
var nonce [12]byte
copy(nonce[4:], dirSeqBytes[:])
ts := uint16(time.Now().UnixMilli() & 0xffff)
plaintext := make([]byte, 4+len(payload))
binary.BigEndian.PutUint16(plaintext[0:], ts)
binary.BigEndian.PutUint16(plaintext[2:], lastTS)
if len(payload) > 0 {
copy(plaintext[4:], payload)
}
tagAndCT := s.ocb.Encrypt(nonce[:], plaintext)
wire := make([]byte, 8+len(tagAndCT))
copy(wire[:8], dirSeqBytes[:])
copy(wire[8:], tagAndCT)
s.conn.WriteToUDP(wire, addr)
}