-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
102 lines (99 loc) · 2.07 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
package main
import (
"gproxy/auth"
"gproxy/proxy"
"log"
"os"
"strconv"
)
func main() {
// 解析命令行参数(gproxy [-p port] [-m mode][--proxy proxypass][--anonymous][--realip realip][--useragent useragent] domain)
args := os.Args[1:]
domain := ""
var err error
port := 80
mode := "http"
cert := ""
key := ""
for i := 0; i < len(args); {
arg := args[i]
if arg == "-p" && len(args) > i+1 {
port, err = strconv.Atoi(args[i+1])
if err != nil {
log.Fatalf("error occured: %v", err)
}
i += 2
continue
}
if arg == "-m" && len(args) > i+1 {
mode = args[i+1]
i += 2
continue
}
if arg == "--key" && len(args) > i+1 {
key = args[i+1]
i += 2
continue
}
if arg == "--cert" && len(args) > i+1 {
cert = args[i+1]
i += 2
continue
}
if arg == "--anonymous" {
proxy.Anonymous = true
i++
continue
}
if arg == "--realip" && len(args) > i+1 {
proxy.RealIP = args[i+1]
i += 2
continue
}
if arg == "--useragent" && len(args) > i+1 {
proxy.UserAgent = args[i+1]
i += 2
continue
}
if arg == "--token" && len(args) > i+1 {
auth.Token = args[i+1]
i += 2
continue
}
if arg == "--proxy" && len(args) > i+1 {
proxy.ProxyPass = args[i+1]
i += 2
continue
}
domain = arg
i++ // 防止死循环
}
// 检验逻辑
if domain == "" {
log.Fatal("domain is required")
}
// 仅http/https/tcp支持代理
if (mode != "http" && mode != "https" && mode != "tcp") && proxy.ProxyPass != "" {
log.Fatalf("proxy is only supported for http/https")
return
}
// 仅http/https/ws/wss支持token
if (mode != "http" && mode != "https" && mode != "ws" && mode != "wss") && auth.Token != "" {
log.Fatalf("token is not supported for %s", mode)
return
}
switch mode {
case "http":
// 启动HTTP代理服务
proxy.NewHttpProxy(domain, port)
case "https":
// 启动HTTPS代理服务
proxy.NewHttpsProxy(domain, port, cert, key)
case "tcp":
proxy.NewTcpProxy(domain, port)
case "ws":
proxy.NewWSProxy(domain, port)
case "wss":
proxy.NewWSSProxy(domain, port, cert, key)
}
}