-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
49 lines (38 loc) · 1.38 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
package main
import (
"github.com/selvakn/libzt"
"gopkg.in/alecthomas/kingpin.v2"
"io"
"p2p-port-forward/client"
"p2p-port-forward/logger"
"p2p-port-forward/server"
"p2p-port-forward/utils"
)
var (
network = kingpin.Flag("network", "zerotier network id").Short('n').Default("8056c2e21c000001").String()
forwardPort = kingpin.Flag("forward-port", "port to forward (in listen mode)").Short('f').Default("22").String()
acceptPort = kingpin.Flag("accept-port", "port to accept (in connect mode)").Short('a').Default("2222").String()
useUDP = kingpin.Flag("use-udp", "UDP instead of TCP (TCP default)").Short('u').Default("false").Bool()
connectTo = kingpin.Flag("connect-to", "server (zerotier) ip to connect").Short('c').String()
)
var log = logger.Logger
func main() {
kingpin.Version("1.1.1")
kingpin.Parse()
zt := libzt.Init(*network, "./zt")
log.Infof("ipv4 = %v ", zt.GetIPv4Address().String())
log.Infof("ipv6 = %v ", zt.GetIPv6Address().String())
var closableConn io.Closer
if len(*connectTo) == 0 {
forwarderServer := server.New(zt, *forwardPort, utils.GetIPProto(*useUDP))
closableConn = forwarderServer.Listen()
} else {
forwarderClient := client.New(zt, *connectTo, *acceptPort, utils.GetIPProto(*useUDP))
closableConn = forwarderClient.ListenAndSync()
}
<-utils.SetupCleanUpOnInterrupt(func() {
if closableConn != nil {
closableConn.Close()
}
})
}