-
Notifications
You must be signed in to change notification settings - Fork 6
/
options.go
92 lines (78 loc) · 1.97 KB
/
options.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
package telnet
import (
"encoding/binary"
"os"
"time"
"golang.org/x/crypto/ssh/terminal"
)
const (
ECHO = byte(1)
TTYPE = byte(24)
NAWS = byte(31)
ENCRYPT = byte(38)
EOR = byte(239)
)
// NAWSOption enables NAWS negotiation on a Server.
func NAWSOption(c *Connection) Negotiator {
return &NAWSHandler{client: false}
}
// ExposeNAWS enables NAWS negotiation on a Client.
func ExposeNAWS(c *Connection) Negotiator {
width, height, _ := terminal.GetSize(int(os.Stdin.Fd()))
return &NAWSHandler{Width: uint16(width), Height: uint16(height), client: true}
}
// NAWSHandler negotiates NAWS for a specific connection.
type NAWSHandler struct {
Width uint16
Height uint16
client bool
}
func (n *NAWSHandler) OptionCode() byte {
return NAWS
}
func (n *NAWSHandler) Offer(c *Connection) {
if !n.client {
c.Conn.Write([]byte{IAC, DO, n.OptionCode()})
}
}
func (n *NAWSHandler) HandleWill(c *Connection) {}
func (n *NAWSHandler) HandleDo(c *Connection) {
if n.client {
c.Conn.Write([]byte{IAC, WILL, n.OptionCode()})
n.writeSize(c)
go n.monitorTTYSize(c)
} else {
c.Conn.Write([]byte{IAC, WONT, n.OptionCode()})
}
}
func (n *NAWSHandler) monitorTTYSize(c *Connection) {
t := time.NewTicker(time.Second)
for range t.C {
w, h, err := terminal.GetSize(int(os.Stdin.Fd()))
if err != nil {
continue
}
width := uint16(w)
height := uint16(h)
if width != n.Width || height != n.Height {
n.Width = width
n.Height = height
n.writeSize(c)
}
}
}
func (n *NAWSHandler) writeSize(c *Connection) {
c.Conn.Write([]byte{IAC, SB, n.OptionCode()})
payload := make([]byte, 4)
binary.BigEndian.PutUint16(payload, n.Width)
binary.BigEndian.PutUint16(payload[2:], n.Height)
// Normal write - we want inadvertent IACs to be escaped in body
c.Write(payload)
c.Conn.Write([]byte{IAC, SE})
}
func (n *NAWSHandler) HandleSB(c *Connection, b []byte) {
if !n.client {
n.Width = binary.BigEndian.Uint16(b[0:2])
n.Height = binary.BigEndian.Uint16(b[2:4])
}
}