go-socks5/ruleset.go

42 lines
1017 B
Go
Raw Normal View History

2014-01-23 19:15:53 +00:00
package socks5
import (
2020-04-19 09:08:22 +00:00
"context"
)
2014-01-23 19:15:53 +00:00
// RuleSet is used to provide custom rules to allow or prohibit actions
type RuleSet interface {
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
}
2020-04-22 02:15:40 +00:00
// Allow implement interface RuleSet
func (p *PermitCommand) Allow(ctx context.Context, req *Request) (context.Context, bool) {
switch req.Command {
2020-04-21 06:03:20 +00:00
case CommandConnect:
return ctx, p.EnableConnect
2020-04-21 06:03:20 +00:00
case CommandBind:
return ctx, p.EnableBind
2020-04-21 06:03:20 +00:00
case CommandAssociate:
return ctx, p.EnableAssociate
}
return ctx, false
2014-01-23 19:15:53 +00:00
}