-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
79 lines (67 loc) · 1.64 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
/**
* Copyright 2015-2016, Wothing Co., Ltd.
* All rights reserved.
*
* Created by Elvizlai on 2016/07/18 09:48
*/
package main
import (
"flag"
"log"
"log/syslog"
"os"
"os/signal"
"syscall"
"github.com/elvizlai/xtunnel/tunnel"
)
var logTo string = "stdout"
var mode = "server"
var laddr = "127.0.0.1:9000"
var raddr = "127.0.0.1:9001"
var cryptoMethod = "blank"
var secret = "xtunnel"
func init() {
flag.StringVar(&mode, "mode", mode, "run mode: server, client")
flag.StringVar(&laddr, "listen", laddr, "xtunnel local listen")
flag.StringVar(&raddr, "backend", raddr, "xtunnel remote backend")
flag.StringVar(&cryptoMethod, "crypto", cryptoMethod, "encryption method: blank, rc4, rc4-md5, aes256cfb, chacha20, salsa20")
flag.StringVar(&secret, "secret", secret, "password used to encrypt data")
flag.StringVar(&logTo, "log", logTo, "stdout or syslog")
flag.Parse()
}
type svr interface {
Run()
}
func wait() {
var sigChan = make(chan os.Signal, 1)
signal.Notify(sigChan)
for sig := range sigChan {
if sig == syscall.SIGINT || sig == syscall.SIGTERM {
log.Printf("terminated by signal %v\n", sig)
return
}
log.Printf("received signal: %v, ignore\n", sig)
}
}
func main() {
if logTo == "syslog" {
w, err := syslog.New(syslog.LOG_INFO, "xtunnel")
if err != nil {
log.Fatal(err)
}
log.SetOutput(w)
} else {
log.SetOutput(os.Stdout)
}
var app svr
switch mode {
case "server":
app = tunnel.NewTunnel(laddr, raddr, false, cryptoMethod, secret, 4096)
case "client":
app = tunnel.NewTunnel(laddr, raddr, true, cryptoMethod, secret, 4096)
default:
log.Fatalf("no such '%s' mode", mode)
}
app.Run()
wait()
}