-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
152 lines (133 loc) · 3.45 KB
/
main.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
package main
import (
"socks5"
"net"
"container/list"
"encoding/json"
"io/ioutil"
"log"
"regexp"
)
type Users struct {
User string `json:"user"`
Pass string `json:"pass"`
}
type Config struct {
Sock5Addr string `json:"addr"`
UserList []Users `json:"userlist"`
Pattern []string `json:"pattern"`
BlockPattern []string `json:"blockpattern"`
IPAllow []string `json:"ipallow"`
}
func (c *Config) String() string {
data, _ := json.Marshal(c)
return string(data)
}
func LoadConfig(s string) (*Config, error) {
data, err := ioutil.ReadFile(s)
if err != nil {
return nil, err
}
cConfig := &Config{}
if err = json.Unmarshal(data, cConfig); err != nil {
return nil, err
}
return cConfig, nil
}
func main() {
conf, err := LoadConfig("socks5-proxy.config")
if err != nil {
log.Println("load configuration failed, err:", err)
return
}
// Build pre-compiled pattern matching list
patterns := list.New()
for _, pattern := range conf.Pattern {
r, _ := regexp.Compile(pattern)
patterns.PushBack(r)
}
// Build pre-compiled blocking pattern matching list
blockpatterns := list.New()
for _, blockpattern := range conf.BlockPattern {
r, _ := regexp.Compile(blockpattern)
blockpatterns.PushBack(r)
}
// Build user list map
users := map[string]string{}
for _, uid := range conf.UserList {
users[uid.User] = uid.Pass
}
// Build IP allow CIDR mask list
ipmasks := list.New()
for _, cidr := range conf.IPAllow {
_, ipsubnet, err := net.ParseCIDR(cidr)
if err != nil {
log.Fatal(err)
}
ipmasks.PushBack(ipsubnet)
}
srv := socks5.New()
srv.AuthNoAuthenticationRequiredCallback = func(c *socks5.Conn) error {
ip, _, err := net.SplitHostPort(c.RemoteAddr())
if err != nil {
log.Fatal(err)
}
pip := net.ParseIP(ip)
for e := ipmasks.Front(); e != nil; e = e.Next() {
snet := e.Value.(*net.IPNet)
if snet.Contains(pip) {
log.Printf("IP OK: '%v'", ip)
return nil
}
}
log.Printf("Not allowed IP: '%v'", ip)
return socks5.ErrAuthenticationFailed
}
srv.AuthUsernamePasswordCallback = func(c *socks5.Conn, username, password []byte) error {
if len(users) == 0 {
return socks5.ErrAuthenticationFailed
}
user := string(username)
pass := string(password)
pwd, ok := users[user]
if ok {
if pass != pwd {
log.Printf("User Refused/Password mismatched: '%v'", user)
return socks5.ErrAuthenticationFailed
} else {
log.Printf("User Connected: '%v'", user)
c.Data = user
return nil
}
}
log.Printf("User Refused/No such user: '%v'", user)
return socks5.ErrAuthenticationFailed
}
srv.HandleConnectFunc(func(c *socks5.Conn, host string) (newHost string, err error) {
for e := patterns.Front(); e != nil; e = e.Next() {
pattern := e.Value.(*regexp.Regexp)
if pattern.MatchString(host) {
if user, ok := c.Data.(string); ok {
log.Printf("%v connecting to %v", user, host)
}
log.Printf("Alowed host: %v", host)
return host, nil
}
}
for e := blockpatterns.Front(); e != nil; e = e.Next() {
blockpattern := e.Value.(*regexp.Regexp)
if blockpattern.MatchString(host) {
log.Printf("Not Alowed host: %v", host)
return host, socks5.ErrConnectionNotAllowedByRuleset
}
}
log.Printf("Not Alowed host(DEFAULT): %v", host)
return host, socks5.ErrConnectionNotAllowedByRuleset
})
srv.HandleCloseFunc(func(c *socks5.Conn) {
if user, ok := c.Data.(string); ok {
log.Printf("Goodbye %v!", user)
}
})
srv.ListenAndServe(conf.Sock5Addr)
}