forked from coder/wsep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
157 lines (142 loc) · 3.64 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
package wsep
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net"
"go.coder.com/flog"
"golang.org/x/sync/errgroup"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"cdr.dev/wsep/internal/proto"
)
// Serve runs the server-side of wsep.
// The execer may be another wsep connection for chaining.
// Use LocalExecer for local command execution.
func Serve(ctx context.Context, c *websocket.Conn, execer Execer) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
c.SetReadLimit(maxMessageSize)
var (
header proto.Header
process Process
wsNetConn = websocket.NetConn(ctx, c, websocket.MessageBinary)
)
defer func() {
if process != nil {
process.Close()
}
}()
for {
if err := ctx.Err(); err != nil {
return err
}
_, byt, err := c.Read(ctx)
if xerrors.Is(err, io.EOF) {
return nil
}
if err != nil {
status := websocket.CloseStatus(err)
if status == -1 {
return xerrors.Errorf("read message: %w", err)
}
if status != websocket.StatusNormalClosure {
return err
}
return nil
}
headerByt, bodyByt := proto.SplitMessage(byt)
err = json.Unmarshal(headerByt, &header)
if err != nil {
return xerrors.Errorf("unmarshal header: %w", err)
}
switch header.Type {
case proto.TypeStart:
var header proto.ClientStartHeader
err = json.Unmarshal(byt, &header)
if err != nil {
return xerrors.Errorf("unmarshal start header: %w", err)
}
process, err = execer.Start(ctx, mapToClientCmd(header.Command))
if err != nil {
return err
}
_ = sendPID(ctx, process.Pid(), wsNetConn)
var outputgroup errgroup.Group
outputgroup.Go(func() error {
return copyWithHeader(process.Stdout(), wsNetConn, proto.Header{Type: proto.TypeStdout})
})
outputgroup.Go(func() error {
return copyWithHeader(process.Stderr(), wsNetConn, proto.Header{Type: proto.TypeStderr})
})
go func() {
defer wsNetConn.Close()
_ = outputgroup.Wait()
err = process.Wait()
if exitErr, ok := err.(ExitError); ok {
_ = sendExitCode(ctx, exitErr.Code, wsNetConn)
return
}
_ = sendExitCode(ctx, 0, wsNetConn)
}()
case proto.TypeResize:
if process == nil {
return errors.New("resize sent before command started")
}
var header proto.ClientResizeHeader
err = json.Unmarshal(byt, &header)
if err != nil {
return xerrors.Errorf("unmarshal resize header: %w", err)
}
err = process.Resize(ctx, header.Rows, header.Cols)
if err != nil {
return xerrors.Errorf("resize: %w", err)
}
case proto.TypeStdin:
_, err := io.Copy(process.Stdin(), bytes.NewReader(bodyByt))
if err != nil {
return xerrors.Errorf("read stdin: %w", err)
}
case proto.TypeCloseStdin:
err = process.Stdin().Close()
if err != nil {
return xerrors.Errorf("close stdin: %w", err)
}
default:
flog.Error("unrecognized header type: %s", header.Type)
}
}
}
func sendExitCode(_ context.Context, exitCode int, conn net.Conn) error {
header, err := json.Marshal(proto.ServerExitCodeHeader{
Type: proto.TypeExitCode,
ExitCode: exitCode,
})
if err != nil {
return err
}
_, err = proto.WithHeader(conn, header).Write(nil)
return err
}
func sendPID(_ context.Context, pid int, conn net.Conn) error {
header, err := json.Marshal(proto.ServerPidHeader{Type: proto.TypePid, Pid: pid})
if err != nil {
return err
}
_, err = proto.WithHeader(conn, header).Write(nil)
return err
}
func copyWithHeader(r io.Reader, w io.Writer, header proto.Header) error {
headerByt, err := json.Marshal(header)
if err != nil {
return err
}
wr := proto.WithHeader(w, headerByt)
_, err = io.Copy(wr, r)
if err != nil {
return err
}
return nil
}