Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
snail007
GitHub Repository: snail007/goproxy
Path: blob/master/config.go
684 views
1
package main
2
3
import (
4
"fmt"
5
"io/ioutil"
6
"log"
7
"os"
8
"github.com/snail007/goproxy/services"
9
"github.com/snail007/goproxy/utils"
10
11
kingpin "gopkg.in/alecthomas/kingpin.v2"
12
)
13
14
var (
15
app *kingpin.Application
16
service *services.ServiceItem
17
)
18
19
func initConfig() (err error) {
20
//keygen
21
if len(os.Args) > 1 {
22
if os.Args[1] == "keygen" {
23
utils.Keygen()
24
os.Exit(0)
25
}
26
}
27
args := services.Args{}
28
//define args
29
tcpArgs := services.TCPArgs{}
30
httpArgs := services.HTTPArgs{}
31
tunnelServerArgs := services.TunnelServerArgs{}
32
tunnelClientArgs := services.TunnelClientArgs{}
33
tunnelBridgeArgs := services.TunnelBridgeArgs{}
34
udpArgs := services.UDPArgs{}
35
36
//build srvice args
37
app = kingpin.New("proxy", "happy with proxy")
38
app.Author("snail").Version(APP_VERSION)
39
args.Parent = app.Flag("parent", "parent address, such as: \"23.32.32.19:28008\"").Default("").Short('P').String()
40
args.Local = app.Flag("local", "local ip:port to listen").Short('p').Default(":33080").String()
41
certTLS := app.Flag("cert", "cert file for tls").Short('C').Default("proxy.crt").String()
42
keyTLS := app.Flag("key", "key file for tls").Short('K').Default("proxy.key").String()
43
44
//########http#########
45
http := app.Command("http", "proxy on http mode")
46
httpArgs.LocalType = http.Flag("local-type", "parent protocol type <tls|tcp>").Default("tcp").Short('t').Enum("tls", "tcp")
47
httpArgs.ParentType = http.Flag("parent-type", "parent protocol type <tls|tcp>").Short('T').Enum("tls", "tcp")
48
httpArgs.Always = http.Flag("always", "always use parent proxy").Default("false").Bool()
49
httpArgs.Timeout = http.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Default("2000").Int()
50
httpArgs.HTTPTimeout = http.Flag("http-timeout", "check domain if blocked , http request timeout milliseconds when connect to host").Default("3000").Int()
51
httpArgs.Interval = http.Flag("interval", "check domain if blocked every interval seconds").Default("10").Int()
52
httpArgs.Blocked = http.Flag("blocked", "blocked domain file , one domain each line").Default("blocked").Short('b').String()
53
httpArgs.Direct = http.Flag("direct", "direct domain file , one domain each line").Default("direct").Short('d').String()
54
httpArgs.AuthFile = http.Flag("auth-file", "http basic auth file,\"username:password\" each line in file").Short('F').String()
55
httpArgs.Auth = http.Flag("auth", "http basic auth username and password, mutiple user repeat -a ,such as: -a user1:pass1 -a user2:pass2").Short('a').Strings()
56
httpArgs.PoolSize = http.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
57
httpArgs.CheckParentInterval = http.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
58
59
//########tcp#########
60
tcp := app.Command("tcp", "proxy on tcp mode")
61
tcpArgs.Timeout = tcp.Flag("timeout", "tcp timeout milliseconds when connect to real server or parent proxy").Short('t').Default("2000").Int()
62
tcpArgs.ParentType = tcp.Flag("parent-type", "parent protocol type <tls|tcp|udp>").Short('T').Enum("tls", "tcp", "udp")
63
tcpArgs.IsTLS = tcp.Flag("tls", "proxy on tls mode").Default("false").Bool()
64
tcpArgs.PoolSize = tcp.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
65
tcpArgs.CheckParentInterval = tcp.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
66
67
//########udp#########
68
udp := app.Command("udp", "proxy on udp mode")
69
udpArgs.Timeout = udp.Flag("timeout", "tcp timeout milliseconds when connect to parent proxy").Short('t').Default("2000").Int()
70
udpArgs.ParentType = udp.Flag("parent-type", "parent protocol type <tls|tcp|udp>").Short('T').Enum("tls", "tcp", "udp")
71
udpArgs.PoolSize = udp.Flag("pool-size", "conn pool size , which connect to parent proxy, zero: means turn off pool").Short('L').Default("20").Int()
72
udpArgs.CheckParentInterval = udp.Flag("check-parent-interval", "check if proxy is okay every interval seconds,zero: means no check").Short('I').Default("3").Int()
73
74
//########tunnel-server#########
75
tunnelServer := app.Command("tserver", "proxy on tunnel server mode")
76
tunnelServerArgs.Timeout = tunnelServer.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
77
tunnelServerArgs.IsUDP = tunnelServer.Flag("udp", "proxy on udp tunnel server mode").Default("false").Bool()
78
tunnelServerArgs.Key = tunnelServer.Flag("k", "key same with client").Default("default").String()
79
80
//########tunnel-client#########
81
tunnelClient := app.Command("tclient", "proxy on tunnel client mode")
82
tunnelClientArgs.Timeout = tunnelClient.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
83
tunnelClientArgs.IsUDP = tunnelClient.Flag("udp", "proxy on udp tunnel client mode").Default("false").Bool()
84
tunnelClientArgs.Key = tunnelClient.Flag("k", "key same with server").Default("default").String()
85
86
//########tunnel-bridge#########
87
tunnelBridge := app.Command("tbridge", "proxy on tunnel bridge mode")
88
tunnelBridgeArgs.Timeout = tunnelBridge.Flag("timeout", "tcp timeout with milliseconds").Short('t').Default("2000").Int()
89
90
kingpin.MustParse(app.Parse(os.Args[1:]))
91
92
if *certTLS != "" && *keyTLS != "" {
93
args.CertBytes, args.KeyBytes = tlsBytes(*certTLS, *keyTLS)
94
}
95
96
//common args
97
httpArgs.Args = args
98
tcpArgs.Args = args
99
udpArgs.Args = args
100
tunnelBridgeArgs.Args = args
101
tunnelClientArgs.Args = args
102
tunnelServerArgs.Args = args
103
104
poster()
105
//regist services and run service
106
serviceName := kingpin.MustParse(app.Parse(os.Args[1:]))
107
services.Regist("http", services.NewHTTP(), httpArgs)
108
services.Regist("tcp", services.NewTCP(), tcpArgs)
109
services.Regist("udp", services.NewUDP(), udpArgs)
110
services.Regist("tserver", services.NewTunnelServer(), tunnelServerArgs)
111
services.Regist("tclient", services.NewTunnelClient(), tunnelClientArgs)
112
services.Regist("tbridge", services.NewTunnelBridge(), tunnelBridgeArgs)
113
service, err = services.Run(serviceName)
114
if err != nil {
115
log.Fatalf("run service [%s] fail, ERR:%s", service, err)
116
}
117
return
118
}
119
120
func poster() {
121
fmt.Printf(`
122
######## ######## ####### ## ## ## ##
123
## ## ## ## ## ## ## ## ## ##
124
## ## ## ## ## ## ## ## ####
125
######## ######## ## ## ### ##
126
## ## ## ## ## ## ## ##
127
## ## ## ## ## ## ## ##
128
## ## ## ####### ## ## ##
129
130
v%s`+" by snail , blog : http://www.host900.com/\n\n", APP_VERSION)
131
}
132
func tlsBytes(cert, key string) (certBytes, keyBytes []byte) {
133
certBytes, err := ioutil.ReadFile(cert)
134
if err != nil {
135
log.Fatalf("err : %s", err)
136
return
137
}
138
keyBytes, err = ioutil.ReadFile(key)
139
if err != nil {
140
log.Fatalf("err : %s", err)
141
return
142
}
143
return
144
}
145
146