inbound.go 1.2 KB

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