Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 5 | // The following enables go generate to generate the doc.go file. |
| 6 | //go:generate go run $V23_ROOT/release/go/src/v.io/x/lib/cmdline/testdata/gendoc.go . -help |
| 7 | |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "errors" |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "fmt" |
| 13 | "net" |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 14 | "strings" |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 15 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 16 | "v.io/x/lib/cmdline" |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 17 | |
| 18 | "v.io/v23/context" |
| 19 | |
Todd Wang | affd04d | 2015-04-08 10:25:35 -0700 | [diff] [blame] | 20 | "v.io/x/ref/examples/tunnel" |
| 21 | "v.io/x/ref/examples/tunnel/internal" |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 22 | "v.io/x/ref/internal/logger" |
Todd Wang | affd04d | 2015-04-08 10:25:35 -0700 | [diff] [blame] | 23 | "v.io/x/ref/lib/signals" |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 24 | "v.io/x/ref/lib/v23cmd" |
Suharsh Sivakumar | dcc11d7 | 2015-05-11 12:19:20 -0700 | [diff] [blame] | 25 | _ "v.io/x/ref/runtime/factories/generic" |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var ( |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 29 | disablePty, forcePty, noshell bool |
| 30 | portforward, lprotocol, rprotocol string |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 33 | func main() { |
| 34 | cmdVsh.Flags.BoolVar(&disablePty, "T", false, "Disable pseudo-terminal allocation.") |
| 35 | cmdVsh.Flags.BoolVar(&forcePty, "t", false, "Force allocation of pseudo-terminal.") |
| 36 | cmdVsh.Flags.BoolVar(&noshell, "N", false, "Do not execute a shell. Only do port forwarding.") |
| 37 | cmdVsh.Flags.StringVar(&portforward, "L", "", `Forward local to remote, format is "localaddr,remoteaddr".`) |
| 38 | cmdVsh.Flags.StringVar(&lprotocol, "local_protocol", "tcp", "Local network protocol for port forwarding.") |
| 39 | cmdVsh.Flags.StringVar(&rprotocol, "remote_protocol", "tcp", "Remote network protocol for port forwarding.") |
| 40 | cmdline.HideGlobalFlagsExcept() |
| 41 | cmdline.Main(cmdVsh) |
| 42 | } |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 44 | var cmdVsh = &cmdline.Command{ |
| 45 | Runner: v23cmd.RunnerFunc(runVsh), |
| 46 | Name: "vsh", |
| 47 | Short: "Vanadium shell", |
| 48 | Long: ` |
| 49 | Command vsh runs the Vanadium shell, a Tunnel client that can be used to run |
| 50 | shell commands or start an interactive shell on a remote tunneld server. |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 51 | |
| 52 | To open an interactive shell, use: |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 53 | vsh <object name> |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 54 | |
| 55 | To run a shell command, use: |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 56 | vsh <object name> <command to run> |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 57 | |
| 58 | The -L flag will forward connections from a local port to a remote address |
| 59 | through the tunneld service. The flag value is localaddr,remoteaddr. E.g. |
| 60 | -L :14141,www.google.com:80 |
| 61 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 62 | vsh can't be used directly with tools like rsync because vanadium object names |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 63 | don't look like traditional hostnames, which rsync doesn't understand. For |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 64 | compatibility with such tools, vsh has a special feature that allows passing the |
Robin Thellend | 320af42 | 2015-03-09 17:18:15 -0700 | [diff] [blame] | 65 | vanadium object name via the VSH_NAME environment variable. |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 66 | |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 67 | $ VSH_NAME=<object name> rsync -avh -e vsh /foo/* v23:/foo/ |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 68 | |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 69 | In this example, the "v23" host will be substituted with $VSH_NAME by vsh and |
Robin Thellend | 320af42 | 2015-03-09 17:18:15 -0700 | [diff] [blame] | 70 | rsync will work as expected. |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 71 | `, |
| 72 | ArgsName: "<object name> [command]", |
| 73 | ArgsLong: ` |
| 74 | <object name> is the Vanadium object name to connect to. |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 75 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 76 | [command] is the shell command and args to run, for non-interactive vsh. |
| 77 | `, |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 80 | func runVsh(ctx *context.T, env *cmdline.Env, args []string) error { |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 81 | oname, cmd, err := objectNameAndCommandLine(env, args) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 82 | if err != nil { |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 83 | return env.UsageErrorf("%v", err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Todd Wang | d8cb55b | 2014-11-07 00:58:32 -0800 | [diff] [blame] | 86 | t := tunnel.TunnelClient(oname) |
Matt Rosencrantz | 7a92378 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 87 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 88 | if len(portforward) > 0 { |
Matt Rosencrantz | 7a92378 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 89 | go runPortForwarding(ctx, t, oname) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 92 | if noshell { |
Suharsh Sivakumar | 546e129 | 2015-01-07 15:04:50 -0800 | [diff] [blame] | 93 | <-signals.ShutdownOnSignals(ctx) |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 94 | return nil |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 97 | opts := shellOptions(env, cmd) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 98 | |
Matt Rosencrantz | 7a92378 | 2014-08-18 09:56:15 -0700 | [diff] [blame] | 99 | stream, err := t.Shell(ctx, cmd, opts) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 100 | if err != nil { |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 101 | return err |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 102 | } |
Robin Thellend | 9faaef3 | 2014-08-01 19:45:40 -0700 | [diff] [blame] | 103 | if opts.UsePty { |
Todd Wang | affd04d | 2015-04-08 10:25:35 -0700 | [diff] [blame] | 104 | saved := internal.EnterRawTerminalMode() |
| 105 | defer internal.RestoreTerminalSettings(saved) |
Robin Thellend | 9faaef3 | 2014-08-01 19:45:40 -0700 | [diff] [blame] | 106 | } |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 107 | runIOManager(env.Stdin, env.Stdout, env.Stderr, stream) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 108 | |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 109 | exitMsg := fmt.Sprintf("Connection to %s closed.", oname) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 110 | exitStatus, err := stream.Finish() |
| 111 | if err != nil { |
| 112 | exitMsg += fmt.Sprintf(" (%v)", err) |
| 113 | } |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 114 | ctx.VI(1).Info(exitMsg) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 115 | // Only show the exit message on stdout for interactive shells. |
| 116 | // Otherwise, the exit message might get confused with the output |
| 117 | // of the command that was run. |
| 118 | if err != nil { |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 119 | fmt.Fprintln(env.Stderr, exitMsg) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 120 | } else if len(cmd) == 0 { |
| 121 | fmt.Println(exitMsg) |
| 122 | } |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 123 | return cmdline.ErrExitCode(exitStatus) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 126 | func shellOptions(env *cmdline.Env, cmd string) (opts tunnel.ShellOpts) { |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 127 | opts.UsePty = (len(cmd) == 0 || forcePty) && !disablePty |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 128 | opts.Environment = environment(env.Vars) |
Todd Wang | affd04d | 2015-04-08 10:25:35 -0700 | [diff] [blame] | 129 | ws, err := internal.GetWindowSize() |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 130 | if err != nil { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 131 | logger.Global().VI(1).Infof("GetWindowSize failed: %v", err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 132 | } else { |
Robin Thellend | c14adc9 | 2015-02-11 13:02:19 -0800 | [diff] [blame] | 133 | opts.WinSize.Rows = ws.Row |
| 134 | opts.WinSize.Cols = ws.Col |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 135 | } |
| 136 | return |
| 137 | } |
| 138 | |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 139 | func environment(vars map[string]string) []string { |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 140 | env := []string{} |
| 141 | for _, name := range []string{"TERM", "COLORTERM"} { |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 142 | if value := vars[name]; value != "" { |
| 143 | env = append(env, name+"="+value) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | return env |
| 147 | } |
| 148 | |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 149 | // objectNameAndCommandLine extracts the object name and the remote command to |
| 150 | // send to the server. The object name is the first non-flag argument. |
| 151 | // The command line is the concatenation of all non-flag arguments excluding |
| 152 | // the object name. |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 153 | func objectNameAndCommandLine(env *cmdline.Env, args []string) (string, string, error) { |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 154 | if len(args) < 1 { |
Bogdan Caprita | 8785124 | 2014-07-02 14:40:39 -0700 | [diff] [blame] | 155 | return "", "", errors.New("object name missing") |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 156 | } |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 157 | name := args[0] |
| 158 | args = args[1:] |
| 159 | // For compatibility with tools like rsync. Because object names |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 160 | // don't look like traditional hostnames, tools that work with rsh and |
| 161 | // ssh can't work directly with vsh. This trick makes the following |
| 162 | // possible: |
Robin Thellend | 320af42 | 2015-03-09 17:18:15 -0700 | [diff] [blame] | 163 | // $ VSH_NAME=<object name> rsync -avh -e vsh /foo/* v23:/foo/ |
| 164 | // The "v23" host will be substituted with <object name>. |
Todd Wang | 3e38992 | 2015-05-14 23:09:38 -0700 | [diff] [blame] | 165 | if envName := env.Vars["VSH_NAME"]; envName != "" && name == "v23" { |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 166 | name = envName |
| 167 | } |
| 168 | cmd := strings.Join(args, " ") |
| 169 | return name, cmd, nil |
| 170 | } |
| 171 | |
Matt Rosencrantz | 77f4b93 | 2014-12-29 11:28:49 -0800 | [diff] [blame] | 172 | func runPortForwarding(ctx *context.T, t tunnel.TunnelClientMethods, oname string) { |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 173 | // portforward is localaddr,remoteaddr |
| 174 | parts := strings.Split(portforward, ",") |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 175 | var laddr, raddr string |
| 176 | if len(parts) != 2 { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 177 | ctx.Fatalf("-L flag expects 2 values separated by a comma") |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 178 | } |
| 179 | laddr = parts[0] |
| 180 | raddr = parts[1] |
| 181 | |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 182 | ln, err := net.Listen(lprotocol, laddr) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 183 | if err != nil { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 184 | ctx.Fatalf("net.Listen(%q, %q) failed: %v", lprotocol, laddr, err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 185 | } |
| 186 | defer ln.Close() |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 187 | ctx.VI(1).Infof("Listening on %q", ln.Addr()) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 188 | for { |
| 189 | conn, err := ln.Accept() |
| 190 | if err != nil { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 191 | ctx.Infof("Accept failed: %v", err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 192 | continue |
| 193 | } |
Todd Wang | 7a6a4c2 | 2015-05-12 23:57:16 -0700 | [diff] [blame] | 194 | stream, err := t.Forward(ctx, rprotocol, raddr) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 195 | if err != nil { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 196 | ctx.Infof("Tunnel(%q, %q) failed: %v", rprotocol, raddr, err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 197 | conn.Close() |
| 198 | continue |
| 199 | } |
Asim Shankar | f9a4168 | 2014-07-08 17:14:01 -0700 | [diff] [blame] | 200 | name := fmt.Sprintf("%v-->%v-->(%v)-->%v", conn.RemoteAddr(), conn.LocalAddr(), oname, raddr) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 201 | go func() { |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 202 | ctx.VI(1).Infof("TUNNEL START: %v", name) |
Todd Wang | affd04d | 2015-04-08 10:25:35 -0700 | [diff] [blame] | 203 | errf := internal.Forward(conn, stream.SendStream(), stream.RecvStream()) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 204 | err := stream.Finish() |
Cosmos Nicolaou | e6fbf98 | 2015-06-18 16:37:19 -0700 | [diff] [blame] | 205 | ctx.VI(1).Infof("TUNNEL END : %v (%v, %v)", name, errf, err) |
Jiri Simsa | d5aae83 | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 206 | }() |
| 207 | } |
| 208 | } |