inbound.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package adapter
  2. import (
  3. "context"
  4. "net/netip"
  5. "github.com/sagernet/sing-box/common/process"
  6. "github.com/sagernet/sing-dns"
  7. M "github.com/sagernet/sing/common/metadata"
  8. )
  9. type Inbound interface {
  10. Service
  11. Type() string
  12. Tag() string
  13. }
  14. type InboundContext struct {
  15. Inbound string
  16. InboundType string
  17. Network string
  18. Source M.Socksaddr
  19. Destination M.Socksaddr
  20. Domain string
  21. Protocol string
  22. User string
  23. Outbound string
  24. // cache
  25. DomainStrategy dns.DomainStrategy
  26. SniffEnabled bool
  27. SniffOverrideDestination bool
  28. DestinationAddresses []netip.Addr
  29. SourceGeoIPCode string
  30. GeoIPCode string
  31. ProcessInfo *process.Info
  32. }
  33. type inboundContextKey struct{}
  34. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  35. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  36. }
  37. func ContextFrom(ctx context.Context) *InboundContext {
  38. metadata := ctx.Value((*inboundContextKey)(nil))
  39. if metadata == nil {
  40. return nil
  41. }
  42. return metadata.(*InboundContext)
  43. }
  44. func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
  45. metadata := ContextFrom(ctx)
  46. if metadata != nil {
  47. return ctx, metadata
  48. }
  49. metadata = new(InboundContext)
  50. return WithContext(ctx, metadata), metadata
  51. }