You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tty-share/main.go

106 lines
2.7 KiB
Go

7 years ago
package main
import (
"bufio"
7 years ago
"flag"
"fmt"
"io"
"os"
7 years ago
"strings"
"github.com/elisescu/tty-share/server"
ttyServer "github.com/elisescu/tty-share/server"
log "github.com/sirupsen/logrus"
7 years ago
)
var version string = "0.0.0"
7 years ago
func createServer(frontListenAddress string, frontendPath string, tty io.Writer) *server.TTYServer {
config := ttyServer.TTYServerConfig{
FrontListenAddress: frontListenAddress,
FrontendPath: frontendPath,
TTYWriter: tty,
}
server := ttyServer.NewTTYServer(config)
log.Info("Listening on address: http://", config.FrontListenAddress)
return server
}
7 years ago
func main() {
commandName := flag.String("command", os.Getenv("SHELL"), "The command to run")
if *commandName == "" {
*commandName = "bash"
}
7 years ago
commandArgs := flag.String("args", "", "The command arguments")
logFileName := flag.String("logfile", "-", "The name of the file to log")
listenAddress := flag.String("listen", "localhost:8080", "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.")
7 years ago
flag.Parse()
if *versionFlag {
fmt.Printf("%s\n", version)
return
}
log.SetLevel(log.ErrorLevel)
7 years ago
if *logFileName != "-" {
fmt.Printf("Writing logs to: %s\n", *logFileName)
logFile, err := os.Create(*logFileName)
if err != nil {
fmt.Printf("Can't open %s for writing logs\n", *logFileName)
}
log.SetLevel(log.DebugLevel)
log.SetOutput(logFile)
7 years ago
}
if !isStdinTerminal() {
fmt.Printf("Input not a tty\n")
os.Exit(1)
}
7 years ago
// Display the session information to the user, before showing any output from the command.
// Wait until the user presses Enter
fmt.Printf("Web terminal: http://%s\n\n\rPress Enter to continue!\n", *listenAddress)
bufio.NewReader(os.Stdin).ReadString('\n')
7 years ago
ptyMaster := ptyMasterNew()
ptyMaster.Start(*commandName, strings.Fields(*commandArgs))
7 years ago
server := createServer(*listenAddress, *frontendPath, ptyMaster)
7 years ago
if cols, rows, e := ptyMaster.GetWinSize(); e == nil {
server.WindowSize(cols, rows)
7 years ago
}
ptyMaster.SetWinChangeCB(func(cols, rows int) {
log.Infof("New window size: %dx%d", cols, rows)
server.WindowSize(cols, rows)
})
mw := io.MultiWriter(os.Stdout, server)
7 years ago
go func() {
server.Run(func (clientAddr string){
ptyMaster.Refresh()
})
7 years ago
}()
go func() {
_, err := io.Copy(mw, ptyMaster)
if err != nil {
log.Error("Lost connection with the server.\n")
ptyMaster.Stop()
7 years ago
}
}()
go func() {
io.Copy(ptyMaster, os.Stdin)
}()
ptyMaster.Wait()
fmt.Printf("tty-share finished.\n\r")
server.Stop()
7 years ago
}