context.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. destinationKey
  10. originalDestinationKey
  11. inboundDestinationKey
  12. inboundTagKey
  13. outboundTagKey
  14. resolvedIPsKey
  15. allowPassiveConnKey
  16. dispatcherKey
  17. )
  18. func ContextWithSource(ctx context.Context, src net.Destination) context.Context {
  19. return context.WithValue(ctx, sourceKey, src)
  20. }
  21. func SourceFromContext(ctx context.Context) net.Destination {
  22. v := ctx.Value(sourceKey)
  23. if v == nil {
  24. return net.Destination{}
  25. }
  26. return v.(net.Destination)
  27. }
  28. func ContextWithOriginalDestination(ctx context.Context, dest net.Destination) context.Context {
  29. return context.WithValue(ctx, originalDestinationKey, dest)
  30. }
  31. func OriginalDestinationFromContext(ctx context.Context) net.Destination {
  32. v := ctx.Value(originalDestinationKey)
  33. if v == nil {
  34. return net.Destination{}
  35. }
  36. return v.(net.Destination)
  37. }
  38. func ContextWithDestination(ctx context.Context, dest net.Destination) context.Context {
  39. return context.WithValue(ctx, destinationKey, dest)
  40. }
  41. func DestinationFromContext(ctx context.Context) net.Destination {
  42. v := ctx.Value(destinationKey)
  43. if v == nil {
  44. return net.Destination{}
  45. }
  46. return v.(net.Destination)
  47. }
  48. func ContextWithInboundDestination(ctx context.Context, dest net.Destination) context.Context {
  49. return context.WithValue(ctx, inboundDestinationKey, dest)
  50. }
  51. func InboundDestinationFromContext(ctx context.Context) net.Destination {
  52. v := ctx.Value(inboundDestinationKey)
  53. if v == nil {
  54. return net.Destination{}
  55. }
  56. return v.(net.Destination)
  57. }
  58. func ContextWithInboundTag(ctx context.Context, tag string) context.Context {
  59. return context.WithValue(ctx, inboundTagKey, tag)
  60. }
  61. func InboundTagFromContext(ctx context.Context) string {
  62. v := ctx.Value(inboundTagKey)
  63. if v == nil {
  64. return ""
  65. }
  66. return v.(string)
  67. }
  68. func ContextWithOutboundTag(ctx context.Context, tag string) context.Context {
  69. return context.WithValue(ctx, outboundTagKey, tag)
  70. }
  71. func OutboundTagFromContext(ctx context.Context) string {
  72. v := ctx.Value(outboundTagKey)
  73. if v == nil {
  74. return ""
  75. }
  76. return v.(string)
  77. }
  78. func ContextWithResolveIPs(ctx context.Context, ips []net.Address) context.Context {
  79. return context.WithValue(ctx, resolvedIPsKey, ips)
  80. }
  81. func ResolvedIPsFromContext(ctx context.Context) ([]net.Address, bool) {
  82. ips, ok := ctx.Value(resolvedIPsKey).([]net.Address)
  83. return ips, ok
  84. }