feat: functionality to open a sandbox workspace in vs code
This commit is contained in:
@@ -16,6 +16,8 @@ wsbx run -p 8000 alpine # host's localhost:8000 reachable as localh
|
|||||||
wsbx exec NAME # shell in a running sandbox (alias: sh), as the image's user
|
wsbx exec NAME # shell in a running sandbox (alias: sh), as the image's user
|
||||||
wsbx exec -u root NAME -- apk add git # command, exit code propagated
|
wsbx exec -u root NAME -- apk add git # command, exit code propagated
|
||||||
wsbx forward NAME 3000 8080:80 # sandbox's localhost:3000 on the Mac's localhost:3000 (until Ctrl-C; alias fwd)
|
wsbx forward NAME 3000 8080:80 # sandbox's localhost:3000 on the Mac's localhost:3000 (until Ctrl-C; alias fwd)
|
||||||
|
wsbx code NAME # VS Code in the browser, running in the sandbox (until Ctrl-C)
|
||||||
|
wsbx code NAME --desktop # desktop VS Code via Remote-SSH; also: ssh NAME.wsbx
|
||||||
wsbx ps # sandboxes: status, CPU, memory, net/block I/O, PIDs, disk, image (alias: ls)
|
wsbx ps # sandboxes: status, CPU, memory, net/block I/O, PIDs, disk, image (alias: ls)
|
||||||
wsbx ps -f # same, refreshed every second like `docker stats`
|
wsbx ps -f # same, refreshed every second like `docker stats`
|
||||||
wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start)
|
wsbx rm NAME... # delete stopped sandboxes; --disk deletes only the disk (fresh on next start)
|
||||||
@@ -35,6 +37,14 @@ You get a pty (with resize) when stdin and stdout are terminals, plain pipes oth
|
|||||||
**forward**: host `127.0.0.1:HOSTPORT` → `exec.sock` → vsock → guest helper → guest `localhost:PORT`, one exec-style session
|
**forward**: host `127.0.0.1:HOSTPORT` → `exec.sock` → vsock → guest helper → guest `localhost:PORT`, one exec-style session
|
||||||
per connection. Works for services bound only to the guest's loopback, on running sandboxes, no restart. `0:PORT` picks a free host port.
|
per connection. Works for services bound only to the guest's loopback, on running sandboxes, no restart. `0:PORT` picks a free host port.
|
||||||
|
|
||||||
|
**code**: browser mode fetches the official VS Code CLI into the sandbox (`~/.wsbx-vscode`, needs internet in the VM) and runs
|
||||||
|
`code serve-web` on the guest's loopback, forwarded like `forward` to a random host port behind a random connection token.
|
||||||
|
The CLI downloads the matching server on first open (glibc or Alpine images). `--desktop` writes `~/.wsbx/ssh_config` (a `NAME.wsbx`
|
||||||
|
host per sandbox) and adds an `Include` for it at the top of `~/.ssh/config` once. ssh reaches an SSH server in the guest helper through `wsbx exec`
|
||||||
|
(`ProxyCommand`): no port, no keys, you're the image user. Remote-SSH then installs its server in the VM as usual.
|
||||||
|
If the VM was killed mid-download and the server won't start, delete `~/.vscode/cli/serve-web` (browser) or `~/.vscode-server` (desktop) in the sandbox.
|
||||||
|
Both need a sandbox started by this wsbx version (the guest helper ships with each start).
|
||||||
|
|
||||||
**Host tunnel** (`-p PORT`): guest `localhost:PORT` → vsock → vfkit → unix socket → host `localhost:PORT`.
|
**Host tunnel** (`-p PORT`): guest `localhost:PORT` → vsock → vfkit → unix socket → host `localhost:PORT`.
|
||||||
Nothing listens on a host network interface, and only the listed ports are reachable.
|
Nothing listens on a host network interface, and only the listed ports are reachable.
|
||||||
|
|
||||||
@@ -66,6 +76,10 @@ The VM is the boundary. The guest (including a prompt-injected agent or a malici
|
|||||||
not from guest processes via vsock loopback.
|
not from guest processes via vsock loopback.
|
||||||
- **forward**: listens on the host's loopback only. Browsers don't isolate `localhost` cookies by port, so a forwarded guest
|
- **forward**: listens on the host's loopback only. Browsers don't isolate `localhost` cookies by port, so a forwarded guest
|
||||||
page can read non-HttpOnly cookies your other localhost apps set. Forward only what you need.
|
page can read non-HttpOnly cookies your other localhost apps set. Forward only what you need.
|
||||||
|
- **code**: the SSH server is only reachable through `wsbx exec` (your user's `~/.wsbx`), so it skips auth, with an ephemeral host key.
|
||||||
|
It allows sessions and host → guest forwarding only; guest → host forwarding (`-R`) and agent forwarding are refused. Desktop VS Code
|
||||||
|
auto-forwards ports the guest opens to your localhost (turn off `remote.autoForwardPorts` if unwanted), and its client
|
||||||
|
trusts the remote server more than a browser tab does. The browser mode has the same localhost-cookie caveat as `forward`.
|
||||||
- **Host tunnel**: only the ports you pass with `-p` (or the local `baseUrl`s in pi's `models.json`), over vsock.
|
- **Host tunnel**: only the ports you pass with `-p` (or the local `baseUrl`s in pi's `models.json`), over vsock.
|
||||||
|
|
||||||
Remaining risks:
|
Remaining risks:
|
||||||
|
|||||||
@@ -2,15 +2,13 @@
|
|||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
const SRC: [&str; 2] = ["guest/fwd.go", "guest/exec.go"];
|
println!("cargo:rerun-if-changed=guest"); // a directory: cargo watches everything in it
|
||||||
for f in SRC {
|
|
||||||
println!("cargo:rerun-if-changed={f}");
|
|
||||||
}
|
|
||||||
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("wsbx-guest");
|
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("wsbx-guest");
|
||||||
let ok = Command::new("go")
|
let ok = Command::new("go")
|
||||||
.args(["build", "-trimpath", "-ldflags=-s -w", "-o"])
|
.args(["build", "-trimpath", "-ldflags=-s -w", "-o"])
|
||||||
.arg(&out)
|
.arg(&out)
|
||||||
.args(SRC)
|
.arg(".")
|
||||||
|
.current_dir("guest")
|
||||||
.env("GOOS", "linux")
|
.env("GOOS", "linux")
|
||||||
.env("GOARCH", "arm64")
|
.env("GOARCH", "arm64")
|
||||||
.env("CGO_ENABLED", "0")
|
.env("CGO_ENABLED", "0")
|
||||||
|
|||||||
+10
-4
@@ -1,8 +1,10 @@
|
|||||||
// `wsbx exec` server: host -> vfkit unix socket -> vsock -> here.
|
// `wsbx exec` server: host -> vfkit unix socket -> vsock -> here.
|
||||||
//
|
//
|
||||||
// Protocol: the host sends one JSON request line, then frames go both ways: kind byte, u32 BE length, payload.
|
// Protocol: the host sends one JSON request line, then frames go both ways: kind byte, u32 BE length, payload.
|
||||||
|
//
|
||||||
// host -> guest: 'd' stdin data, 'e' stdin EOF, 'w' window size (rows, cols: u16 BE)
|
// host -> guest: 'd' stdin data, 'e' stdin EOF, 'w' window size (rows, cols: u16 BE)
|
||||||
// guest -> host: 'o' stdout, 'r' stderr, 'k' keepalive (ignored), 'x' exit status (u32 BE), always last
|
// guest -> host: 'o' stdout, 'r' stderr, 'k' keepalive (ignored), 'x' exit status (u32 BE), always last
|
||||||
|
//
|
||||||
// With Tty the command gets a pty (stdout carries everything); otherwise plain pipes.
|
// With Tty the command gets a pty (stdout carries everything); otherwise plain pipes.
|
||||||
// With Connect (`wsbx forward`) there's no command: 'd'/'e' go to the guest's localhost:Connect, its output
|
// With Connect (`wsbx forward`) there's no command: 'd'/'e' go to the guest's localhost:Connect, its output
|
||||||
// comes back as 'o', and 'x' marks its EOF (status 0) or a failed dial (1, after an 'r' message).
|
// comes back as 'o', and 'x' marks its EOF (status 0) or a failed dial (1, after an 'r' message).
|
||||||
@@ -174,11 +176,15 @@ func session(c *os.File) {
|
|||||||
case <-output:
|
case <-output:
|
||||||
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
|
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
|
||||||
}
|
}
|
||||||
code := uint32(cmd.ProcessState.ExitCode())
|
send('x', binary.BigEndian.AppendUint32(nil, exitCode(cmd.ProcessState)))
|
||||||
if ws, ok := cmd.ProcessState.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
|
|
||||||
code = 128 + uint32(ws.Signal())
|
|
||||||
}
|
}
|
||||||
send('x', binary.BigEndian.AppendUint32(nil, code))
|
|
||||||
|
// exitCode like a shell's $?: 128+N for a signal.
|
||||||
|
func exitCode(ps *os.ProcessState) uint32 {
|
||||||
|
if ws, ok := ps.Sys().(syscall.WaitStatus); ok && ws.Signaled() {
|
||||||
|
return 128 + uint32(ws.Signal())
|
||||||
|
}
|
||||||
|
return uint32(ps.ExitCode())
|
||||||
}
|
}
|
||||||
|
|
||||||
// forward pipes a session to the guest's localhost:port until both sides sent EOF or the host is gone.
|
// forward pipes a session to the guest's localhost:port until both sides sent EOF or the host is gone.
|
||||||
|
|||||||
+10
-1
@@ -1,5 +1,6 @@
|
|||||||
// wsbx-guest: guest helper, started by run.sh as root.
|
// wsbx-guest: guest helper, started by run.sh as root.
|
||||||
// Usage: wsbx-guest [-exec VSOCKPORT] PORT=VSOCKPORT...
|
// Usage: wsbx-guest [-exec VSOCKPORT] PORT=VSOCKPORT... | wsbx-guest -ssh-stdio
|
||||||
|
// -ssh-stdio serves one SSH connection on stdin/stdout (ssh.go), run through `wsbx exec`.
|
||||||
// -exec serves `wsbx exec` sessions (exec.go). Each PORT=VSOCKPORT is a host tunnel: listens on
|
// -exec serves `wsbx exec` sessions (exec.go). Each PORT=VSOCKPORT is a host tunnel: listens on
|
||||||
// 127.0.0.1:PORT and [::1]:PORT and pipes each connection to the host (vsock CID 2).
|
// 127.0.0.1:PORT and [::1]:PORT and pipes each connection to the host (vsock CID 2).
|
||||||
package main
|
package main
|
||||||
@@ -65,7 +66,15 @@ func handle(c *net.TCPConn, port uint32) {
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
execPort := flag.Uint("exec", 0, "serve `wsbx exec` sessions on this vsock port")
|
execPort := flag.Uint("exec", 0, "serve `wsbx exec` sessions on this vsock port")
|
||||||
|
sshStdio := flag.Bool("ssh-stdio", false, "serve one SSH connection on stdin/stdout")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
if *sshStdio {
|
||||||
|
if err := serveSSH(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, "wsbx-guest: ssh:", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
// ash starts '&' jobs with SIGINT/SIGQUIT ignored, and ignored signals survive exec: Ctrl-C would
|
// ash starts '&' jobs with SIGINT/SIGQUIT ignored, and ignored signals survive exec: Ctrl-C would
|
||||||
// not work in exec sessions. Handled signals are reset to default in children, so take them over.
|
// not work in exec sessions. Handled signals are reset to default in children, so take them over.
|
||||||
signal.Notify(make(chan os.Signal, 1), syscall.SIGINT, syscall.SIGQUIT)
|
signal.Notify(make(chan os.Signal, 1), syscall.SIGINT, syscall.SIGQUIT)
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
module wsbx-guest
|
||||||
|
|
||||||
|
go 1.26.0
|
||||||
|
|
||||||
|
require golang.org/x/crypto v0.58.0
|
||||||
|
|
||||||
|
require golang.org/x/sys v0.49.0 // indirect
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||||
|
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
|
||||||
|
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
|
||||||
|
golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||||
|
golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM=
|
||||||
|
golang.org/x/term v0.47.0/go.mod h1:efAfAuG6RKABvg1ArGebRkvX6LXul14SM67o2e4mUCY=
|
||||||
+211
@@ -0,0 +1,211 @@
|
|||||||
|
// `wsbx code --desktop`: an SSH server on stdin/stdout for VS Code Remote-SSH (or plain ssh), one per
|
||||||
|
// connection, started by the host's ProxyCommand: `wsbx exec NAME -- /.wsbx/host/guest -ssh-stdio`.
|
||||||
|
// The exec channel is the authentication (only the host user can open it), so the client gets in with no
|
||||||
|
// auth against an ephemeral host key, and runs as the user exec started us as (the image user).
|
||||||
|
// Supported: sessions (exec, shell, pty, env, window-change) and host -> guest forwarding (direct-tcpip,
|
||||||
|
// direct-streamlocal). Guest -> host forwarding (tcpip-forward) and agent forwarding are refused.
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"strconv"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/ssh"
|
||||||
|
)
|
||||||
|
|
||||||
|
func serveSSH() error {
|
||||||
|
_, key, err := ed25519.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
signer, err := ssh.NewSignerFromKey(key)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
cfg := &ssh.ServerConfig{NoClientAuth: true}
|
||||||
|
cfg.AddHostKey(signer)
|
||||||
|
_, chans, reqs, err := ssh.NewServerConn(stdio{}, cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
go ssh.DiscardRequests(reqs) // global requests: tcpip-forward etc. are refused
|
||||||
|
for nc := range chans {
|
||||||
|
switch nc.ChannelType() {
|
||||||
|
case "session":
|
||||||
|
go sshSession(nc)
|
||||||
|
case "direct-tcpip":
|
||||||
|
var p struct {
|
||||||
|
Host string
|
||||||
|
Port uint32
|
||||||
|
OrigHost string
|
||||||
|
OrigPort uint32
|
||||||
|
}
|
||||||
|
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
|
||||||
|
nc.Reject(ssh.ConnectionFailed, "bad request")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go sshForward(nc, "tcp", net.JoinHostPort(p.Host, strconv.Itoa(int(p.Port))))
|
||||||
|
case "direct-streamlocal@openssh.com":
|
||||||
|
var p struct {
|
||||||
|
Path string
|
||||||
|
Reserved0 string
|
||||||
|
Reserved1 uint32
|
||||||
|
}
|
||||||
|
if ssh.Unmarshal(nc.ExtraData(), &p) != nil {
|
||||||
|
nc.Reject(ssh.ConnectionFailed, "bad request")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
go sshForward(nc, "unix", p.Path)
|
||||||
|
default:
|
||||||
|
nc.Reject(ssh.UnknownChannelType, "not supported by wsbx")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sshForward(nc ssh.NewChannel, network, addr string) {
|
||||||
|
c, err := net.Dial(network, addr)
|
||||||
|
if err != nil {
|
||||||
|
nc.Reject(ssh.ConnectionFailed, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer c.Close()
|
||||||
|
ch, reqs, err := nc.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ch.Close()
|
||||||
|
go ssh.DiscardRequests(reqs)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
io.Copy(ch, c)
|
||||||
|
ch.CloseWrite()
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
io.Copy(c, ch)
|
||||||
|
if cw, ok := c.(interface{ CloseWrite() error }); ok {
|
||||||
|
cw.CloseWrite()
|
||||||
|
}
|
||||||
|
<-done
|
||||||
|
}
|
||||||
|
|
||||||
|
func sshSession(nc ssh.NewChannel) {
|
||||||
|
ch, reqs, err := nc.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer ch.Close()
|
||||||
|
var env []string
|
||||||
|
var pty, tty *os.File
|
||||||
|
defer func() {
|
||||||
|
if pty != nil {
|
||||||
|
pty.Close()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
started, command := false, ""
|
||||||
|
for req := range reqs {
|
||||||
|
ok, start := false, false
|
||||||
|
switch req.Type {
|
||||||
|
case "env":
|
||||||
|
var kv struct{ K, V string }
|
||||||
|
if ssh.Unmarshal(req.Payload, &kv) == nil {
|
||||||
|
env, ok = append(env, kv.K+"="+kv.V), true
|
||||||
|
}
|
||||||
|
case "pty-req":
|
||||||
|
var p struct {
|
||||||
|
Term string
|
||||||
|
Cols, Rows, W, H uint32
|
||||||
|
Modes string
|
||||||
|
}
|
||||||
|
if pty == nil && !started && ssh.Unmarshal(req.Payload, &p) == nil {
|
||||||
|
if m, s, err := openPty(); err == nil {
|
||||||
|
pty, tty, ok = m, s, true
|
||||||
|
setWinsize(m, uint16(p.Rows), uint16(p.Cols))
|
||||||
|
env = append(env, "TERM="+p.Term)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "window-change":
|
||||||
|
var p struct{ Cols, Rows, W, H uint32 }
|
||||||
|
if pty != nil && ssh.Unmarshal(req.Payload, &p) == nil {
|
||||||
|
setWinsize(pty, uint16(p.Rows), uint16(p.Cols))
|
||||||
|
ok = true
|
||||||
|
}
|
||||||
|
case "shell", "exec":
|
||||||
|
var p struct{ Cmd string }
|
||||||
|
if !started && (req.Type == "shell" || ssh.Unmarshal(req.Payload, &p) == nil) {
|
||||||
|
started, ok, start, command = true, true, true, p.Cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if req.WantReply {
|
||||||
|
req.Reply(ok, nil)
|
||||||
|
}
|
||||||
|
if start {
|
||||||
|
go func(env []string) {
|
||||||
|
code := sshRun(ch, command, env, pty, tty)
|
||||||
|
ch.SendRequest("exit-status", false, ssh.Marshal(struct{ C uint32 }{code}))
|
||||||
|
ch.Close()
|
||||||
|
}(env)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// sshRun runs command (empty: a login shell) like sshd: `$SHELL -c command`, in run.sh's env and dir.
|
||||||
|
func sshRun(ch ssh.Channel, command string, env []string, pty, tty *os.File) uint32 {
|
||||||
|
shell := "/bin/sh"
|
||||||
|
if _, err := os.Stat("/bin/bash"); err == nil {
|
||||||
|
shell = "/bin/bash"
|
||||||
|
}
|
||||||
|
cmd := exec.Command(shell, "-c", command)
|
||||||
|
if command == "" {
|
||||||
|
cmd = exec.Command(shell, "-l")
|
||||||
|
}
|
||||||
|
cmd.Env = append(os.Environ(), env...)
|
||||||
|
if pty != nil {
|
||||||
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = tty, tty, tty
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true, Setctty: true}
|
||||||
|
} else {
|
||||||
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = ch, ch, ch.Stderr()
|
||||||
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
||||||
|
cmd.WaitDelay = time.Second // don't hang on daemons (the VS Code server) that keep stdout open
|
||||||
|
}
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
fmt.Fprintln(ch.Stderr(), "wsbx:", err)
|
||||||
|
return 127
|
||||||
|
}
|
||||||
|
if pty == nil {
|
||||||
|
cmd.Wait()
|
||||||
|
return exitCode(cmd.ProcessState)
|
||||||
|
}
|
||||||
|
tty.Close() // our copy: the master reads EIO once the session's are gone
|
||||||
|
go io.Copy(pty, ch)
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
io.Copy(ch, pty)
|
||||||
|
close(done)
|
||||||
|
}()
|
||||||
|
cmd.Wait()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(500 * time.Millisecond): // a background job still holds the pty
|
||||||
|
}
|
||||||
|
return exitCode(cmd.ProcessState)
|
||||||
|
}
|
||||||
|
|
||||||
|
// stdio is our stdin/stdout as the net.Conn the SSH server runs on.
|
||||||
|
type stdio struct{}
|
||||||
|
|
||||||
|
func (stdio) Read(b []byte) (int, error) { return os.Stdin.Read(b) }
|
||||||
|
func (stdio) Write(b []byte) (int, error) { return os.Stdout.Write(b) }
|
||||||
|
func (stdio) Close() error { os.Stdin.Close(); return os.Stdout.Close() }
|
||||||
|
func (stdio) LocalAddr() net.Addr { return &net.UnixAddr{Name: "stdio", Net: "unix"} }
|
||||||
|
func (stdio) RemoteAddr() net.Addr { return &net.UnixAddr{Name: "wsbx", Net: "unix"} }
|
||||||
|
func (stdio) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (stdio) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (stdio) SetWriteDeadline(time.Time) error { return nil }
|
||||||
+121
@@ -0,0 +1,121 @@
|
|||||||
|
//! `wsbx code NAME`: VS Code for a running sandbox.
|
||||||
|
//! Browser (default): `code serve-web` runs in the VM on its localhost, forwarded to the host's localhost
|
||||||
|
//! (like `wsbx forward`) behind a random connection token, and opened in the browser.
|
||||||
|
//! --desktop: desktop VS Code via Remote-SSH. ssh reaches the SSH server in the guest helper (guest/ssh.go)
|
||||||
|
//! through `wsbx exec` as ProxyCommand: no network port, no keys.
|
||||||
|
use crate::exec::{self, sock};
|
||||||
|
use crate::forward;
|
||||||
|
use crate::util::sq;
|
||||||
|
use anyhow::{Context, Result, bail};
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::fs;
|
||||||
|
use std::io::Read;
|
||||||
|
use std::net::TcpListener;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::process::Command;
|
||||||
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
|
/// Guest side of the browser mode: fetch the VS Code CLI once (static musl binary, onto the sandbox disk),
|
||||||
|
/// then serve. The CLI downloads the matching server on first use. $1 port, $2 token.
|
||||||
|
const WEB_SH: &str = r#"set -e
|
||||||
|
B=/.wsbx/busybox D="$HOME/.wsbx-vscode"
|
||||||
|
U=https://update.code.visualstudio.com/latest/cli-alpine-arm64/stable
|
||||||
|
if [ ! -x "$D/code" ]; then
|
||||||
|
echo "wsbx: downloading the VS Code CLI..."
|
||||||
|
$B rm -rf "$D/tmp" && $B mkdir -p "$D/tmp"
|
||||||
|
if command -v curl >/dev/null; then curl -fsSL "$U"; else wget -qO- "$U"; fi | $B tar -xzf - -C "$D/tmp"
|
||||||
|
$B sync && $B mv "$D/tmp/code" "$D/code" && $B rmdir "$D/tmp" # never a half-written code, even if the VM is killed
|
||||||
|
fi
|
||||||
|
exec "$D/code" serve-web --host 127.0.0.1 --port "$1" --connection-token "$2" --accept-server-license-terms
|
||||||
|
"#;
|
||||||
|
|
||||||
|
pub fn code(h: &Path, name: &str, desktop: bool) -> Result<()> {
|
||||||
|
let sock = sock(h, name)?; // also checks the name and that it's running
|
||||||
|
let meta: Value = serde_json::from_slice(&fs::read(h.join("sandboxes").join(name).join("meta.json"))?)?;
|
||||||
|
let folder = enc(meta["workspace"].as_str().unwrap_or("/"));
|
||||||
|
if desktop {
|
||||||
|
return self::desktop(h, name, &folder);
|
||||||
|
}
|
||||||
|
|
||||||
|
let l = TcpListener::bind(("127.0.0.1", 0))?;
|
||||||
|
let port = l.local_addr()?.port(); // the same port in the guest: random and high, so most likely free there too
|
||||||
|
let mut t = [0u8; 16];
|
||||||
|
fs::File::open("/dev/urandom")?.read_exact(&mut t)?;
|
||||||
|
let token = hex::encode(t);
|
||||||
|
eprintln!("wsbx: starting VS Code in {name} (the first start downloads it)");
|
||||||
|
eprintln!("wsbx: by using it you accept the VS Code Server license: https://aka.ms/vscode-server-license");
|
||||||
|
let (s, tk) = (sock.clone(), token.clone());
|
||||||
|
let server = std::thread::spawn(move || exec::stream(&s, &["/.wsbx/busybox", "sh", "-c", WEB_SH, "sh", &port.to_string(), &tk]));
|
||||||
|
let t0 = Instant::now();
|
||||||
|
while !forward::listening(&sock, port) {
|
||||||
|
if server.is_finished() {
|
||||||
|
let r = server.join().unwrap();
|
||||||
|
bail!("VS Code server in {name} stopped ({:?})", r.map_err(|e| e.to_string()));
|
||||||
|
}
|
||||||
|
if t0.elapsed() > Duration::from_secs(300) {
|
||||||
|
bail!("VS Code server in {name} didn't start within 5 minutes");
|
||||||
|
}
|
||||||
|
std::thread::sleep(Duration::from_millis(500));
|
||||||
|
}
|
||||||
|
let (s, n) = (sock.clone(), name.to_string());
|
||||||
|
std::thread::spawn(move || forward::serve(&s, &n, vec![(l, port)]));
|
||||||
|
let url = format!("http://127.0.0.1:{port}/?tkn={token}&folder={folder}");
|
||||||
|
eprintln!("wsbx: VS Code for {name}: {url}\nwsbx: Ctrl-C stops it");
|
||||||
|
let _ = Command::new("open").arg(&url).status();
|
||||||
|
match server.join().unwrap()? {
|
||||||
|
Some(c) => bail!("VS Code server in {name} exited with {c}"),
|
||||||
|
None => bail!("lost connection to {name} (VM stopped?)"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Write ssh config for every sandbox, include it from ~/.ssh/config once, open VS Code on NAME.wsbx.
|
||||||
|
fn desktop(h: &Path, name: &str, folder: &str) -> Result<()> {
|
||||||
|
let exe = std::env::current_exe()?;
|
||||||
|
let mut cfg = String::from("# Written by `wsbx code --desktop`, regenerated each time: don't edit.\n");
|
||||||
|
for e in fs::read_dir(h.join("sandboxes"))?.flatten().filter(|e| e.path().join("meta.json").exists()) {
|
||||||
|
let n = e.file_name().to_string_lossy().into_owned(); // sandbox names are [a-z0-9-]
|
||||||
|
cfg += &format!(
|
||||||
|
"\nHost {n}.wsbx\n ProxyCommand {} exec {n} -- /.wsbx/host/guest -ssh-stdio\n \
|
||||||
|
StrictHostKeyChecking no\n UserKnownHostsFile /dev/null\n LogLevel ERROR\n ForwardAgent no\n ForwardX11 no\n",
|
||||||
|
sq(&exe.to_string_lossy())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let ours = h.join("ssh_config");
|
||||||
|
fs::write(&ours, cfg)?;
|
||||||
|
|
||||||
|
let home = std::path::PathBuf::from(std::env::var_os("HOME").context("HOME")?);
|
||||||
|
let user_cfg = home.join(".ssh/config");
|
||||||
|
let include = format!("Include \"{}\"", ours.display());
|
||||||
|
let cur = match fs::read_to_string(&user_cfg) {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) if e.kind() == std::io::ErrorKind::NotFound => String::new(),
|
||||||
|
Err(e) => return Err(e).context("~/.ssh/config"),
|
||||||
|
};
|
||||||
|
if !cur.lines().any(|l| l.trim() == include) {
|
||||||
|
fs::create_dir_all(home.join(".ssh"))?;
|
||||||
|
// At the top: an Include after a `Host` line would only apply to that host.
|
||||||
|
fs::write(&user_cfg, format!("{include}\n\n{cur}"))?;
|
||||||
|
eprintln!("wsbx: added `{include}` to the top of ~/.ssh/config");
|
||||||
|
}
|
||||||
|
let url = format!("vscode://vscode-remote/ssh-remote+{name}.wsbx{folder}");
|
||||||
|
eprintln!("wsbx: opening {url} (needs the Remote - SSH extension); plain ssh works too: ssh {name}.wsbx");
|
||||||
|
if !Command::new("open").arg(&url).status()?.success() {
|
||||||
|
bail!("couldn't open {url} (is VS Code installed?)");
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Percent-encode a path for a URL, keeping '/'.
|
||||||
|
fn enc(p: &str) -> String {
|
||||||
|
p.bytes()
|
||||||
|
.map(|b| if b.is_ascii_alphanumeric() || b"/-._~".contains(&b) { (b as char).to_string() } else { format!("%{b:02X}") })
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn enc() {
|
||||||
|
assert_eq!(super::enc("/Users/me/my repo/ä&x"), "/Users/me/my%20repo/%C3%A4%26x");
|
||||||
|
}
|
||||||
|
}
|
||||||
+15
-4
@@ -92,11 +92,9 @@ fn send(w: &Mutex<UnixStream>, kind: u8, p: &[u8]) -> io::Result<()> {
|
|||||||
|
|
||||||
/// Run a command as root in a running sandbox (no tty, empty stdin) and return its stdout, at most 64 KiB.
|
/// Run a command as root in a running sandbox (no tty, empty stdin) and return its stdout, at most 64 KiB.
|
||||||
pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
|
pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
|
||||||
let mut s = UnixStream::connect(sock)?;
|
|
||||||
s.set_read_timeout(Some(Duration::from_secs(2)))?;
|
|
||||||
// uid 0, not "root": works even if the image has no /etc/passwd
|
// uid 0, not "root": works even if the image has no /etc/passwd
|
||||||
s.write_all(format!("{}\n", json!({"argv": argv, "user": "0", "tty": false})).as_bytes())?;
|
let s = start(sock, argv, Some("0"))?;
|
||||||
s.write_all(&frame(b'e', &[]))?;
|
s.set_read_timeout(Some(Duration::from_secs(2)))?;
|
||||||
const CAP: usize = 64 << 10;
|
const CAP: usize = 64 << 10;
|
||||||
let mut buf = vec![0u8; CAP];
|
let mut buf = vec![0u8; CAP];
|
||||||
let mut w = &mut buf[..]; // a slice writer errors once full: the guest can't make us buffer more
|
let mut w = &mut buf[..]; // a slice writer errors once full: the guest can't make us buffer more
|
||||||
@@ -109,6 +107,19 @@ pub fn capture(sock: &Path, argv: &[&str]) -> Result<String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start a command without tty, with empty stdin; user None: the image user.
|
||||||
|
fn start(sock: &Path, argv: &[&str], user: Option<&str>) -> Result<UnixStream> {
|
||||||
|
let mut s = UnixStream::connect(sock)?;
|
||||||
|
s.write_all(format!("{}\n", json!({"argv": argv, "user": user, "tty": false})).as_bytes())?;
|
||||||
|
s.write_all(&frame(b'e', &[]))?;
|
||||||
|
Ok(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Run a command as the image user, all its output to our stderr. None: the connection ended first.
|
||||||
|
pub fn stream(sock: &Path, argv: &[&str]) -> Result<Option<i32>> {
|
||||||
|
Ok(output(start(sock, argv, None)?, &mut io::stderr(), &mut io::stderr())?)
|
||||||
|
}
|
||||||
|
|
||||||
/// Copy guest output frames to out/err until the exit status arrives (None: connection closed first).
|
/// Copy guest output frames to out/err until the exit status arrives (None: connection closed first).
|
||||||
fn output(s: UnixStream, out: &mut impl Write, err: &mut impl Write) -> io::Result<Option<i32>> {
|
fn output(s: UnixStream, out: &mut impl Write, err: &mut impl Write) -> io::Result<Option<i32>> {
|
||||||
let mut r = io::BufReader::new(s);
|
let mut r = io::BufReader::new(s);
|
||||||
|
|||||||
+16
-2
@@ -28,11 +28,17 @@ pub fn forward(h: &Path, name: &str, specs: Vec<String>) -> Result<()> {
|
|||||||
ls.push((l, guest));
|
ls.push((l, guest));
|
||||||
eprintln!("wsbx: localhost:{host} -> {name}:{guest}");
|
eprintln!("wsbx: localhost:{host} -> {name}:{guest}");
|
||||||
}
|
}
|
||||||
|
serve(&sock, name, ls);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Forward connections on each listener to its guest port, forever.
|
||||||
|
pub fn serve(sock: &Path, name: &str, ls: Vec<(TcpListener, u16)>) {
|
||||||
// ponytail: thread per connection, fine for dev servers; async if someone forwards thousands of sockets.
|
// ponytail: thread per connection, fine for dev servers; async if someone forwards thousands of sockets.
|
||||||
let threads: Vec<_> = ls
|
let threads: Vec<_> = ls
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(l, port)| {
|
.map(|(l, port)| {
|
||||||
let (sock, name) = (sock.clone(), name.to_string());
|
let (sock, name) = (sock.to_path_buf(), name.to_string());
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
for c in l.incoming().flatten() {
|
for c in l.incoming().flatten() {
|
||||||
let (sock, name) = (sock.clone(), name.clone());
|
let (sock, name) = (sock.clone(), name.clone());
|
||||||
@@ -48,7 +54,15 @@ pub fn forward(h: &Path, name: &str, specs: Vec<String>) -> Result<()> {
|
|||||||
for t in threads {
|
for t in threads {
|
||||||
let _ = t.join();
|
let _ = t.join();
|
||||||
}
|
}
|
||||||
Ok(())
|
}
|
||||||
|
|
||||||
|
/// Whether something accepts connections on the guest's localhost:port.
|
||||||
|
pub fn listening(sock: &Path, port: u16) -> bool {
|
||||||
|
let Ok(mut s) = std::os::unix::net::UnixStream::connect(sock) else { return false };
|
||||||
|
let req = format!("{}\n", json!({"connect": port}));
|
||||||
|
let mut h = [0u8; 1];
|
||||||
|
// A failed dial answers with an 'r' (error) frame; a connected one with output, keepalive or EOF ('x').
|
||||||
|
s.write_all(req.as_bytes()).is_ok() && s.write_all(&frame(b'e', &[])).is_ok() && s.read_exact(&mut h).is_ok() && h[0] != b'r'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn conn(sock: &Path, name: &str, c: TcpStream, port: u16) -> Result<()> {
|
fn conn(sock: &Path, name: &str, c: TcpStream, port: u16) -> Result<()> {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
//! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit).
|
//! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit).
|
||||||
mod agents;
|
mod agents;
|
||||||
|
mod code;
|
||||||
mod disk;
|
mod disk;
|
||||||
mod exec;
|
mod exec;
|
||||||
mod forward;
|
mod forward;
|
||||||
@@ -81,6 +82,13 @@ enum Cmd {
|
|||||||
#[arg(required = true)]
|
#[arg(required = true)]
|
||||||
ports: Vec<String>,
|
ports: Vec<String>,
|
||||||
},
|
},
|
||||||
|
/// Open VS Code in a running sandbox: in the browser (default), or desktop VS Code via Remote-SSH
|
||||||
|
Code {
|
||||||
|
name: String,
|
||||||
|
/// Desktop VS Code via Remote-SSH (adds an Include line to ~/.ssh/config once)
|
||||||
|
#[arg(long)]
|
||||||
|
desktop: bool,
|
||||||
|
},
|
||||||
/// List sandboxes with live stats (CPU, memory, network, block I/O, processes)
|
/// List sandboxes with live stats (CPU, memory, network, block I/O, processes)
|
||||||
#[command(visible_alias = "ps")]
|
#[command(visible_alias = "ps")]
|
||||||
Ls {
|
Ls {
|
||||||
@@ -172,6 +180,7 @@ fn main() -> Result<()> {
|
|||||||
Cmd::Pi { a, pi_auth } => agents::pi(&h, a, pi_auth),
|
Cmd::Pi { a, pi_auth } => agents::pi(&h, a, pi_auth),
|
||||||
Cmd::Exec { name, user, cmd } => exec::exec(&h, &name, user, cmd),
|
Cmd::Exec { name, user, cmd } => exec::exec(&h, &name, user, cmd),
|
||||||
Cmd::Forward { name, ports } => forward::forward(&h, &name, ports),
|
Cmd::Forward { name, ports } => forward::forward(&h, &name, ports),
|
||||||
|
Cmd::Code { name, desktop } => code::code(&h, &name, desktop),
|
||||||
Cmd::Ls { follow } => sandbox::ls(&h, follow),
|
Cmd::Ls { follow } => sandbox::ls(&h, follow),
|
||||||
Cmd::Gc => sandbox::gc(&h),
|
Cmd::Gc => sandbox::gc(&h),
|
||||||
Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk),
|
Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk),
|
||||||
|
|||||||
@@ -97,6 +97,8 @@ pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g
|
|||||||
.unwrap_or("/".into());
|
.unwrap_or("/".into());
|
||||||
let mut s = String::from("B=/.wsbx/busybox\n");
|
let mut s = String::from("B=/.wsbx/busybox\n");
|
||||||
s += &format!("$B date -s @{} >/dev/null\n", now());
|
s += &format!("$B date -s @{} >/dev/null\n", now());
|
||||||
|
// Some images ship no /etc/hosts: without a localhost entry, Go and others resolve it via DNS and fail.
|
||||||
|
s += "$B grep -qw localhost /etc/hosts 2>/dev/null || printf '127.0.0.1 localhost\\n::1 localhost\\n' >> /etc/hosts\n";
|
||||||
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
|
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
|
||||||
if let Some((rows, cols)) = term_size() {
|
if let Some((rows, cols)) = term_size() {
|
||||||
s += &format!("$B stty rows {rows} cols {cols}\n");
|
s += &format!("$B stty rows {rows} cols {cols}\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user