context.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package proxy
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. )
  6. type key int
  7. const (
  8. dialerKey key = iota
  9. sourceKey
  10. destinationKey
  11. originalDestinationKey
  12. inboundDestinationKey
  13. inboundTagKey
  14. outboundTagKey
  15. resolvedIPsKey
  16. allowPassiveConnKey
  17. )
  18. func ContextWithDialer(ctx context.Context, dialer Dialer) context.Context {
  19. return context.WithValue(ctx, dialerKey, dialer)
  20. }
  21. func DialerFromContext(ctx context.Context) Dialer {
  22. v := ctx.Value(dialerKey)
  23. if v == nil {
  24. return nil
  25. }
  26. return v.(Dialer)
  27. }
  28. func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
  29. return context.WithValue(ctx, sourceKey, src)
  30. }
  31. func SourceFromContext(ctx context.Context) net.Destination {
  32. v := ctx.Value(sourceKey)
  33. if v == nil {
  34. return net.Destination{}
  35. }
  36. return v.(net.Destination)
  37. }
  38. func ContextWithOriginalDestination(ctx context.Context, dest net.Destination) context.Context {
  39. return context.WithValue(ctx, originalDestinationKey, dest)
  40. }
  41. func OriginalDestinationFromContext(ctx context.Context) net.Destination {
  42. v := ctx.Value(originalDestinationKey)
  43. if v == nil {
  44. return net.Destination{}
  45. }
  46. return v.(net.Destination)
  47. }
  48. func ContextWithDestination(ctx context.Context, dest net.Destination) context.Context {
  49. return context.WithValue(ctx, destinationKey, dest)
  50. }
  51. func DestinationFromContext(ctx context.Context) net.Destination {
  52. v := ctx.Value(destinationKey)
  53. if v == nil {
  54. return net.Destination{}
  55. }
  56. return v.(net.Destination)
  57. }
  58. func ContextWithInboundDestination(ctx context.Context, dest net.Destination) context.Context {
  59. return context.WithValue(ctx, inboundDestinationKey, dest)
  60. }
  61. func InboundDestinationFromContext(ctx context.Context) net.Destination {
  62. v := ctx.Value(inboundDestinationKey)
  63. if v == nil {
  64. return net.Destination{}
  65. }
  66. return v.(net.Destination)
  67. }
  68. func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
  69. return context.WithValue(ctx, inboundTagKey, tag)
  70. }
  71. func InboundTagFromContext(ctx context.Context) string {
  72. v := ctx.Value(inboundTagKey)
  73. if v == nil {
  74. return ""
  75. }
  76. return v.(string)
  77. }
  78. func ContextWithOutboundTag(ctx context.Context, tag string) context.Context {
  79. return context.WithValue(ctx, outboundTagKey, tag)
  80. }
  81. func OutboundTagFromContext(ctx context.Context) string {
  82. v := ctx.Value(outboundTagKey)
  83. if v == nil {
  84. return ""
  85. }
  86. return v.(string)
  87. }
  88. func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
  89. return context.WithValue(ctx, resolvedIPsKey, ips)
  90. }
  91. func ResolvedIPsFromContext(ctx context.Context) ([]net.Address, bool) {
  92. ips, ok := ctx.Value(resolvedIPsKey).([]net.Address)
  93. return ips, ok
  94. }
  95. func ContextWithAllowPassiveConnection(ctx context.Context, allowPassiveConnection bool) context.Context {
  96. return context.WithValue(ctx, allowPassiveConnKey, allowPassiveConnection)
  97. }
  98. func AllowPassiveConnectionFromContext(ctx context.Context) (bool, bool) {
  99. allow, ok := ctx.Value(allowPassiveConnKey).(bool)
  100. return allow, ok
  101. }