forked from updroidinc/cmdr-pty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
86 lines (71 loc) · 1.86 KB
/
tcp.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
package main
import "fmt"
import "net"
import "os"
import "unicode/utf8"
// Copy everything from the pty master to the socket.
func handleOutputSock(ptym *os.File, conn *net.TCPConn) {
buf := make([]byte, 512)
var payload, overflow []byte
// TODO: more graceful exit on socket close / process exit.
for {
n, err := ptym.Read(buf)
if err != nil {
fmt.Println("failed to read from pty master: ", err)
return
}
// Empty the overflow from the last read into the payload first.
payload = append(payload[0:], overflow...)
overflow = nil
// Then empty the new buf read into the payload.
payload = append(payload, buf[:n]...)
// Strip out any incomplete utf-8 from current payload into overflow.
for !utf8.Valid(payload) {
overflow = append(overflow[:0], append(payload[len(payload)-1:], overflow[0:]...)...)
payload = payload[:len(payload)-1]
}
// Send out the finished payload as long as it's not empty.
if len(payload) >= 1 {
_, err = conn.Write(payload)
if err != nil {
fmt.Println("Write: ", err)
}
}
// Empty the payload.
payload = nil
}
}
// Read from the websocket, copying to the pty master.
func handleInputSock(ptym *os.File, conn *net.TCPConn) {
for {
buf := make([]byte, 512)
bytes, err := conn.Read(buf)
if err != nil {
return
}
buf = buf[:bytes]
ptym.Write(buf)
}
}
func ptySetupSock(conn *net.TCPConn, sizeFlag string) {
ptym, cmd := start()
setPtySize(ptym, sizeFlag)
go func() {
handleOutputSock(ptym, conn)
}()
go func() {
handleInputSock(ptym, conn)
}()
// Listen for a new winsize on stdin.
for {
var newSize string
_, scanErr := fmt.Scanln(&newSize)
if scanErr != nil {
fmt.Println("scan failed: ", scanErr)
}
setPtySize(ptym, newSize)
fmt.Println("new size: ", newSize)
}
stop(ptym, cmd)
conn.Close()
}