forked from txthinking/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socks5tohttp.go
165 lines (154 loc) · 3.26 KB
/
socks5tohttp.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
package brook
import (
"bytes"
"errors"
"io"
"log"
"net"
"time"
"github.com/txthinking/ant"
"github.com/txthinking/brook/plugin"
"golang.org/x/net/proxy"
)
type Socks5ToHTTP struct {
Addr *net.TCPAddr
Socks5Address string
Dial proxy.Dialer
Timeout int
Deadline int // Not refreshed
Listen *net.TCPListener
HTTPMiddleman plugin.HTTPMiddleman
}
func NewSocks5ToHTTP(addr, socks5addr string, timeout, deadline int) (*Socks5ToHTTP, error) {
dial, err := proxy.SOCKS5("tcp", socks5addr, nil, &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
})
if err != nil {
return nil, err
}
ta, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
return &Socks5ToHTTP{
Addr: ta,
Socks5Address: socks5addr,
Dial: dial,
Timeout: timeout,
Deadline: deadline,
}, nil
}
// SetHTTPMiddleman sets httpmiddleman plugin
func (s *Socks5ToHTTP) SetHTTPMiddleman(m plugin.HTTPMiddleman) {
s.HTTPMiddleman = m
}
func (s *Socks5ToHTTP) ListenAndServe() error {
l, err := net.ListenTCP("tcp", s.Addr)
if err != nil {
return err
}
defer l.Close()
s.Listen = l
for {
c, err := l.AcceptTCP()
if err != nil {
return err
}
go func(c *net.TCPConn) {
defer c.Close()
if s.Timeout != 0 {
if err := c.SetKeepAlivePeriod(time.Duration(s.Timeout) * time.Second); err != nil {
log.Println(err)
return
}
}
if s.Deadline != 0 {
if err := c.SetDeadline(time.Now().Add(time.Duration(s.Deadline) * time.Second)); err != nil {
log.Println(err)
return
}
}
if err := s.Handle(c); err != nil {
log.Println(err)
return
}
}(c)
}
}
func (s *Socks5ToHTTP) Handle(c *net.TCPConn) error {
b := make([]byte, 0, 1024)
for {
var b1 [1024]byte
n, err := c.Read(b1[:])
if err != nil {
return err
}
b = append(b, b1[:n]...)
if bytes.Contains(b, []byte{0x0d, 0x0a, 0x0d, 0x0a}) {
break
}
if len(b) >= 2083+18 {
return errors.New("HTTP header too long")
}
}
bb := bytes.SplitN(b, []byte(" "), 3)
if len(bb) != 3 {
return errors.New("Invalid Request")
}
method, address := string(bb[0]), string(bb[1])
var addr string
if method == "CONNECT" {
addr = address
}
if method != "CONNECT" {
var err error
addr, err = ant.GetAddressFromURL(address)
if err != nil {
return err
}
}
if s.HTTPMiddleman != nil {
if done, err := s.HTTPMiddleman.Handle(method, addr, b, c); err != nil || done {
return err
}
}
tmp, err := s.Dial.Dial("tcp", addr)
if err != nil {
return err
}
rc := tmp.(*net.TCPConn)
defer rc.Close()
if s.Timeout != 0 {
if err := rc.SetKeepAlivePeriod(time.Duration(s.Timeout) * time.Second); err != nil {
return err
}
}
if s.Deadline != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(s.Deadline) * time.Second)); err != nil {
return err
}
}
if method == "CONNECT" {
_, err := c.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n"))
if err != nil {
return err
}
}
if method != "CONNECT" {
if _, err := rc.Write(b); err != nil {
return err
}
}
go func() {
_, _ = io.Copy(rc, c)
}()
_, _ = io.Copy(c, rc)
return nil
}
func (s *Socks5ToHTTP) Shutdown() error {
if s.Listen == nil {
return nil
}
return s.Listen.Close()
}