118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
// toolsrv — 9P tool execution server.
|
|
//
|
|
// Executes tool calls in a sandboxed environment. Used both locally
|
|
// (spawned by olliesrv) and remotely (deployed via SSH bootstrap).
|
|
//
|
|
// Usage:
|
|
//
|
|
// toolsrv serve --cwd /path/to/project --listen /path/to/sock [--yolo]
|
|
//
|
|
// Listens on a Unix socket and serves a 9P filesystem for tool execution.
|
|
// Authentication happens via Tauth: first client to connect sets the secret,
|
|
// subsequent clients must provide the same secret.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"syscall"
|
|
|
|
"ollie/env"
|
|
"ollie/fsedsl"
|
|
"ollie/toolsrv"
|
|
)
|
|
|
|
var (
|
|
cwd = flag.String("cwd", ".", "working directory for execution")
|
|
listenPath = flag.String("listen", "", "Unix socket path to listen on (required)")
|
|
yolo = flag.Bool("yolo", false, "skip sandbox enforcement")
|
|
)
|
|
|
|
func main() {
|
|
if len(os.Args) < 2 || os.Args[1] != "serve" {
|
|
fmt.Fprintln(os.Stderr, "usage: toolsrv serve --cwd <path> --listen <socket> [--yolo]")
|
|
os.Exit(1)
|
|
}
|
|
flag.CommandLine.Parse(os.Args[2:])
|
|
|
|
if *listenPath == "" {
|
|
fmt.Fprintln(os.Stderr, "error: --listen is required")
|
|
os.Exit(1)
|
|
}
|
|
|
|
env.EnsureDefaults()
|
|
|
|
// Prepend our bin dir to PATH so landrun is found (deployed alongside us).
|
|
home, _ := os.UserHomeDir()
|
|
binDir := filepath.Join(home, ".config", "ollie", "bin")
|
|
os.Setenv("PATH", binDir+":"+os.Getenv("PATH"))
|
|
|
|
// Create tool registry
|
|
toolReg, _ := toolsrv.NewRegistry()
|
|
sessionID := os.Getenv("OLLIE_SESSION_ID")
|
|
|
|
// Create 9P server (secret is established via Tauth, not env var)
|
|
srv := toolsrv.NewServer9P()
|
|
srv.SetYolo(*yolo)
|
|
if toolReg != nil && sessionID != "" {
|
|
srv.SetRegistry(toolReg, sessionID)
|
|
}
|
|
|
|
// Build the filesystem tree
|
|
ctx := toolsrv.ToolsrvCtx{Server: srv}
|
|
tree := fsedsl.BuildTree(toolsrv.ToolsrvSpec(), ctx)
|
|
if tree == nil {
|
|
fmt.Fprintln(os.Stderr, "error: failed to build filesystem tree")
|
|
os.Exit(1)
|
|
}
|
|
|
|
// Set up signal handling
|
|
runCtx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
sigCh := make(chan os.Signal, 1)
|
|
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
|
|
go func() {
|
|
<-sigCh
|
|
cancel()
|
|
}()
|
|
|
|
// Remove stale socket if it exists
|
|
os.Remove(*listenPath)
|
|
|
|
// Listen on Unix socket
|
|
ln, err := net.Listen("unix", *listenPath)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "error: listen: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
defer ln.Close()
|
|
defer os.Remove(*listenPath)
|
|
|
|
fmt.Fprintf(os.Stderr, "toolsrv: listening on %s\n", *listenPath)
|
|
|
|
// Accept connections
|
|
go func() {
|
|
<-runCtx.Done()
|
|
ln.Close()
|
|
}()
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
select {
|
|
case <-runCtx.Done():
|
|
return
|
|
default:
|
|
fmt.Fprintf(os.Stderr, "accept error: %v\n", err)
|
|
continue
|
|
}
|
|
}
|
|
go serve9P(runCtx, conn, tree, srv)
|
|
}
|
|
}
|