121 lines
3.3 KiB
Go
121 lines
3.3 KiB
Go
// wsbx-guest: guest helper, started by run.sh as root.
|
|
// 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
|
|
// 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")
|
|
sshStdio := flag.Bool("ssh-stdio", false, "serve one SSH connection on stdin/stdout")
|
|
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
|
|
// 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 {}
|
|
}
|