-
Notifications
You must be signed in to change notification settings - Fork 40
/
main.go
executable file
·150 lines (129 loc) · 3.49 KB
/
main.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
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"golang.org/x/net/websocket"
)
var port = flag.String("port", "8888", "Port for server")
var host = flag.String("host", "127.0.0.1:2735", "Docker host")
var contextPath = "/"
func main() {
flag.Parse()
if cp := os.Getenv("CONTEXT_PATH"); cp != "" {
contextPath = strings.TrimRight(cp, "/")
}
http.Handle(contextPath+"/exec/", websocket.Handler(ExecContainer))
http.Handle(contextPath+"/", http.StripPrefix(contextPath+"/", http.FileServer(http.Dir("./"))))
if err := http.ListenAndServe(":"+*port, nil); err != nil {
panic(err)
}
}
func ExecContainer(ws *websocket.Conn) {
wsParams := strings.Split(ws.Request().URL.Path[len(contextPath+"/exec/"):], ",")
container := wsParams[0]
cmd, _ := base64.StdEncoding.DecodeString(wsParams[1])
if container == "" {
ws.Write([]byte("Container does not exist"))
return
}
type stuff struct {
Id string
}
var s stuff
params := bytes.NewBufferString("{\"AttachStdin\":true,\"AttachStdout\":true,\"AttachStderr\":true,\"Tty\":true,\"Cmd\":[\"" + string(cmd) + "\"]}")
resp, err := http.Post("http://"+*host+"/containers/"+container+"/exec", "application/json", params)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
json.Unmarshal([]byte(data), &s)
if err := hijack(*host, "POST", "/exec/"+s.Id+"/start", true, ws, ws, ws, nil, nil); err != nil {
panic(err)
}
fmt.Println("Connection!")
fmt.Println(ws)
spew.Dump(ws)
}
func hijack(addr, method, path string, setRawTerminal bool, in io.ReadCloser, stdout, stderr io.Writer, started chan io.Closer, data interface{}) error {
params := bytes.NewBufferString("{\"Detach\": false, \"Tty\": true}")
req, err := http.NewRequest(method, path, params)
if err != nil {
return err
}
req.Header.Set("User-Agent", "Docker-Client")
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "tcp")
req.Host = addr
dial, err := net.Dial("tcp", addr)
// When we set up a TCP connection for hijack, there could be long periods
// of inactivity (a long running command with no output) that in certain
// network setups may cause ECONNTIMEOUT, leaving the client in an unknown
// state. Setting TCP KeepAlive on the socket connection will prohibit
// ECONNTIMEOUT unless the socket connection truly is broken
if tcpConn, ok := dial.(*net.TCPConn); ok {
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(30 * time.Second)
}
if err != nil {
return err
}
clientconn := httputil.NewClientConn(dial, nil)
defer clientconn.Close()
// Server hijacks the connection, error 'connection closed' expected
clientconn.Do(req)
rwc, br := clientconn.Hijack()
defer rwc.Close()
if started != nil {
started <- rwc
}
var receiveStdout chan error
if stdout != nil || stderr != nil {
go func() (err error) {
if setRawTerminal && stdout != nil {
_, err = io.Copy(stdout, br)
}
return err
}()
}
go func() error {
if in != nil {
io.Copy(rwc, in)
}
if conn, ok := rwc.(interface {
CloseWrite() error
}); ok {
if err := conn.CloseWrite(); err != nil {
}
}
return nil
}()
if stdout != nil || stderr != nil {
if err := <-receiveStdout; err != nil {
return err
}
}
spew.Dump(br)
go func() {
for {
fmt.Println(br)
spew.Dump(br)
}
}()
return nil
}