-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathterminal.go
75 lines (60 loc) · 1.32 KB
/
terminal.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
// Copyright 2014 Chris McGee <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"net/http"
"code.google.com/p/go.net/websocket"
)
type ConnectResult struct {
AttachWsURI string `json:"attachWsURI"`
}
func terminalSocket(ws *websocket.Conn) {
c := createShellCommand()
out, in, err := start(c)
if err != nil {
panic(err)
}
go func() {
for {
buf := make([]byte, 1024, 1024)
n, err := out.Read(buf)
if err != nil {
break
}
n, err = ws.Write(buf[:n])
if err != nil {
break
}
}
ws.Close()
}()
buf := make([]byte, 1024, 1024)
for {
n, err := ws.Read(buf)
if err != nil {
break
}
n, err = in.Write(buf[:n])
if err != nil {
break
}
}
out.Close()
in.Close()
c.Wait()
}
func terminalHandler(writer http.ResponseWriter, req *http.Request, path string, pathSegs []string) bool {
switch {
case req.Method == "POST" && len(pathSegs) == 2 && pathSegs[1] == "connect":
result := &ConnectResult{}
if hostName == loopbackHost {
result.AttachWsURI = "ws://" + hostName + ":" + *port + "/docker/socket"
} else {
result.AttachWsURI = "wss://" + hostName + ":" + *port + "/docker/socket"
}
ShowJson(writer, 200, result)
return true
}
return false
}