-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
209 lines (179 loc) · 4.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/urfave/cli/v2"
"github.com/kost/tty2web/backend/localcommand"
"github.com/kost/tty2web/pkg/homedir"
"github.com/kost/tty2web/server"
"github.com/kost/tty2web/utils"
)
func main() {
SCEnvAndExecute()
app := cli.NewApp()
app.Name = "tty2web"
app.Version = Version + "+" + CommitID
app.Usage = "Share your terminal as a web application"
app.HideHelp = true
cli.AppHelpTemplate = helpTemplate
appOptions := &server.Options{}
if err := utils.ApplyDefaultValues(appOptions); err != nil {
log.Printf("Error applying default value: %v", err)
exit(err, 1)
}
backendOptions := &localcommand.Options{}
if err := utils.ApplyDefaultValues(backendOptions); err != nil {
log.Printf("Error applying backend default value: %v", err)
exit(err, 1)
}
cliFlags, flagMappings, err := utils.GenerateFlags(appOptions, backendOptions)
if err != nil {
log.Printf("Error generating flags: %v", err)
exit(err, 3)
}
app.Flags = append(
cliFlags,
&cli.StringFlag{
Name: "config",
Value: "~/.tty2web",
Usage: "Config file path",
EnvVars: []string{"TTY2WEB_CONFIG"},
},
&cli.BoolFlag{
Name: "help",
Usage: "Displays help",
},
)
app.Action = func(c *cli.Context) error {
configFile := c.String("config")
_, err := os.Stat(homedir.Expand(configFile))
if configFile != "~/.tty2web" || !os.IsNotExist(err) {
if err := utils.ApplyConfigFile(configFile, appOptions, backendOptions); err != nil {
log.Printf("Error applying config file: %v", err)
exit(err, 2)
}
}
utils.ApplyFlags(cliFlags, flagMappings, c, appOptions, backendOptions)
appOptions.EnableBasicAuth = c.IsSet("credential")
appOptions.EnableTLSClientAuth = c.IsSet("tls-ca-crt")
err = appOptions.Validate()
if err != nil {
log.Printf("Error validating options: %v", err)
exit(err, 6)
}
if appOptions.Dns!="" {
if appOptions.DnsKey == "" {
appOptions.DnsKey=GenerateKey()
log.Printf("No password specified, generated following (recheck if same on both sides): %s", appOptions.DnsKey)
}
if len(appOptions.DnsKey) != 64 {
fmt.Fprintf(os.Stderr, "Specified key of incorrect size for DNS (should be 64 in hex)\n")
os.Exit(1)
}
if appOptions.DnsListen!="" {
go func() {
log.Fatal(ServeDNS (appOptions.DnsListen,appOptions.Dns, appOptions.Server, appOptions.DnsKey, appOptions.DnsDelay))
}()
wait4Signals()
return nil
}
}
if appOptions.Listen!="" {
log.Printf("Listening for reverse connection %s", appOptions.Listen)
go func() {
log.Fatal(listenForAgents(appOptions.Verbose, appOptions.AgentTLS, appOptions.Listen, appOptions.Server, appOptions.ListenCert, appOptions.Password))
}()
wait4Signals()
return nil
}
args := c.Args()
if args.Len() == 0 {
msg := "Error: No command given."
cli.ShowAppHelp(c)
exit(fmt.Errorf(msg), 1)
}
if c.Bool("help") {
cli.ShowAppHelp(c)
exit(err, 1)
}
factory, err := localcommand.NewFactory(c.Args().First(), c.Args().Tail(), backendOptions)
if err != nil {
log.Printf("Error creating local command: %v", err)
exit(err, 3)
}
hostname, _ := os.Hostname()
appOptions.TitleVariables = map[string]interface{}{
"command": c.Args().First(),
"argv": c.Args().Tail(),
"hostname": hostname,
}
srv, err := server.New(factory, appOptions)
if err != nil {
log.Printf("Error creating new server: %v", err)
exit(err, 5)
}
log.Printf("tty2web is starting with command: %s", strings.Join(args.Slice(), " "))
ctx, cancel := context.WithCancel(context.Background())
gCtx, gCancel := context.WithCancel(context.Background())
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)
}
return nil
}
app.Run(os.Args)
}
func exit(err error, code int) {
if err != nil {
fmt.Println(err)
}
os.Exit(code)
}
func wait4Signals() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt)
select {
case sig := <-c:
fmt.Printf("Got %s signal. Aborting...\n", sig)
os.Exit(1)
}
}
func waitSignals(errs chan error, cancel context.CancelFunc, gracefullCancel context.CancelFunc) error {
sigChan := make(chan os.Signal, 1)
signal.Notify(
sigChan,
syscall.SIGINT,
syscall.SIGTERM,
)
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:
fmt.Println("Force closing...")
cancel()
return <-errs
}
default:
cancel()
return <-errs
}
}
}