-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.go
520 lines (480 loc) · 13.2 KB
/
socket.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !appengine
// Package socket implements an WebSocket-based playground backend.
// Clients connect to a websocket handler and send run/kill commands, and
// the server sends the output and exit status of the running processes.
// Multiple clients running multiple processes may be served concurrently.
// The wire format is JSON and is described by the Message type.
//
// This will not run on App Engine as WebSockets are not supported there.
package socket // import "github.com/april1989/origin-go-tools/playground/socket"
import (
"bytes"
"encoding/json"
"errors"
"go/parser"
"go/token"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
"unicode/utf8"
"golang.org/x/net/websocket"
"github.com/april1989/origin-go-tools/txtar"
)
// RunScripts specifies whether the socket handler should execute shell scripts
// (snippets that start with a shebang).
var RunScripts = true
// Environ provides an environment when a binary, such as the go tool, is
// invoked.
var Environ func() []string = os.Environ
const (
// The maximum number of messages to send per session (avoid flooding).
msgLimit = 1000
// Batch messages sent in this interval and send as a single message.
msgDelay = 10 * time.Millisecond
)
// Message is the wire format for the websocket connection to the browser.
// It is used for both sending output messages and receiving commands, as
// distinguished by the Kind field.
type Message struct {
Id string // client-provided unique id for the process
Kind string // in: "run", "kill" out: "stdout", "stderr", "end"
Body string
Options *Options `json:",omitempty"`
}
// Options specify additional message options.
type Options struct {
Race bool // use -race flag when building code (for "run" only)
}
// NewHandler returns a websocket server which checks the origin of requests.
func NewHandler(origin *url.URL) websocket.Server {
return websocket.Server{
Config: websocket.Config{Origin: origin},
Handshake: handshake,
Handler: websocket.Handler(socketHandler),
}
}
// handshake checks the origin of a request during the websocket handshake.
func handshake(c *websocket.Config, req *http.Request) error {
o, err := websocket.Origin(c, req)
if err != nil {
log.Println("bad websocket origin:", err)
return websocket.ErrBadWebSocketOrigin
}
_, port, err := net.SplitHostPort(c.Origin.Host)
if err != nil {
log.Println("bad websocket origin:", err)
return websocket.ErrBadWebSocketOrigin
}
ok := c.Origin.Scheme == o.Scheme && (c.Origin.Host == o.Host || c.Origin.Host == net.JoinHostPort(o.Host, port))
if !ok {
log.Println("bad websocket origin:", o)
return websocket.ErrBadWebSocketOrigin
}
log.Println("accepting connection from:", req.RemoteAddr)
return nil
}
// socketHandler handles the websocket connection for a given present session.
// It handles transcoding Messages to and from JSON format, and starting
// and killing processes.
func socketHandler(c *websocket.Conn) {
in, out := make(chan *Message), make(chan *Message)
errc := make(chan error, 1)
// Decode messages from client and send to the in channel.
go func() {
dec := json.NewDecoder(c)
for {
var m Message
if err := dec.Decode(&m); err != nil {
errc <- err
return
}
in <- &m
}
}()
// Receive messages from the out channel and encode to the client.
go func() {
enc := json.NewEncoder(c)
for m := range out {
if err := enc.Encode(m); err != nil {
errc <- err
return
}
}
}()
defer close(out)
// Start and kill processes and handle errors.
proc := make(map[string]*process)
for {
select {
case m := <-in:
switch m.Kind {
case "run":
log.Println("running snippet from:", c.Request().RemoteAddr)
proc[m.Id].Kill()
proc[m.Id] = startProcess(m.Id, m.Body, out, m.Options)
case "kill":
proc[m.Id].Kill()
}
case err := <-errc:
if err != io.EOF {
// A encode or decode has failed; bail.
log.Println(err)
}
// Shut down any running processes.
for _, p := range proc {
p.Kill()
}
return
}
}
}
// process represents a running process.
type process struct {
out chan<- *Message
done chan struct{} // closed when wait completes
run *exec.Cmd
path string
}
// startProcess builds and runs the given program, sending its output
// and end event as Messages on the provided channel.
func startProcess(id, body string, dest chan<- *Message, opt *Options) *process {
var (
done = make(chan struct{})
out = make(chan *Message)
p = &process{out: out, done: done}
)
go func() {
defer close(done)
for m := range buffer(limiter(out, p), time.After) {
m.Id = id
dest <- m
}
}()
var err error
if path, args := shebang(body); path != "" {
if RunScripts {
err = p.startProcess(path, args, body)
} else {
err = errors.New("script execution is not allowed")
}
} else {
err = p.start(body, opt)
}
if err != nil {
p.end(err)
return nil
}
go func() {
p.end(p.run.Wait())
}()
return p
}
// end sends an "end" message to the client, containing the process id and the
// given error value. It also removes the binary, if present.
func (p *process) end(err error) {
if p.path != "" {
defer os.RemoveAll(p.path)
}
m := &Message{Kind: "end"}
if err != nil {
m.Body = err.Error()
}
p.out <- m
close(p.out)
}
// A killer provides a mechanism to terminate a process.
// The Kill method returns only once the process has exited.
type killer interface {
Kill()
}
// limiter returns a channel that wraps the given channel.
// It receives Messages from the given channel and sends them to the returned
// channel until it passes msgLimit messages, at which point it will kill the
// process and pass only the "end" message.
// When the given channel is closed, or when the "end" message is received,
// it closes the returned channel.
func limiter(in <-chan *Message, p killer) <-chan *Message {
out := make(chan *Message)
go func() {
defer close(out)
n := 0
for m := range in {
switch {
case n < msgLimit || m.Kind == "end":
out <- m
if m.Kind == "end" {
return
}
case n == msgLimit:
// Kill in a goroutine as Kill will not return
// until the process' output has been
// processed, and we're doing that in this loop.
go p.Kill()
default:
continue // don't increment
}
n++
}
}()
return out
}
// buffer returns a channel that wraps the given channel. It receives messages
// from the given channel and sends them to the returned channel.
// Message bodies are gathered over the period msgDelay and coalesced into a
// single Message before they are passed on. Messages of the same kind are
// coalesced; when a message of a different kind is received, any buffered
// messages are flushed. When the given channel is closed, buffer flushes the
// remaining buffered messages and closes the returned channel.
// The timeAfter func should be time.After. It exists for golibexec_testing.
func buffer(in <-chan *Message, timeAfter func(time.Duration) <-chan time.Time) <-chan *Message {
out := make(chan *Message)
go func() {
defer close(out)
var (
tc <-chan time.Time
buf []byte
kind string
flush = func() {
if len(buf) == 0 {
return
}
out <- &Message{Kind: kind, Body: safeString(buf)}
buf = buf[:0] // recycle buffer
kind = ""
}
)
for {
select {
case m, ok := <-in:
if !ok {
flush()
return
}
if m.Kind == "end" {
flush()
out <- m
return
}
if kind != m.Kind {
flush()
kind = m.Kind
if tc == nil {
tc = timeAfter(msgDelay)
}
}
buf = append(buf, m.Body...)
case <-tc:
flush()
tc = nil
}
}
}()
return out
}
// Kill stops the process if it is running and waits for it to exit.
func (p *process) Kill() {
if p == nil || p.run == nil {
return
}
p.run.Process.Kill()
<-p.done // block until process exits
}
// shebang looks for a shebang ('#!') at the beginning of the passed string.
// If found, it returns the path and args after the shebang.
// args includes the command as args[0].
func shebang(body string) (path string, args []string) {
body = strings.TrimSpace(body)
if !strings.HasPrefix(body, "#!") {
return "", nil
}
if i := strings.Index(body, "\n"); i >= 0 {
body = body[:i]
}
fs := strings.Fields(body[2:])
return fs[0], fs
}
// startProcess starts a given program given its path and passing the given body
// to the command standard input.
func (p *process) startProcess(path string, args []string, body string) error {
cmd := &exec.Cmd{
Path: path,
Args: args,
Stdin: strings.NewReader(body),
Stdout: &messageWriter{kind: "stdout", out: p.out},
Stderr: &messageWriter{kind: "stderr", out: p.out},
}
if err := cmd.Start(); err != nil {
return err
}
p.run = cmd
return nil
}
// start builds and starts the given program, sending its output to p.out,
// and stores the running *exec.Cmd in the run field.
func (p *process) start(body string, opt *Options) error {
// We "go build" and then exec the binary so that the
// resultant *exec.Cmd is a handle to the user's program
// (rather than the go tool process).
// This makes Kill work.
path, err := ioutil.TempDir("", "present-")
if err != nil {
return err
}
p.path = path // to be removed by p.end
out := "prog"
if runtime.GOOS == "windows" {
out = "prog.exe"
}
bin := filepath.Join(path, out)
// write body to x.go files
a := txtar.Parse([]byte(body))
if len(a.Comment) != 0 {
a.Files = append(a.Files, txtar.File{Name: "prog.go", Data: a.Comment})
a.Comment = nil
}
hasModfile := false
for _, f := range a.Files {
err = ioutil.WriteFile(filepath.Join(path, f.Name), f.Data, 0666)
if err != nil {
return err
}
if f.Name == "go.mod" {
hasModfile = true
}
}
// build x.go, creating x
args := []string{"go", "build", "-tags", "OMIT"}
if opt != nil && opt.Race {
p.out <- &Message{
Kind: "stderr",
Body: "Running with race detector.\n",
}
args = append(args, "-race")
}
args = append(args, "-o", bin)
cmd := p.cmd(path, args...)
if !hasModfile {
cmd.Env = append(cmd.Env, "GO111MODULE=off")
}
cmd.Stdout = cmd.Stderr // send compiler output to stderr
if err := cmd.Run(); err != nil {
return err
}
// run x
if isNacl() {
cmd, err = p.naclCmd(bin)
if err != nil {
return err
}
} else {
cmd = p.cmd("", bin)
}
if opt != nil && opt.Race {
cmd.Env = append(cmd.Env, "GOMAXPROCS=2")
}
if err := cmd.Start(); err != nil {
// If we failed to exec, that might be because they built
// a non-main package instead of an executable.
// Check and report that.
if name, err := packageName(body); err == nil && name != "main" {
return errors.New(`executable programs must use "package main"`)
}
return err
}
p.run = cmd
return nil
}
// cmd builds an *exec.Cmd that writes its standard output and error to the
// process' output channel.
func (p *process) cmd(dir string, args ...string) *exec.Cmd {
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = dir
cmd.Env = Environ()
cmd.Stdout = &messageWriter{kind: "stdout", out: p.out}
cmd.Stderr = &messageWriter{kind: "stderr", out: p.out}
return cmd
}
func isNacl() bool {
for _, v := range append(Environ(), os.Environ()...) {
if v == "GOOS=nacl" {
return true
}
}
return false
}
// naclCmd returns an *exec.Cmd that executes bin under native client.
func (p *process) naclCmd(bin string) (*exec.Cmd, error) {
pwd, err := os.Getwd()
if err != nil {
return nil, err
}
var args []string
env := []string{
"NACLENV_GOOS=" + runtime.GOOS,
"NACLENV_GOROOT=/go",
"NACLENV_NACLPWD=" + strings.Replace(pwd, runtime.GOROOT(), "/go", 1),
}
switch runtime.GOARCH {
case "amd64":
env = append(env, "NACLENV_GOARCH=amd64p32")
args = []string{"sel_ldr_x86_64"}
case "386":
env = append(env, "NACLENV_GOARCH=386")
args = []string{"sel_ldr_x86_32"}
case "arm":
env = append(env, "NACLENV_GOARCH=arm")
selLdr, err := exec.LookPath("sel_ldr_arm")
if err != nil {
return nil, err
}
args = []string{"nacl_helper_bootstrap_arm", selLdr, "--reserved_at_zero=0xXXXXXXXXXXXXXXXX"}
default:
return nil, errors.New("native client does not support GOARCH=" + runtime.GOARCH)
}
cmd := p.cmd("", append(args, "-l", "/dev/null", "-S", "-e", bin)...)
cmd.Env = append(cmd.Env, env...)
return cmd, nil
}
func packageName(body string) (string, error) {
f, err := parser.ParseFile(token.NewFileSet(), "prog.go",
strings.NewReader(body), parser.PackageClauseOnly)
if err != nil {
return "", err
}
return f.Name.String(), nil
}
// messageWriter is an io.Writer that converts all writes to Message sends on
// the out channel with the specified id and kind.
type messageWriter struct {
kind string
out chan<- *Message
}
func (w *messageWriter) Write(b []byte) (n int, err error) {
w.out <- &Message{Kind: w.kind, Body: safeString(b)}
return len(b), nil
}
// safeString returns b as a valid UTF-8 string.
func safeString(b []byte) string {
if utf8.Valid(b) {
return string(b)
}
var buf bytes.Buffer
for len(b) > 0 {
r, size := utf8.DecodeRune(b)
b = b[size:]
buf.WriteRune(r)
}
return buf.String()
}