-
Notifications
You must be signed in to change notification settings - Fork 23
/
init.go
225 lines (198 loc) · 9.34 KB
/
init.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package main
import (
"errors"
"flag"
"fmt"
"github.com/BurntSushi/toml"
"github.com/mythologyli/zju-connect/configs"
"os"
"regexp"
"strings"
)
func getTOMLVal[T int | uint64 | string | bool](valPointer *T, defaultVal T) T {
if valPointer == nil {
return defaultVal
} else {
return *valPointer
}
}
func parseTOMLConfig(configFile string, conf *configs.Config) error {
var confTOML configs.ConfigTOML
_, err := toml.DecodeFile(configFile, &confTOML)
if err != nil {
return errors.New("ZJU Connect: error parsing the config file")
}
conf.ServerAddress = getTOMLVal(confTOML.ServerAddress, "rvpn.zju.edu.cn")
conf.ServerPort = getTOMLVal(confTOML.ServerPort, 443)
conf.Username = getTOMLVal(confTOML.Username, "")
conf.Password = getTOMLVal(confTOML.Password, "")
conf.DisableServerConfig = getTOMLVal(confTOML.DisableServerConfig, false)
conf.DisableZJUConfig = getTOMLVal(confTOML.DisableZJUConfig, false)
conf.DisableZJUDNS = getTOMLVal(confTOML.DisableZJUDNS, false)
conf.DisableMultiLine = getTOMLVal(confTOML.DisableMultiLine, false)
conf.ProxyAll = getTOMLVal(confTOML.ProxyAll, false)
conf.SocksBind = getTOMLVal(confTOML.SocksBind, ":1080")
conf.SocksUser = getTOMLVal(confTOML.SocksUser, "")
conf.SocksPasswd = getTOMLVal(confTOML.SocksPasswd, "")
conf.HTTPBind = getTOMLVal(confTOML.HTTPBind, ":1081")
conf.ShadowsocksURL = getTOMLVal(confTOML.ShadowsocksURL, "")
conf.DialDirectProxy = getTOMLVal(confTOML.DialDirectProxy, "")
conf.TUNMode = getTOMLVal(confTOML.TUNMode, false)
conf.AddRoute = getTOMLVal(confTOML.AddRoute, false)
conf.DNSTTL = getTOMLVal(confTOML.DNSTTL, uint64(3600))
conf.DebugDump = getTOMLVal(confTOML.DebugDump, false)
conf.DisableKeepAlive = getTOMLVal(confTOML.DisableKeepAlive, false)
conf.ZJUDNSServer = getTOMLVal(confTOML.ZJUDNSServer, "10.10.0.21")
conf.SecondaryDNSServer = getTOMLVal(confTOML.SecondaryDNSServer, "114.114.114.114")
conf.DNSServerBind = getTOMLVal(confTOML.DNSServerBind, "")
conf.DNSHijack = getTOMLVal(confTOML.DNSHijack, false)
for _, singlePortForwarding := range confTOML.PortForwarding {
if singlePortForwarding.NetworkType == nil {
return errors.New("ZJU Connect: network type is not set")
}
if singlePortForwarding.BindAddress == nil {
return errors.New("ZJU Connect: bind address is not set")
}
if singlePortForwarding.RemoteAddress == nil {
return errors.New("ZJU Connect: remote address is not set")
}
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: *singlePortForwarding.NetworkType,
BindAddress: *singlePortForwarding.BindAddress,
RemoteAddress: *singlePortForwarding.RemoteAddress,
})
}
for _, singleCustomDns := range confTOML.CustomDNS {
if singleCustomDns.HostName == nil {
return errors.New("ZJU Connect: host name is not set")
}
if singleCustomDns.IP == nil {
fmt.Println("ZJU Connect: IP is not set")
return errors.New("ZJU Connect: IP is not set")
}
conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{
HostName: *singleCustomDns.HostName,
IP: *singleCustomDns.IP,
})
}
for _, singleCustomProxyDomain := range confTOML.CustomProxyDomain {
var domainRegex = regexp.MustCompile(`^[a-zA-Z\d-]+(\.[a-zA-Z\d-]+)*\.[a-zA-Z]{2,}$`)
if !domainRegex.MatchString(singleCustomProxyDomain) {
fmt.Printf("ZJU Connect: %s is not a valid domain\n", singleCustomProxyDomain)
return errors.New(fmt.Sprintf("ZJU Connect: %s is not a valid domain", singleCustomProxyDomain))
}
conf.CustomProxyDomain = append(conf.CustomProxyDomain, singleCustomProxyDomain)
}
return nil
}
func init() {
configFile, tcpPortForwarding, udpPortForwarding, customDns, customProxyDomain := "", "", "", "", ""
showVersion := false
flag.StringVar(&conf.ServerAddress, "server", "rvpn.zju.edu.cn", "EasyConnect server address")
flag.IntVar(&conf.ServerPort, "port", 443, "EasyConnect port address")
flag.StringVar(&conf.Username, "username", "", "Your username")
flag.StringVar(&conf.Password, "password", "", "Your password")
flag.BoolVar(&conf.DisableServerConfig, "disable-server-config", false, "Don't parse server config")
flag.BoolVar(&conf.DisableZJUConfig, "disable-zju-config", false, "Don't use ZJU config")
flag.BoolVar(&conf.DisableZJUDNS, "disable-zju-dns", false, "Use local DNS instead of ZJU DNS")
flag.BoolVar(&conf.DisableMultiLine, "disable-multi-line", false, "Disable multi line auto select")
flag.BoolVar(&conf.ProxyAll, "proxy-all", false, "Proxy all IPv4 traffic")
flag.StringVar(&conf.SocksBind, "socks-bind", ":1080", "The address SOCKS5 server listens on (e.g. 127.0.0.1:1080)")
flag.StringVar(&conf.SocksUser, "socks-user", "", "SOCKS5 username, default is don't use auth")
flag.StringVar(&conf.SocksPasswd, "socks-passwd", "", "SOCKS5 password, default is don't use auth")
flag.StringVar(&conf.HTTPBind, "http-bind", ":1081", "The address HTTP server listens on (e.g. 127.0.0.1:1081)")
flag.StringVar(&conf.ShadowsocksURL, "shadowsocks-url", "", "The address Shadowsocks server listens on (e.g. ss://method:password@host:port)")
flag.StringVar(&conf.DialDirectProxy, "dial-direct-proxy", "", "Dial with proxy when the connection doesn't match RVPN rules (e.g. http://127.0.0.1:7890)")
flag.BoolVar(&conf.TUNMode, "tun-mode", false, "Enable TUN mode (experimental)")
flag.BoolVar(&conf.AddRoute, "add-route", false, "Add route from rules for TUN interface")
flag.Uint64Var(&conf.DNSTTL, "dns-ttl", 3600, "DNS record time to live, unit is second")
flag.BoolVar(&conf.DebugDump, "debug-dump", false, "Enable traffic debug dump (only for debug usage)")
flag.BoolVar(&conf.DisableKeepAlive, "disable-keep-alive", false, "Disable keep alive")
flag.StringVar(&conf.ZJUDNSServer, "zju-dns-server", "10.10.0.21", "ZJU DNS server address")
flag.StringVar(&conf.SecondaryDNSServer, "secondary-dns-server", "114.114.114.114", "Secondary DNS server address. Leave empty to use system default DNS server")
flag.StringVar(&conf.DNSServerBind, "dns-server-bind", "", "The address DNS server listens on (e.g. 127.0.0.1:53)")
flag.BoolVar(&conf.DNSHijack, "dns-hijack", false, "Hijack all dns query to ZJU Connect")
flag.StringVar(&conf.TwfID, "twf-id", "", "Login using twfID captured (mostly for debug usage)")
flag.StringVar(&tcpPortForwarding, "tcp-port-forwarding", "", "TCP port forwarding (e.g. 0.0.0.0:9898-10.10.98.98:80,127.0.0.1:9899-10.10.98.98:80)")
flag.StringVar(&udpPortForwarding, "udp-port-forwarding", "", "UDP port forwarding (e.g. 127.0.0.1:53-10.10.0.21:53)")
flag.StringVar(&customDns, "custom-dns", "", "Custom set dns lookup (e.g. www.cc98.org:10.10.98.98,appservice.zju.edu.cn:10.203.8.198)")
flag.StringVar(&customProxyDomain, "custom-proxy-domain", "", "Custom set domains which force use RVPN proxy (e.g. science.org, nature.com)")
flag.StringVar(&configFile, "config", "", "Config file")
flag.BoolVar(&showVersion, "version", false, "Show version")
flag.Parse()
if showVersion {
fmt.Printf("ZJU Connect v%s\n", zjuConnectVersion)
os.Exit(0)
}
if configFile != "" {
err := parseTOMLConfig(configFile, &conf)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
} else {
if tcpPortForwarding != "" {
forwardingStringList := strings.Split(tcpPortForwarding, ",")
for _, forwardingString := range forwardingStringList {
addressStringList := strings.Split(forwardingString, "-")
if len(addressStringList) != 2 {
fmt.Println("ZJU Connect: wrong tcp port forwarding format")
os.Exit(1)
}
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: "tcp",
BindAddress: addressStringList[0],
RemoteAddress: addressStringList[1],
})
}
}
if udpPortForwarding != "" {
forwardingStringList := strings.Split(udpPortForwarding, ",")
for _, forwardingString := range forwardingStringList {
addressStringList := strings.Split(forwardingString, "-")
if len(addressStringList) != 2 {
fmt.Println("ZJU Connect: wrong udp port forwarding format")
os.Exit(1)
}
conf.PortForwardingList = append(conf.PortForwardingList, configs.SinglePortForwarding{
NetworkType: "udp",
BindAddress: addressStringList[0],
RemoteAddress: addressStringList[1],
})
}
}
if customDns != "" {
dnsList := strings.Split(customDns, ",")
for _, dnsString := range dnsList {
dnsStringSplit := strings.Split(dnsString, ":")
if len(dnsStringSplit) != 2 {
fmt.Println("ZJU Connect: wrong custom dns format")
os.Exit(1)
}
conf.CustomDNSList = append(conf.CustomDNSList, configs.SingleCustomDNS{
HostName: dnsStringSplit[0],
IP: dnsStringSplit[1],
})
}
}
if customProxyDomain != "" {
domainList := strings.Split(customProxyDomain, ",")
for _, domain := range domainList {
var domainRegex = regexp.MustCompile(`^[a-zA-Z\d-]+(\.[a-zA-Z\d-]+)*\.[a-zA-Z]{2,}$`)
if !domainRegex.MatchString(domain) {
fmt.Printf("ZJU Connect: %s is not a valid domain\n", domain)
os.Exit(1)
}
conf.CustomProxyDomain = append(conf.CustomProxyDomain, domain)
}
}
}
if conf.ServerAddress == "" || ((conf.Username == "" || conf.Password == "") && conf.TwfID == "") {
fmt.Println("ZJU Connect")
fmt.Println("Please see: https://github.com/mythologyli/zju-connect")
fmt.Printf("\nUsage: %s -username <username> -password <password>\n", os.Args[0])
fmt.Println("\nFull usage:")
flag.PrintDefaults()
os.Exit(1)
}
}