github.com/sagernet/sing-box@v1.9.0-rc.20/adapter/conn_router.go (about) 1 package adapter 2 3 import ( 4 "context" 5 "net" 6 7 "github.com/sagernet/sing/common/logger" 8 M "github.com/sagernet/sing/common/metadata" 9 N "github.com/sagernet/sing/common/network" 10 ) 11 12 type ConnectionRouter interface { 13 RouteConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error 14 RoutePacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error 15 } 16 17 func NewRouteHandler( 18 metadata InboundContext, 19 router ConnectionRouter, 20 logger logger.ContextLogger, 21 ) UpstreamHandlerAdapter { 22 return &routeHandlerWrapper{ 23 metadata: metadata, 24 router: router, 25 logger: logger, 26 } 27 } 28 29 func NewRouteContextHandler( 30 router ConnectionRouter, 31 logger logger.ContextLogger, 32 ) UpstreamHandlerAdapter { 33 return &routeContextHandlerWrapper{ 34 router: router, 35 logger: logger, 36 } 37 } 38 39 var _ UpstreamHandlerAdapter = (*routeHandlerWrapper)(nil) 40 41 type routeHandlerWrapper struct { 42 metadata InboundContext 43 router ConnectionRouter 44 logger logger.ContextLogger 45 } 46 47 func (w *routeHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { 48 myMetadata := w.metadata 49 if metadata.Source.IsValid() { 50 myMetadata.Source = metadata.Source 51 } 52 if metadata.Destination.IsValid() { 53 myMetadata.Destination = metadata.Destination 54 } 55 return w.router.RouteConnection(ctx, conn, myMetadata) 56 } 57 58 func (w *routeHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error { 59 myMetadata := w.metadata 60 if metadata.Source.IsValid() { 61 myMetadata.Source = metadata.Source 62 } 63 if metadata.Destination.IsValid() { 64 myMetadata.Destination = metadata.Destination 65 } 66 return w.router.RoutePacketConnection(ctx, conn, myMetadata) 67 } 68 69 func (w *routeHandlerWrapper) NewError(ctx context.Context, err error) { 70 w.logger.ErrorContext(ctx, err) 71 } 72 73 var _ UpstreamHandlerAdapter = (*routeContextHandlerWrapper)(nil) 74 75 type routeContextHandlerWrapper struct { 76 router ConnectionRouter 77 logger logger.ContextLogger 78 } 79 80 func (w *routeContextHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { 81 myMetadata := ContextFrom(ctx) 82 if metadata.Source.IsValid() { 83 myMetadata.Source = metadata.Source 84 } 85 if metadata.Destination.IsValid() { 86 myMetadata.Destination = metadata.Destination 87 } 88 return w.router.RouteConnection(ctx, conn, *myMetadata) 89 } 90 91 func (w *routeContextHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error { 92 myMetadata := ContextFrom(ctx) 93 if metadata.Source.IsValid() { 94 myMetadata.Source = metadata.Source 95 } 96 if metadata.Destination.IsValid() { 97 myMetadata.Destination = metadata.Destination 98 } 99 return w.router.RoutePacketConnection(ctx, conn, *myMetadata) 100 } 101 102 func (w *routeContextHandlerWrapper) NewError(ctx context.Context, err error) { 103 w.logger.ErrorContext(ctx, err) 104 }