forked from WireGuard/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathapp.go
457 lines (383 loc) · 11 KB
/
app.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package app
import (
"context"
"encoding/base64"
"errors"
"fmt"
"log/slog"
"net/netip"
"path"
"github.com/bepass-org/warp-plus/iputils"
"github.com/bepass-org/warp-plus/psiphon"
"github.com/bepass-org/warp-plus/warp"
"github.com/bepass-org/warp-plus/wireguard/tun"
"github.com/bepass-org/warp-plus/wireguard/tun/netstack"
"github.com/bepass-org/warp-plus/wiresocks"
)
const singleMTU = 1330
const doubleMTU = 1280 // minimum mtu for IPv6, may cause frag reassembly somewhere
type WarpOptions struct {
Bind netip.AddrPort
Endpoint string
License string
DnsAddr netip.Addr
Psiphon *PsiphonOptions
Gool bool
Scan *wiresocks.ScanOptions
CacheDir string
FwMark uint32
WireguardConfig string
Reserved string
}
type PsiphonOptions struct {
Country string
}
func RunWarp(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
if opts.WireguardConfig != "" {
if err := runWireguard(ctx, l, opts); err != nil {
return err
}
return nil
}
if opts.Psiphon != nil && opts.Gool {
return errors.New("can't use psiphon and gool at the same time")
}
if opts.Psiphon != nil && opts.Psiphon.Country == "" {
return errors.New("must provide country for psiphon")
}
// Decide Working Scenario
endpoints := []string{opts.Endpoint, opts.Endpoint}
if opts.Scan != nil {
// make primary identity
ident, err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "primary"), opts.License)
if err != nil {
l.Error("couldn't load primary warp identity")
return err
}
// Reading the private key from the 'Interface' section
opts.Scan.PrivateKey = ident.PrivateKey
// Reading the public key from the 'Peer' section
opts.Scan.PublicKey = ident.Config.Peers[0].PublicKey
res, err := wiresocks.RunScan(ctx, l, *opts.Scan)
if err != nil {
return err
}
l.Debug("scan results", "endpoints", res)
endpoints = make([]string, len(res))
for i := 0; i < len(res); i++ {
endpoints[i] = res[i].AddrPort.String()
}
}
l.Info("using warp endpoints", "endpoints", endpoints)
var warpErr error
switch {
case opts.Psiphon != nil:
l.Info("running in Psiphon (cfon) mode")
// run primary warp on a random tcp port and run psiphon on bind address
warpErr = runWarpWithPsiphon(ctx, l, opts, endpoints[0])
case opts.Gool:
l.Info("running in warp-in-warp (gool) mode")
// run warp in warp
warpErr = runWarpInWarp(ctx, l, opts, endpoints)
default:
l.Info("running in normal warp mode")
// just run primary warp on bindAddress
warpErr = runWarp(ctx, l, opts, endpoints[0])
}
return warpErr
}
func runWireguard(ctx context.Context, l *slog.Logger, opts WarpOptions) error {
conf, err := wiresocks.ParseConfig(opts.WireguardConfig)
if err != nil {
return err
}
// Set up MTU
conf.Interface.MTU = singleMTU
// Set up DNS Address
conf.Interface.DNS = []netip.Addr{opts.DnsAddr}
// Enable trick and keepalive on all peers in config
for i, peer := range conf.Peers {
peer.Trick = true
peer.KeepAlive = 5
// Try resolving if the endpoint is a domain
addr, err := iputils.ParseResolveAddressPort(peer.Endpoint, false, opts.DnsAddr.String())
if err == nil {
peer.Endpoint = addr.String()
}
conf.Peers[i] = peer
}
// Establish wireguard on userspace stack
var werr error
var tnet *netstack.Net
var tunDev tun.Device
for _, t := range []string{"t1", "t2"} {
// Create userspace tun network stack
tunDev, tnet, werr = netstack.CreateNetTUN(conf.Interface.Addresses, conf.Interface.DNS, conf.Interface.MTU)
if werr != nil {
continue
}
werr = establishWireguard(l, conf, tunDev, opts.FwMark, t)
if werr != nil {
continue
}
// Test wireguard connectivity
werr = usermodeTunTest(ctx, l, tnet)
if werr != nil {
continue
}
break
}
if werr != nil {
return werr
}
// Run a proxy on the userspace stack
_, err = wiresocks.StartProxy(ctx, l, tnet, opts.Bind)
if err != nil {
return err
}
l.Info("serving proxy", "address", opts.Bind)
return nil
}
func runWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoint string) error {
// make primary identity
ident, err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "primary"), opts.License)
if err != nil {
l.Error("couldn't load primary warp identity")
return err
}
conf := generateWireguardConfig(ident)
// Set up MTU
conf.Interface.MTU = singleMTU
// Set up DNS Address
conf.Interface.DNS = []netip.Addr{opts.DnsAddr}
// Enable trick and keepalive on all peers in config
for i, peer := range conf.Peers {
peer.Endpoint = endpoint
peer.Trick = true
peer.KeepAlive = 5
if opts.Reserved != "" {
r, err := wiresocks.ParseReserved(opts.Reserved)
if err != nil {
return err
}
peer.Reserved = r
}
conf.Peers[i] = peer
}
// Establish wireguard on userspace stack
var werr error
var tnet *netstack.Net
var tunDev tun.Device
for _, t := range []string{"t1", "t2"} {
tunDev, tnet, werr = netstack.CreateNetTUN(conf.Interface.Addresses, conf.Interface.DNS, conf.Interface.MTU)
if werr != nil {
continue
}
werr = establishWireguard(l, &conf, tunDev, opts.FwMark, t)
if werr != nil {
continue
}
// Test wireguard connectivity
werr = usermodeTunTest(ctx, l, tnet)
if werr != nil {
continue
}
break
}
if werr != nil {
return werr
}
// Run a proxy on the userspace stack
_, err = wiresocks.StartProxy(ctx, l, tnet, opts.Bind)
if err != nil {
return err
}
l.Info("serving proxy", "address", opts.Bind)
return nil
}
func runWarpInWarp(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoints []string) error {
// make primary identity
ident1, err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "primary"), opts.License)
if err != nil {
l.Error("couldn't load primary warp identity")
return err
}
conf := generateWireguardConfig(ident1)
// Set up MTU
conf.Interface.MTU = singleMTU
// Set up DNS Address
conf.Interface.DNS = []netip.Addr{opts.DnsAddr}
// Enable trick and keepalive on all peers in config
for i, peer := range conf.Peers {
peer.Endpoint = endpoints[0]
peer.Trick = true
peer.KeepAlive = 5
if opts.Reserved != "" {
r, err := wiresocks.ParseReserved(opts.Reserved)
if err != nil {
return err
}
peer.Reserved = r
}
conf.Peers[i] = peer
}
// Establish wireguard on userspace stack and bind the wireguard sockets to the default interface and apply
var werr error
var tnet1 *netstack.Net
var tunDev tun.Device
for _, t := range []string{"t1", "t2"} {
// Create userspace tun network stack
tunDev, tnet1, werr = netstack.CreateNetTUN(conf.Interface.Addresses, conf.Interface.DNS, conf.Interface.MTU)
if werr != nil {
continue
}
werr = establishWireguard(l.With("gool", "outer"), &conf, tunDev, opts.FwMark, t)
if werr != nil {
continue
}
// Test wireguard connectivity
werr = usermodeTunTest(ctx, l, tnet1)
if werr != nil {
continue
}
break
}
if werr != nil {
return werr
}
// Create a UDP port forward between localhost and the remote endpoint
addr, err := wiresocks.NewVtunUDPForwarder(ctx, netip.MustParseAddrPort("127.0.0.1:0"), endpoints[0], tnet1, singleMTU)
if err != nil {
return err
}
// make secondary
ident2, err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "secondary"), opts.License)
if err != nil {
l.Error("couldn't load secondary warp identity")
return err
}
conf = generateWireguardConfig(ident2)
// Set up MTU
conf.Interface.MTU = doubleMTU
// Set up DNS Address
conf.Interface.DNS = []netip.Addr{opts.DnsAddr}
// Enable keepalive on all peers in config
for i, peer := range conf.Peers {
peer.Endpoint = addr.String()
peer.KeepAlive = 20
if opts.Reserved != "" {
r, err := wiresocks.ParseReserved(opts.Reserved)
if err != nil {
return err
}
peer.Reserved = r
}
conf.Peers[i] = peer
}
// Create userspace tun network stack
tunDev, tnet2, err := netstack.CreateNetTUN(conf.Interface.Addresses, conf.Interface.DNS, conf.Interface.MTU)
if err != nil {
return err
}
// Establish wireguard on userspace stack
if err := establishWireguard(l.With("gool", "inner"), &conf, tunDev, opts.FwMark, "t0"); err != nil {
return err
}
// Test wireguard connectivity
if err := usermodeTunTest(ctx, l, tnet2); err != nil {
return err
}
_, err = wiresocks.StartProxy(ctx, l, tnet2, opts.Bind)
if err != nil {
return err
}
l.Info("serving proxy", "address", opts.Bind)
return nil
}
func runWarpWithPsiphon(ctx context.Context, l *slog.Logger, opts WarpOptions, endpoint string) error {
// make primary identity
ident, err := warp.LoadOrCreateIdentity(l, path.Join(opts.CacheDir, "primary"), opts.License)
if err != nil {
l.Error("couldn't load primary warp identity")
return err
}
conf := generateWireguardConfig(ident)
// Set up MTU
conf.Interface.MTU = singleMTU
// Set up DNS Address
conf.Interface.DNS = []netip.Addr{opts.DnsAddr}
// Enable trick and keepalive on all peers in config
for i, peer := range conf.Peers {
peer.Endpoint = endpoint
peer.Trick = true
peer.KeepAlive = 5
if opts.Reserved != "" {
r, err := wiresocks.ParseReserved(opts.Reserved)
if err != nil {
return err
}
peer.Reserved = r
}
conf.Peers[i] = peer
}
// Establish wireguard on userspace stack
var werr error
var tnet *netstack.Net
var tunDev tun.Device
for _, t := range []string{"t1", "t2"} {
// Create userspace tun network stack
tunDev, tnet, werr = netstack.CreateNetTUN(conf.Interface.Addresses, conf.Interface.DNS, conf.Interface.MTU)
if werr != nil {
continue
}
werr = establishWireguard(l, &conf, tunDev, opts.FwMark, t)
if werr != nil {
continue
}
// Test wireguard connectivity
werr = usermodeTunTest(ctx, l, tnet)
if werr != nil {
continue
}
break
}
if werr != nil {
return werr
}
// Run a proxy on the userspace stack
warpBind, err := wiresocks.StartProxy(ctx, l, tnet, netip.MustParseAddrPort("127.0.0.1:0"))
if err != nil {
return err
}
// run psiphon
err = psiphon.RunPsiphon(ctx, l.With("subsystem", "psiphon"), warpBind, opts.CacheDir, opts.Bind, opts.Psiphon.Country)
if err != nil {
return fmt.Errorf("unable to run psiphon %w", err)
}
l.Info("serving proxy", "address", opts.Bind)
return nil
}
func generateWireguardConfig(i *warp.Identity) wiresocks.Configuration {
priv, _ := wiresocks.EncodeBase64ToHex(i.PrivateKey)
pub, _ := wiresocks.EncodeBase64ToHex(i.Config.Peers[0].PublicKey)
clientID, _ := base64.StdEncoding.DecodeString(i.Config.ClientID)
return wiresocks.Configuration{
Interface: &wiresocks.InterfaceConfig{
PrivateKey: priv,
Addresses: []netip.Addr{
netip.MustParseAddr(i.Config.Interface.Addresses.V4),
netip.MustParseAddr(i.Config.Interface.Addresses.V6),
},
},
Peers: []wiresocks.PeerConfig{{
PublicKey: pub,
PreSharedKey: "0000000000000000000000000000000000000000000000000000000000000000",
AllowedIPs: []netip.Prefix{
netip.MustParsePrefix("0.0.0.0/0"),
netip.MustParsePrefix("::/0"),
},
Endpoint: i.Config.Peers[0].Endpoint.Host,
Reserved: [3]byte{clientID[0], clientID[1], clientID[2]},
}},
}
}