-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
61 lines (49 loc) · 1.44 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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
"golang.org/x/crypto/ssh"
"github.com/shmilwdc/gst/conf"
"github.com/shmilwdc/gst/log"
"github.com/shmilwdc/gst/tunnel"
)
var fpath string
func main() {
flag.StringVar(&fpath, "f", "conf/gst.toml", "config file location.")
flag.Parse()
// 加载配置文件
cfg := conf.LoadConf(fpath)
// 加载私钥文件
auth := tunnel.PrivateKeyFile(cfg.Key, cfg.Passphrase)
if auth == nil {
log.Fatal("tunnel.PrivateKeyFile error: ssh.AuthMethod is nil")
}
// 开始建立隧道
dial(cfg, auth)
// 监控配置文件
watch(fpath)
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
}
func dial(config *conf.Conf, auth ssh.AuthMethod) {
for _, v := range config.Binds {
log.Debugf("tag: %-50s\tforward: %-50s\n", v.Tag, fmt.Sprintf("[%s -> %s]", v.Local, v.Remote))
go tunnel.NewTunnel(v.Local, config.Jump, v.Remote, auth).Dial()
}
}
func watch(fpath string) {
log.Debugf("start watch conf file: %s", fpath)
viper.WatchConfig()
viper.OnConfigChange(func(in fsnotify.Event) {
log.Debugf("receive watch conf file event: %s", in)
if in.Op&fsnotify.Create == fsnotify.Create || in.Op&fsnotify.Write == fsnotify.Write {
os.Exit(1)
}
})
}