forked from Ptechgithub/wireguard-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (129 loc) · 3.24 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"syscall"
"github.com/bepass-org/wireguard-go/app"
)
type Flags struct {
Verbose bool
BindAddress string
Endpoint string
License string
Country string
PsiphonEnabled bool
Gool bool
Scan bool
Rtt int
}
var validFlags = map[string]bool{
"-v": true,
"-b": true,
"-e": true,
"-k": true,
"-country": true,
"-cfon": true,
"-gool": true,
"-scan": true,
"-rtt": true,
}
func newFlags() *Flags {
return &Flags{}
}
func (f *Flags) setup() {
flag.BoolVar(&f.Verbose, "v", false, "verbose")
flag.StringVar(&f.BindAddress, "b", "127.0.0.1:8086", "socks bind address")
flag.StringVar(&f.Endpoint, "e", "notset", "warp clean IP")
flag.StringVar(&f.License, "k", "notset", "license key")
flag.StringVar(&f.Country, "country", "", "psiphon country code in ISO 3166-1 alpha-2 format")
flag.BoolVar(&f.PsiphonEnabled, "cfon", false, "enable Psiphon over warp")
flag.BoolVar(&f.Gool, "gool", false, "enable warp gooling")
flag.BoolVar(&f.Scan, "scan", false, "enable warp scanner(experimental)")
flag.IntVar(&f.Rtt, "rtt", 1000, "scanner rtt threshold, default 1000")
flag.Usage = usage
flag.Parse()
}
var validCountryCodes = map[string]bool{
"AT": true,
"BE": true,
"BG": true,
"BR": true,
"CA": true,
"CH": true,
"CZ": true,
"DE": true,
"DK": true,
"EE": true,
"ES": true,
"FI": true,
"FR": true,
"GB": true,
"HU": true,
"IE": true,
"IN": true,
"IT": true,
"JP": true,
"LV": true,
"NL": true,
"NO": true,
"PL": true,
"RO": true,
"RS": true,
"SE": true,
"SG": true,
"SK": true,
"UA": true,
"US": true,
}
func usage() {
log.Println("./warp-plus-go [-v] [-b addr:port] [-c config-file-path] [-e warp-ip] [-k license-key] [-country country-code] [-cfon] [-gool]")
flag.PrintDefaults()
}
func validateFlags(f *Flags) error {
if _, err := net.ResolveTCPAddr("tcp", f.BindAddress); err != nil {
return fmt.Errorf("invalid bindAddress format: %s", f.BindAddress)
}
if ip := net.ParseIP(f.Endpoint); ip == nil {
return fmt.Errorf("invalid warp clean IP: %s", f.Endpoint)
}
if f.PsiphonEnabled && f.Country == "" {
return fmt.Errorf("if Psiphon is enabled, country code must be provided")
}
if !validCountryCodes[f.Country] {
validCountries := make([]string, 0, len(validCountryCodes))
for code, _ := range validCountryCodes {
validCountries = append(validCountries, code)
}
return fmt.Errorf("invalid country code: %s. Valid country codes: $s", f.Country, validCountries)
}
return nil
}
func main() {
// Check for unexpected flags
for _, arg := range os.Args[1:] {
if !validFlags[arg] {
log.Fatalf("Invalid flag: %s", arg)
}
}
flags := newFlags()
flags.setup()
if err := validateFlags(flags); err != nil {
log.Fatalf("Validatrion error: %v", err)
}
sigchan := make(chan os.Signal)
signal.Notify(sigchan, os.Interrupt, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go func() {
err := app.RunWarp(flags.PsiphonEnabled, flags.Gool, flags.Scan, flags.Verbose, flags.Country, flags.BindAddress, flags.Endpoint, flags.License, ctx, flags.Rtt)
if err != nil {
log.Fatal(err)
}
}()
<-sigchan
cancel()
}