| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | package main |
| 12 | |
| 13 | import ( |
| 14 | "fmt" |
| 15 | "io" |
| 16 | "os" |
| 17 | "os/exec" |
| 18 | |
| 19 | "github.com/lima-vm/sshocker/pkg/reversesshfs" |
| 20 | "github.com/lima-vm/sshocker/pkg/util" |
| 21 | ) |
| 22 | |
| 23 | var readonlyNames = []string{".git", ".hg"} |
| 24 | |
| 25 | func main() { |
| 26 | if len(os.Args) < 3 { |
| 27 | fmt.Fprintln(os.Stderr, "usage: harness mount <hostdir> <mountpoint> [sshfs args...] | harness stdio <hostdir>") |
| 28 | os.Exit(2) |
| 29 | } |
| 30 | switch os.Args[1] { |
| 31 | case "mount": |
| 32 | mount(os.Args[2], os.Args[3], os.Args[4:]) |
| 33 | case "stdio": |
| 34 | serve(&util.RWC{ReadCloser: os.Stdin, WriteCloser: os.Stdout}, os.Args[2]) |
| 35 | default: |
| 36 | os.Exit(2) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func mount(hostDir, mnt string, extra []string) { |
| 41 | |
| 42 | |
| 43 | args := append([]string{":" + hostDir, mnt, "-o", "slave", "-o", "allow_other", "-o", "no_contain_symlinks", "-f"}, extra...) |
| 44 | cmd := exec.Command("sshfs", args...) |
| 45 | cmd.Stderr = os.Stderr |
| 46 | in, err := cmd.StdinPipe() |
| 47 | if err != nil { |
| 48 | panic(err) |
| 49 | } |
| 50 | out, err := cmd.StdoutPipe() |
| 51 | if err != nil { |
| 52 | panic(err) |
| 53 | } |
| 54 | if err := cmd.Start(); err != nil { |
| 55 | panic(err) |
| 56 | } |
| 57 | fmt.Fprintf(os.Stderr, "harness: serving %s on %s (pid %d)\n", hostDir, mnt, os.Getpid()) |
| 58 | serve(&util.RWC{ReadCloser: out, WriteCloser: in}, hostDir) |
| 59 | _ = cmd.Process.Kill() |
| 60 | } |
| 61 | |
| 62 | func serve(rwc io.ReadWriteCloser, hostDir string) { |
| 63 | srv, err := reversesshfs.HarnessServer(rwc, hostDir, readonlyNames) |
| 64 | if err != nil { |
| 65 | panic(err) |
| 66 | } |
| 67 | fmt.Fprintln(os.Stderr, "harness: Serve returned:", srv.Serve()) |
| 68 | } |