-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
94 lines (82 loc) · 1.94 KB
/
main.go
File metadata and controls
94 lines (82 loc) · 1.94 KB
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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"github.com/tech-thinker/telepath/services"
"github.com/urfave/cli/v2"
)
var (
AppVersion = "v0.0.0"
CommitHash = "unknown"
BuildDate = "unknown"
)
func main() {
var configPath string
var dryRun bool = false
cli.VersionPrinter = func(c *cli.Context) {
fmt.Printf("Version: %s\n", AppVersion)
fmt.Printf("Commit: %s\n", CommitHash)
fmt.Printf("Build Date: %s\n", BuildDate)
}
app := &cli.App{
Name: "telepath",
Version: AppVersion,
Description: "telepath is a cli application to forward port securly.",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "config-file",
Aliases: []string{"f"},
Usage: "accepts config file path.",
Required: true,
Destination: &configPath,
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "use to tests your configuration.",
Required: false,
Destination: &dryRun,
},
},
Action: func(ctx *cli.Context) error {
cfgs, err := services.ParseConfig(configPath)
if err != nil {
return err
}
cfgs, err = services.ValidateConfig(cfgs)
if err != nil {
return err
}
if dryRun {
fmt.Println("Your configuration format is valid.")
return nil
}
// Root context — cancelled on Ctrl+C / SIGTERM
rootCtx, cancel := context.WithCancel(context.Background())
defer cancel()
// Listen for shutdown signals
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
sig := <-sigCh
log.Printf("Received signal %s, shutting down gracefully...", sig)
cancel()
}()
s := services.NewServer(cfgs)
var wg sync.WaitGroup
s.StartAll(rootCtx, &wg)
// Block until all tunnels have exited cleanly
wg.Wait()
log.Println("All tunnels stopped. Goodbye.")
return nil
},
}
if err := app.Run(os.Args); err != nil {
fmt.Println(err)
os.Exit(1)
}
}