2015-08-16 17:47:23 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-02-26 06:37:07 +08:00
|
|
|
"context"
|
2015-08-16 17:47:23 +08:00
|
|
|
"fmt"
|
2023-10-21 23:49:19 +08:00
|
|
|
"io"
|
2022-01-13 07:34:32 +08:00
|
|
|
"log"
|
2015-08-16 17:47:23 +08:00
|
|
|
"os"
|
2015-08-27 14:23:54 +08:00
|
|
|
"os/signal"
|
2017-08-22 11:11:11 +08:00
|
|
|
"strings"
|
2015-08-27 14:23:54 +08:00
|
|
|
"syscall"
|
2015-08-16 17:47:23 +08:00
|
|
|
|
2021-04-13 11:58:39 +08:00
|
|
|
cli "github.com/urfave/cli/v2"
|
2015-08-16 17:47:23 +08:00
|
|
|
|
2021-04-11 12:39:41 +08:00
|
|
|
"github.com/sorenisanerd/gotty/backend/localcommand"
|
|
|
|
"github.com/sorenisanerd/gotty/pkg/homedir"
|
|
|
|
"github.com/sorenisanerd/gotty/server"
|
|
|
|
"github.com/sorenisanerd/gotty/utils"
|
2015-08-16 17:47:23 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2017-02-26 06:37:07 +08:00
|
|
|
app := cli.NewApp()
|
|
|
|
app.Name = "gotty"
|
2021-04-22 23:20:08 +08:00
|
|
|
app.Version = Version
|
2017-02-26 06:37:07 +08:00
|
|
|
app.Usage = "Share your terminal as a web application"
|
2021-04-18 22:01:31 +08:00
|
|
|
app.HideHelpCommand = true
|
2017-02-26 06:37:07 +08:00
|
|
|
appOptions := &server.Options{}
|
2021-04-18 22:01:31 +08:00
|
|
|
|
2017-01-10 00:13:36 +08:00
|
|
|
if err := utils.ApplyDefaultValues(appOptions); err != nil {
|
|
|
|
exit(err, 1)
|
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
backendOptions := &localcommand.Options{}
|
2017-01-10 00:13:36 +08:00
|
|
|
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
|
2017-01-09 22:06:46 +08:00
|
|
|
exit(err, 1)
|
2015-08-27 14:23:54 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 00:13:36 +08:00
|
|
|
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
|
2015-08-27 14:23:54 +08:00
|
|
|
if err != nil {
|
|
|
|
exit(err, 3)
|
|
|
|
}
|
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
app.Flags = append(
|
2015-08-27 14:23:54 +08:00
|
|
|
cliFlags,
|
2021-04-13 11:58:39 +08:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Value: "~/.gotty",
|
|
|
|
Usage: "Config file path",
|
|
|
|
EnvVars: []string{"GOTTY_CONFIG"},
|
2015-08-24 18:35:49 +08:00
|
|
|
},
|
2015-08-27 14:23:54 +08:00
|
|
|
)
|
|
|
|
|
2021-04-13 11:58:39 +08:00
|
|
|
app.Action = func(c *cli.Context) error {
|
|
|
|
if c.NArg() == 0 {
|
2017-01-10 00:13:36 +08:00
|
|
|
msg := "Error: No command given."
|
2015-08-16 17:47:23 +08:00
|
|
|
cli.ShowAppHelp(c)
|
2017-01-10 00:13:36 +08:00
|
|
|
exit(fmt.Errorf(msg), 1)
|
2015-08-27 14:23:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
configFile := c.String("config")
|
2017-02-26 06:37:07 +08:00
|
|
|
_, err := os.Stat(homedir.Expand(configFile))
|
2015-08-27 14:23:54 +08:00
|
|
|
if configFile != "~/.gotty" || !os.IsNotExist(err) {
|
2017-01-10 00:13:36 +08:00
|
|
|
if err := utils.ApplyConfigFile(configFile, appOptions, backendOptions); err != nil {
|
2015-08-27 14:23:54 +08:00
|
|
|
exit(err, 2)
|
|
|
|
}
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
2015-08-23 19:40:18 +08:00
|
|
|
|
2022-08-31 08:02:18 +08:00
|
|
|
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
|
|
|
|
|
2021-11-10 00:23:04 +08:00
|
|
|
if appOptions.Quiet {
|
|
|
|
log.SetFlags(0)
|
2023-10-21 23:49:19 +08:00
|
|
|
log.SetOutput(io.Discard)
|
2021-11-10 00:23:04 +08:00
|
|
|
}
|
|
|
|
|
2021-05-30 17:11:07 +08:00
|
|
|
if c.IsSet("credential") {
|
|
|
|
appOptions.EnableBasicAuth = true
|
|
|
|
}
|
|
|
|
if c.IsSet("tls-ca-crt") {
|
|
|
|
appOptions.EnableTLSClientAuth = true
|
|
|
|
}
|
2015-10-05 15:08:25 +08:00
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
err = appOptions.Validate()
|
|
|
|
if err != nil {
|
2015-10-05 15:08:25 +08:00
|
|
|
exit(err, 6)
|
|
|
|
}
|
2015-08-27 14:23:54 +08:00
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
args := c.Args()
|
2021-04-13 11:58:39 +08:00
|
|
|
factory, err := localcommand.NewFactory(args.First(), args.Tail(), backendOptions)
|
2017-01-10 00:13:36 +08:00
|
|
|
if err != nil {
|
|
|
|
exit(err, 3)
|
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
|
|
|
|
hostname, _ := os.Hostname()
|
|
|
|
appOptions.TitleVariables = map[string]interface{}{
|
2021-04-13 11:58:39 +08:00
|
|
|
"command": args.First(),
|
|
|
|
"argv": args.Tail(),
|
2017-02-26 06:37:07 +08:00
|
|
|
"hostname": hostname,
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, err := server.New(factory, appOptions)
|
2015-08-16 17:47:23 +08:00
|
|
|
if err != nil {
|
2015-08-27 14:23:54 +08:00
|
|
|
exit(err, 3)
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
2015-08-23 19:40:18 +08:00
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
gCtx, gCancel := context.WithCancel(context.Background())
|
2015-08-24 18:22:25 +08:00
|
|
|
|
2021-04-13 11:58:39 +08:00
|
|
|
log.Printf("GoTTY is starting with command: %s", strings.Join(args.Slice(), " "))
|
2017-08-22 11:11:11 +08:00
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
errs := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
errs <- srv.Run(ctx, server.WithGracefullContext(gCtx))
|
|
|
|
}()
|
|
|
|
err = waitSignals(errs, cancel, gCancel)
|
|
|
|
|
|
|
|
if err != nil && err != context.Canceled {
|
|
|
|
fmt.Printf("Error: %s\n", err)
|
|
|
|
exit(err, 8)
|
2015-08-23 19:40:18 +08:00
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
|
2021-04-13 11:58:39 +08:00
|
|
|
return nil
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
app.Run(os.Args)
|
2015-08-16 17:47:23 +08:00
|
|
|
}
|
2015-08-24 18:22:25 +08:00
|
|
|
|
2015-08-27 14:23:54 +08:00
|
|
|
func exit(err error, code int) {
|
2015-08-29 12:11:46 +08:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2015-08-27 14:23:54 +08:00
|
|
|
os.Exit(code)
|
|
|
|
}
|
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
func waitSignals(errs chan error, cancel context.CancelFunc, gracefullCancel context.CancelFunc) error {
|
2015-08-24 18:22:25 +08:00
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(
|
|
|
|
sigChan,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
)
|
|
|
|
|
2017-02-26 06:37:07 +08:00
|
|
|
select {
|
|
|
|
case err := <-errs:
|
|
|
|
return err
|
|
|
|
|
|
|
|
case s := <-sigChan:
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGINT:
|
|
|
|
gracefullCancel()
|
|
|
|
fmt.Println("C-C to force close")
|
|
|
|
select {
|
|
|
|
case err := <-errs:
|
|
|
|
return err
|
|
|
|
case <-sigChan:
|
2017-08-20 12:38:42 +08:00
|
|
|
fmt.Println("Force closing...")
|
2017-02-26 06:37:07 +08:00
|
|
|
cancel()
|
|
|
|
return <-errs
|
2015-08-24 18:22:25 +08:00
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
default:
|
|
|
|
cancel()
|
|
|
|
return <-errs
|
2015-08-24 18:22:25 +08:00
|
|
|
}
|
2017-02-26 06:37:07 +08:00
|
|
|
}
|
2015-08-24 18:22:25 +08:00
|
|
|
}
|