forked from ToyoDAdoubi/brook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.go
226 lines (213 loc) · 4.58 KB
/
relay.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
package brook
import (
"io"
"log"
"net"
"time"
cache "github.com/patrickmn/go-cache"
"github.com/txthinking/socks5"
)
// Relay is stream relay server
type Relay struct {
TCPAddr *net.TCPAddr
UDPAddr *net.UDPAddr
RemoteTCPAddr *net.TCPAddr
RemoteUDPAddr *net.UDPAddr
TCPListen *net.TCPListener
UDPConn *net.UDPConn
UDPExchanges *cache.Cache
TCPDeadline int // Not refreshed
TCPTimeout int
UDPDeadline int
}
// NewRelay
func NewRelay(addr, remote string, tcpTimeout, tcpDeadline, udpDeadline int) (*Relay, error) {
taddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return nil, err
}
uaddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return nil, err
}
rtaddr, err := net.ResolveTCPAddr("tcp", remote)
if err != nil {
return nil, err
}
ruaddr, err := net.ResolveUDPAddr("udp", remote)
if err != nil {
return nil, err
}
cs := cache.New(60*time.Minute, 10*time.Minute)
s := &Relay{
TCPAddr: taddr,
UDPAddr: uaddr,
RemoteTCPAddr: rtaddr,
RemoteUDPAddr: ruaddr,
UDPExchanges: cs,
TCPTimeout: tcpTimeout,
TCPDeadline: tcpDeadline,
UDPDeadline: udpDeadline,
}
return s, nil
}
// Run server
func (s *Relay) ListenAndServe() error {
errch := make(chan error)
go func() {
errch <- s.RunTCPServer()
}()
go func() {
errch <- s.RunUDPServer()
}()
return <-errch
}
// RunTCPServer starts tcp server
func (s *Relay) RunTCPServer() error {
var err error
s.TCPListen, err = net.ListenTCP("tcp", s.TCPAddr)
if err != nil {
return err
}
defer s.TCPListen.Close()
for {
c, err := s.TCPListen.AcceptTCP()
if err != nil {
return err
}
go func(c *net.TCPConn) {
defer c.Close()
if s.TCPTimeout != 0 {
if err := c.SetKeepAlivePeriod(time.Duration(s.TCPTimeout) * time.Second); err != nil {
log.Println(err)
return
}
}
if s.TCPDeadline != 0 {
if err := c.SetDeadline(time.Now().Add(time.Duration(s.TCPDeadline) * time.Second)); err != nil {
log.Println(err)
return
}
}
if err := s.TCPHandle(c); err != nil {
log.Println(err)
}
}(c)
}
return nil
}
// RunUDPServer starts udp server
func (s *Relay) RunUDPServer() error {
var err error
s.UDPConn, err = net.ListenUDP("udp", s.UDPAddr)
if err != nil {
return err
}
defer s.UDPConn.Close()
for {
b := make([]byte, 65536)
n, addr, err := s.UDPConn.ReadFromUDP(b)
if err != nil {
return err
}
go func(addr *net.UDPAddr, b []byte) {
if err := s.UDPHandle(addr, b); err != nil {
log.Println(err)
return
}
}(addr, b[0:n])
}
return nil
}
// Shutdown server
func (s *Relay) Shutdown() error {
var err, err1 error
if s.TCPListen != nil {
err = s.TCPListen.Close()
}
if s.UDPConn != nil {
err1 = s.UDPConn.Close()
}
if err != nil {
return err
}
return err1
}
// TCPHandle handle request
func (s *Relay) TCPHandle(c *net.TCPConn) error {
tmp, err := Dial.Dial("tcp", s.RemoteTCPAddr.String())
if err != nil {
return err
}
rc := tmp.(*net.TCPConn)
defer rc.Close()
if s.TCPTimeout != 0 {
if err := rc.SetKeepAlivePeriod(time.Duration(s.TCPTimeout) * time.Second); err != nil {
return err
}
}
if s.TCPDeadline != 0 {
if err := rc.SetDeadline(time.Now().Add(time.Duration(s.TCPDeadline) * time.Second)); err != nil {
return err
}
}
go func() {
_, _ = io.Copy(c, rc)
}()
_, _ = io.Copy(rc, c)
return nil
}
// UDPHandle handle packet
func (s *Relay) UDPHandle(addr *net.UDPAddr, b []byte) error {
send := func(ue *socks5.UDPExchange, data []byte) error {
_, err := ue.RemoteConn.Write(data)
if err != nil {
return err
}
return nil
}
var ue *socks5.UDPExchange
iue, ok := s.UDPExchanges.Get(addr.String())
if ok {
ue = iue.(*socks5.UDPExchange)
return send(ue, b)
}
tmp, err := Dial.Dial("udp", s.RemoteUDPAddr.String())
if err != nil {
return err
}
rc := tmp.(*net.UDPConn)
ue = &socks5.UDPExchange{
ClientAddr: addr,
RemoteConn: rc,
}
s.UDPExchanges.Set(ue.ClientAddr.String(), ue, cache.DefaultExpiration)
if err := send(ue, b); err != nil {
return err
}
go func(ue *socks5.UDPExchange) {
defer func() {
s.UDPExchanges.Delete(ue.ClientAddr.String())
ue.RemoteConn.Close()
}()
var b [65536]byte
for {
if s.UDPDeadline != 0 {
if err := ue.RemoteConn.SetDeadline(time.Now().Add(time.Duration(s.UDPDeadline) * time.Second)); err != nil {
log.Println(err)
break
}
}
n, err := ue.RemoteConn.Read(b[:])
if err != nil {
log.Println(err)
break
}
if _, err := s.UDPConn.WriteToUDP(b[0:n], ue.ClientAddr); err != nil {
log.Println(err)
break
}
}
}(ue)
return nil
}