github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/adapter/inbound.go (about) 1 package adapter 2 3 import ( 4 "context" 5 "net" 6 "net/netip" 7 8 "github.com/inazumav/sing-box/common/process" 9 "github.com/inazumav/sing-box/option" 10 M "github.com/sagernet/sing/common/metadata" 11 N "github.com/sagernet/sing/common/network" 12 ) 13 14 type Inbound interface { 15 Service 16 Type() string 17 Tag() string 18 } 19 20 type InjectableInbound interface { 21 Inbound 22 Network() []string 23 NewConnection(ctx context.Context, conn net.Conn, metadata InboundContext) error 24 NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata InboundContext) error 25 } 26 27 type InboundContext struct { 28 Inbound string 29 InboundType string 30 IPVersion uint8 31 Network string 32 Source M.Socksaddr 33 Destination M.Socksaddr 34 Domain string 35 Protocol string 36 User string 37 Outbound string 38 39 // cache 40 41 InboundDetour string 42 LastInbound string 43 OriginDestination M.Socksaddr 44 InboundOptions option.InboundOptions 45 DestinationAddresses []netip.Addr 46 SourceGeoIPCode string 47 GeoIPCode string 48 ProcessInfo *process.Info 49 FakeIP bool 50 51 // dns cache 52 53 QueryType uint16 54 } 55 56 type inboundContextKey struct{} 57 58 func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context { 59 return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext) 60 } 61 62 func ContextFrom(ctx context.Context) *InboundContext { 63 metadata := ctx.Value((*inboundContextKey)(nil)) 64 if metadata == nil { 65 return nil 66 } 67 return metadata.(*InboundContext) 68 } 69 70 func AppendContext(ctx context.Context) (context.Context, *InboundContext) { 71 metadata := ContextFrom(ctx) 72 if metadata != nil { 73 return ctx, metadata 74 } 75 metadata = new(InboundContext) 76 return WithContext(ctx, metadata), metadata 77 }