2023-11-08 12:09:22 +08:00
|
|
|
package mux
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing-mux"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
"github.com/sagernet/sing/common/logger"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Router struct {
|
2024-10-21 23:38:34 +08:00
|
|
|
router adapter.ConnectionRouterEx
|
2023-11-08 12:09:22 +08:00
|
|
|
service *mux.Service
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func NewRouterWithOptions(router adapter.ConnectionRouterEx, logger logger.ContextLogger, options option.InboundMultiplexOptions) (adapter.ConnectionRouterEx, error) {
|
2023-11-08 12:09:22 +08:00
|
|
|
if !options.Enabled {
|
|
|
|
return router, nil
|
|
|
|
}
|
|
|
|
var brutalOptions mux.BrutalOptions
|
|
|
|
if options.Brutal != nil && options.Brutal.Enabled {
|
|
|
|
brutalOptions = mux.BrutalOptions{
|
|
|
|
Enabled: true,
|
|
|
|
SendBPS: uint64(options.Brutal.UpMbps * C.MbpsToBps),
|
|
|
|
ReceiveBPS: uint64(options.Brutal.DownMbps * C.MbpsToBps),
|
|
|
|
}
|
|
|
|
if brutalOptions.SendBPS < mux.BrutalMinSpeedBPS {
|
|
|
|
return nil, E.New("brutal: invalid upload speed")
|
|
|
|
}
|
|
|
|
if brutalOptions.ReceiveBPS < mux.BrutalMinSpeedBPS {
|
|
|
|
return nil, E.New("brutal: invalid download speed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
service, err := mux.NewService(mux.ServiceOptions{
|
|
|
|
NewStreamContext: func(ctx context.Context, conn net.Conn) context.Context {
|
|
|
|
return log.ContextWithNewID(ctx)
|
|
|
|
},
|
|
|
|
Logger: logger,
|
|
|
|
Handler: adapter.NewRouteContextHandler(router, logger),
|
|
|
|
Padding: options.Padding,
|
|
|
|
Brutal: brutalOptions,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Router{router, service}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) RouteConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
if metadata.Destination == mux.Destination {
|
2024-10-21 23:38:34 +08:00
|
|
|
// TODO: check if WithContext is necessary
|
2023-11-08 12:09:22 +08:00
|
|
|
return r.service.NewConnection(adapter.WithContext(ctx, &metadata), conn, adapter.UpstreamMetadata(metadata))
|
|
|
|
} else {
|
|
|
|
return r.router.RouteConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
return r.router.RoutePacketConnection(ctx, conn, metadata)
|
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
|
|
|
|
func (r *Router) RouteConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
if metadata.Destination == mux.Destination {
|
|
|
|
r.service.NewConnectionEx(adapter.WithContext(ctx, &metadata), conn, metadata.Source, metadata.Destination, onClose)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Router) RoutePacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
r.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
|
|
|
}
|