inbound.go 1.3 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. DomainStrategy dns.DomainStrategy
  27. SniffEnabled bool
  28. SniffOverrideDestination bool
  29. DestinationAddresses []netip.Addr
  30. SourceGeoIPCode string
  31. GeoIPCode string
  32. ProcessInfo *process.Info
  33. }
  34. type inboundContextKey struct{}
  35. func WithContext(ctx context.Context, inboundContext *InboundContext) context.Context {
  36. return context.WithValue(ctx, (*inboundContextKey)(nil), inboundContext)
  37. }
  38. func ContextFrom(ctx context.Context) *InboundContext {
  39. metadata := ctx.Value((*inboundContextKey)(nil))
  40. if metadata == nil {
  41. return nil
  42. }
  43. return metadata.(*InboundContext)
  44. }
  45. func AppendContext(ctx context.Context) (context.Context, *InboundContext) {
  46. metadata := ContextFrom(ctx)
  47. if metadata != nil {
  48. return ctx, metadata
  49. }
  50. metadata = new(InboundContext)
  51. return WithContext(ctx, metadata), metadata
  52. }