-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathserver.go
342 lines (297 loc) · 8.64 KB
/
server.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package server
import (
"bepass/cache"
"bepass/doh"
"bepass/logger"
"bepass/socks5"
"bepass/socks5/statute"
"bepass/transport"
"bytes"
"context"
"fmt"
"io"
"math/rand"
"net"
"regexp"
"strings"
"sync"
"time"
"github.com/ameshkov/dnscrypt/v2"
"github.com/miekg/dns"
)
// ChunkConfig Constants for chunk lengths and delays.
type ChunkConfig struct {
BeforeSniLength [2]int
AfterSniLength [2]int
Delay [2]int
}
// WorkerConfig Constants for cloudflare worker.
type WorkerConfig struct {
WorkerAddress string
WorkerIPPortAddress string
WorkerEnabled bool
WorkerDNSOnly bool
}
type Server struct {
RemoteDNSAddr string
Cache *cache.Cache
ResolveSystem string
DoHClient *doh.Client
ChunkConfig ChunkConfig
WorkerConfig WorkerConfig
Logger *logger.Std
BindAddress string
}
var sniRegex = regexp.MustCompile(`^(?:[a-z0-9-]+\.)+[a-z]+$`)
// getHostname returns the Server Name Indication (SNI) from a TLS Client Hello message.
func (s *Server) getHostname(data []byte) ([]byte, error) {
const (
sniTypeByte = 0x00
sniLengthOffset = 2
)
if data[0] != 0x16 {
return nil, fmt.Errorf("not a tls packet")
}
// Find the SNI type byte
sniTypeIndex := bytes.IndexByte(data, sniTypeByte)
if sniTypeIndex == -1 {
return nil, fmt.Errorf("could not find SNI type byte in Server Hello message")
}
// Ensure sufficient data to read the SNI length and value
if len(data) < sniTypeIndex+sniLengthOffset+1 {
return nil, fmt.Errorf("insufficient data to read SNI length")
}
var sni string
var prev byte
for i := 0; i < len(data); i++ {
if prev == 0 && data[i] == 0 {
start := i + 2
end := start + int(data[i+1])
if start < end && end < len(data) {
str := string(data[start:end])
if sniRegex.MatchString(str) {
sni = str
break
}
}
}
prev = data[i]
}
return []byte(sni), nil
}
// getChunkedPackets splits the data into chunks based on SNI and chunk lengths.
func (s *Server) getChunkedPackets(data []byte) map[int][]byte {
const (
chunkIndexHostname = 0
chunkIndexSNIValue = 1
chunkIndexRemainder = 2
)
chunks := make(map[int][]byte)
hostname, err := s.getHostname(data)
if err != nil {
s.Logger.Errorf("get hostname error: %v", err)
chunks[chunkIndexRemainder] = data
return chunks
}
s.Logger.Printf("Hostname %s", string(hostname))
index := bytes.Index(data, hostname)
if index == -1 {
return nil
}
chunks[chunkIndexHostname] = make([]byte, index)
copy(chunks[chunkIndexHostname], data[:index])
chunks[chunkIndexSNIValue] = make([]byte, len(hostname))
copy(chunks[chunkIndexSNIValue], data[index:index+len(hostname)])
chunks[chunkIndexRemainder] = make([]byte, len(data)-index-len(hostname))
copy(chunks[chunkIndexRemainder], data[index+len(hostname):])
return chunks
}
// sendSplitChunks sends the chunks to the destination with specified delays.
func (s *Server) sendSplitChunks(dst io.Writer, chunks map[int][]byte, config ChunkConfig) {
chunkLengthMin, chunkLengthMax := config.BeforeSniLength[0], config.BeforeSniLength[1]
if len(chunks) > 1 {
chunkLengthMin, chunkLengthMax = config.AfterSniLength[0], config.AfterSniLength[1]
}
for _, chunk := range chunks {
position := 0
for position < len(chunk) {
chunkLength := rand.Intn(chunkLengthMax-chunkLengthMin) + chunkLengthMin
if chunkLength > len(chunk)-position {
chunkLength = len(chunk) - position
}
delay := rand.Intn(config.Delay[1]-config.Delay[0]) + config.Delay[0]
_, errWrite := dst.Write(chunk[position : position+chunkLength])
if errWrite != nil {
return
}
position += chunkLength
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}
}
// Handle handles the SOCKS5 request and forwards traffic to the destination.
func (s *Server) Handle(ctx context.Context, w io.Writer, req *socks5.Request) error {
if s.WorkerConfig.WorkerEnabled &&
!s.WorkerConfig.WorkerDNSOnly &&
!strings.Contains(s.WorkerConfig.WorkerAddress, req.DstAddr.FQDN) {
if err := socks5.SendReply(w, statute.RepSuccess, nil); err != nil {
s.Logger.Errorf("failed to send reply: %v", err)
return err
}
// , s.WorkerConfig.WorkerIPPortAddress
return transport.TunnelToWorkerThroughWs(ctx, w, req, s.WorkerConfig.WorkerAddress, s.BindAddress, s.Logger)
}
dest, err := s.resolveDestination(ctx, req)
if err != nil {
return err
}
if err := socks5.SendReply(w, statute.RepSuccess, nil); err != nil {
s.Logger.Errorf("failed to send reply: %v", err)
return err
}
conn, err := s.connectToDestination(dest)
if err != nil {
return err
}
defer conn.Close()
if err := conn.SetNoDelay(true); err != nil {
s.Logger.Errorf("failed to set NODELAY option: %v", err)
return err
}
var wg sync.WaitGroup
wg.Add(2)
closeSignal := make(chan error, 1)
go s.sendChunks(conn, req.Reader, true, &wg)
go s.sendChunks(w, conn, false, &wg)
go func() {
wg.Wait()
close(closeSignal)
}()
return <-closeSignal
}
// resolveDestination resolves the destination address using DNS.
func (s *Server) resolveDestination(ctx context.Context, req *socks5.Request) (*net.TCPAddr, error) {
dest := req.RawDestAddr
if dest.FQDN != "" {
ip, err := s.Resolve(dest.FQDN)
if err != nil {
return nil, err
}
dest.IP = net.ParseIP(ip)
s.Logger.Printf("resolved %s to %s", req.RawDestAddr, dest)
} else {
s.Logger.Printf("skipping resolution for %s", req.RawDestAddr)
}
addr := &net.TCPAddr{IP: dest.IP, Port: dest.Port}
s.Logger.Printf("dialing %s", addr)
return addr, nil
}
// connectToDestination connects to the destination address.
func (s *Server) connectToDestination(addr *net.TCPAddr) (*net.TCPConn, error) {
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
s.Logger.Errorf("failed to connect to %s: %v", addr, err)
return nil, err
}
return conn, nil
}
// sendChunks sends chunks from src to dst
func (s *Server) sendChunks(dst io.Writer, src io.Reader, shouldSplit bool, wg *sync.WaitGroup) {
defer wg.Done()
dataBuffer := make([]byte, 256*1024)
for index := 0; ; index++ {
bytesRead, err := src.Read(dataBuffer)
if bytesRead > 0 {
// check if it's the first packet and its tls packet
if index == 0 && dataBuffer[0] == 0x16 && shouldSplit {
chunks := s.getChunkedPackets(dataBuffer[:bytesRead])
s.sendSplitChunks(dst, chunks, s.ChunkConfig)
} else {
_, _ = dst.Write(dataBuffer[:bytesRead])
}
}
if err != nil {
return
}
}
}
// Resolve resolves the FQDN to an IP address using the specified resolution mechanism.
func (s *Server) Resolve(fqdn string) (string, error) {
if s.WorkerConfig.WorkerEnabled &&
strings.Contains(s.WorkerConfig.WorkerAddress, fqdn) {
return strings.Split(s.WorkerConfig.WorkerIPPortAddress, ":")[0], nil
}
// Ensure fqdn ends with a period
if !strings.HasSuffix(fqdn, ".") {
fqdn += "."
}
// Check the cache for fqdn
if cachedValue, _ := s.Cache.Get(fqdn); cachedValue != nil {
s.Logger.Printf("using cached value for %s", fqdn)
return cachedValue.(string), nil
}
// Build request message
req := dns.Msg{}
req.Id = dns.Id()
req.RecursionDesired = true
req.Question = []dns.Question{{
Name: fqdn,
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}}
// Determine which DNS resolution mechanism to use
var exchange *dns.Msg
var err error
switch s.ResolveSystem {
case "doh":
exchange, err = s.resolveDNSWithDOH(&req)
default:
exchange, err = s.resolveDNSWithDNSCrypt(&req)
}
if err != nil {
return "", err
}
// Parse answer and store in cache
answer := exchange.Answer[0]
s.Logger.Printf("resolved %s to %s", fqdn, answer.String())
record := strings.Fields(answer.String())
if record[3] == "CNAME" {
return s.Resolve(record[4])
}
ip := record[4]
s.Cache.Set(fqdn, ip)
return ip, nil
}
// resolveDNSWithDOH resolves DNS using DNS-over-HTTP (DoH) client.
func (s *Server) resolveDNSWithDOH(req *dns.Msg) (*dns.Msg, error) {
needsFragmentation := false
dnsAddr := s.RemoteDNSAddr
if s.WorkerConfig.WorkerEnabled && s.WorkerConfig.WorkerDNSOnly {
needsFragmentation = true
dnsAddr = s.WorkerConfig.WorkerAddress
}
exchange, _, err := s.DoHClient.Exchange(req, dnsAddr, needsFragmentation)
if err != nil {
return nil, err
}
if len(exchange.Answer) == 0 {
return nil, fmt.Errorf("no answer")
}
return exchange, nil
}
// resolveDNSWithDNSCrypt resolves DNS using DNSCrypt client.
func (s *Server) resolveDNSWithDNSCrypt(req *dns.Msg) (*dns.Msg, error) {
c := dnscrypt.Client{Net: "tcp", Timeout: 10 * time.Second}
resolverInfo, err := c.Dial(s.RemoteDNSAddr)
if err != nil {
return nil, err
}
exchange, err := c.Exchange(req, resolverInfo)
if err != nil {
return nil, err
}
if len(exchange.Answer) == 0 {
return nil, fmt.Errorf("no answer")
}
return exchange, nil
}