forked from unclejack/termproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinch.go
64 lines (50 loc) · 1.35 KB
/
winch.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
package main
import (
"net"
"os"
"sync"
"github.com/erikh/termproxy/server"
"github.com/erikh/termproxy/termproxy"
)
var (
connectionWinsizeMap = map[string]termproxy.Winch{}
winsizeMutex = new(sync.Mutex)
)
func compareAndSetWinsize(host string, ws termproxy.Winch, command *termproxy.Command, s *server.SSHServer) {
winsizeMutex.Lock()
connectionWinsizeMap[host] = ws
var height, width uint
for _, wm := range connectionWinsizeMap {
if height == 0 || width == 0 {
height = wm.Height
width = wm.Width
}
if wm.Height <= height {
height = wm.Height
}
if wm.Width <= width {
width = wm.Width
}
}
// send the clear only in the height case, it will resolve itself with width.
myws, _ := termproxy.GetWinsize(0)
if myws.Height != height {
s.Iterate(func(s *server.SSHServer, c net.Conn, index int) error {
termproxy.WriteClear(c)
return nil
})
termproxy.WriteClear(os.Stdout)
}
termproxy.SetWinsize(command.PTY().Fd(), termproxy.Winch{Height: height, Width: width})
s.Iterate(func(s *server.SSHServer, c net.Conn, index int) error {
payload := []byte{
0, 0, byte(ws.Width >> 8 & 0xFF), byte(ws.Width & 0xFF),
0, 0, byte(ws.Height >> 8 & 0xFF), byte(ws.Height & 0xFF),
0, 0, 0, 0,
0, 0, 0, 0,
}
c.(*server.Conn).SendRequest("window-change", false, payload)
return nil
})
winsizeMutex.Unlock()
}