Skip to content

Commit aad92b1

Browse files
author
ap4y
committed
Refactor RuleSet api using Request struct
1 parent 1fcd99d commit aad92b1

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

ruleset.go

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
11
package socks5
22

3-
import (
4-
"net"
5-
)
6-
73
// RuleSet is used to provide custom rules to allow or prohibit actions
84
type RuleSet interface {
9-
// AllowConnect is used to filter connect requests
10-
AllowConnect(dstIP net.IP, dstPort int, srcIP net.IP, srcPort int) bool
11-
12-
// AllowBind is used to filter bind requests
13-
AllowBind(dstIP net.IP, dstPort int, srcIP net.IP, srcPort int) bool
14-
15-
// AllowAssociate is used to filter associate requests
16-
AllowAssociate(dstIP net.IP, dstPort int, srcIP net.IP, srcPort int) bool
5+
Allow(req *Request) bool
176
}
187

198
// PermitAll returns a RuleSet which allows all types of connections
@@ -34,14 +23,15 @@ type PermitCommand struct {
3423
EnableAssociate bool
3524
}
3625

37-
func (p *PermitCommand) AllowConnect(net.IP, int, net.IP, int) bool {
38-
return p.EnableConnect
39-
}
40-
41-
func (p *PermitCommand) AllowBind(net.IP, int, net.IP, int) bool {
42-
return p.EnableBind
43-
}
44-
45-
func (p *PermitCommand) AllowAssociate(net.IP, int, net.IP, int) bool {
46-
return p.EnableAssociate
26+
func (p *PermitCommand) Allow(req *Request) bool {
27+
switch req.Command {
28+
case ConnectCommand:
29+
return p.EnableConnect
30+
case BindCommand:
31+
return p.EnableBind
32+
case AssociateCommand:
33+
return p.EnableAssociate
34+
}
35+
36+
return false
4737
}

ruleset_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
package socks5
22

3-
import (
4-
"testing"
5-
)
3+
import "testing"
64

75
func TestPermitCommand(t *testing.T) {
86
r := &PermitCommand{true, false, false}
97

10-
if !r.AllowConnect(nil, 500, nil, 1000) {
8+
if !r.Allow(&Request{Command: ConnectCommand}) {
119
t.Fatalf("expect connect")
1210
}
1311

14-
if r.AllowBind(nil, 500, nil, 1000) {
12+
if r.Allow(&Request{Command: BindCommand}) {
1513
t.Fatalf("do not expect bind")
1614
}
1715

18-
if r.AllowAssociate(nil, 500, nil, 1000) {
16+
if r.Allow(&Request{Command: AssociateCommand}) {
1917
t.Fatalf("do not expect associate")
2018
}
2119
}

0 commit comments

Comments
 (0)