mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-13 21:54:13 +08:00
proxy provider: remove port filter
This commit is contained in:
parent
62ec6d2969
commit
186c17abe5
@ -12,7 +12,6 @@ import (
|
||||
type Outbound interface {
|
||||
Type() string
|
||||
Tag() string
|
||||
Port() int
|
||||
Network() []string
|
||||
Dependencies() []string
|
||||
N.Dialer
|
||||
|
@ -4,7 +4,6 @@ type FilterOptions struct {
|
||||
Includes Listable[string] `json:"includes,omitempty"`
|
||||
Excludes string `json:"excludes,omitempty"`
|
||||
Types Listable[string] `json:"types,omitempty"`
|
||||
Ports Listable[string] `json:"ports,omitempty"`
|
||||
}
|
||||
|
||||
type GroupOutboundOptions struct {
|
||||
|
@ -102,39 +102,6 @@ func (h *Outbound) UnmarshalJSON(bytes []byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Outbound) Port() int {
|
||||
var port uint16
|
||||
switch h.Type {
|
||||
case C.TypeSOCKS:
|
||||
port = h.SocksOptions.ServerPort
|
||||
case C.TypeHTTP:
|
||||
port = h.HTTPOptions.ServerPort
|
||||
case C.TypeShadowsocks:
|
||||
port = h.ShadowsocksOptions.ServerPort
|
||||
case C.TypeVMess:
|
||||
port = h.VMessOptions.ServerPort
|
||||
case C.TypeTrojan:
|
||||
port = h.TrojanOptions.ServerPort
|
||||
case C.TypeWireGuard:
|
||||
port = h.WireGuardOptions.ServerPort
|
||||
case C.TypeHysteria:
|
||||
port = h.HysteriaOptions.ServerPort
|
||||
case C.TypeSSH:
|
||||
port = h.SSHOptions.ServerPort
|
||||
case C.TypeShadowTLS:
|
||||
port = h.ShadowTLSOptions.ServerPort
|
||||
case C.TypeShadowsocksR:
|
||||
port = h.ShadowsocksROptions.ServerPort
|
||||
case C.TypeVLESS:
|
||||
port = h.VLESSOptions.ServerPort
|
||||
case C.TypeTUIC:
|
||||
port = h.TUICOptions.ServerPort
|
||||
case C.TypeHysteria2:
|
||||
port = h.Hysteria2Options.ServerPort
|
||||
}
|
||||
return int(port)
|
||||
}
|
||||
|
||||
type DialerOptionsWrapper interface {
|
||||
TakeDialerOptions() DialerOptions
|
||||
ReplaceDialerOptions(options DialerOptions)
|
||||
|
@ -27,7 +27,6 @@ type myOutboundAdapter struct {
|
||||
router adapter.Router
|
||||
logger log.ContextLogger
|
||||
tag string
|
||||
port uint16
|
||||
dependencies []string
|
||||
}
|
||||
|
||||
@ -39,15 +38,6 @@ func (a *myOutboundAdapter) Tag() string {
|
||||
return a.tag
|
||||
}
|
||||
|
||||
func (a *myOutboundAdapter) Port() int {
|
||||
switch a.protocol {
|
||||
case C.TypeDirect, C.TypeBlock, C.TypeDNS, C.TypeTor, C.TypeSelector, C.TypeURLTest:
|
||||
return 65536
|
||||
default:
|
||||
return int(a.port)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *myOutboundAdapter) SetTag(tag string) {
|
||||
a.tag = tag
|
||||
}
|
||||
|
@ -2,14 +2,10 @@ package outbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
R "github.com/dlclark/regexp2"
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing/common"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
|
||||
R "github.com/dlclark/regexp2"
|
||||
)
|
||||
|
||||
type myGroupAdapter struct {
|
||||
@ -20,7 +16,6 @@ type myGroupAdapter struct {
|
||||
includes []*R.Regexp
|
||||
excludes *R.Regexp
|
||||
types []string
|
||||
ports map[int]bool
|
||||
providers map[string]adapter.OutboundProvider
|
||||
}
|
||||
|
||||
@ -34,49 +29,8 @@ func CheckType(types []string) bool {
|
||||
})
|
||||
}
|
||||
|
||||
func CreatePortsMap(ports []string) (map[int]bool, error) {
|
||||
portReg1 := R.MustCompile(`^\d+$`, R.None)
|
||||
portReg2 := R.MustCompile(`^(\d*):(\d*)$`, R.None)
|
||||
portMap := map[int]bool{}
|
||||
for i, portRaw := range ports {
|
||||
if matched, _ := portReg1.MatchString(portRaw); matched {
|
||||
port, _ := strconv.Atoi(portRaw)
|
||||
if port < 0 || port > 65535 {
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
portMap[port] = true
|
||||
continue
|
||||
}
|
||||
if portRaw == ":" {
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
if match, _ := portReg2.FindStringMatch(portRaw); match != nil {
|
||||
start, _ := strconv.Atoi(match.Groups()[1].String())
|
||||
end, _ := strconv.Atoi(match.Groups()[2].String())
|
||||
if start < 0 || start > 65535 {
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
if end < 0 || end > 65535 {
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
if end == 0 {
|
||||
end = 65535
|
||||
}
|
||||
if start > end {
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
for port := start; port <= end; port++ {
|
||||
portMap[port] = true
|
||||
}
|
||||
continue
|
||||
}
|
||||
return nil, E.New("invalid ports item[", i, "]")
|
||||
}
|
||||
return portMap, nil
|
||||
}
|
||||
|
||||
func (s *myGroupAdapter) OutboundFilter(out adapter.Outbound) bool {
|
||||
return TestIncludes(out.Tag(), s.includes) && TestExcludes(out.Tag(), s.excludes) && TestTypes(out.Type(), s.types) && TestPorts(out.Port(), s.ports)
|
||||
return TestIncludes(out.Tag(), s.includes) && TestExcludes(out.Tag(), s.excludes) && TestTypes(out.Type(), s.types)
|
||||
}
|
||||
|
||||
func TestIncludes(tag string, includes []*R.Regexp) bool {
|
||||
@ -105,11 +59,3 @@ func TestTypes(oType string, types []string) bool {
|
||||
return oType == it
|
||||
})
|
||||
}
|
||||
|
||||
func TestPorts(port int, ports map[int]bool) bool {
|
||||
if port == 0 || len(ports) == 0 {
|
||||
return true
|
||||
}
|
||||
_, ok := ports[port]
|
||||
return ok
|
||||
}
|
||||
|
@ -40,7 +40,6 @@ func NewHTTP(ctx context.Context, router adapter.Router, logger log.ContextLogge
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
sHTTP.NewClient(sHTTP.Options{
|
||||
|
@ -95,7 +95,6 @@ func NewHysteria(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
client: client,
|
||||
|
@ -81,7 +81,6 @@ func NewHysteria2(ctx context.Context, router adapter.Router, logger log.Context
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
client: client,
|
||||
|
@ -51,7 +51,6 @@ func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
uses: options.Providers,
|
||||
useAllProviders: options.UseAllProviders,
|
||||
types: options.Types,
|
||||
ports: make(map[int]bool),
|
||||
providers: make(map[string]adapter.OutboundProvider),
|
||||
},
|
||||
defaultTag: options.Default,
|
||||
@ -85,11 +84,6 @@ func NewSelector(ctx context.Context, router adapter.Router, logger log.ContextL
|
||||
if !CheckType(outbound.types) {
|
||||
return nil, E.New("invalid types")
|
||||
}
|
||||
if portMap, err := CreatePortsMap(options.Ports); err == nil {
|
||||
outbound.ports = portMap
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
return outbound, nil
|
||||
}
|
||||
|
||||
|
@ -50,7 +50,6 @@ func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.Conte
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
dialer: outboundDialer,
|
||||
|
@ -32,7 +32,6 @@ func NewShadowTLS(ctx context.Context, router adapter.Router, logger log.Context
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
}
|
||||
|
@ -52,7 +52,6 @@ func NewSocks(router adapter.Router, logger log.ContextLogger, tag string, optio
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
client: socks.NewClient(outboundDialer, options.ServerOptions.Build(), version, options.Username, options.Password),
|
||||
|
@ -56,7 +56,6 @@ func NewSSH(ctx context.Context, router adapter.Router, logger log.ContextLogger
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
ctx: ctx,
|
||||
|
@ -44,7 +44,6 @@ func NewTrojan(ctx context.Context, router adapter.Router, logger log.ContextLog
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
dialer: outboundDialer,
|
||||
|
@ -58,7 +58,6 @@ func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
uses: options.Providers,
|
||||
useAllProviders: options.UseAllProviders,
|
||||
types: options.Types,
|
||||
ports: make(map[int]bool),
|
||||
providers: make(map[string]adapter.OutboundProvider),
|
||||
},
|
||||
tags: options.Outbounds,
|
||||
@ -92,11 +91,6 @@ func NewURLTest(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
if !CheckType(outbound.types) {
|
||||
return nil, E.New("invalid types")
|
||||
}
|
||||
if portMap, err := CreatePortsMap(options.Ports); err == nil {
|
||||
outbound.ports = portMap
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
return outbound, nil
|
||||
}
|
||||
|
||||
|
@ -47,7 +47,6 @@ func NewVLESS(ctx context.Context, router adapter.Router, logger log.ContextLogg
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
dialer: outboundDialer,
|
||||
|
@ -47,7 +47,6 @@ func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogg
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
dialer: outboundDialer,
|
||||
|
@ -60,7 +60,6 @@ func NewWireGuard(ctx context.Context, router adapter.Router, logger log.Context
|
||||
router: router,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
port: options.ServerPort,
|
||||
dependencies: withDialerDependency(options.DialerOptions),
|
||||
},
|
||||
ctx: ctx,
|
||||
|
@ -61,7 +61,6 @@ type myProviderAdapter struct {
|
||||
includes []*R.Regexp
|
||||
excludes *R.Regexp
|
||||
types []string
|
||||
ports map[int]bool
|
||||
|
||||
// Update cache
|
||||
checking atomic.Bool
|
||||
@ -100,15 +99,10 @@ func (a *myProviderAdapter) Outbounds() []adapter.Outbound {
|
||||
return outbounds
|
||||
}
|
||||
|
||||
func (a *myProviderAdapter) firstStart(ports []string) error {
|
||||
func (a *myProviderAdapter) firstStart() error {
|
||||
if !O.CheckType(a.types) {
|
||||
return E.New("invalid types")
|
||||
}
|
||||
if portMap, err := O.CreatePortsMap(ports); err == nil {
|
||||
a.ports = portMap
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
if !rw.IsFile(a.path) {
|
||||
return nil
|
||||
}
|
||||
@ -254,7 +248,7 @@ func (p *myProviderAdapter) parseOutbounds(ctx context.Context, router adapter.R
|
||||
return nil, err
|
||||
}
|
||||
finalOuts := common.Filter(outbounds, func(it option.Outbound) bool {
|
||||
return O.TestIncludes(it.Tag, p.includes) && O.TestExcludes(it.Tag, p.excludes) && O.TestTypes(it.Type, p.types) && O.TestPorts(it.Port(), p.ports)
|
||||
return O.TestIncludes(it.Tag, p.includes) && O.TestExcludes(it.Tag, p.excludes) && O.TestTypes(it.Type, p.types)
|
||||
})
|
||||
if !p.checkChange(finalOuts) {
|
||||
return nil, nil
|
||||
|
@ -52,7 +52,6 @@ func NewLocalProvider(ctx context.Context, router adapter.Router, logger log.Con
|
||||
healthcheckInterval: interval,
|
||||
outboundOverride: options.OutboundOverride,
|
||||
types: options.Types,
|
||||
ports: make(map[int]bool),
|
||||
providerType: C.ProviderTypeLocal,
|
||||
close: make(chan struct{}),
|
||||
pauseManager: service.FromContext[pause.Manager](ctx),
|
||||
@ -79,7 +78,7 @@ func NewLocalProvider(ctx context.Context, router adapter.Router, logger log.Con
|
||||
}
|
||||
provider.excludes = regex
|
||||
}
|
||||
if err := provider.firstStart(options.Ports); err != nil {
|
||||
if err := provider.firstStart(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return provider, nil
|
||||
|
@ -88,7 +88,6 @@ func NewRemoteProvider(ctx context.Context, router adapter.Router, logger log.Co
|
||||
healthcheckUrl: healthcheckUrl,
|
||||
healthcheckInterval: healthcheckInterval,
|
||||
types: options.Types,
|
||||
ports: make(map[int]bool),
|
||||
providerType: C.ProviderTypeRemote,
|
||||
outboundOverride: options.OutboundOverride,
|
||||
close: make(chan struct{}),
|
||||
@ -120,7 +119,7 @@ func NewRemoteProvider(ctx context.Context, router adapter.Router, logger log.Co
|
||||
}
|
||||
provider.excludes = regex
|
||||
}
|
||||
if err := provider.firstStart(options.Ports); err != nil {
|
||||
if err := provider.firstStart(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return provider, nil
|
||||
|
Loading…
x
Reference in New Issue
Block a user