forked from TuanKiri/socks5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
59 lines (48 loc) · 1.04 KB
/
rules.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
package socks5
import (
"context"
"net"
)
type Rules interface {
IsAllowCommand(ctx context.Context, cmd byte) bool
IsAllowConnection(addr net.Addr) bool
IsAllowDestination(ctx context.Context, host string) bool
}
type serverRules struct {
allowCommands map[byte]struct{}
blockListHosts map[string]struct{}
allowIPs []net.IP
}
func (r *serverRules) IsAllowCommand(ctx context.Context, cmd byte) bool {
_, ok := r.allowCommands[cmd]
return ok
}
func (r *serverRules) IsAllowConnection(addr net.Addr) bool {
if r.allowIPs == nil {
return true
}
tcpAddr, ok := addr.(*net.TCPAddr)
if !ok {
return false
}
for _, allowIP := range r.allowIPs {
if allowIP.Equal(tcpAddr.IP) {
return true
}
}
return false
}
func (r *serverRules) IsAllowDestination(ctx context.Context, host string) bool {
if r.blockListHosts == nil {
return true
}
_, ok := r.blockListHosts[host]
return !ok
}
func permitAllCommands() map[byte]struct{} {
return map[byte]struct{}{
connect: {},
bind: {},
udpAssociate: {},
}
}