mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-09-15 21:00:27 +00:00
limiter: use named routers, keep original inbound metadata
This commit is contained in:
@@ -254,7 +254,7 @@ func New(options Options) (*Box, error) {
|
|||||||
httpClientManager := httpclient.NewManager(ctx, logFactory.NewLogger("httpclient"), options.HTTPClients, routeOptions.DefaultHTTPClient)
|
httpClientManager := httpclient.NewManager(ctx, logFactory.NewLogger("httpclient"), options.HTTPClients, routeOptions.DefaultHTTPClient)
|
||||||
service.MustRegister[adapter.HTTPClientManager](ctx, httpClientManager)
|
service.MustRegister[adapter.HTTPClientManager](ctx, httpClientManager)
|
||||||
httpClientService := adapter.LifecycleService(httpClientManager)
|
httpClientService := adapter.LifecycleService(httpClientManager)
|
||||||
router := route.NewRouter(ctx, logFactory, routeOptions, dnsOptions)
|
router := route.NewRouter(ctx, logFactory, "router", routeOptions, dnsOptions)
|
||||||
service.MustRegister[adapter.Router](ctx, router)
|
service.MustRegister[adapter.Router](ctx, router)
|
||||||
err = router.Initialize(routeOptions.Rules, routeOptions.RuleSet)
|
err = router.Initialize(routeOptions.Rules, routeOptions.RuleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
|
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
F "github.com/sagernet/sing/common/format"
|
||||||
"github.com/sagernet/sing/common/logger"
|
"github.com/sagernet/sing/common/logger"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -64,7 +65,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
logFactory := service.FromContext[log.Factory](ctx)
|
logFactory := service.FromContext[log.Factory](ctx)
|
||||||
r := route.NewRouter(ctx, logFactory, options.Route, option.DNSOptions{})
|
r := route.NewRouter(ctx, logFactory, F.ToString("router/", C.TypeBandwidthLimiter, "[", tag, "]"), options.Route, option.DNSOptions{})
|
||||||
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -135,8 +136,6 @@ func (h *Outbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RouteConnectionEx(ctx, wrappedConn, metadata, onClose)
|
h.router.RouteConnectionEx(ctx, wrappedConn, metadata, onClose)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -149,8 +148,6 @@ func (h *Outbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RoutePacketConnectionEx(ctx, bufio.NewPacketConn(packetConn), metadata, onClose)
|
h.router.RoutePacketConnectionEx(ctx, bufio.NewPacketConn(packetConn), metadata, onClose)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/sagernet/sing-box/route"
|
"github.com/sagernet/sing-box/route"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
F "github.com/sagernet/sing/common/format"
|
||||||
"github.com/sagernet/sing/common/logger"
|
"github.com/sagernet/sing/common/logger"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -64,7 +65,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
logFactory := service.FromContext[log.Factory](ctx)
|
logFactory := service.FromContext[log.Factory](ctx)
|
||||||
r := route.NewRouter(ctx, logFactory, options.Route, option.DNSOptions{})
|
r := route.NewRouter(ctx, logFactory, F.ToString("router/", C.TypeConnectionLimiter, "[", tag, "]"), options.Route, option.DNSOptions{})
|
||||||
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -146,8 +147,6 @@ func (h *Outbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata
|
|||||||
if lockCtx != nil {
|
if lockCtx != nil {
|
||||||
go connChecker(lockCtx, conn.Close)
|
go connChecker(lockCtx, conn.Close)
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -163,8 +162,6 @@ func (h *Outbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
|||||||
if lockCtx != nil {
|
if lockCtx != nil {
|
||||||
go connChecker(lockCtx, conn.Close)
|
go connChecker(lockCtx, conn.Close)
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing-box/route"
|
"github.com/sagernet/sing-box/route"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
F "github.com/sagernet/sing/common/format"
|
||||||
"github.com/sagernet/sing/common/logger"
|
"github.com/sagernet/sing/common/logger"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -62,7 +63,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
logFactory := service.FromContext[log.Factory](ctx)
|
logFactory := service.FromContext[log.Factory](ctx)
|
||||||
r := route.NewRouter(ctx, logFactory, options.Route, option.DNSOptions{})
|
r := route.NewRouter(ctx, logFactory, F.ToString("router/", C.TypeRateLimiter, "[", tag, "]"), options.Route, option.DNSOptions{})
|
||||||
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
err = r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -119,8 +120,6 @@ func (h *Outbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,8 +129,6 @@ func (h *Outbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/sagernet/sing-box/route"
|
"github.com/sagernet/sing-box/route"
|
||||||
"github.com/sagernet/sing/common/bufio"
|
"github.com/sagernet/sing/common/bufio"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
|
F "github.com/sagernet/sing/common/format"
|
||||||
"github.com/sagernet/sing/common/logger"
|
"github.com/sagernet/sing/common/logger"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
@@ -40,7 +41,7 @@ func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextL
|
|||||||
}
|
}
|
||||||
strategy := NewManagerTrafficStrategy()
|
strategy := NewManagerTrafficStrategy()
|
||||||
logFactory := service.FromContext[log.Factory](ctx)
|
logFactory := service.FromContext[log.Factory](ctx)
|
||||||
r := route.NewRouter(ctx, logFactory, options.Route, option.DNSOptions{})
|
r := route.NewRouter(ctx, logFactory, F.ToString("router/", C.TypeTrafficLimiter, "[", tag, "]"), options.Route, option.DNSOptions{})
|
||||||
err := r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
err := r.Initialize(options.Route.Rules, options.Route.RuleSet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -112,8 +113,6 @@ func (h *Outbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RouteConnectionEx(ctx, wrappedConn, metadata, onClose)
|
h.router.RouteConnectionEx(ctx, wrappedConn, metadata, onClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,8 +125,6 @@ func (h *Outbound) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
|||||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
metadata.Inbound = h.Tag()
|
|
||||||
metadata.InboundType = h.Type()
|
|
||||||
h.router.RoutePacketConnectionEx(ctx, bufio.NewPacketConn(packetConn), metadata, onClose)
|
h.router.RoutePacketConnectionEx(ctx, bufio.NewPacketConn(packetConn), metadata, onClose)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -52,10 +52,10 @@ type Router struct {
|
|||||||
started chan struct{}
|
started chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter(ctx context.Context, logFactory log.Factory, options option.RouteOptions, dnsOptions option.DNSOptions) *Router {
|
func NewRouter(ctx context.Context, logFactory log.Factory, name string, options option.RouteOptions, dnsOptions option.DNSOptions) *Router {
|
||||||
return &Router{
|
return &Router{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
logger: logFactory.NewLogger("router"),
|
logger: logFactory.NewLogger(name),
|
||||||
inbound: service.FromContext[adapter.InboundManager](ctx),
|
inbound: service.FromContext[adapter.InboundManager](ctx),
|
||||||
outbound: service.FromContext[adapter.OutboundManager](ctx),
|
outbound: service.FromContext[adapter.OutboundManager](ctx),
|
||||||
dns: service.FromContext[adapter.DNSRouter](ctx),
|
dns: service.FromContext[adapter.DNSRouter](ctx),
|
||||||
|
|||||||
Reference in New Issue
Block a user