-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproxy.go
282 lines (258 loc) · 7.27 KB
/
proxy.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
package vproxy
import (
"context"
"crypto/tls"
"encoding/base64"
"fmt"
"io"
"log"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const defaultDataBufioSize = 1 << 20 // 默认数据缓冲1MB
type LogLevel int
const (
OriginAddr LogLevel = iota + 1 // 登录 vproxy 每个请求的目标。
Authenticate // 认证
Host // 访问的Host地址
URI // 路径
Request // 显示报头解析
Response // 日志写入到网络的所有数据
Error // 非致命错误
)
type Proxy struct {
// 这个支持单条连接。不要使用在浏览器中。
// 支持:
// http://192.168.2.31/http://www.baidu.com/
// http://192.168.2.31/?url=http://www.baidu.com/
LinkPosterior bool // 支持连接后面的,如:http://192.168.2.31/http://www.baidu.com/
DataBufioSize int // 缓冲区大小
Auth func(username, password string) bool // 认证
Addr string // 代理IP地址
Server http.Server // 服务器
DialContext func(ctx context.Context, network, address string) (net.Conn, error) // 拨号
ErrorLog *log.Logger // 日志
ErrorLogLevel LogLevel // 日志级别
l net.Listener // 连接对象
Tr http.RoundTripper // 代理
}
// initServer 初始化服务器
func (p *Proxy) initServer() *http.Server {
srv := &p.Server
if srv.Handler == nil {
srv.Handler = http.HandlerFunc(p.ServeHTTP)
}
return srv
}
// ServeHTTP 处理服务
//
// rw http.ResponseWriter 响应
// req *http.Request 请求
func (p *Proxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if p.Tr == nil {
p.Tr = http.DefaultTransport
}
p.logf(OriginAddr, "接入客户端IP: %s", req.RemoteAddr)
// 认证用户密码
if p.Auth != nil {
var (
username, password string
ok bool
)
auth := req.Header.Get("Proxy-Authorization")
if auth != "" {
// 标头中读取
username, password, ok = parseBasicAuth(auth)
} else if username, password, ok = req.BasicAuth(); !ok {
// 在query中读取
query := req.URL.Query()
auth = query.Get("@auth")
if auth != "" {
query.Del("@auth")
req.URL.RawQuery = query.Encode()
auths := strings.SplitN(auth, ":", 2)
if len(auths) != 2 {
http.Error(rw, "Connection parameters 'auth=user:pass' not set? user or pass exist ':' use %3A substitute!", http.StatusNotImplemented)
return
}
var err error
username, err = url.QueryUnescape(auths[0])
if err == nil {
password, err = url.QueryUnescape(auths[1])
ok = err == nil
}
} else {
http.Error(rw, "Proxy server link/requires authentication to log in!", http.StatusProxyAuthRequired)
return
}
}
p.logf(Authenticate, "认证用户:%s,密码:%s", username, password)
if !ok || !p.Auth(username, password) {
http.Error(rw, "User or password is not valid!", http.StatusProxyAuthRequired)
return
}
}
var rewriteHost bool
if p.LinkPosterior {
//http://www.baidu.com/ 错的
//http://www.baidu.com/a 对的
//?url=http://www.baidu.com/* 对的
var (
rawurl string
query = req.URL.Query()
)
if len(req.URL.Path) > 1 {
rawurl = req.URL.Path[1:]
} else {
rawurl = query.Get("@url")
query.Del("@url")
req.URL.RawQuery = query.Encode()
}
if strings.Index(rawurl, "//") == 0 || strings.Index(rawurl, "http://") == 0 || strings.Index(rawurl, "https://") == 0 {
lpurl, err := url.Parse(rawurl)
if err != nil {
p.logf(Host, "%s Host: %s", req.Method, req.Host)
p.logf(URI, "连接路径错误: %s", req.RequestURI)
http.Error(rw, "Connection path error!", http.StatusBadRequest)
return
}
rewriteHost = true
req.Host = lpurl.Host
req.URL.User = nil
req.URL.Host = lpurl.Host
req.URL.Path = lpurl.Path
if lpurl.Scheme != "" {
req.URL.Scheme = lpurl.Scheme
}
}
} else if req.URL.Host == "" {
p.logf(URI, "连接路径错误: %s", req.RequestURI)
http.Error(rw, "Connection path error!", http.StatusBadRequest)
return
}
if localAddr, ok := req.Context().Value(http.LocalAddrContextKey).(*net.TCPAddr); ok && !rewriteHost {
lhost := localAddr.IP.String()
rhost, _, _ := net.SplitHostPort(req.RemoteAddr)
lport := localAddr.Port
_, rport, _ := net.SplitHostPort(req.Host)
if rport == "" {
switch req.URL.Scheme {
case "http":
rport = "80"
case "https":
rport = "443"
}
}
// 同Ip,同端口。拒绝循环
if lhost == rhost && strconv.Itoa(lport) == rport {
http.Error(rw, "Connection loopback error!", http.StatusBadRequest)
return
}
}
p.logf(Host, "%s Host: %s", req.Method, req.Host)
p.logf(URI, "URI: %s", req.RequestURI)
p.logf(Request, "请求:\r\n%v", req)
// 请求
switch req.Method {
case "CONNECT":
cp := &proxyConnect{
Proxy: p,
}
cp.ServeHTTP(rw, req)
default:
hp := &proxyHTTP{
Proxy: p,
}
hp.ServeHTTP(rw, req)
}
// p.logf(OriginAddr, "断开客户端IP: %s", req.RemoteAddr)
}
// ListenAndServe 开启监听
//
// 返:
// error 错误
func (p *Proxy) ListenAndServe() error {
addr := p.Addr
if addr == "" {
addr = ":0"
}
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
return p.Serve(l)
}
// Serve 开启监听
//
// 参:
// l net.Listener 监听对象
// 返:
// error 错误
func (p *Proxy) Serve(l net.Listener) error {
srv := p.initServer()
p.l = l
p.Addr = l.Addr().String()
srv.Addr = p.Addr
if srv.TLSConfig != nil {
l = tls.NewListener(l, srv.TLSConfig)
}
return srv.Serve(l)
}
// Close 关闭
//
// 返:
// error 错误
func (p *Proxy) Close() error {
if tr, ok := p.Tr.(*http.Transport); ok {
tr.CloseIdleConnections()
}
if p.l != nil {
return p.l.Close()
}
return nil
}
func (p *Proxy) logf(level LogLevel, format string, v ...interface{}) error {
if p.ErrorLog != nil && p.ErrorLogLevel >= level {
err := fmt.Errorf(format+"\n", v...)
p.ErrorLog.Output(2, err.Error())
return err
}
return nil
}
func copyDate(dst io.Writer, src io.ReadCloser, bufSize int) (n int64, err error) {
defer src.Close()
buf := make([]byte, bufSize)
return io.CopyBuffer(dst, src, buf)
}
func parseBasicAuth(auth string) (username, password string, ok bool) {
const prefix = "Basic "
if !strings.HasPrefix(auth, prefix) {
return
}
c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}
return cs[:s], cs[s+1:], true
}
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := ln.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(3 * time.Minute)
return tc, nil
}