context.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package internet
  2. import (
  3. "context"
  4. "v2ray.com/core/common/net"
  5. )
  6. type key int
  7. const (
  8. streamSettingsKey key = iota
  9. dialerSrcKey
  10. transportSettingsKey
  11. securitySettingsKey
  12. )
  13. func ContextWithStreamSettings(ctx context.Context, streamSettings *StreamConfig) context.Context {
  14. return context.WithValue(ctx, streamSettingsKey, streamSettings)
  15. }
  16. func StreamSettingsFromContext(ctx context.Context) (*StreamConfig, bool) {
  17. ss, ok := ctx.Value(streamSettingsKey).(*StreamConfig)
  18. return ss, ok
  19. }
  20. func ContextWithDialerSource(ctx context.Context, addr net.Address) context.Context {
  21. return context.WithValue(ctx, dialerSrcKey, addr)
  22. }
  23. func DialerSourceFromContext(ctx context.Context) net.Address {
  24. if addr, ok := ctx.Value(dialerSrcKey).(net.Address); ok {
  25. return addr
  26. }
  27. return net.AnyIP
  28. }
  29. func ContextWithTransportSettings(ctx context.Context, transportSettings interface{}) context.Context {
  30. return context.WithValue(ctx, transportSettingsKey, transportSettings)
  31. }
  32. func TransportSettingsFromContext(ctx context.Context) interface{} {
  33. return ctx.Value(transportSettingsKey)
  34. }
  35. func ContextWithSecuritySettings(ctx context.Context, securitySettings interface{}) context.Context {
  36. return context.WithValue(ctx, securitySettingsKey, securitySettings)
  37. }
  38. func SecuritySettingsFromContext(ctx context.Context) interface{} {
  39. return ctx.Value(securitySettingsKey)
  40. }