forked from txthinking/brook
/
white.go
119 lines (108 loc) · 2.71 KB
/
white.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
package middleman
import (
"io"
"net"
"time"
"github.com/txthinking/ant"
"github.com/txthinking/pac/blackwhite"
"github.com/txthinking/socks5"
)
var Dial ant.Dialer = ant.DefaultDial
// WhiteSocks5 is a Socks5Middleman who only handles domain in white list
type WhiteSocks5 struct {
Handle *socks5.DefaultHandle
}
// NewWhiteSocks5 returns a WhiteSocks5 which can be used to handle http proxy request
func NewWhiteSocks5() *WhiteSocks5 {
return &WhiteSocks5{
Handle: &socks5.DefaultHandle{},
}
}
// TCPHandle handles tcp request
func (w *WhiteSocks5) TCPHandle(s *socks5.Server, c *net.TCPConn, r *socks5.Request) (bool, error) {
if r.Cmd == socks5.CmdConnect {
h, _, err := net.SplitHostPort(r.Address())
if err != nil {
return false, err
}
if !blackwhite.IsWhite(h) {
return false, nil
}
if err := w.Handle.TCPHandle(s, c, r); err != nil {
return true, err
}
return true, nil
}
if r.Cmd == socks5.CmdUDP {
return false, nil
}
return false, socks5.ErrUnsupportCmd
}
// UDPHandle handles udp packet
func (w *WhiteSocks5) UDPHandle(s *socks5.Server, ca *net.UDPAddr, d *socks5.Datagram) (bool, error) {
h, _, err := net.SplitHostPort(d.Address())
if err != nil {
return false, err
}
if !blackwhite.IsWhite(h) {
return false, nil
}
if err := w.Handle.UDPHandle(s, ca, d); err != nil {
return true, err
}
return true, nil
}
// WhiteHTTP is a HTTPMiddleman who only handles domain in white list
type WhiteHTTP struct {
Timeout int
Deadline int
}
// NewWhiteHTTP returns a WhiteHTTP which can used to handle http proxy request
func NewWhiteHTTP(timeout, deadline int) *WhiteHTTP {
return &WhiteHTTP{
Timeout: timeout,
Deadline: deadline,
}
}
// Handle handles http proxy request, if the domain is in the white list
func (w *WhiteHTTP) Handle(method, addr string, request []byte, conn *net.TCPConn) (handled bool, err error) {
h, _, err := net.SplitHostPort(addr)
if err != nil {
return false, err
}
if !blackwhite.IsWhite(h) {
return false, nil
}
tmp, err := Dial.Dial("tcp", addr)
if err != nil {
return true, err
}
rc := tmp.(*net.TCPConn)
defer rc.Close()
if w.Timeout != 0 {
if err := rc.SetKeepAlivePeriod(time.Duration(w.Timeout) * time.Second); err != nil {
return true, err
}
}
if w.Deadline != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(w.Deadline) * time.Second)); err != nil {
return true, err
}
}
if method == "CONNECT" {
_, err := conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil {
return true, err
}
}
if method != "CONNECT" {
if _, err := rc.Write(request); err != nil {
return true, err
}
}
go func() {
_, _ = io.Copy(rc, conn)
}()
_, _ = io.Copy(conn, rc)
return true, nil
}