2014-01-24 03:15:53 +08:00
|
|
|
package socks5
|
|
|
|
|
2016-03-08 22:29:11 +08:00
|
|
|
import (
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2014-01-24 03:15:53 +08:00
|
|
|
// RuleSet is used to provide custom rules to allow or prohibit actions
|
|
|
|
type RuleSet interface {
|
2016-03-08 22:29:11 +08:00
|
|
|
Allow(ctx context.Context, req *Request) (context.Context, bool)
|
2014-01-24 03:15:53 +08:00
|
|
|
}
|
|
|
|
|
2014-02-14 02:49:35 +08:00
|
|
|
// PermitAll returns a RuleSet which allows all types of connections
|
2014-01-24 03:15:53 +08:00
|
|
|
func PermitAll() RuleSet {
|
|
|
|
return &PermitCommand{true, true, true}
|
|
|
|
}
|
|
|
|
|
2014-02-14 02:49:35 +08:00
|
|
|
// PermitNone returns a RuleSet which disallows all types of connections
|
|
|
|
func PermitNone() RuleSet {
|
|
|
|
return &PermitCommand{false, false, false}
|
|
|
|
}
|
|
|
|
|
2014-01-24 03:15:53 +08:00
|
|
|
// PermitCommand is an implementation of the RuleSet which
|
|
|
|
// enables filtering supported commands
|
|
|
|
type PermitCommand struct {
|
|
|
|
EnableConnect bool
|
|
|
|
EnableBind bool
|
|
|
|
EnableAssociate bool
|
|
|
|
}
|
|
|
|
|
2016-03-08 22:29:11 +08:00
|
|
|
func (p *PermitCommand) Allow(ctx context.Context, req *Request) (context.Context, bool) {
|
2016-01-11 15:36:53 +08:00
|
|
|
switch req.Command {
|
|
|
|
case ConnectCommand:
|
2016-03-08 22:29:11 +08:00
|
|
|
return ctx, p.EnableConnect
|
2016-01-11 15:36:53 +08:00
|
|
|
case BindCommand:
|
2016-03-08 22:29:11 +08:00
|
|
|
return ctx, p.EnableBind
|
2016-01-11 15:36:53 +08:00
|
|
|
case AssociateCommand:
|
2016-03-08 22:29:11 +08:00
|
|
|
return ctx, p.EnableAssociate
|
2016-01-11 15:36:53 +08:00
|
|
|
}
|
|
|
|
|
2016-03-08 22:29:11 +08:00
|
|
|
return ctx, false
|
2014-01-24 03:15:53 +08:00
|
|
|
}
|