/
main.go
416 lines (369 loc) · 11.3 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
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
package main
import (
"context"
"crypto/x509"
"encoding/csv"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net"
"net/http"
"net/url"
"os"
"sort"
"strconv"
"time"
xproxy "golang.org/x/net/proxy"
"github.com/Snawoot/windscribe-proxy/wndclient"
)
const (
DEFAULT_CLIENT_AUTH_SECRET = "952b4412f002315aa50751032fcaab03"
ASSUMED_PROXY_PORT uint16 = 443
)
var (
version = "undefined"
)
func perror(msg string) {
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, msg)
}
func arg_fail(msg string) {
perror(msg)
perror("Usage:")
flag.PrintDefaults()
os.Exit(2)
}
type CLIArgs struct {
location string
listLocations bool
listProxies bool
bindAddress string
verbosity int
timeout time.Duration
showVersion bool
proxy string
resolver string
caFile string
clientAuthSecret string
stateFile string
username string
password string
tfacode string
fakeSNI string
forceColdInit bool
}
func parse_args() CLIArgs {
var args CLIArgs
flag.StringVar(&args.location, "location", "", "desired proxy location. Default: best location")
flag.BoolVar(&args.listLocations, "list-locations", false, "list available locations and exit")
flag.BoolVar(&args.listProxies, "list-proxies", false, "output proxy list and exit")
flag.StringVar(&args.bindAddress, "bind-address", "127.0.0.1:28080", "HTTP proxy listen address")
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
flag.DurationVar(&args.timeout, "timeout", 10*time.Second, "timeout for network operations")
flag.BoolVar(&args.showVersion, "version", false, "show program version and exit")
flag.StringVar(&args.proxy, "proxy", "", "sets base proxy to use for all dial-outs. "+
"Format: <http|https|socks5|socks5h>://[login:password@]host[:port] "+
"Examples: http://user:password@192.168.1.1:3128, socks5://10.0.0.1:1080")
// TODO: implement DNS resolving or remove it
flag.StringVar(&args.resolver, "resolver", "",
"Use DNS/DoH/DoT/DoQ resolver for all dial-outs. "+
"See https://github.com/ameshkov/dnslookup/ for upstream DNS URL format. "+
"Examples: https://1.1.1.1/dns-query, quic://dns.adguard.com")
flag.StringVar(&args.caFile, "cafile", "", "use custom CA certificate bundle file")
flag.StringVar(&args.clientAuthSecret, "auth-secret", DEFAULT_CLIENT_AUTH_SECRET, "client auth secret")
flag.StringVar(&args.stateFile, "state-file", "wndstate.json", "file name used to persist "+
"Windscribe API client state")
flag.StringVar(&args.username, "username", "", "username for login")
flag.StringVar(&args.password, "password", "", "password for login")
flag.StringVar(&args.tfacode, "2fa", "", "2FA code for login")
flag.StringVar(&args.fakeSNI, "fake-sni", "com", "fake SNI to use to contact windscribe servers")
flag.BoolVar(&args.forceColdInit, "force-cold-init", false, "force cold init")
flag.Parse()
if args.listLocations && args.listProxies {
arg_fail("list-locations and list-proxies flags are mutually exclusive")
}
return args
}
func proxyFromURLWrapper(u *url.URL, next xproxy.Dialer) (xproxy.Dialer, error) {
cdialer, ok := next.(ContextDialer)
if !ok {
return nil, errors.New("only context dialers are accepted")
}
return ProxyDialerFromURL(u, cdialer)
}
func run() int {
var err error
args := parse_args()
if args.showVersion {
fmt.Println(version)
return 0
}
logWriter := NewLogWriter(os.Stderr)
defer logWriter.Close()
mainLogger := NewCondLogger(log.New(logWriter, "MAIN : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
proxyLogger := NewCondLogger(log.New(logWriter, "PROXY : ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
resolverLogger := NewCondLogger(log.New(logWriter, "RESOLVER: ",
log.LstdFlags|log.Lshortfile),
args.verbosity)
mainLogger.Info("windscribe-proxy client version %s is starting...", version)
var dialer ContextDialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
var caPool *x509.CertPool
if args.caFile != "" {
caPool = x509.NewCertPool()
certs, err := ioutil.ReadFile(args.caFile)
if err != nil {
mainLogger.Error("Can't load CA file: %v", err)
return 15
}
if ok := caPool.AppendCertsFromPEM(certs); !ok {
mainLogger.Error("Can't load certificates from CA file")
return 15
}
}
if args.proxy != "" {
xproxy.RegisterDialerType("http", proxyFromURLWrapper)
xproxy.RegisterDialerType("https", proxyFromURLWrapper)
proxyURL, err := url.Parse(args.proxy)
if err != nil {
mainLogger.Critical("Unable to parse base proxy URL: %v", err)
return 6
}
pxDialer, err := xproxy.FromURL(proxyURL, dialer)
if err != nil {
mainLogger.Critical("Unable to instantiate base proxy dialer: %v", err)
return 7
}
dialer = pxDialer.(ContextDialer)
}
if args.resolver != "" {
dialer, err = NewResolvingDialer(args.resolver, args.timeout, dialer, resolverLogger)
if err != nil {
mainLogger.Critical("Unable to instantiate resolver: %v", err)
return 5
}
}
wndclientDialer := dialer
wndc, err := wndclient.NewWndClient(&http.Transport{
DialContext: wndclientDialer.DialContext,
DialTLSContext: NewFakeSNIDialer(caPool, args.fakeSNI, wndclientDialer).DialTLSContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
})
if err != nil {
mainLogger.Critical("Unable to construct WndClient: %v", err)
return 8
}
wndc.Mux.Lock()
wndc.State.Settings.ClientAuthSecret = args.clientAuthSecret
wndc.Mux.Unlock()
// Try to resurrect state
state, err := maybeLoadState(args.forceColdInit, args.stateFile)
if err != nil {
switch err {
case errColdInitForced:
mainLogger.Info("Cold init forced.")
default:
mainLogger.Warning("Failed to load client state: %v. It is OK for a first run. Performing cold init...", err)
}
err = coldInit(wndc, args.username, args.password, args.tfacode, args.timeout)
if err != nil {
mainLogger.Critical("Cold init failed: %v", err)
return 9
}
err = saveState(args.stateFile, &wndc.State)
if err != nil {
mainLogger.Error("Unable to save state file! Error: %v", err)
}
} else {
wndc.Mux.Lock()
wndc.State = *state
wndc.Mux.Unlock()
}
var serverList wndclient.ServerList
if args.listProxies || args.listLocations || args.location != "" {
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
serverList, err = wndc.ServerList(ctx)
cl()
if err != nil {
mainLogger.Critical("Server list retrieve failed: %v", err)
return 12
}
}
if args.listProxies {
username, password := wndc.GetProxyCredentials()
return printProxies(username, password, serverList)
}
if args.listLocations {
return printLocations(serverList)
}
var proxyHostname string
if args.location == "" {
ctx, cl := context.WithTimeout(context.Background(), args.timeout)
bestLocation, err := wndc.BestLocation(ctx)
cl()
if err != nil {
mainLogger.Critical("Unable to get best location endpoint: %v", err)
return 13
}
proxyHostname = bestLocation.Hostname
} else {
proxyHostname = pickServer(serverList, args.location)
if proxyHostname == "" {
mainLogger.Critical("Server for location \"%s\" not found.", args.location)
return 13
}
}
auth := func() string {
return basic_auth_header(wndc.GetProxyCredentials())
}
proxyNetAddr := net.JoinHostPort(proxyHostname, strconv.FormatUint(uint64(ASSUMED_PROXY_PORT), 10))
handlerDialer := NewProxyDialer(proxyNetAddr, proxyHostname, args.fakeSNI, auth, caPool, dialer)
mainLogger.Info("Endpoint: %s", proxyNetAddr)
mainLogger.Info("Starting proxy server...")
handler := NewProxyHandler(handlerDialer, proxyLogger)
mainLogger.Info("Init complete.")
err = http.ListenAndServe(args.bindAddress, handler)
mainLogger.Critical("Server terminated with a reason: %v", err)
mainLogger.Info("Shutting down...")
return 0
}
type locationPair struct {
country string
city string
}
func printLocations(serverList wndclient.ServerList) int {
var locs []locationPair
for _, country := range serverList {
for _, group := range country.Groups {
if len(group.Hosts) > 1 {
locs = append(locs, locationPair{country.Name, group.City})
}
}
}
if len(locs) == 0 {
return 0
}
sort.Slice(locs, func(i, j int) bool {
if locs[i].country < locs[j].country {
return true
}
if locs[i].country == locs[j].country && locs[i].city < locs[j].city {
return true
}
return false
})
var prevLoc locationPair
for _, loc := range locs {
if loc != prevLoc {
fmt.Println(loc.country + "/" + loc.city)
prevLoc = loc
}
}
return 0
}
func printProxies(username, password string, serverList wndclient.ServerList) int {
wr := csv.NewWriter(os.Stdout)
defer wr.Flush()
fmt.Println("Proxy login:", username)
fmt.Println("Proxy password:", password)
fmt.Println("Proxy-Authorization:", basic_auth_header(username, password))
fmt.Println("")
wr.Write([]string{"location", "hostname", "port"})
for _, country := range serverList {
for _, group := range country.Groups {
for _, host := range group.Hosts {
wr.Write([]string{
country.Name + "/" + group.City,
host.Hostname,
strconv.FormatUint(uint64(ASSUMED_PROXY_PORT), 10),
})
}
}
}
return 0
}
func pickServer(serverList wndclient.ServerList, location string) string {
var candidates []string
for _, country := range serverList {
for _, group := range country.Groups {
for _, host := range group.Hosts {
if country.Name+"/"+group.City == location {
candidates = append(candidates, host.Hostname)
}
}
}
}
if len(candidates) == 0 {
return ""
}
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
return candidates[rnd.Intn(len(candidates))]
}
var errColdInitForced = errors.New("cold init forced!")
func maybeLoadState(forceColdInit bool, filename string) (*wndclient.WndClientState, error) {
if forceColdInit {
return nil, errColdInitForced
}
return loadState(filename)
}
func loadState(filename string) (*wndclient.WndClientState, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
var state wndclient.WndClientState
dec := json.NewDecoder(file)
err = dec.Decode(&state)
if err != nil {
return nil, err
}
return &state, nil
}
func saveState(filename string, state *wndclient.WndClientState) error {
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
return err
}
defer file.Close()
enc := json.NewEncoder(file)
enc.SetIndent("", " ")
err = enc.Encode(state)
return err
}
func coldInit(wndc *wndclient.WndClient, username, password, tfacode string, timeout time.Duration) error {
if username == "" || password == "" {
return errors.New(`Please provide "-username" and "-password" command line arguments!`)
}
ctx, cl := context.WithTimeout(context.Background(), timeout)
err := wndc.Session(ctx, username, password, tfacode)
cl()
if err != nil {
return fmt.Errorf("Session call failed: %w", err)
}
ctx, cl = context.WithTimeout(context.Background(), timeout)
err = wndc.ServerCredentials(ctx)
cl()
if err != nil {
return fmt.Errorf("ServerCredentials call failed: %w", err)
}
return nil
}
func main() {
os.Exit(run())
}