context.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package proxy
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. )
  6. type key int
  7. const (
  8. sourceKey key = iota
  9. targetKey
  10. originalTargetKey
  11. inboundEntryPointKey
  12. inboundTagKey
  13. resolvedIPsKey
  14. )
  15. func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
  16. return context.WithValue(ctx, sourceKey, src)
  17. }
  18. func SourceFromContext(ctx context.Context) (net.Destination, bool) {
  19. v, ok := ctx.Value(sourceKey).(net.Destination)
  20. return v, ok
  21. }
  22. func ContextWithOriginalTarget(ctx context.Context, dest net.Destination) context.Context {
  23. return context.WithValue(ctx, originalTargetKey, dest)
  24. }
  25. func OriginalTargetFromContext(ctx context.Context) (net.Destination, bool) {
  26. v, ok := ctx.Value(originalTargetKey).(net.Destination)
  27. return v, ok
  28. }
  29. func ContextWithTarget(ctx context.Context, dest net.Destination) context.Context {
  30. return context.WithValue(ctx, targetKey, dest)
  31. }
  32. func TargetFromContext(ctx context.Context) (net.Destination, bool) {
  33. v, ok := ctx.Value(targetKey).(net.Destination)
  34. return v, ok
  35. }
  36. func ContextWithInboundEntryPoint(ctx context.Context, dest net.Destination) context.Context {
  37. return context.WithValue(ctx, inboundEntryPointKey, dest)
  38. }
  39. func InboundEntryPointFromContext(ctx context.Context) (net.Destination, bool) {
  40. v, ok := ctx.Value(inboundEntryPointKey).(net.Destination)
  41. return v, ok
  42. }
  43. func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
  44. return context.WithValue(ctx, inboundTagKey, tag)
  45. }
  46. func InboundTagFromContext(ctx context.Context) (string, bool) {
  47. v, ok := ctx.Value(inboundTagKey).(string)
  48. return v, ok
  49. }
  50. func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
  51. return context.WithValue(ctx, resolvedIPsKey, ips)
  52. }
  53. func ResolvedIPsFromContext(ctx context.Context) ([]net.Address, bool) {
  54. ips, ok := ctx.Value(resolvedIPsKey).([]net.Address)
  55. return ips, ok
  56. }