forked from bepass-org/warp-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
152 lines (132 loc) · 3.23 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
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net/netip"
"os"
"os/signal"
"syscall"
"time"
_ "net/http/pprof"
"github.com/bepass-org/warp-plus/app"
"github.com/bepass-org/warp-plus/warp"
"github.com/bepass-org/warp-plus/wiresocks"
"github.com/peterbourgon/ff/v4"
"github.com/peterbourgon/ff/v4/ffhelp"
"github.com/peterbourgon/ff/v4/ffjson"
)
var psiphonCountries = []string{
"AT",
"BE",
"BG",
"BR",
"CA",
"CH",
"CZ",
"DE",
"DK",
"EE",
"ES",
"FI",
"FR",
"GB",
"HU",
"IE",
"IN",
"IT",
"JP",
"LV",
"NL",
"NO",
"PL",
"RO",
"RS",
"SE",
"SG",
"SK",
"UA",
"US",
}
func main() {
fs := ff.NewFlagSet("warp-plus")
var (
v4 = fs.BoolShort('4', "only use IPv4 for random warp endpoint")
v6 = fs.BoolShort('6', "only use IPv6 for random warp endpoint")
verbose = fs.Bool('v', "verbose", "enable verbose logging")
bind = fs.String('b', "bind", "127.0.0.1:8086", "socks bind address")
endpoint = fs.String('e', "endpoint", "", "warp endpoint")
key = fs.String('k', "key", "", "warp key")
gool = fs.BoolLong("gool", "enable gool mode (warp in warp)")
psiphon = fs.BoolLong("cfon", "enable psiphon mode (must provide country as well)")
country = fs.StringEnumLong("country", fmt.Sprintf("psiphon country code (valid values: %s)", psiphonCountries), psiphonCountries...)
scan = fs.BoolLong("scan", "enable warp scanning")
rtt = fs.DurationLong("rtt", 1000*time.Millisecond, "scanner rtt limit")
_ = fs.String('c', "config", "", "path to config file")
)
err := ff.Parse(
fs,
os.Args[1:],
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ffjson.Parse),
)
switch {
case errors.Is(err, ff.ErrHelp):
fmt.Fprintf(os.Stderr, "%s\n", ffhelp.Flags(fs))
os.Exit(0)
case err != nil:
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
l := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}))
if *verbose {
l = slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug}))
}
if *psiphon && *gool {
fatal(l, errors.New("can't use cfon and gool at the same time"))
}
if *v4 && *v6 {
fatal(l, errors.New("can't force v4 and v6 at the same time"))
}
if !*v4 && !*v6 {
*v4, *v6 = true, true
}
bindAddrPort, err := netip.ParseAddrPort(*bind)
if err != nil {
fatal(l, fmt.Errorf("invalid bind address: %w", err))
}
opts := app.WarpOptions{
Bind: bindAddrPort,
Endpoint: *endpoint,
License: *key,
Gool: *gool,
}
if *psiphon {
l.Info("psiphon mode enabled", "country", *country)
opts.Psiphon = &app.PsiphonOptions{Country: *country}
}
if *scan {
l.Info("scanner mode enabled", "max-rtt", rtt)
opts.Scan = &wiresocks.ScanOptions{V4: *v4, V6: *v6, MaxRTT: *rtt}
}
// If the endpoint is not set, choose a random warp endpoint
if opts.Endpoint == "" {
addrPort, err := warp.RandomWarpEndpoint(*v4, *v6)
if err != nil {
fatal(l, err)
}
opts.Endpoint = addrPort.String()
}
ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
go func() {
if err := app.RunWarp(ctx, l, opts); err != nil {
fatal(l, err)
}
}()
<-ctx.Done()
}
func fatal(l *slog.Logger, err error) {
l.Error(err.Error())
os.Exit(1)
}