2015-08-16 09:47:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
|
|
|
|
"github.com/yudai/gotty/app"
|
2015-08-24 10:22:25 +00:00
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2015-08-16 09:47:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cmd := cli.NewApp()
|
2015-08-22 06:41:03 +00:00
|
|
|
cmd.Version = "0.0.3"
|
2015-08-16 09:47:23 +00:00
|
|
|
cmd.Name = "gotty"
|
|
|
|
cmd.Usage = "Share your terminal as a web application"
|
2015-08-21 09:42:04 +00:00
|
|
|
cmd.HideHelp = true
|
2015-08-16 09:47:23 +00:00
|
|
|
cmd.Flags = []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "addr, a",
|
|
|
|
Value: "",
|
2015-08-17 02:30:44 +00:00
|
|
|
Usage: "IP address to listen",
|
2015-08-16 09:47:23 +00:00
|
|
|
EnvVar: "GOTTY_ADDR",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "port, p",
|
|
|
|
Value: "8080",
|
2015-08-17 02:30:44 +00:00
|
|
|
Usage: "Port number to listen",
|
2015-08-16 09:47:23 +00:00
|
|
|
EnvVar: "GOTTY_PORT",
|
|
|
|
},
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "permit-write, w",
|
2015-08-17 02:30:44 +00:00
|
|
|
Usage: "Permit clients to write to the TTY (BE CAREFUL)",
|
2015-08-16 09:47:23 +00:00
|
|
|
EnvVar: "GOTTY_PERMIT_WRITE",
|
|
|
|
},
|
2015-08-20 06:40:38 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "credential, c",
|
|
|
|
Usage: "Credential for Basic Authentication (ex: user:pass)",
|
|
|
|
EnvVar: "GOTTY_CREDENTIAL",
|
|
|
|
},
|
2015-08-19 11:35:04 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "random-url, r",
|
|
|
|
Usage: "Add a random string to the URL",
|
|
|
|
EnvVar: "GOTTY_RANDOM_URL",
|
|
|
|
},
|
2015-08-23 13:55:06 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "profile-file, f",
|
|
|
|
Value: app.DefaultProfileFilePath,
|
|
|
|
Usage: "Path to profile file",
|
|
|
|
EnvVar: "GOTTY_PROFILE_FILE",
|
|
|
|
},
|
2015-08-24 07:43:03 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "enable-tls, t",
|
|
|
|
Usage: "Enable TLS/SSL",
|
|
|
|
EnvVar: "GOTTY_ENABLE_TLS",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tls-cert",
|
|
|
|
Value: app.DefaultTLSCertPath,
|
|
|
|
Usage: "TLS/SSL cert",
|
|
|
|
EnvVar: "GOTTY_TLS_CERT",
|
|
|
|
},
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "tls-key",
|
|
|
|
Value: app.DefaultTLSKeyPath,
|
|
|
|
Usage: "TLS/SSL key",
|
|
|
|
EnvVar: "GOTTY_TLS_KEY",
|
|
|
|
},
|
2015-08-23 05:04:31 +00:00
|
|
|
cli.StringFlag{
|
|
|
|
Name: "title-format",
|
|
|
|
Value: "GoTTY - {{ .Command }} ({{ .Hostname }})",
|
|
|
|
Usage: "Title format of browser window",
|
2015-08-23 12:32:32 +00:00
|
|
|
EnvVar: "GOTTY_TITLE_FORMAT",
|
2015-08-23 05:04:31 +00:00
|
|
|
},
|
2015-08-23 22:14:24 +00:00
|
|
|
cli.IntFlag{
|
|
|
|
Name: "auto-reconnect",
|
|
|
|
Value: -1,
|
|
|
|
Usage: "Seconds to automatically reconnect to the server when the connection is closed (default: disabled)",
|
|
|
|
EnvVar: "GOTTY_AUTO_RECONNECT",
|
|
|
|
},
|
2015-08-24 10:35:49 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "once",
|
|
|
|
Usage: "Accept only one client and exits on disconnection",
|
|
|
|
EnvVar: "GOTTY_ONCE",
|
|
|
|
},
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
|
|
|
cmd.Action = func(c *cli.Context) {
|
|
|
|
if len(c.Args()) == 0 {
|
2015-08-17 02:30:44 +00:00
|
|
|
fmt.Println("Error: No command given.\n")
|
2015-08-16 09:47:23 +00:00
|
|
|
cli.ShowAppHelp(c)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2015-08-23 11:40:18 +00:00
|
|
|
|
|
|
|
app, err := app.New(
|
2015-08-21 03:48:07 +00:00
|
|
|
app.Options{
|
|
|
|
c.String("addr"),
|
|
|
|
c.String("port"),
|
|
|
|
c.Bool("permit-write"),
|
|
|
|
c.String("credential"),
|
|
|
|
c.Bool("random-url"),
|
2015-08-23 13:55:06 +00:00
|
|
|
c.String("profile-file"),
|
2015-08-24 07:43:03 +00:00
|
|
|
c.Bool("enable-tls"),
|
|
|
|
c.String("tls-cert"),
|
|
|
|
c.String("tls-key"),
|
2015-08-23 05:04:31 +00:00
|
|
|
c.String("title-format"),
|
2015-08-23 22:14:24 +00:00
|
|
|
c.Int("auto-reconnect"),
|
2015-08-24 10:35:49 +00:00
|
|
|
c.Bool("once"),
|
2015-08-21 03:48:07 +00:00
|
|
|
c.Args(),
|
|
|
|
},
|
|
|
|
)
|
2015-08-16 09:47:23 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
2015-08-23 11:40:18 +00:00
|
|
|
|
2015-08-24 10:22:25 +00:00
|
|
|
registerSignals(app)
|
|
|
|
|
2015-08-23 11:40:18 +00:00
|
|
|
err = app.Run()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(3)
|
|
|
|
}
|
2015-08-16 09:47:23 +00:00
|
|
|
}
|
2015-08-17 02:30:44 +00:00
|
|
|
|
2015-08-21 09:42:04 +00:00
|
|
|
cli.AppHelpTemplate = helpTemplate
|
2015-08-17 02:30:44 +00:00
|
|
|
|
2015-08-16 09:47:23 +00:00
|
|
|
cmd.Run(os.Args)
|
|
|
|
}
|
2015-08-24 10:22:25 +00:00
|
|
|
|
|
|
|
func registerSignals(app *app.App) {
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
|
|
sigChan,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
s := <-sigChan
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGINT, syscall.SIGTERM:
|
|
|
|
if !app.Exit() {
|
|
|
|
os.Exit(4)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|