-
Notifications
You must be signed in to change notification settings - Fork 62
/
server.go
192 lines (174 loc) · 5.24 KB
/
server.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
const (
dockerAPIVersion = "v1.35"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
type handler struct {
dcli *client.Client
dockerURL *url.URL
tlsConfig *tls.Config
}
type message struct {
Type string `json:"type"`
Data string `json:"data"`
Height uint `json:"height,omitempty"`
Width uint `json:"width,omitempty"`
}
// pingHander returns pong.
func pingHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, "pong")
}
// infoHander returns information about the connected docker daemon.
func (h *handler) infoHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
w.WriteHeader(http.StatusNotFound)
return
}
info, err := h.dcli.Info(context.Background())
if err != nil {
logrus.Errorf("getting docker info failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
b, err := json.MarshalIndent(info, "", " ")
if err != nil {
logrus.Errorf("marshal indent info failed: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", b)
}
func (h *handler) websocketHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
logrus.Errorf("websocket upgrader failed: %v", err)
return
}
// start the container and create the container websocket connection
cid, containerWSConn, err := h.startContainer()
if err != nil {
logrus.Errorf("starting container failed: %v", err)
return
}
defer containerWSConn.Close()
logrus.Infof("container started with id: %s", cid)
// start a go routine to listen on the container websocket and send to the browser websocket
done := make(chan struct{})
go func() {
defer containerWSConn.Close()
defer close(done)
for {
// TODO: this will panic if we couldn't start the container
_, msg, err := containerWSConn.ReadMessage()
if err != nil {
if e, ok := err.(*websocket.CloseError); ok {
logrus.Warnf("container websocket closed %s %d", e.Text, e.Code)
// cleanup and remove the container
if err := h.removeContainer(cid); err != nil {
logrus.Errorf("removing container %s failed: %v", cid, err)
}
// cleanly close the browser connection
if err := conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")); err != nil {
logrus.Errorf("closing broswer websocket failed: %v", err)
}
break
}
if err == io.EOF {
continue
}
logrus.Errorf("reading from container websocket failed: %v", err)
continue
}
logrus.Debugf("received from container websocket: %s", string(msg))
// send it back through to the browser websocket as a binary frame
b := message{
Type: "stdout",
Data: string(msg),
}
if err := conn.WriteJSON(b); err != nil {
if err == websocket.ErrCloseSent {
logrus.Warn("browser websocket close sent")
// cleanup and remove the container
if err := h.removeContainer(cid); err != nil {
logrus.Errorf("removing container %s failed: %v", cid, err)
}
break
}
logrus.Errorf("writing to browser websocket failed: %v", err)
continue
}
logrus.Debugf("wrote to browser websocket: %#v", b)
}
}()
for {
var data message
if err := conn.ReadJSON(&data); err != nil {
if e, ok := err.(*websocket.CloseError); ok {
logrus.Warnf("browser websocket closed %s %d", e.Text, e.Code)
// cleanup and remove the container
if err := h.removeContainer(cid); err != nil {
logrus.Errorf("removing container %s failed: %v", cid, err)
}
break
}
logrus.Errorf("reading from browser websocket failed: %v", err)
continue
}
logrus.Debugf("recieved from browser websocket: %#v", data)
// send to container websocket or resize
switch data.Type {
case "stdin":
if len(data.Data) > 0 {
if err := containerWSConn.WriteMessage(websocket.TextMessage, []byte(data.Data)); err != nil {
if err == websocket.ErrCloseSent {
logrus.Warn("container websocket close sent")
// cleanup and remove the container
if err := h.removeContainer(cid); err != nil {
logrus.Errorf("removing container %s failed: %v", cid, err)
}
break
}
logrus.Errorf("writing to container websocket failed: %v", err)
continue
}
logrus.Debugf("wrote to container websocket: %q", data.Data)
}
case "resize":
if err := h.dcli.ContainerResize(context.Background(), cid, types.ResizeOptions{
Height: data.Height,
Width: data.Width,
}); err != nil {
logrus.Errorf("resize container to height -> %d, width: %d failed: %v", data.Height, data.Width, err)
}
default:
logrus.Warnf("got unknown data type: %s", data.Type)
}
}
// cleanup and remove the container
if err := h.removeContainer(cid); err != nil {
logrus.Errorf("removing container %s failed: %v", cid, err)
}
}