-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connection not allowed by ruleset (#11)
* RuleSet interface * server rules * with allow commands * test not allowed command * package comment * bind command * white list IPs * RemoteAddressFromContext return net.Addr type * test IP rules * not allowed destination * rename
- Loading branch information
Showing
8 changed files
with
267 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,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: {}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,51 @@ | ||
package socks5 | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIPRules(t *testing.T) { | ||
cases := map[string]struct { | ||
allowIPs []net.IP | ||
address net.Addr | ||
allow bool | ||
}{ | ||
"allow_connection": { | ||
allowIPs: []net.IP{ | ||
net.ParseIP("192.168.0.100"), | ||
}, | ||
address: &net.TCPAddr{ | ||
IP: net.ParseIP("192.168.0.100"), | ||
}, | ||
allow: true, | ||
}, | ||
"not_allow_connection": { | ||
allowIPs: []net.IP{}, | ||
address: &net.TCPAddr{ | ||
IP: net.ParseIP("192.168.0.101"), | ||
}, | ||
allow: false, | ||
}, | ||
"incorrect_address_type": { | ||
allowIPs: []net.IP{}, | ||
address: &net.UDPAddr{}, | ||
allow: false, | ||
}, | ||
"without_slice_allow_IPs": { | ||
allow: true, | ||
}, | ||
} | ||
|
||
for name, tc := range cases { | ||
t.Run(name, func(t *testing.T) { | ||
rules := &serverRules{ | ||
allowIPs: tc.allowIPs, | ||
} | ||
|
||
assert.Equal(t, rules.IsAllowConnection(tc.address), tc.allow) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.