diff --git a/client.go b/client.go index 10c030c..eaabd11 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "fmt" "io" "net/http" "net/url" @@ -35,7 +36,7 @@ func (w *wsTextWriter) Write(data []byte) (n int, err error) { } func (c *ttyShareClient) Run() (err error) { - log.Printf("Starting tty-share client on %s", c.url) + log.Debugf("Starting tty-share client on %s", c.url) resp, err := http.Get(c.url) @@ -57,7 +58,7 @@ func (c *ttyShareClient) Run() (err error) { } wsURL := wsScheme + "://" + httpURL.Host + wsPath - log.Printf("Connecting to WS URL: %s", wsURL) + log.Debugf("Connecting to WS URL: %s", wsURL) conn, _, err := websocket.DefaultDialer.Dial(wsURL, nil) if err != nil { @@ -73,7 +74,7 @@ func (c *ttyShareClient) Run() (err error) { var msg ttyServer.MsgAll _, r, err := conn.NextReader() if err != nil { - log.Infof("Connection closed: %s", err.Error()) + fmt.Printf("Connection closed\n") return } err = json.NewDecoder(r).Decode(&msg) @@ -92,7 +93,13 @@ func (c *ttyShareClient) Run() (err error) { os.Stdout.Write(msgWrite.Data) case ttyServer.MsgIDWinSize: - log.Debugf("Got Win Size msg") + log.Debugf("Remote window changed its size") + // We ignore the window size changes - can't do much about that for + // now. + + // TODO: Maybe just clear the screen, and display an error message + // if the remote window gets bigger than this terminal window - when + // it does, it usually messes up the output } } } @@ -105,7 +112,8 @@ func (c *ttyShareClient) Run() (err error) { _, err := io.Copy(ttyServer.NewTTYProtocolWriter(ww), os.Stdin) if err != nil { - log.Errorf("Finished io.Copy with %s", err.Error()) + fmt.Printf("Connection closed.\n") + return } } go writeLoop() diff --git a/main.go b/main.go index 93ed854..0ee1b5d 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( var version string = "0.0.0" + func createServer(frontListenAddress string, frontendPath string, tty io.Writer, sessionID string) *server.TTYServer { config := ttyServer.TTYServerConfig{ FrontListenAddress: frontListenAddress, @@ -36,6 +37,24 @@ func (nw *nilWriter) Write(data []byte) (int, error) { } func main() { + usageString := ` +Usage: + tty-share creates a session to a terminal application with remote participants. The session can be joined either from the browser, or by tty-share command itself. + + tty-share [flags] ; share the terminal and get a session URL, as a server + tty-share ; connect to an existing session, as a client + +Examples: + Start bash and create a public sharing session, so it's accessible outside the local network, and make the session read only: + + tty-share --public --readonly --command bash + + Join a remote session by providing the URL created another tty-share command: + + tty-share http://localhost:8000/local/ + +Flags: +` commandName := flag.String("command", os.Getenv("SHELL"), "The command to run") if *commandName == "" { *commandName = "bash" @@ -44,11 +63,16 @@ func main() { logFileName := flag.String("logfile", "-", "The name of the file to log") listenAddress := flag.String("listen", "localhost:8000", "tty-server address") versionFlag := flag.Bool("version", false, "Print the tty-share version") - frontendPath := flag.String("frontend_path", "", "The path to the frontend resources. By default, these resources are included in the server binary, so you only need this path if you don't want to use the bundled ones.") - proxyServerAddress := flag.String("proxy_address", "localhost:9000", "Address of the proxy for public facing connections") + frontendPath := flag.String("frontend-path", "", "The path to the frontend resources. By default, these resources are included in the server binary, so you only need this path if you don't want to use the bundled ones.") + proxyServerAddress := flag.String("tty-proxy", "localhost:9000", "Address of the proxy for public facing connections") readOnly := flag.Bool("readonly", false, "Start a read only session") publicSession := flag.Bool("public", false, "Create a public session") - connectURL := flag.String("connect", "", "Use as client to connect to a remote tty-share, instead of using the browser") + flag.Usage = func() { + fmt.Fprintf(flag.CommandLine.Output(), "%s", usageString) + flag.PrintDefaults() + fmt.Fprintf(flag.CommandLine.Output(), "\n") + } + flag.Parse() if *versionFlag { @@ -56,6 +80,22 @@ func main() { return } + // tty-share can work in two modes: either starting a command to be shared by acting as a + // server, or by acting as a client for the remote side If we have an argument, that is not + // a flag, passed to tty-share, we expect that to be the URl to connect to, as a + // client. Otherwise, tty-share will act as the server. + args := flag.Args() + if len(args) == 1 { + connectURL := args[0] + client := newTtyShareClient(connectURL) + + err := client.Run() + if err != nil { + fmt.Printf("Cannot connect to the remote session: %s\n", err.Error()) + } + return + } + log.SetLevel(log.InfoLevel) if *logFileName != "-" { fmt.Printf("Writing logs to: %s\n", *logFileName) @@ -72,18 +112,6 @@ func main() { os.Exit(1) } - - if *connectURL != "" { - client := newTtyShareClient(*connectURL) - - err := client.Run() - - if err != nil { - panic(err.Error()) - } - return - } - sessionID := "local" if *publicSession { proxy, err := proxy.NewProxyConnection(*listenAddress, *proxyServerAddress) @@ -94,13 +122,13 @@ func main() { go proxy.RunProxy() sessionID = proxy.SessionID - fmt.Printf("%s\n", proxy.PublicURL) + fmt.Printf("public session: %s\n", proxy.PublicURL) defer proxy.Stop() } // Display the session information to the user, before showing any output from the command. // Wait until the user presses Enter - fmt.Printf("http://%s/local/\n", *listenAddress) + fmt.Printf("local session: http://%s/local/\n", *listenAddress) fmt.Printf("Press Enter to continue!\n") bufio.NewReader(os.Stdin).ReadString('\n') @@ -118,7 +146,7 @@ func main() { } ptyMaster.SetWinChangeCB(func(cols, rows int) { - log.Infof("New window size: %dx%d", cols, rows) + log.Debugf("New window size: %dx%d", cols, rows) server.WindowSize(cols, rows) }) diff --git a/proxy/proxy.go b/proxy/proxy.go index e772979..9b281ab 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -68,7 +68,7 @@ func (p *proxyConnection) RunProxy() { for { conn, err := p.muxSession.Accept() if err != nil { - log.Errorf("Client: Accept returned %s. Done with this client.\n", err.Error()) + log.Errorf("tty-proxy connection closed.\n") return }