-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.go
185 lines (156 loc) · 4.82 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
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
//go:build !tun
package main
import (
"context"
"fmt"
"github.com/mythologyli/zju-connect/client"
"github.com/mythologyli/zju-connect/configs"
"github.com/mythologyli/zju-connect/dial"
"github.com/mythologyli/zju-connect/internal/hook_func"
"github.com/mythologyli/zju-connect/log"
"github.com/mythologyli/zju-connect/resolve"
"github.com/mythologyli/zju-connect/service"
"github.com/mythologyli/zju-connect/stack"
"github.com/mythologyli/zju-connect/stack/gvisor"
"github.com/mythologyli/zju-connect/stack/tun"
"inet.af/netaddr"
"net"
"os"
"os/signal"
"syscall"
)
var conf configs.Config
const zjuConnectVersion = "0.7.1"
func main() {
log.Init()
log.Println("Start ZJU Connect v" + zjuConnectVersion)
if conf.DebugDump {
log.EnableDebug()
}
if errs := hook_func.ExecInitialFunc(context.Background(), conf); errs != nil {
for _, err := range errs {
log.Printf("Initial ZJU-Connect failed: %s", err)
}
os.Exit(1)
}
vpnClient := client.NewEasyConnectClient(
conf.ServerAddress+":"+fmt.Sprintf("%d", conf.ServerPort),
conf.Username,
conf.Password,
conf.TwfID,
!conf.DisableMultiLine,
!conf.DisableServerConfig,
)
err := vpnClient.Setup()
if err != nil {
log.Fatalf("EasyConnect client setup error: %s", err)
}
log.Printf("EasyConnect client started")
ipResource, err := vpnClient.IPResource()
if err != nil && !conf.DisableMultiLine {
log.Println("No IP resource")
}
domainResource, err := vpnClient.DomainResource()
if err != nil && !conf.DisableMultiLine {
log.Println("No domain resource")
}
dnsResource, err := vpnClient.DNSResource()
if err != nil && !conf.DisableMultiLine {
log.Println("No DNS resource")
}
if !conf.DisableZJUConfig {
if domainResource != nil {
domainResource["zju.edu.cn"] = true
} else {
domainResource = map[string]bool{"zju.edu.cn": true}
}
ipSetBuilder := netaddr.IPSetBuilder{}
if ipResource != nil {
ipSetBuilder.AddSet(ipResource)
}
ipSetBuilder.AddPrefix(netaddr.MustParseIPPrefix("10.0.0.0/8"))
ipResource, _ = ipSetBuilder.IPSet()
}
for _, customProxyDomain := range conf.CustomProxyDomain {
domainResource[customProxyDomain] = true
}
var vpnStack stack.Stack
if conf.TUNMode {
vpnTUNStack, err := tun.NewStack(vpnClient, conf.DNSHijack)
if err != nil {
log.Fatalf("Tun stack setup error, make sure you are root user : %s", err)
}
if conf.AddRoute && ipResource != nil {
for _, prefix := range ipResource.Prefixes() {
log.Printf("Add route to %s", prefix.String())
_ = vpnTUNStack.AddRoute(prefix.String())
}
}
vpnStack = vpnTUNStack
} else {
vpnStack, err = gvisor.NewStack(vpnClient)
if err != nil {
log.Fatalf("gVisor stack setup error: %s", err)
}
}
vpnResolver := resolve.NewResolver(
vpnStack,
conf.ZJUDNSServer,
conf.SecondaryDNSServer,
conf.DNSTTL,
domainResource,
dnsResource,
!conf.DisableZJUDNS,
)
for _, customDns := range conf.CustomDNSList {
ipAddr := net.ParseIP(customDns.IP)
if ipAddr == nil {
log.Printf("Custom DNS for host name %s is invalid, SKIP", customDns.HostName)
}
vpnResolver.SetPermanentDNS(customDns.HostName, ipAddr)
log.Printf("Add custom DNS: %s -> %s\n", customDns.HostName, customDns.IP)
}
localResolver := service.NewDnsServer(vpnResolver, []string{conf.ZJUDNSServer, conf.SecondaryDNSServer})
vpnStack.SetupResolve(localResolver)
go vpnStack.Run()
vpnDialer := dial.NewDialer(vpnStack, vpnResolver, ipResource, conf.ProxyAll, conf.DialDirectProxy)
if conf.DNSServerBind != "" {
go service.ServeDNS(conf.DNSServerBind, localResolver)
}
if conf.TUNMode {
clientIP, _ := vpnClient.IP()
go service.ServeDNS(clientIP.String()+":53", localResolver)
}
if conf.SocksBind != "" {
go service.ServeSocks5(conf.SocksBind, vpnDialer, vpnResolver, conf.SocksUser, conf.SocksPasswd)
}
if conf.HTTPBind != "" {
go service.ServeHTTP(conf.HTTPBind, vpnDialer)
}
if conf.ShadowsocksURL != "" {
go service.ServeShadowsocks(vpnDialer, conf.ShadowsocksURL)
}
for _, portForwarding := range conf.PortForwardingList {
if portForwarding.NetworkType == "tcp" {
go service.ServeTCPForwarding(vpnStack, portForwarding.BindAddress, portForwarding.RemoteAddress)
} else if portForwarding.NetworkType == "udp" {
go service.ServeUDPForwarding(vpnStack, portForwarding.BindAddress, portForwarding.RemoteAddress)
} else {
log.Printf("Port forwarding: unknown network type %s. Aborting", portForwarding.NetworkType)
}
}
if !conf.DisableKeepAlive {
go service.KeepAlive(vpnResolver)
}
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
<-quit
log.Println("Shutdown ZJU-Connect ......")
if errs := hook_func.ExecTerminalFunc(context.Background()); errs != nil {
for _, err := range errs {
log.Printf("Shutdown ZJU-Connect failed: %s", err)
}
} else {
log.Println("Shutdown ZJU-Connect success, Bye~")
}
}