-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsshd.go
163 lines (144 loc) · 2.9 KB
/
wsshd.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
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
package main
import(
"github.com/krockot/goterm/term"
"websocket"
"http"
"io"
"os"
"syscall"
"time"
"flag"
"fmt"
)
type Client struct {
conn io.ReadWriteCloser
tty *term.Terminal
pid int
quit chan bool
}
func (c *Client) Close() os.Error {
if c.tty != nil {
c.tty.Close()
}
if c.pid > 0 {
syscall.Kill(c.pid, 1)
os.Wait(c.pid, 0)
}
c.conn.Close()
return nil
}
func HandleLogin(c *Client) LoginHandlerFunc {
return func(username *string) os.Error {
tty, pid, err := term.ForkPty(
"/bin/login",
[]string{"/bin/login"},
term.DefaultAttributes(),
term.NewWindowSize(80,25))
if err != nil {
return err
}
c.tty = tty
c.pid = pid
go func() {
buffer := make([]byte, 1024)
for {
n, err := tty.Read(buffer)
if err != nil {
break
}
msg := NewDataMessage(buffer[:n])
data, err := msg.Encode()
if err != nil {
break
}
n, err = c.conn.Write(data)
if err != nil {
break
}
}
c.quit <- true
}()
go func() {
t := time.NewTicker(1e9)
for {
select {
case <-c.quit:
c.Close()
return
case <-t.C:
msg, err := os.Wait(c.pid, os.WNOHANG)
if err == nil && msg.Pid == c.pid {
c.Close()
return
}
}
}
}()
return nil
}
}
func HandleData(c *Client) DataHandlerFunc {
return func(data []byte) os.Error {
if c.tty != nil {
_, err := c.tty.Write(data)
if err != nil {
return err
}
}
return nil
}
}
func HandleClose(c *Client) CloseHandlerFunc {
return func() os.Error {
return c.Close()
}
}
func HandleWindow(c *Client) WindowHandlerFunc {
return func(w, h int) os.Error {
if c.tty != nil {
return c.tty.SetWindowSize(term.NewWindowSize(uint16(w), uint16(h)))
}
return nil
}
}
func ShellHandler(ws *websocket.Conn) {
c := &Client{conn: ws, quit: make(chan bool, 2)}
mp := NewMessageProcessor(ws)
mp.HandleLogin(HandleLogin(c))
mp.HandleData(HandleData(c))
mp.HandleClose(HandleClose(c))
mp.HandleWindow(HandleWindow(c))
for {
if err := mp.ProcessNext(); err != nil {
c.quit <- true
return
}
}
}
func main() {
certFile := flag.String("cert", "", "Path to certificate file")
keyFile := flag.String("key", "", "Path to private key file")
port := flag.String("port", "8022", "Service port")
dontCare := flag.Bool("dontcare", false, "Force non-secure mode if no cert or key is given")
flag.Parse()
notSoGood := false
if len(*certFile) == 0 || len(*keyFile) == 0 {
if *dontCare {
notSoGood = true
} else {
fmt.Fprintln(os.Stderr,
"Cannot use TLS without a cert and key. Use -dontcare to serve without encryption.")
return
}
}
http.Handle("/sh", websocket.Handler(ShellHandler))
var err os.Error
if notSoGood {
err = http.ListenAndServe(":" + *port, nil)
} else {
err = http.ListenAndServeTLS(":" + *port, *certFile, *keyFile, nil)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s", err.String())
}
}