2014-01-23 19:15:53 +00:00
|
|
|
package socks5
|
|
|
|
|
2016-03-08 14:29:11 +00:00
|
|
|
import (
|
2020-04-19 09:08:22 +00:00
|
|
|
"context"
|
2016-03-08 14:29:11 +00:00
|
|
|
)
|
|
|
|
|
2014-01-23 19:15:53 +00:00
|
|
|
// RuleSet is used to provide custom rules to allow or prohibit actions
|
|
|
|
type RuleSet interface {
|
2016-03-08 14:29:11 +00:00
|
|
|
Allow(ctx context.Context, req *Request) (context.Context, bool)
|
2014-01-23 19:15:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-13 18:49:35 +00:00
|
|
|
// PermitAll returns a RuleSet which allows all types of connections
|
2014-01-23 19:15:53 +00:00
|
|
|
func PermitAll() RuleSet {
|
|
|
|
return &PermitCommand{true, true, true}
|
|
|
|
}
|
|
|
|
|
2014-02-13 18:49:35 +00:00
|
|
|
// PermitNone returns a RuleSet which disallows all types of connections
|
|
|
|
func PermitNone() RuleSet {
|
|
|
|
return &PermitCommand{false, false, false}
|
|
|
|
}
|
|
|
|
|
2014-01-23 19:15:53 +00: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 14:29:11 +00:00
|
|
|
func (p *PermitCommand) Allow(ctx context.Context, req *Request) (context.Context, bool) {
|
2016-01-11 07:36:53 +00:00
|
|
|
switch req.Command {
|
2020-04-21 06:03:20 +00:00
|
|
|
case CommandConnect:
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, p.EnableConnect
|
2020-04-21 06:03:20 +00:00
|
|
|
case CommandBind:
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, p.EnableBind
|
2020-04-21 06:03:20 +00:00
|
|
|
case CommandAssociate:
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, p.EnableAssociate
|
2016-01-11 07:36:53 +00:00
|
|
|
}
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, false
|
2014-01-23 19:15:53 +00:00
|
|
|
}
|