forked from prime-radiant-inc/stockyard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcover.html
More file actions
355 lines (301 loc) · 10.5 KB
/
cover.html
File metadata and controls
355 lines (301 loc) · 10.5 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
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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>shell: Go Coverage Report</title>
<style>
body {
background: black;
color: rgb(80, 80, 80);
}
body, pre, #legend span {
font-family: Menlo, monospace;
font-weight: bold;
}
#topbar {
background: black;
position: fixed;
top: 0; left: 0; right: 0;
height: 42px;
border-bottom: 1px solid rgb(80, 80, 80);
}
#content {
margin-top: 50px;
}
#nav, #legend {
float: left;
margin-left: 10px;
}
#legend {
margin-top: 12px;
}
#nav {
margin-top: 10px;
}
#legend span {
margin: 0 5px;
}
.cov0 { color: rgb(192, 0, 0) }
.cov1 { color: rgb(128, 128, 128) }
.cov2 { color: rgb(116, 140, 131) }
.cov3 { color: rgb(104, 152, 134) }
.cov4 { color: rgb(92, 164, 137) }
.cov5 { color: rgb(80, 176, 140) }
.cov6 { color: rgb(68, 188, 143) }
.cov7 { color: rgb(56, 200, 146) }
.cov8 { color: rgb(44, 212, 149) }
.cov9 { color: rgb(32, 224, 152) }
.cov10 { color: rgb(20, 236, 155) }
</style>
</head>
<body>
<div id="topbar">
<div id="nav">
<select id="files">
<option value="file0">github.com/obra/stockyard/pkg/shell/protocol.go (87.1%)</option>
<option value="file1">github.com/obra/stockyard/pkg/shell/session.go (11.4%)</option>
</select>
</div>
<div id="legend">
<span>not tracked</span>
<span class="cov0">not covered</span>
<span class="cov8">covered</span>
</div>
</div>
<div id="content">
<pre class="file" id="file0" style="display: none">package shell
import (
"encoding/binary"
"encoding/json"
"fmt"
"io"
)
// ShellPort is the vsock port for the shell service.
// Used by both stockyard-shell (VM) and dashboard (host).
const ShellPort = 52
// Message types for vsock shell protocol
const (
MsgOpen uint8 = 0x01 // Host -> VM: open session
MsgData uint8 = 0x02 // Bidirectional: terminal data
MsgResize uint8 = 0x03 // Host -> VM: resize terminal
MsgExit uint8 = 0x04 // VM -> Host: shell exited
MsgError uint8 = 0x05 // VM -> Host: error occurred
)
// MaxPayloadSize limits message payload to 1MB
const MaxPayloadSize = 1024 * 1024
// WriteMessage writes a framed message: type(1) + length(4) + payload
func WriteMessage(w io.Writer, msgType uint8, payload []byte) error <span class="cov8" title="1">{
// Write type
if _, err := w.Write([]byte{msgType}); err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("write type: %w", err)
}</span>
// Write length (big-endian)
<span class="cov8" title="1">length := uint32(len(payload))
if err := binary.Write(w, binary.BigEndian, length); err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("write length: %w", err)
}</span>
// Write payload
<span class="cov8" title="1">if len(payload) > 0 </span><span class="cov8" title="1">{
if _, err := w.Write(payload); err != nil </span><span class="cov0" title="0">{
return fmt.Errorf("write payload: %w", err)
}</span>
}
<span class="cov8" title="1">return nil</span>
}
// ReadMessage reads a framed message, returns type and payload
func ReadMessage(r io.Reader) (uint8, []byte, error) <span class="cov8" title="1">{
// Read type
typeBuf := make([]byte, 1)
if _, err := io.ReadFull(r, typeBuf); err != nil </span><span class="cov8" title="1">{
return 0, nil, fmt.Errorf("read type: %w", err)
}</span>
<span class="cov8" title="1">msgType := typeBuf[0]
// Read length
var length uint32
if err := binary.Read(r, binary.BigEndian, &length); err != nil </span><span class="cov0" title="0">{
return 0, nil, fmt.Errorf("read length: %w", err)
}</span>
<span class="cov8" title="1">if length > MaxPayloadSize </span><span class="cov8" title="1">{
return 0, nil, fmt.Errorf("payload too large: %d > %d", length, MaxPayloadSize)
}</span>
// Read payload
<span class="cov8" title="1">payload := make([]byte, length)
if length > 0 </span><span class="cov8" title="1">{
if _, err := io.ReadFull(r, payload); err != nil </span><span class="cov8" title="1">{
return 0, nil, fmt.Errorf("read payload: %w", err)
}</span>
}
<span class="cov8" title="1">return msgType, payload, nil</span>
}
// OpenMessage requests a shell session for a user.
// Includes terminal type and initial window size.
type OpenMessage struct {
User string `json:"user"`
Term string `json:"term"` // e.g., "xterm-256color"
Cols int `json:"cols"`
Rows int `json:"rows"`
}
func (m *OpenMessage) Marshal() ([]byte, error) <span class="cov8" title="1">{
return json.Marshal(m)
}</span>
func (m *OpenMessage) Unmarshal(data []byte) error <span class="cov8" title="1">{
return json.Unmarshal(data, m)
}</span>
// ResizeMessage requests terminal resize
type ResizeMessage struct {
Cols int `json:"cols"`
Rows int `json:"rows"`
}
func (m *ResizeMessage) Marshal() ([]byte, error) <span class="cov8" title="1">{
return json.Marshal(m)
}</span>
func (m *ResizeMessage) Unmarshal(data []byte) error <span class="cov8" title="1">{
return json.Unmarshal(data, m)
}</span>
// ExitMessage indicates shell has exited
type ExitMessage struct {
Code int `json:"code"`
}
func (m *ExitMessage) Marshal() ([]byte, error) <span class="cov8" title="1">{
return json.Marshal(m)
}</span>
func (m *ExitMessage) Unmarshal(data []byte) error <span class="cov8" title="1">{
return json.Unmarshal(data, m)
}</span>
// ErrorMessage indicates an error
type ErrorMessage struct {
Error string `json:"error"`
}
func (m *ErrorMessage) Marshal() ([]byte, error) <span class="cov8" title="1">{
return json.Marshal(m)
}</span>
func (m *ErrorMessage) Unmarshal(data []byte) error <span class="cov8" title="1">{
return json.Unmarshal(data, m)
}</span>
</pre>
<pre class="file" id="file1" style="display: none">package shell
import (
"fmt"
"os"
"os/exec"
"os/user"
"sync"
"github.com/creack/pty"
)
// ValidateUser checks if a user exists on the system
func ValidateUser(username string) error <span class="cov8" title="1">{
_, err := user.Lookup(username)
if err != nil </span><span class="cov8" title="1">{
return fmt.Errorf("user %q not found: %w", username, err)
}</span>
<span class="cov8" title="1">return nil</span>
}
// Session represents an active shell session
type Session struct {
user string
cmd *exec.Cmd
ptyFile *os.File
mu sync.Mutex
closed bool
}
// NewSession creates a new shell session for the given user.
// Requires root privileges to use login -f.
// Sets TERM environment variable and initial window size.
func NewSession(username, term string, cols, rows int) (*Session, error) <span class="cov0" title="0">{
if err := ValidateUser(username); err != nil </span><span class="cov0" title="0">{
return nil, err
}</span>
// Use login -f for a proper login shell with full environment setup
// This handles PAM, sets up environment variables, etc.
<span class="cov0" title="0">cmd := exec.Command("login", "-f", username)
// Set TERM environment variable
cmd.Env = append(os.Environ(), "TERM="+term)
// Start with PTY
ptyFile, err := pty.StartWithSize(cmd, &pty.Winsize{
Cols: uint16(cols),
Rows: uint16(rows),
})
if err != nil </span><span class="cov0" title="0">{
return nil, fmt.Errorf("start pty: %w", err)
}</span>
<span class="cov0" title="0">return &Session{
user: username,
cmd: cmd,
ptyFile: ptyFile,
}, nil</span>
}
// PTY returns the PTY file for I/O
func (s *Session) PTY() *os.File <span class="cov0" title="0">{
return s.ptyFile
}</span>
// Resize changes the PTY window size
func (s *Session) Resize(cols, rows int) error <span class="cov0" title="0">{
s.mu.Lock()
defer s.mu.Unlock()
if s.closed </span><span class="cov0" title="0">{
return fmt.Errorf("session closed")
}</span>
<span class="cov0" title="0">return pty.Setsize(s.ptyFile, &pty.Winsize{
Cols: uint16(cols),
Rows: uint16(rows),
})</span>
}
// Wait waits for the shell to exit and returns the exit code
func (s *Session) Wait() (int, error) <span class="cov0" title="0">{
err := s.cmd.Wait()
if err != nil </span><span class="cov0" title="0">{
if exitErr, ok := err.(*exec.ExitError); ok </span><span class="cov0" title="0">{
return exitErr.ExitCode(), nil
}</span>
<span class="cov0" title="0">return -1, err</span>
}
<span class="cov0" title="0">return 0, nil</span>
}
// Close terminates the session
func (s *Session) Close() error <span class="cov0" title="0">{
s.mu.Lock()
defer s.mu.Unlock()
if s.closed </span><span class="cov0" title="0">{
return nil
}</span>
<span class="cov0" title="0">s.closed = true
if s.ptyFile != nil </span><span class="cov0" title="0">{
s.ptyFile.Close()
}</span>
<span class="cov0" title="0">if s.cmd != nil && s.cmd.Process != nil </span><span class="cov0" title="0">{
s.cmd.Process.Kill()
s.cmd.Wait()
}</span>
<span class="cov0" title="0">return nil</span>
}
</pre>
</div>
</body>
<script>
(function() {
var files = document.getElementById('files');
var visible;
files.addEventListener('change', onChange, false);
function select(part) {
if (visible)
visible.style.display = 'none';
visible = document.getElementById(part);
if (!visible)
return;
files.value = part;
visible.style.display = 'block';
location.hash = part;
}
function onChange() {
select(files.value);
window.scrollTo(0, 0);
}
if (location.hash != "") {
select(location.hash.substr(1));
}
if (!visible) {
select("file0");
}
})();
</script>
</html>