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"
|
2020-08-05 05:17:05 +00:00
|
|
|
|
2022-10-17 01:36:23 +00:00
|
|
|
"git.tcp.direct/kayos/go-socks5/statute"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// PermitCommand is an implementation of the RuleSet which
|
|
|
|
// enables filtering supported commands
|
|
|
|
type PermitCommand struct {
|
|
|
|
EnableConnect bool
|
|
|
|
EnableBind bool
|
|
|
|
EnableAssociate bool
|
|
|
|
}
|
|
|
|
|
2020-08-05 06:40:07 +00:00
|
|
|
// NewPermitNone returns a RuleSet which disallows all types of connections
|
|
|
|
func NewPermitNone() RuleSet {
|
|
|
|
return &PermitCommand{false, false, false}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPermitAll returns a RuleSet which allows all types of connections
|
|
|
|
func NewPermitAll() RuleSet {
|
|
|
|
return &PermitCommand{true, true, true}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPermitConnAndAss returns a RuleSet which allows Connect and Associate connection
|
|
|
|
func NewPermitConnAndAss() RuleSet {
|
|
|
|
return &PermitCommand{true, false, true}
|
|
|
|
}
|
|
|
|
|
2020-04-22 02:15:40 +00:00
|
|
|
// Allow implement interface RuleSet
|
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-08-05 05:17:05 +00:00
|
|
|
case statute.CommandConnect:
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, p.EnableConnect
|
2020-08-05 05:17:05 +00:00
|
|
|
case statute.CommandBind:
|
2016-03-08 14:29:11 +00:00
|
|
|
return ctx, p.EnableBind
|
2020-08-05 05:17:05 +00:00
|
|
|
case statute.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
|
|
|
}
|