ruleset.go 998 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package socks5
  2. import (
  3. "golang.org/x/net/context"
  4. )
  5. // RuleSet is used to provide custom rules to allow or prohibit actions
  6. type RuleSet interface {
  7. Allow(ctx context.Context, req *Request) (context.Context, bool)
  8. }
  9. // PermitAll returns a RuleSet which allows all types of connections
  10. func PermitAll() RuleSet {
  11. return &PermitCommand{true, true, true}
  12. }
  13. // PermitNone returns a RuleSet which disallows all types of connections
  14. func PermitNone() RuleSet {
  15. return &PermitCommand{false, false, false}
  16. }
  17. // PermitCommand is an implementation of the RuleSet which
  18. // enables filtering supported commands
  19. type PermitCommand struct {
  20. EnableConnect bool
  21. EnableBind bool
  22. EnableAssociate bool
  23. }
  24. func (p *PermitCommand) Allow(ctx context.Context, req *Request) (context.Context, bool) {
  25. switch req.Command {
  26. case ConnectCommand:
  27. return ctx, p.EnableConnect
  28. case BindCommand:
  29. return ctx, p.EnableBind
  30. case AssociateCommand:
  31. return ctx, p.EnableAssociate
  32. }
  33. return ctx, false
  34. }