forked from bepass-org/warp-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
109 lines (90 loc) · 2.57 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
package main
import (
"context"
"flag"
"github.com/uoosef/wireguard-go/device"
"github.com/uoosef/wireguard-go/psiphon"
"github.com/uoosef/wireguard-go/warp"
"github.com/uoosef/wireguard-go/wiresocks"
"log"
"net"
"os"
"os/signal"
"syscall"
)
func usage() {
log.Println("Usage: wiresocks [-v] [-b addr:port] [-l license] <config file path>")
flag.PrintDefaults()
}
func main() {
var (
verbose = flag.Bool("v", false, "verbose")
bindAddress = flag.String("b", "127.0.0.1:8086", "socks bind address")
configFile = flag.String("c", "./wgcf-profile.ini", "ini config file path")
endpoint = flag.String("e", "notset", "warp clean ip")
license = flag.String("k", "notset", "license key")
country = flag.String("country", "", "psiphon country code in ISO 3166-1 alpha-2 format")
psiphonEnabled = flag.Bool("cfon", false, "enable psiphonEnabled over warp")
pbind = "127.0.0.1:8086"
psiphonCtx context.Context
)
flag.Usage = usage
flag.Parse()
wiresocks.Verbose = *verbose
if *psiphonEnabled {
pbind = *bindAddress
randomBind, err := findFreePort()
if err != nil {
log.Fatal("unable to find a free port :/")
}
bindAddress = &randomBind
}
// check if wgcf-profile.conf exists
if !warp.CheckProfileExists(*license) {
if *license == "notset" {
*license = ""
}
err := warp.LoadOrCreateIdentity(*license)
if err != nil {
log.Fatalf("error: %v", err)
}
}
conf, err := wiresocks.ParseConfig(*configFile, *endpoint)
if err != nil {
log.Fatal(err)
}
logLevel := device.LogLevelVerbose
if !*verbose {
logLevel = device.LogLevelSilent
}
// Setup channel to listen for interrupt signal (Ctrl+C)
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
tnet, err := wiresocks.StartWireguard(conf.Device, logLevel)
if err != nil {
log.Fatal(err)
}
go wiresocks.StartProxy(tnet, *bindAddress)
if *psiphonEnabled {
psiphonCtx = psiphon.RunPsiphon(*bindAddress, pbind, *country)
} else {
log.Println("Wiresocks started successfully")
}
// Wait for interrupt signal
<-sigChan
if *psiphonEnabled {
psiphonCtx.Done()
}
log.Println("Bye!")
}
func findFreePort() (string, error) {
// Listen on TCP port 0, which tells the OS to pick a free port.
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
return "", err // Return error if unable to listen on a port
}
defer listener.Close() // Ensure the listener is closed when the function returns
// Get the port from the listener's address
addr := listener.Addr().String()
return addr, nil
}