Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
340 lines (319 loc) · 9.71 KB
/
Copy pathmain.go
File metadata and controls
340 lines (319 loc) · 9.71 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
// work — the front door to your dev box.
//
// work login <email> sign in (your own Google account, via Jumpstart Lab SSO)
// work open your box (if you have exactly one)
// work <box> wake it if it is asleep, then attach its `work` tmux session
// work list your boxes and whether each is running
// work code <box> [dir] open VS Code on this machine, Remote-SSH'd into the box
// work doctor check sign-in, Switchboard, tailnet, and et/ssh
// work logout forget the sign-in
//
// Boxes live on your own tailnet; Switchboard (boxes.mine / status / wake) says
// which are yours and starts one that the idle reaper has suspended.
package main
import (
"encoding/base64"
"errors"
"fmt"
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
)
var version = "dev"
func main() {
if err := run(os.Args[1:]); err != nil {
fmt.Fprintln(os.Stderr, "work:", err)
os.Exit(1)
}
}
func run(args []string) error {
cmd := ""
if len(args) > 0 {
cmd = args[0]
}
switch cmd {
case "login":
email := ""
if len(args) > 1 {
email = args[1]
}
return login(email)
case "logout":
return logout()
case "list", "ls":
return list()
case "code":
if len(args) < 2 {
return errors.New("usage: work code <box> [dir]")
}
dir := "/workspace"
if len(args) > 2 {
dir = args[2]
}
return code(args[1], dir)
case "bus":
return bus(args[1:])
case "doctor":
return doctor()
case "version", "--version":
fmt.Println(version)
return nil
case "help", "-h", "--help":
fmt.Print(usage)
return nil
case "":
boxes, err := myBoxes()
if err != nil {
return err
}
switch len(boxes) {
case 0:
return errors.New("you have no boxes yet — ask Jeff")
case 1:
return open(boxes[0].Name)
default:
return errors.New("you have several boxes — `work list`, then `work <box>`")
}
default:
return open(cmd)
}
}
const usage = `usage: work [<box>] open your box (wakes it if asleep)
work login <email> sign in with your own work email
work bus enroll [name] put this machine on your private party-line bus
work list your boxes and their state
work code <box> [dir] VS Code here, Remote-SSH'd into the box
work doctor check the whole path
work logout forget the sign-in
`
func list() error {
boxes, err := myBoxes()
if err != nil {
return err
}
if len(boxes) == 0 {
fmt.Println("no boxes")
return nil
}
for _, b := range boxes {
state := "?"
if s, err := status(b.Name); err == nil {
state = s.State
if s.Health != "" && s.Health != "none" {
state += " (" + s.Health + ")"
}
} else {
state = "unknown: " + err.Error()
}
fmt.Printf("%-16s %-24s %s@%s\n", b.Name, state, b.Connect.User, b.Connect.Host)
}
return nil
}
// ready wakes the box if needed and waits until its et/ssh port answers on
// the tailnet, returning where to connect.
func ready(name string) (connect, error) {
s, err := wake(name)
if err != nil {
return connect{}, err
}
c := s.Connect
if c.Host == "" {
return c, fmt.Errorf("switchboard gave no connect host for %s", name)
}
deadline := time.Now().Add(90 * time.Second)
said := false
for {
if reachable(c.Host, "2022") || reachable(c.Host, "22") {
return c, nil
}
if time.Now().After(deadline) {
return c, fmt.Errorf("%s is %s but %s is not answering on your tailnet — is Tailscale up on this machine? (`work doctor`)", name, s.State, c.Host)
}
if !said {
fmt.Fprintf(os.Stderr, "waking %s…\n", name)
said = true
}
time.Sleep(3 * time.Second)
}
}
func open(name string) error {
c, err := ready(name)
if err != nil {
return err
}
if p, err := exec.LookPath("et"); err == nil && reachable(c.Host, "2022") {
return execve(p, connectArgs("et", c))
}
p, err := exec.LookPath("ssh")
if err != nil {
return errors.New("neither et nor ssh is installed")
}
return stayConnected(p, c)
}
// stayConnected runs ssh and, when the connection drops (the laptop slept,
// the network changed), reconnects to the same tmux session on the box, so
// nothing is lost and nothing extra (Eternal Terminal) has to be installed.
// ssh exits 255 for a lost connection AND for a refused login, so a drop is
// only retried after a session that had actually been up; a first attempt
// that fails at once is reported, not looped on.
// Tunables, variables so tests can shrink them.
var (
settled = 15 * time.Second // up this long = it was a real session
giveUp = 10 * time.Minute // stop retrying after this long offline
reconnectWait = 2 * time.Second
)
func stayConnected(sshPath string, c connect) error {
// Ctrl-C belongs to the remote terminal; don't let it kill the loop.
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
defer signal.Stop(sig)
openerPort, stopOpener := startOpener(openBrowser)
defer stopOpener()
everUp := false
var offlineSince time.Time
wait := reconnectWait
for {
start := time.Now()
cmd := exec.Command(sshPath, sshArgs(c, openerPort)[1:]...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
err := cmd.Run()
var exit *exec.ExitError
if err == nil || !errors.As(err, &exit) || exit.ExitCode() != 255 {
return err // detached, exited, or ssh itself failed to start
}
if time.Since(start) >= settled {
everUp, offlineSince, wait = true, time.Now(), reconnectWait
}
if !everUp {
return fmt.Errorf("could not connect to %s (see the message above; `work doctor` checks each piece)", c.Host)
}
if time.Since(offlineSince) > giveUp {
return fmt.Errorf("lost the connection to %s and could not get it back for %s; run `work` when you're online", c.Host, giveUp)
}
fmt.Fprintf(os.Stderr, "\r\nconnection to %s lost — reconnecting…\r\n", c.Host)
time.Sleep(wait)
if wait < 30*time.Second {
wait *= 2
}
}
}
// mcpCallbackPort is where Claude Code on the box listens for an MCP sign-in's
// OAuth redirect (the switchboard entry's `callbackPort`, set by server-admin's
// devbox user-prep). The browser doing the sign-in runs on THIS machine, so its
// redirect to localhost:<port> has to be carried to the box: every connection
// forwards it. If the port is busy here (a second session), ssh and et warn and
// carry on.
const mcpCallbackPort = "3335"
// connectArgs is the argv for opening the box's persistent tmux session with
// the MCP callback port forwarded.
func connectArgs(tool string, c connect) []string {
target := c.User + "@" + c.Host
attach := "tmux new-session -A -s work"
if tool == "et" {
return []string{"et", "-t", mcpCallbackPort + ":" + mcpCallbackPort, target, "-c", attach}
}
return sshArgs(c, "")
}
// sshArgs is connectArgs for ssh; openerPort, when set, is the local port that
// opens the box's sign-in links (opener.go).
func sshArgs(c connect, openerPort string) []string {
args := []string{"ssh", "-t",
// Notice a dead connection within ~45s instead of hanging on it.
"-o", "ServerAliveInterval=15", "-o", "ServerAliveCountMax=3",
// macOS: ask for the key's passphrase once and keep it in the Keychain.
"-o", "IgnoreUnknown=UseKeychain", "-o", "UseKeychain=yes", "-o", "AddKeysToAgent=yes",
"-L", mcpCallbackPort + ":localhost:" + mcpCallbackPort}
if openerPort != "" {
args = append(args, "-R", "127.0.0.1:"+openPort+":127.0.0.1:"+openerPort)
}
return append(args, c.User+"@"+c.Host, "tmux new-session -A -s work")
}
func code(name, dir string) error {
c, err := ready(name)
if err != nil {
return err
}
p, err := exec.LookPath("code")
if err != nil {
return errors.New("VS Code's `code` command is not on PATH (VS Code → Command Palette → \"Shell Command: Install 'code' command\")")
}
return exec.Command(p, "--remote", "ssh-remote+"+c.User+"@"+c.Host, dir).Run()
}
func doctor() error {
ok := true
check := func(name string, err error, fix string) {
if err == nil {
fmt.Printf(" ok %s\n", name)
return
}
ok = false
fmt.Printf(" FAIL %s: %v\n → %s\n", name, err, fix)
}
_, err := accessToken()
check("signed in", err, "work login <your email>")
if err == nil {
who, _ := whoami()
fmt.Printf(" as %s\n", who)
}
boxes, err := myBoxes()
check("switchboard answers", err, "check your network; if it says refused, `work login`")
_, err = exec.LookPath("tailscale")
if err != nil {
_, err2 := os.Stat("/Applications/Tailscale.app")
if err2 == nil {
err = nil
}
}
check("tailscale installed", err, "install Tailscale and sign in to your own tailnet")
_, etErr := exec.LookPath("et")
check("et installed (optional; ssh is the fallback)", etErr, "brew install MisterTea/et/et")
for _, b := range boxes {
s, err := status(b.Name)
check("box "+b.Name+" known to switchboard", err, "ask Jeff")
if err == nil {
fmt.Printf(" %s (%s)\n", s.State, s.Health)
if s.State == "running" {
var e error
if !reachable(b.Connect.Host, "22") {
e = fmt.Errorf("%s:22 unreachable", b.Connect.Host)
}
check("box "+b.Name+" reachable on your tailnet", e, "is Tailscale connected? is "+b.Connect.Host+" in your tailnet's machine list?")
}
}
}
if !ok {
return errors.New("some checks failed")
}
return nil
}
func reachable(host, port string) bool {
conn, err := net.DialTimeout("tcp", net.JoinHostPort(host, port), 3*time.Second)
if err != nil {
return false
}
conn.Close()
return true
}
func execve(path string, argv []string) error {
return syscall.Exec(path, argv, os.Environ())
}
func openBrowser(u string) {
var c *exec.Cmd
switch runtime.GOOS {
case "darwin":
c = exec.Command("open", u)
case "linux":
c = exec.Command("xdg-open", u)
default:
return
}
_ = c.Start()
}
func decodeSegment(s string) ([]byte, error) {
return base64.RawURLEncoding.DecodeString(strings.TrimRight(s, "="))
}