Skip to content

Commit ca40be5

Browse files
committed
Added Regex destination matching
1 parent 76cd1a2 commit ca40be5

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ Simple socks5 server using go-socks5 with authentication options
77

88
## Start container with proxy
99

10-
```docker run -d --name socks5 -p 1080:1080 -e PROXY_USER=<PROXY_USER> -e PROXY_PASSWORD=<PROXY_PASSWORD> serjs/go-socks5-proxy```
10+
```
11+
docker run -d --name socks5 -p 1080:1080 \
12+
-e PROXY_USER=<PROXY_USER> \
13+
-e PROXY_PASSWORD=<PROXY_PASSWORD> \
14+
-e ALLOWED_DEST_FQDN=<REGEX_PATTERN> \
15+
serjs/go-socks5-proxy
16+
```
1117

1218
Leave `PROXY_USER` and `PROXY_PASSWORD` empty for skip authentication options while running socks5 server.
1319

@@ -18,6 +24,7 @@ Leave `PROXY_USER` and `PROXY_PASSWORD` empty for skip authentication options wh
1824
|PROXY_USER|String|EMPTY|Set proxy user (also required existed PROXY_PASS)|
1925
|PROXY_PASSWORD|String|EMPTY|Set proxy password for auth, used with PROXY_USER|
2026
|PROXY_PORT|String|1080|Set listen port for application inside docker container|
27+
|ALLOWED_DEST_FQDN|String|EMPTY|Allowed destination address regular expression pattern. Default allows all.|
2128
|TZ|String|UTC|Set Timezone like in many common Operation Systems|
2229

2330
## Test running service

ruleset.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/armon/go-socks5"
7+
"golang.org/x/net/context"
8+
)
9+
10+
// PermitDestAddrPattern returns a RuleSet which selectively allows addresses
11+
func PermitDestAddrPattern(pattern string) socks5.RuleSet {
12+
return &PermitDestAddrPatternRuleSet{pattern}
13+
}
14+
15+
// PermitDestAddrPatternRuleSet is an implementation of the RuleSet which
16+
// enables filtering supported destination address
17+
type PermitDestAddrPatternRuleSet struct {
18+
AllowedFqdnPattern string
19+
}
20+
21+
func (p *PermitDestAddrPatternRuleSet) Allow(ctx context.Context, req *socks5.Request) (context.Context, bool) {
22+
match, _ := regexp.MatchString(p.AllowedFqdnPattern, req.DestAddr.FQDN)
23+
return ctx, match
24+
}

server.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type params struct {
12-
User string `env:"PROXY_USER" envDefault:""`
13-
Password string `env:"PROXY_PASSWORD" envDefault:""`
14-
Port string `env:"PROXY_PORT" envDefault:"1080"`
12+
User string `env:"PROXY_USER" envDefault:""`
13+
Password string `env:"PROXY_PASSWORD" envDefault:""`
14+
Port string `env:"PROXY_PORT" envDefault:"1080"`
15+
AllowedDestFqdn string `env:"ALLOWED_DEST_FQDN" envDefault:""`
1516
}
1617

1718
func main() {
@@ -23,7 +24,7 @@ func main() {
2324
}
2425

2526
//Initialize socks5 config
26-
socsk5conf := &socks5.Config{
27+
socks5conf := &socks5.Config{
2728
Logger: log.New(os.Stdout, "", log.LstdFlags),
2829
}
2930

@@ -32,10 +33,14 @@ func main() {
3233
os.Getenv("PROXY_USER"): os.Getenv("PROXY_PASSWORD"),
3334
}
3435
cator := socks5.UserPassAuthenticator{Credentials: creds}
35-
socsk5conf.AuthMethods = []socks5.Authenticator{cator}
36+
socks5conf.AuthMethods = []socks5.Authenticator{cator}
3637
}
3738

38-
server, err := socks5.New(socsk5conf)
39+
if cfg.AllowedDestFqdn != "" {
40+
socks5conf.Rules = PermitDestAddrPattern(cfg.AllowedDestFqdn)
41+
}
42+
43+
server, err := socks5.New(socks5conf)
3944
if err != nil {
4045
log.Fatal(err)
4146
}

0 commit comments

Comments
 (0)