| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "net" |
| 7 | "os/user" |
| 8 | "path/filepath" |
| 9 | "strconv" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/lima-vm/sshocker/pkg/mount" |
| 13 | "github.com/lima-vm/sshocker/pkg/ssh" |
| 14 | "github.com/lima-vm/sshocker/pkg/sshocker" |
| 15 | "github.com/urfave/cli/v2" |
| 16 | ) |
| 17 | |
| 18 | var ( |
| 19 | runFlags = []cli.Flag{ |
| 20 | &cli.StringFlag{ |
| 21 | Name: "ssh-config", |
| 22 | Aliases: []string{"F"}, |
| 23 | Usage: "ssh config file", |
| 24 | }, |
| 25 | &cli.BoolFlag{ |
| 26 | Name: "ssh-persist", |
| 27 | Usage: "enable ControlPersist", |
| 28 | Value: true, |
| 29 | }, |
| 30 | &cli.StringSliceFlag{ |
| 31 | Name: "v", |
| 32 | Usage: "Mount a reverse SSHFS, " + |
| 33 | "e.g. `.:/mnt/ssh` to mount the current directory on the client onto /mnt/ssh on the server, " + |
| 34 | "append `:ro` for read-only mount", |
| 35 | }, |
| 36 | &cli.StringSliceFlag{ |
| 37 | Name: "p", |
| 38 | Usage: "Expose a port, e.g. `8080:80` to forward the port 8080 the client onto the port 80 on the server", |
| 39 | }, |
| 40 | &cli.BoolFlag{ |
| 41 | Name: "sshfs-nonempty", |
| 42 | Usage: "enable sshfs nonempty", |
| 43 | Value: false, |
| 44 | }, |
| 45 | &cli.StringSliceFlag{ |
| 46 | Name: "sshfs-option", |
| 47 | Usage: "Set sshfs mount options.", |
| 48 | }, |
| 49 | &cli.StringFlag{ |
| 50 | Name: "driver", |
| 51 | Usage: "SFTP server driver. \"builtin\" (legacy) or \"openssh-sftp-server\" (robust and secure, recommended), automatically chosen by default", |
| 52 | Value: "auto", |
| 53 | }, |
| 54 | &cli.StringFlag{ |
| 55 | Name: "openssh-sftp-server", |
| 56 | Usage: "OpenSSH SFTP Server binary, automatically chosen by default", |
| 57 | Value: "", |
| 58 | }, |
| 59 | } |
| 60 | runCommand = &cli.Command{ |
| 61 | Name: "run", |
| 62 | Usage: "Akin to `docker run` (The default subcommand)", |
| 63 | Action: runAction, |
| 64 | Flags: runFlags, |
| 65 | } |
| 66 | ) |
| 67 | |
| 68 | func parseHost(s string) (string, int, error) { |
| 69 | if !strings.Contains(s, ":") { |
| 70 | FIXME: |
| 71 | return s, 0, nil |
| 72 | } |
| 73 | host, portStr, err := net.SplitHostPort(s) |
| 74 | if err != nil { |
| 75 | return "", 0, err |
| 76 | } |
| 77 | port, err := strconv.Atoi(portStr) |
| 78 | if err != nil { |
| 79 | return "", 0, err |
| 80 | } |
| 81 | |
| 82 | return host, port, nil |
| 83 | } |
| 84 | |
| 85 | func runAction(clicontext *cli.Context) error { |
| 86 | if clicontext.NArg() < 1 { |
| 87 | return errors.New("no host specified") |
| 88 | } |
| 89 | sshConfig := &ssh.SSHConfig{ |
| 90 | ConfigFile: clicontext.String("ssh-config"), |
| 91 | Persist: clicontext.Bool("ssh-persist"), |
| 92 | } |
| 93 | host, port, err := parseHost(clicontext.Args().First()) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | var sshfsAdditionalArgs []string |
| 98 | if clicontext.Bool("sshfs-nonempty") { |
| 99 | sshfsAdditionalArgs = append(sshfsAdditionalArgs, "-o", "nonempty") |
| 100 | } |
| 101 | for _, o := range clicontext.StringSlice("sshfs-option") { |
| 102 | sshfsAdditionalArgs = append(sshfsAdditionalArgs, "-o", o) |
| 103 | } |
| 104 | |
| 105 | x := &sshocker.Sshocker{ |
| 106 | SSHConfig: sshConfig, |
| 107 | Host: host, |
| 108 | Port: port, |
| 109 | Command: clicontext.Args().Tail(), |
| 110 | SSHFSAdditionalArgs: sshfsAdditionalArgs, |
| 111 | Driver: clicontext.String("driver"), |
| 112 | OpensshSftpServerBinary: clicontext.String("openssh-sftp-server"), |
| 113 | } |
| 114 | if len(x.Command) > 0 && x.Command[0] == "--" { |
| 115 | x.Command = x.Command[1:] |
| 116 | } |
| 117 | for _, v := range clicontext.StringSlice("v") { |
| 118 | m, err := parseFlagV(v) |
| 119 | if err != nil { |
| 120 | return err |
| 121 | } |
| 122 | x.Mounts = append(x.Mounts, m) |
| 123 | } |
| 124 | for _, p := range clicontext.StringSlice("p") { |
| 125 | lforward, err := parseFlagP(p) |
| 126 | if err != nil { |
| 127 | return err |
| 128 | } |
| 129 | x.LForwards = append(x.LForwards, lforward) |
| 130 | } |
| 131 | return x.Run() |
| 132 | } |
| 133 | |
| 134 | func expandLocalPath(localPath string) (string, error) { |
| 135 | s := localPath |
| 136 | if s == "" { |
| 137 | return "", errors.New("got empty local path") |
| 138 | } |
| 139 | if strings.HasPrefix(s, "~/") { |
| 140 | u, err := user.Current() |
| 141 | if err != nil { |
| 142 | return "", err |
| 143 | } |
| 144 | if u.HomeDir == "" { |
| 145 | return "", errors.New("cannot determine the local home directory") |
| 146 | } |
| 147 | s = strings.Replace(s, "~", u.HomeDir, 1) |
| 148 | } |
| 149 | return filepath.Abs(s) |
| 150 | } |
| 151 | |
| 152 | |
| 153 | func parseFlagV(s string) (mount.Mount, error) { |
| 154 | m := mount.Mount{ |
| 155 | Type: mount.MountTypeReverseSSHFS, |
| 156 | } |
| 157 | TODO: |
| 158 | split := strings.Split(s, ":") |
| 159 | switch len(split) { |
| 160 | case 2: |
| 161 | m.Source = split[0] |
| 162 | m.Destination = split[1] |
| 163 | case 3: |
| 164 | m.Source = split[0] |
| 165 | m.Destination = split[1] |
| 166 | if split[2] == "ro" { |
| 167 | m.Readonly = true |
| 168 | } else { |
| 169 | return m, fmt.Errorf("cannot parse %q: unknown option %q", s, split[2]) |
| 170 | } |
| 171 | default: |
| 172 | return m, fmt.Errorf("cannot parse %q", s) |
| 173 | } |
| 174 | var err error |
| 175 | m.Source, err = expandLocalPath(m.Source) |
| 176 | if err != nil { |
| 177 | return m, fmt.Errorf("cannot use %q: %w", s, err) |
| 178 | } |
| 179 | return m, nil |
| 180 | } |