-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
276 lines (253 loc) · 7.54 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
package main
import (
"bytes"
"flag"
"io"
"io/ioutil"
"log"
"net"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/valyala/fasthttp"
"h12.io/socks"
)
var httpClientTimeout = 15 * time.Second
var dialTimeout = 7 * time.Second
var socksDialFunc func(string, string) (net.Conn, error)
var localDialFunc = (&net.Dialer{
Timeout: dialTimeout,
DualStack: true,
}).Dial
var httpClientLocal = &fasthttp.Client{
ReadTimeout: 30 * time.Second,
MaxConnsPerHost: 233,
MaxIdleConnDuration: 15 * time.Minute,
ReadBufferSize: 1024 * 8,
Dial: func(addr string) (net.Conn, error) {
// no suitable address found => ipv6 can not dial to ipv4,..
hostname, port, err := net.SplitHostPort(addr)
if err != nil {
if err1, ok := err.(*net.AddrError); ok && strings.Contains(err1.Err, "missing port") {
hostname, port, err = net.SplitHostPort(strings.TrimRight(addr, ":") + ":80")
}
if err != nil {
return nil, err
}
}
if port == "" || port == ":" {
port = "80"
}
return localDialFunc("tcp", "["+hostname+"]:"+port)
},
}
var httpClientSocks = &fasthttp.Client{
ReadTimeout: 30 * time.Second,
MaxConnsPerHost: 233,
MaxIdleConnDuration: 15 * time.Minute,
ReadBufferSize: 1024 * 8,
Dial: func(addr string) (net.Conn, error) {
// no suitable address found => ipv6 can not dial to ipv4,..
hostname, port, err := net.SplitHostPort(addr)
if err != nil {
if err1, ok := err.(*net.AddrError); ok && strings.Contains(err1.Err, "missing port") {
hostname, port, err = net.SplitHostPort(strings.TrimRight(addr, ":") + ":80")
}
if err != nil {
return nil, err
}
}
if port == "" || port == ":" {
port = "80"
}
return socksDialFunc("tcp", "["+hostname+"]:"+port)
},
}
func httpsHandler(ctx *fasthttp.RequestCtx, remoteAddr string, isMustProxify bool) error {
var r net.Conn
if isMustProxify {
var err error
r, err = socksDialFunc("tcp", remoteAddr)
if err != nil {
return err
}
} else {
var err error
r, err = localDialFunc("tcp", remoteAddr)
if err != nil {
return err
}
}
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.Response.Header.Set("Connection", "keep-alive")
ctx.Response.Header.Set("Keep-Alive", "timeout=120, max=5")
ctx.Hijack(func(clientConn net.Conn) {
defer clientConn.Close()
defer r.Close()
go io.Copy(r, clientConn)
io.Copy(clientConn, r)
})
return nil
}
func requestHandler(ctx *fasthttp.RequestCtx) {
// Some library must set header: Connection: keep-alive
// ctx.Response.Header.Del("Connection")
// ctx.Response.ConnectionClose() // ==> false
// log.Println(string(ctx.Path()), string(ctx.Host()), ctx.String(), "\r\n\r\n", ctx.Request.String())
host := string(ctx.Host())
if len(host) < 1 {
host = string(ctx.Path())[1:]
}
if len(host) < 1 {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
log.Println("Reject: Empty host")
return
}
hostname, port, err := net.SplitHostPort(host)
if err != nil {
if err1, ok := err.(*net.AddrError); ok && strings.Contains(err1.Err, "missing port") {
hostname, port, err = net.SplitHostPort(host + ":443")
}
if err != nil {
ctx.SetStatusCode(fasthttp.StatusBadRequest)
log.Println("Reject: Invalid host", host, err)
return
}
}
isMustProxify := mustProxify(hostname)
// https connecttion
if bytes.Equal(ctx.Method(), []byte("CONNECT")) {
err = httpsHandler(ctx, `[`+hostname+`]:`+port, isMustProxify)
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
log.Println("httpsHandler:", host, err)
}
return
}
if isMustProxify {
err = httpClientSocks.DoTimeout(&ctx.Request, &ctx.Response, httpClientTimeout)
} else {
err = httpClientLocal.DoTimeout(&ctx.Request, &ctx.Response, httpClientTimeout)
}
if err != nil {
ctx.SetStatusCode(fasthttp.StatusInternalServerError)
log.Println("httpHandler:", host, err)
}
}
// Domains
var domainList = flag.String("d", "domains.txt", "Domains List File")
var domainRegexList = flag.String("r", "domains-regex.txt", "Domains Regex List File")
var domainProxiesCache = map[string]bool{}
var domainProxiesCacheLock sync.RWMutex
var domainsRegex []*regexp.Regexp
var lineRegex = regexp.MustCompile(`[\r\n]+`)
func parseDomains() bool {
if len(*domainList) > 0 {
c, err := ioutil.ReadFile(*domainList)
if err == nil {
lines := lineRegex.Split(string(c), -1)
for _, line := range lines {
line = strings.Trim(line, "\r\n\t ")
if len(line) < 1 || line[0] == '#' {
continue
}
domainProxiesCacheLock.Lock()
domainProxiesCache[line] = true
domainProxiesCacheLock.Unlock()
}
} else {
log.Println(err)
}
}
if len(*domainRegexList) > 0 {
c, err := ioutil.ReadFile(*domainRegexList)
if err == nil {
lines := lineRegex.Split(string(c), -1)
for _, line := range lines {
line = strings.Trim(line, "\r\n\t ")
if len(line) < 1 || line[0] == '#' {
continue
}
domainsRegex = append(domainsRegex, regexp.MustCompile(line))
}
} else {
log.Println(err)
}
}
if len(domainsRegex) < 1 && len(domainProxiesCache) < 1 {
log.Println("No domains to proxy? Please specify a domain name in", *domainList, "or", *domainRegexList)
return false
}
return true
}
// OK, no lock need here
func mustProxify(hostname string) bool {
domainProxiesCacheLock.RLock()
b, ok := domainProxiesCache[hostname]
domainProxiesCacheLock.RUnlock()
if ok {
return b
}
b = false
for _, re := range domainsRegex {
b = re.MatchString(hostname)
if b {
break
}
}
domainProxiesCacheLock.Lock()
domainProxiesCache[hostname] = b
domainProxiesCacheLock.Unlock()
log.Println("Proxify:", hostname, b)
return b
}
var listen = flag.String(`l`, `:8081`, `Listen address. Eg: :8443; unix:/tmp/proxy.sock`)
var socksURI = flag.String(`x`, `socks5://127.0.0.1:1080?timeout=5m`, `Socks proxy URI`)
func main() {
flag.Parse()
socksDialFunc = socks.Dial(*socksURI)
if !parseDomains() {
return
}
// Server
var err error
var ln net.Listener
if strings.HasPrefix(*listen, `unix:`) {
unixFile := (*listen)[5:]
os.Remove(unixFile)
ln, err = net.Listen(`unix`, unixFile)
os.Chmod(unixFile, os.ModePerm)
log.Println(`Listening:`, unixFile)
} else {
ln, err = net.Listen(`tcp`, *listen)
log.Println(`Listening:`, ln.Addr().String())
}
if err != nil {
log.Panicln(err)
}
srv := &fasthttp.Server{
// ErrorHandler: nil,
Handler: requestHandler,
NoDefaultServerHeader: true, // Don't send Server: fasthttp
// Name: "nginx", // Send Server header
ReadBufferSize: 2 * 4096, // Make sure these are big enough.
WriteBufferSize: 4096,
ReadTimeout: 5 * time.Second,
WriteTimeout: time.Second,
IdleTimeout: time.Minute, // This can be long for keep-alive connections.
DisableHeaderNamesNormalizing: false, // If you're not going to look at headers or know the casing you can set this.
// NoDefaultContentType: true, // Don't send Content-Type: text/plain if no Content-Type is set manually.
MaxRequestBodySize: 200 * 1024 * 1024, // 200MB
DisableKeepalive: false,
KeepHijackedConns: false,
// NoDefaultDate: len(*staticDir) == 0,
ReduceMemoryUsage: true,
TCPKeepalive: true,
// TCPKeepalivePeriod: 10 * time.Second,
// MaxRequestsPerConn: 1000,
// MaxConnsPerIP: 20,
}
log.Panicln(srv.Serve(ln))
}