2017-10-14 11:13:47 +00:00
package main
import (
2020-09-26 08:41:54 +00:00
"bufio"
2017-10-14 11:13:47 +00:00
"flag"
"fmt"
"io"
"os"
"strings"
2020-09-26 21:01:33 +00:00
"github.com/elisescu/tty-share/proxy"
2020-09-26 08:41:54 +00:00
"github.com/elisescu/tty-share/server"
ttyServer "github.com/elisescu/tty-share/server"
log "github.com/sirupsen/logrus"
2017-10-14 11:13:47 +00:00
)
2020-01-10 21:22:33 +00:00
var version string = "0.0.0"
2017-10-14 11:13:47 +00:00
2020-10-10 19:42:01 +00:00
func createServer ( frontListenAddress string , frontendPath string , pty server . PTYHandler , sessionID string ) * server . TTYServer {
2020-09-26 08:41:54 +00:00
config := ttyServer . TTYServerConfig {
FrontListenAddress : frontListenAddress ,
FrontendPath : frontendPath ,
2020-10-10 19:42:01 +00:00
PTY : pty ,
2020-09-26 21:01:33 +00:00
SessionID : sessionID ,
2020-09-26 08:41:54 +00:00
}
server := ttyServer . NewTTYServer ( config )
return server
}
2020-10-10 19:42:01 +00:00
type nilPTY struct {
2020-09-26 21:01:33 +00:00
}
2020-10-10 19:42:01 +00:00
func ( nw * nilPTY ) Write ( data [ ] byte ) ( int , error ) {
2020-09-26 21:01:33 +00:00
return len ( data ) , nil
}
2020-10-10 19:42:01 +00:00
func ( nw * nilPTY ) Refresh ( ) {
}
2017-10-14 11:13:47 +00:00
func main ( ) {
2020-10-02 22:02:20 +00:00
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 .
2020-10-04 11:10:49 +00:00
tty - share [ [ -- args < "args" > ] -- command < executable > ] # share the terminal and get a session URL , as a server
[ -- logfile < file name > ] [ -- listen < [ ip ] : port > ]
[ -- frontend - path < path > ] [ -- tty - proxy < host : port > ]
[ -- readonly ] [ -- public ] [ no - tls ] [ -- verbose ] [ -- version ]
tty - share [ -- verbose ] [ -- logfile < file name > ]
[ -- detach - keys ] < session URL > # connect to an existing session , as a client
2020-10-02 22:02:20 +00:00
Examples :
Start bash and create a public sharing session , so it ' s accessible outside the local network , and make the session read only :
2020-10-04 11:10:49 +00:00
tty - share -- public -- readonly -- command bash
2020-10-02 22:02:20 +00:00
Join a remote session by providing the URL created another tty - share command :
2020-10-04 11:10:49 +00:00
tty - share http : //localhost:8000/s/local/
2020-10-02 22:02:20 +00:00
Flags :
`
2020-01-07 21:21:38 +00:00
commandName := flag . String ( "command" , os . Getenv ( "SHELL" ) , "The command to run" )
if * commandName == "" {
* commandName = "bash"
}
2017-10-14 11:13:47 +00:00
commandArgs := flag . String ( "args" , "" , "The command arguments" )
logFileName := flag . String ( "logfile" , "-" , "The name of the file to log" )
2020-09-26 21:01:33 +00:00
listenAddress := flag . String ( "listen" , "localhost:8000" , "tty-server address" )
2020-01-10 21:22:33 +00:00
versionFlag := flag . Bool ( "version" , false , "Print the tty-share version" )
2020-10-02 22:02:20 +00:00
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." )
2020-10-03 11:22:25 +00:00
proxyServerAddress := flag . String ( "tty-proxy" , "on.tty-share.com:4567" , "Address of the proxy for public facing connections" )
2020-09-26 21:01:33 +00:00
readOnly := flag . Bool ( "readonly" , false , "Start a read only session" )
publicSession := flag . Bool ( "public" , false , "Create a public session" )
2020-10-02 22:49:55 +00:00
noTLS := flag . Bool ( "no-tls" , false , "Don't use TLS to connect to the tty-proxy server. Useful for local debugging" )
2020-10-04 11:10:49 +00:00
detachKeys := flag . String ( "detach-keys" , "ctrl-o,ctrl-c" , "Sequence of keys to press for closing the connection. Supported: https://godoc.org/github.com/moby/term#pkg-variables." )
2020-10-03 11:22:25 +00:00
verbose := flag . Bool ( "verbose" , false , "Verbose logging" )
2020-10-02 22:02:20 +00:00
flag . Usage = func ( ) {
fmt . Fprintf ( flag . CommandLine . Output ( ) , "%s" , usageString )
flag . PrintDefaults ( )
fmt . Fprintf ( flag . CommandLine . Output ( ) , "\n" )
}
2017-10-14 11:13:47 +00:00
flag . Parse ( )
2020-01-10 21:22:33 +00:00
if * versionFlag {
fmt . Printf ( "%s\n" , version )
return
}
2020-10-03 11:22:25 +00:00
// Log setup
log . SetLevel ( log . WarnLevel )
if * verbose {
log . SetLevel ( log . DebugLevel )
}
if * logFileName != "-" {
logFile , err := os . Create ( * logFileName )
if err != nil {
fmt . Printf ( "Can't open %s for writing logs\n" , * logFileName )
}
log . SetOutput ( logFile )
}
2020-10-02 22:02:20 +00:00
// 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 ]
2020-10-04 11:10:49 +00:00
client := newTtyShareClient ( connectURL , * detachKeys )
2020-10-02 22:02:20 +00:00
err := client . Run ( )
if err != nil {
2020-10-04 11:10:49 +00:00
fmt . Printf ( "Cannot connect to the remote session. Make sure the URL points to a valid tty-share session.\n" )
2020-10-02 22:02:20 +00:00
}
2020-10-04 11:10:49 +00:00
fmt . Printf ( "\ntty-share disconnected\n\n" )
2020-10-02 22:02:20 +00:00
return
}
2020-10-03 11:22:25 +00:00
// tty-share works as a server, from here on
2020-01-11 22:06:59 +00:00
if ! isStdinTerminal ( ) {
fmt . Printf ( "Input not a tty\n" )
os . Exit ( 1 )
}
2017-10-14 11:13:47 +00:00
2020-12-20 18:06:06 +00:00
sessionID := ""
publicURL := ""
2020-09-26 21:01:33 +00:00
if * publicSession {
2020-10-02 22:49:55 +00:00
proxy , err := proxy . NewProxyConnection ( * listenAddress , * proxyServerAddress , * noTLS )
2020-09-26 21:01:33 +00:00
if err != nil {
2020-10-03 11:22:25 +00:00
log . Errorf ( "Can't connect to the proxy: %s\n" , err . Error ( ) )
2020-09-26 21:01:33 +00:00
return
}
go proxy . RunProxy ( )
sessionID = proxy . SessionID
2020-12-20 18:06:06 +00:00
publicURL = proxy . PublicURL
2020-09-26 21:01:33 +00:00
defer proxy . Stop ( )
}
2020-12-20 18:06:06 +00:00
envVars := os . Environ ( )
envVars = append ( envVars ,
fmt . Sprintf ( "TTY_SHARE_LOCAL_URL=http://%s" , * listenAddress ) ,
fmt . Sprintf ( "TTY_SHARE=1" , os . Getpid ( ) ) ,
)
if publicURL != "" {
envVars = append ( envVars ,
fmt . Sprintf ( "TTY_SHARE_PUBLIC_URL=%s" , publicURL ) ,
)
}
ptyMaster := ptyMasterNew ( )
err := ptyMaster . Start ( * commandName , strings . Fields ( * commandArgs ) , envVars )
if err != nil {
log . Errorf ( "Cannot start the %s command: %s" , * commandName , err . Error ( ) )
return
}
2017-10-14 11:13:47 +00:00
// Display the session information to the user, before showing any output from the command.
// Wait until the user presses Enter
2020-12-20 18:06:06 +00:00
if publicURL != "" {
fmt . Printf ( "public session: %s\n" , publicURL )
}
2020-10-03 11:22:25 +00:00
fmt . Printf ( "local session: http://%s/s/local/\n" , * listenAddress )
2020-09-26 21:01:33 +00:00
fmt . Printf ( "Press Enter to continue!\n" )
2020-09-26 08:41:54 +00:00
bufio . NewReader ( os . Stdin ) . ReadString ( '\n' )
2017-10-14 11:13:47 +00:00
2020-11-04 18:39:00 +00:00
stopPtyAndRestore := func ( ) {
ptyMaster . Stop ( )
ptyMaster . Restore ( )
}
2020-11-02 22:04:24 +00:00
ptyMaster . MakeRaw ( )
2020-11-04 18:39:00 +00:00
defer stopPtyAndRestore ( )
2020-10-10 19:42:01 +00:00
var pty server . PTYHandler = ptyMaster
2020-09-26 21:01:33 +00:00
if * readOnly {
2020-10-10 19:42:01 +00:00
pty = & nilPTY { }
2020-09-26 21:01:33 +00:00
}
2020-10-10 19:42:01 +00:00
server := createServer ( * listenAddress , * frontendPath , pty , sessionID )
2017-10-14 11:13:47 +00:00
if cols , rows , e := ptyMaster . GetWinSize ( ) ; e == nil {
2020-09-26 08:41:54 +00:00
server . WindowSize ( cols , rows )
2017-10-14 11:13:47 +00:00
}
2020-09-26 08:41:54 +00:00
ptyMaster . SetWinChangeCB ( func ( cols , rows int ) {
2020-10-02 22:02:20 +00:00
log . Debugf ( "New window size: %dx%d" , cols , rows )
2020-09-26 08:41:54 +00:00
server . WindowSize ( cols , rows )
} )
mw := io . MultiWriter ( os . Stdout , server )
2017-10-14 11:13:47 +00:00
2020-11-04 18:39:00 +00:00
2017-10-14 11:13:47 +00:00
go func ( ) {
2020-10-10 19:42:01 +00:00
err := server . Run ( )
2020-09-26 21:01:33 +00:00
if err != nil {
2020-11-04 18:39:00 +00:00
stopPtyAndRestore ( )
log . Errorf ( "Server finished: %s" , err . Error ( ) )
2020-09-26 21:01:33 +00:00
}
2017-10-14 11:13:47 +00:00
} ( )
go func ( ) {
2020-09-26 08:41:54 +00:00
_ , err := io . Copy ( mw , ptyMaster )
if err != nil {
2020-11-04 18:39:00 +00:00
stopPtyAndRestore ( )
2017-10-14 11:13:47 +00:00
}
} ( )
go func ( ) {
2020-11-04 18:39:00 +00:00
_ , err := io . Copy ( ptyMaster , os . Stdin )
if err != nil {
stopPtyAndRestore ( )
}
2017-10-14 11:13:47 +00:00
} ( )
ptyMaster . Wait ( )
2020-11-03 12:59:55 +00:00
fmt . Printf ( "tty-share finished\n\n\r" )
2020-09-26 08:41:54 +00:00
server . Stop ( )
2017-10-14 11:13:47 +00:00
}