Files
woiken-sandboxes/guest/fwd.go
T
niklas bc7512a373
release / release (push) Successful in 2s
release / build (macos-arm64) (push) Successful in 21s
release / build (ubuntu-latest) (push) Successful in 1m5s
feat: implement exec and forward commands for running commands in sandboxes
- Added `exec` command to run a shell or command in a running sandbox, utilizing a new exec server.
- Introduced `forward` command to expose ports of a running sandbox on the host's localhost.
- Updated Cargo.toml and Cargo.lock to reflect version bump and new dependencies.
- Enhanced README.md with usage instructions for new commands.
- Refactored build.rs to include new guest helper files for exec functionality.
- Created new source files for exec and forward logic, including protocol handling and connection management.
2026-10-03 21:48:44 +02:00

112 lines
3.0 KiB
Go

// wsbx-guest: guest helper, started by run.sh as root.
// Usage: wsbx-guest [-exec VSOCKPORT] PORT=VSOCKPORT...
// -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).
package main
import (
"encoding/binary"
"flag"
"fmt"
"io"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"unsafe"
)
const (
afVsock = 40
cidHost = 2
)
// struct sockaddr_vm { u16 family; u16 reserved; u32 port; u32 cid; u8 flags; u8 zero[3]; }
func vsockAddr(port, cid uint32) (sa [16]byte) {
binary.LittleEndian.PutUint16(sa[0:], afVsock)
binary.LittleEndian.PutUint32(sa[4:], port)
binary.LittleEndian.PutUint32(sa[8:], cid)
return
}
func dialHost(port uint32) (*os.File, error) {
fd, err := syscall.Socket(afVsock, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
if err != nil {
return nil, err
}
sa := vsockAddr(port, cidHost)
if _, _, e := syscall.Syscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(unsafe.Pointer(&sa[0])), 16); e != 0 {
syscall.Close(fd)
return nil, e
}
return os.NewFile(uintptr(fd), "vsock"), nil
}
func handle(c *net.TCPConn, port uint32) {
defer c.Close()
f, err := dialHost(port)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest: vsock:", err)
return
}
defer f.Close()
done := make(chan struct{})
go func() {
io.Copy(c, f)
c.CloseWrite()
close(done)
}()
io.Copy(f, c)
syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
<-done
}
func main() {
execPort := flag.Uint("exec", 0, "serve `wsbx exec` sessions on this vsock port")
flag.Parse()
// 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.
signal.Notify(make(chan os.Signal, 1), syscall.SIGINT, syscall.SIGQUIT)
if *execPort != 0 {
if err := serveExec(uint32(*execPort)); err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest: exec:", err)
}
}
for _, spec := range flag.Args() {
p, v, _ := strings.Cut(spec, "=")
vport, err := strconv.ParseUint(v, 10, 32)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest: bad spec", spec)
os.Exit(2)
}
for _, addr := range []string{"127.0.0.1:" + p, "[::1]:" + p} {
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-guest:", err)
continue
}
go func() {
for {
c, err := ln.Accept()
if err == nil {
go handle(c.(*net.TCPConn), uint32(vport))
}
}
}()
}
}
// Signal readiness (run.sh waits on this) before the agent starts.
fmt.Println("ready")
// /dev/null over fd 1 instead of closing it: a socket reusing fd 1 would make Go treat EPIPE on it
// as a broken stdout and die of SIGPIPE (e.g. keepalive to a host that's gone).
if null, err := os.OpenFile("/dev/null", os.O_WRONLY, 0); err == nil {
syscall.Dup3(int(null.Fd()), 1, 0)
null.Close()
} else {
os.Stdout.Close()
}
select {}
}