Repository navigation
Expand file tree
/
Copy pathwebsocket.go
More file actions
289 lines (257 loc) · 9.3 KB
/
Copy pathwebsocket.go
File metadata and controls
289 lines (257 loc) · 9.3 KB
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"slices"
"strconv"
"strings"
"sync/atomic"
"time"
"unicode/utf8"
"github.com/gorilla/websocket"
"golang.org/x/net/publicsuffix"
)
const (
// maxMessageSize - max size of a WebSocket message from the client, a bigger message closes the connection
maxMessageSize = maxLineSize
// stdinQueueSize - how many messages from the client may wait for the command to read stdin
// (in addition to the pipe buffer), when the queue is full the command is stopped
stdinQueueSize = 64
)
// getWebSocketHandler - get handler function for a command connected to the client by WebSocket (endpoint option websocket=1):
// each message from the client is written to stdin with "\n" appended, each line of the output is sent as a message,
// when the command has finished the connection is closed with the exit code as the close reason
func getWebSocketHandler(appConfig Config, shell string, params []string, allowOrigin string) http.HandlerFunc {
upgrader := websocket.Upgrader{
HandshakeTimeout: waitDelay,
CheckOrigin: originChecker(allowOrigin),
}
return func(rw http.ResponseWriter, req *http.Request) {
// check the handshake before the command is prepared,
// a request body is not expected (with -cgi it would be written to stdin)
if req.Method != http.MethodGet {
rw.Header().Set("Allow", http.MethodGet)
http.Error(rw, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if !websocket.IsWebSocketUpgrade(req) {
http.Error(rw, "WebSocket handshake expected", http.StatusBadRequest)
return
}
// a handshake has no body, and with -form a body (eg: multipart uploads) would be parsed before the upgrade
if req.ContentLength != 0 {
http.Error(rw, "WebSocket handshake must not have a body", http.StatusBadRequest)
return
}
if !upgrader.CheckOrigin(req) {
log.Printf("websocket: origin %q is not allowed", req.Header.Get("Origin"))
http.Error(rw, http.StatusText(http.StatusForbidden), http.StatusForbidden)
return
}
if appConfig.hijacked != nil {
appConfig.hijacked.Add(1)
defer appConfig.hijacked.Done()
}
// the command is killed when the client has gone, on timeout and on shutdown of the server,
// a hijacked connection doesn't cancel the request context, the client is watched by reading messages
ctx, cancelFn := context.WithCancel(req.Context())
defer cancelFn()
stopOnShutdown := context.AfterFunc(appConfig.commandContext(), cancelFn)
defer stopOnShutdown()
if appConfig.timeout > 0 {
var cancelTimeout context.CancelFunc
ctx, cancelTimeout = context.WithTimeout(ctx, time.Duration(appConfig.timeout)*time.Second)
defer cancelTimeout()
}
shellCmd, err := newShellCommand(ctx, appConfig, shell, params, req)
if err != nil {
if !writeFormError(rw, err) {
log.Printf("prepare command failed: %s", err)
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
return
}
conn, err := upgrader.Upgrade(rw, req, nil)
if err != nil {
// the error response is already sent
log.Printf("websocket: handshake failed: %s", err)
shellCmd.finalizer()
return
}
defer func() { _ = conn.Close() }()
conn.SetReadLimit(maxMessageSize)
stdin, err := shellCmd.cmd.StdinPipe()
if err != nil {
log.Printf("websocket: stdin of the command failed: %s", err)
shellCmd.finalizer()
closeMsg := websocket.FormatCloseMessage(websocket.CloseInternalServerErr, "")
_ = conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(waitDelay))
return
}
// when the command is killed, a write to a client that doesn't read must not block forever,
// the deadline of gorilla/websocket can't be changed concurrently with a write, so the connection is closed
stopCloseOnKill := context.AfterFunc(ctx, func() {
time.AfterFunc(waitDelay, func() { _ = conn.NetConn().Close() })
})
defer stopCloseOnKill()
// the client has closed the connection or doesn't accept the output
var clientGone atomic.Bool
// messages to stdin are written by a worker, so the reader is not blocked by a command that doesn't read stdin
// and still sees the client closing the connection; a blocked write is interrupted when the command is killed
stdinQueue := make(chan []byte, stdinQueueSize)
stopCloseStdin := context.AfterFunc(ctx, func() { _ = stdin.Close() })
defer stopCloseStdin()
go func() {
var writeErr error
for message := range stdinQueue {
// the command has exited or closed stdin: the rest of the input is discarded
if writeErr == nil {
_, writeErr = stdin.Write(message)
}
}
}()
// messages from the client to stdin, until the client has closed the connection
readerDone := make(chan struct{})
go func() {
defer close(readerDone)
defer close(stdinQueue)
queueing := true
for {
_, message, err := conn.ReadMessage()
if err != nil {
if errors.Is(err, websocket.ErrReadLimit) {
log.Printf("websocket: a message from the client is bigger than %d bytes", maxMessageSize)
}
break
}
if !queueing {
continue
}
select {
case stdinQueue <- append(message, '\n'):
default:
// the command doesn't read stdin, the input can't be buffered without a limit
log.Printf("websocket: the command doesn't read stdin, more than %d messages are waiting, the command is stopped", stdinQueueSize)
queueing = false
cancelFn()
}
}
clientGone.Store(true)
cancelFn()
}()
stdout := &lineWriter{fn: func(line []byte) error {
messageType := websocket.TextMessage
if !utf8.Valid(line) {
// a text message must be valid UTF-8
messageType = websocket.BinaryMessage
}
if err := conn.WriteMessage(messageType, line); err != nil {
clientGone.Store(true)
cancelFn()
return err
}
return nil
}}
stderrLog := &lineWriter{fn: func(line []byte) error {
log.Printf("stderr: %s", line)
return nil
}}
var stderr io.Writer = stderrLog
if appConfig.includeStderr {
// the same writer for stdout and stderr: exec.Cmd uses one pipe for both
// and writes from one goroutine, so messages are not written concurrently
stderr = stdout
}
exitCode, err := shellCmd.run(stdout, stderr)
// the last line without a line break
_ = stdout.flush()
_ = stderrLog.flush()
// a client disconnect is the usual end of an endless command, don't log it as an error
if err != nil && !clientGone.Load() {
log.Printf("exec error: %s", err)
}
// the exit code is sent as the close reason, then wait for the client to confirm the close
closeMsg := websocket.FormatCloseMessage(websocket.CloseNormalClosure, strconv.Itoa(exitCode))
if conn.WriteControl(websocket.CloseMessage, closeMsg, time.Now().Add(waitDelay)) == nil {
select {
case <-readerDone:
case <-time.After(waitDelay):
}
}
_ = conn.Close()
<-readerDone
}
}
// checkAllowOrigin - check the value of the allow-origin endpoint option
func checkAllowOrigin(value string) error {
switch value {
case allowOriginSameOrigin, allowOriginSameSite, allowOriginAll:
return nil
}
for origin := range strings.SplitSeq(value, ",") {
u, err := url.Parse(origin)
if err != nil || u.Scheme == "" || u.Host == "" || u.Opaque != "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return fmt.Errorf("%q is not an origin (scheme://host[:port]), supported: %s, %s, %s or a comma-separated list of origins",
origin, allowOriginSameOrigin, allowOriginSameSite, allowOriginAll)
}
}
return nil
}
// originChecker - get the function that checks the Origin header of a WebSocket handshake (the allow-origin option),
// a request without Origin is not from a browser and is always allowed
func originChecker(allowOrigin string) func(*http.Request) bool {
var isAllowed func(origin *url.URL, req *http.Request) bool
switch allowOrigin {
case allowOriginAll:
return func(*http.Request) bool { return true }
case "", allowOriginSameOrigin:
isAllowed = func(origin *url.URL, req *http.Request) bool {
return strings.EqualFold(origin.Host, req.Host)
}
case allowOriginSameSite:
isAllowed = func(origin *url.URL, req *http.Request) bool {
return siteOf(origin.Host) == siteOf(req.Host)
}
default:
origins := strings.Split(allowOrigin, ",")
isAllowed = func(origin *url.URL, _ *http.Request) bool {
return slices.ContainsFunc(origins, func(allowed string) bool {
return strings.EqualFold(allowed, origin.Scheme+"://"+origin.Host)
})
}
}
return func(req *http.Request) bool {
values := req.Header["Origin"]
if len(values) == 0 {
return true
}
origin, err := url.Parse(values[0])
if err != nil || origin.Host == "" {
// eg: "null" from a sandboxed page or a local file
return false
}
return isAllowed(origin, req)
}
}
// siteOf - get the site (registrable domain, eg: example.com for api.example.com) of the host with optional port,
// an IP address, localhost or a public suffix is the site itself
func siteOf(hostPort string) string {
host := hostPort
if h, _, err := net.SplitHostPort(hostPort); err == nil {
host = h
}
host = strings.ToLower(strings.TrimSuffix(strings.Trim(host, "[]"), "."))
if net.ParseIP(host) != nil {
return host
}
if site, err := publicsuffix.EffectiveTLDPlusOne(host); err == nil {
return site
}
return host
}