-
Notifications
You must be signed in to change notification settings - Fork 29
/
shell_linux.go
102 lines (85 loc) · 1.99 KB
/
shell_linux.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
package main
import (
"net/url"
"os"
"os/exec"
"strings"
"time"
"github.com/creack/pty"
"github.com/gorilla/websocket"
)
func shellSocket() {
var disconnected bool
remoteURL, _ := url.Parse(conf.Remote)
readTeamID()
curTeamID := string(teamID)
u := url.URL{Scheme: "ws", Host: remoteURL.Host, Path: "/ws/" + curTeamID + "-" + conf.Name}
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
disconnected = true
} else {
disconnected = false
if err := c.WriteMessage(websocket.TextMessage, []byte("WRITER")); err != nil {
disconnected = true
}
}
defer c.Close()
cmd := exec.Command("bash")
cmd.Env = append(os.Environ(), "TERM=xterm")
term, _ := pty.Start(cmd)
go func() {
for {
if !disconnected {
buf := make([]byte, 512)
_, err := term.Read(buf)
if err != nil {
cmd = exec.Command("bash")
cmd.Env = append(os.Environ(), "TERM=xterm")
term, _ = pty.Start(cmd)
continue
}
if err := c.WriteMessage(websocket.TextMessage, buf); err != nil {
disconnected = true
continue
}
}
}
}()
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
for {
if !disconnected {
_, message, err := c.ReadMessage()
if err != nil {
if strings.Contains(err.Error(), "1006") {
disconnected = true
}
}
_, err = term.Write([]byte(message))
if err != nil {
cmd = exec.Command("bash")
cmd.Env = append(os.Environ(), "TERM=xterm")
term, _ = pty.Start(cmd)
continue
}
} else {
select {
case <-ticker.C:
if disconnected {
readTeamID()
curTeamID := string(teamID)
u = url.URL{Scheme: "ws", Host: remoteURL.Host, Path: "/ws/" + curTeamID + "-" + conf.Name}
c, _, err = websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
disconnected = true
} else {
disconnected = false
if err := c.WriteMessage(websocket.TextMessage, []byte("WRITER")); err != nil {
disconnected = true
}
}
}
}
}
}
}