gotty/main.go

147 lines
3.6 KiB
Go
Raw Normal View History

2015-08-16 09:47:23 +00:00
package main
import (
"fmt"
"os"
2015-08-27 06:23:54 +00:00
"os/signal"
"syscall"
2015-08-16 09:47:23 +00:00
"github.com/codegangsta/cli"
"github.com/yudai/gotty/app"
)
func main() {
cmd := cli.NewApp()
cmd.Version = app.Version
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-27 06:23:54 +00:00
flags := []flag{
flag{"address", "a", "IP address to listen"},
flag{"port", "p", "Port number to listen"},
flag{"permit-write", "w", "Permit clients to write to the TTY (BE CAREFUL)"},
flag{"credential", "c", "Credential for Basic Authentication (ex: user:pass, default disabled)"},
flag{"random-url", "r", "Add a random string to the URL"},
flag{"random-url-length", "", "Random URL length"},
flag{"tls", "t", "Enable TLS/SSL"},
flag{"tls-crt", "", "TLS/SSL certificate file path"},
2015-08-27 06:23:54 +00:00
flag{"tls-key", "", "TLS/SSL key file path"},
flag{"tls-ca-crt", "", "TLS/SSL CA certificate file for client certifications"},
2015-08-30 08:27:37 +00:00
flag{"index", "", "Custom index.html file"},
2015-08-27 06:23:54 +00:00
flag{"title-format", "", "Title format of browser window"},
flag{"reconnect", "", "Enable reconnection"},
flag{"reconnect-time", "", "Time to reconnect"},
2017-01-09 03:01:30 +00:00
flag{"timeout", "", "Timeout seconds for waiting a client (0 to disable)"},
flag{"max-connection", "", "Maximum connection to gotty, 0(default) means no limit"},
2015-08-27 06:23:54 +00:00
flag{"once", "", "Accept only one client and exit on disconnection"},
flag{"permit-arguments", "", "Permit clients to send command line arguments in URL (e.g. http://example.com:8080/?arg=AAA&arg=BBB)"},
flag{"close-signal", "", "Signal sent to the command process when gotty close it (default: SIGHUP)"},
flag{"width", "", "Static width of the screen, 0(default) means dynamically resize"},
flag{"height", "", "Static height of the screen, 0(default) means dynamically resize"},
2015-08-27 06:23:54 +00:00
}
mappingHint := map[string]string{
"index": "IndexFile",
2015-08-27 06:23:54 +00:00
"tls": "EnableTLS",
"tls-crt": "TLSCrtFile",
"tls-key": "TLSKeyFile",
"tls-ca-crt": "TLSCACrtFile",
2015-08-27 06:23:54 +00:00
"random-url": "EnableRandomUrl",
"reconnect": "EnableReconnect",
}
cliFlags, err := generateFlags(flags, mappingHint)
if err != nil {
exit(err, 3)
}
cmd.Flags = append(
cliFlags,
cli.StringFlag{
2015-08-27 06:23:54 +00:00
Name: "config",
Value: "~/.gotty",
Usage: "Config file path",
EnvVar: "GOTTY_CONFIG",
},
2015-08-27 06:23:54 +00:00
)
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)
2015-08-27 06:23:54 +00:00
exit(err, 1)
}
options := app.DefaultOptions
configFile := c.String("config")
2015-08-29 04:11:46 +00:00
_, err := os.Stat(app.ExpandHomeDir(configFile))
2015-08-27 06:23:54 +00:00
if configFile != "~/.gotty" || !os.IsNotExist(err) {
if err := app.ApplyConfigFile(&options, configFile); err != nil {
exit(err, 2)
}
2015-08-16 09:47:23 +00:00
}
2015-08-23 11:40:18 +00:00
2015-08-27 06:23:54 +00:00
applyFlags(&options, flags, mappingHint, c)
if c.IsSet("credential") {
options.EnableBasicAuth = true
}
if c.IsSet("tls-ca-crt") {
options.EnableTLSClientAuth = true
}
if err := app.CheckConfig(&options); err != nil {
exit(err, 6)
}
2015-08-27 06:23:54 +00:00
app, err := app.New(c.Args(), &options)
2015-08-16 09:47:23 +00:00
if err != nil {
2015-08-27 06:23:54 +00:00
exit(err, 3)
2015-08-16 09:47:23 +00:00
}
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 {
2015-08-27 06:23:54 +00:00
exit(err, 4)
2015-08-23 11:40:18 +00:00
}
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
2015-08-27 06:23:54 +00:00
func exit(err error, code int) {
2015-08-29 04:11:46 +00:00
if err != nil {
fmt.Println(err)
}
2015-08-27 06:23:54 +00:00
os.Exit(code)
}
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:
2015-09-01 06:07:04 +00:00
if app.Exit() {
fmt.Println("Send ^C to force exit.")
} else {
2015-08-27 06:23:54 +00:00
os.Exit(5)
2015-08-24 10:22:25 +00:00
}
}
}
}()
}