inbound.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. IPVersion int
  18. Network string
  19. Source M.Socksaddr
  20. Destination M.Socksaddr
  21. Domain string
  22. Protocol string
  23. User string
  24. Outbound string
  25. // cache
  26. OriginDestination M.Socksaddr
  27. DomainStrategy dns.DomainStrategy
  28. SniffEnabled bool
  29. SniffOverrideDestination bool
  30. DestinationAddresses []netip.Addr
  31. SourceGeoIPCode string
  32. GeoIPCode string
  33. ProcessInfo *process.Info
  34. }
  35. type inboundContextKey struct{}
  36. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  37. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  38. }
  39. func ContextFrom(ctx context.Context) *InboundContext {
  40. metadata := ctx.Value((*inboundContextKey)(nil))
  41. if metadata == nil {
  42. return nil
  43. }
  44. return metadata.(*InboundContext)
  45. }
  46. func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
  47. metadata := ContextFrom(ctx)
  48. if metadata != nil {
  49. return ctx, metadata
  50. }
  51. metadata = new(InboundContext)
  52. return WithContext(ctx, metadata), metadata
  53. }