github.com/sagernet/sing-box@v1.9.0-rc.20/adapter/upstream.go (about) 1 package adapter 2 3 import ( 4 "context" 5 "net" 6 7 E "github.com/sagernet/sing/common/exceptions" 8 M "github.com/sagernet/sing/common/metadata" 9 N "github.com/sagernet/sing/common/network" 10 ) 11 12 type ( 13 ConnectionHandlerFunc = func(ctx context.Context, conn net.Conn, metadata InboundContext) error 14 PacketConnectionHandlerFunc = func(ctx context.Context, conn N.PacketConn, metadata InboundContext) error 15 ) 16 17 func NewUpstreamHandler( 18 metadata InboundContext, 19 connectionHandler ConnectionHandlerFunc, 20 packetHandler PacketConnectionHandlerFunc, 21 errorHandler E.Handler, 22 ) UpstreamHandlerAdapter { 23 return &myUpstreamHandlerWrapper{ 24 metadata: metadata, 25 connectionHandler: connectionHandler, 26 packetHandler: packetHandler, 27 errorHandler: errorHandler, 28 } 29 } 30 31 var _ UpstreamHandlerAdapter = (*myUpstreamHandlerWrapper)(nil) 32 33 type myUpstreamHandlerWrapper struct { 34 metadata InboundContext 35 connectionHandler ConnectionHandlerFunc 36 packetHandler PacketConnectionHandlerFunc 37 errorHandler E.Handler 38 } 39 40 func (w *myUpstreamHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error { 41 myMetadata := w.metadata 42 if metadata.Source.IsValid() { 43 myMetadata.Source = metadata.Source 44 } 45 if metadata.Destination.IsValid() { 46 myMetadata.Destination = metadata.Destination 47 } 48 return w.connectionHandler(ctx, conn, myMetadata) 49 } 50 51 func (w *myUpstreamHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error { 52 myMetadata := w.metadata 53 if metadata.Source.IsValid() { 54 myMetadata.Source = metadata.Source 55 } 56 if metadata.Destination.IsValid() { 57 myMetadata.Destination = metadata.Destination 58 } 59 return w.packetHandler(ctx, conn, myMetadata) 60 } 61 62 func (w *myUpstreamHandlerWrapper) NewError(ctx context.Context, err error) { 63 w.errorHandler.NewError(ctx, err) 64 } 65 66 func UpstreamMetadata(metadata InboundContext) M.Metadata { 67 return M.Metadata{ 68 Source: metadata.Source, 69 Destination: metadata.Destination, 70 } 71 } 72 73 type myUpstreamContextHandlerWrapper struct { 74 connectionHandler ConnectionHandlerFunc 75 packetHandler PacketConnectionHandlerFunc 76 errorHandler E.Handler 77 } 78 79 func NewUpstreamContextHandler( 80 connectionHandler ConnectionHandlerFunc, 81 packetHandler PacketConnectionHandlerFunc, 82 errorHandler E.Handler, 83 ) UpstreamHandlerAdapter { 84 return &myUpstreamContextHandlerWrapper{ 85 connectionHandler: connectionHandler, 86 packetHandler: packetHandler, 87 errorHandler: errorHandler, 88 } 89 } 90 91 func (w *myUpstreamContextHandlerWrapper) NewConnection(ctx context.Context, conn net.Conn, 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.connectionHandler(ctx, conn, *myMetadata) 100 } 101 102 func (w *myUpstreamContextHandlerWrapper) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata M.Metadata) error { 103 myMetadata := ContextFrom(ctx) 104 if metadata.Source.IsValid() { 105 myMetadata.Source = metadata.Source 106 } 107 if metadata.Destination.IsValid() { 108 myMetadata.Destination = metadata.Destination 109 } 110 return w.packetHandler(ctx, conn, *myMetadata) 111 } 112 113 func (w *myUpstreamContextHandlerWrapper) NewError(ctx context.Context, err error) { 114 w.errorHandler.NewError(ctx, err) 115 }