config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package internet
  2. import (
  3. "v2ray.com/core/common/serial"
  4. "v2ray.com/core/features"
  5. )
  6. type ConfigCreator func() interface{}
  7. var (
  8. globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
  9. globalTransportSettings []*TransportConfig
  10. )
  11. const unknownProtocol = "unknown"
  12. func transportProtocolToString(protocol TransportProtocol) string {
  13. switch protocol {
  14. case TransportProtocol_TCP:
  15. return "tcp"
  16. case TransportProtocol_UDP:
  17. return "udp"
  18. case TransportProtocol_HTTP:
  19. return "http"
  20. case TransportProtocol_MKCP:
  21. return "mkcp"
  22. case TransportProtocol_WebSocket:
  23. return "websocket"
  24. case TransportProtocol_DomainSocket:
  25. return "domainsocket"
  26. default:
  27. return unknownProtocol
  28. }
  29. }
  30. func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
  31. if _, found := globalTransportConfigCreatorCache[name]; found {
  32. return newError("protocol ", name, " is already registered").AtError()
  33. }
  34. globalTransportConfigCreatorCache[name] = creator
  35. return nil
  36. }
  37. func CreateTransportConfig(name string) (interface{}, error) {
  38. creator, ok := globalTransportConfigCreatorCache[name]
  39. if !ok {
  40. return nil, newError("unknown transport protocol: ", name)
  41. }
  42. return creator(), nil
  43. }
  44. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  45. return c.Settings.GetInstance()
  46. }
  47. func (c *TransportConfig) GetUnifiedProtocolName() string {
  48. if len(c.ProtocolName) > 0 {
  49. return c.ProtocolName
  50. }
  51. return transportProtocolToString(c.Protocol)
  52. }
  53. func (c *StreamConfig) GetEffectiveProtocol() string {
  54. if c == nil {
  55. return "tcp"
  56. }
  57. if len(c.ProtocolName) > 0 {
  58. return c.ProtocolName
  59. }
  60. return transportProtocolToString(c.Protocol)
  61. }
  62. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  63. protocol := c.GetEffectiveProtocol()
  64. return c.GetTransportSettingsFor(protocol)
  65. }
  66. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  67. if c != nil {
  68. for _, settings := range c.TransportSettings {
  69. if settings.GetUnifiedProtocolName() == protocol {
  70. return settings.GetTypedSettings()
  71. }
  72. }
  73. }
  74. for _, settings := range globalTransportSettings {
  75. if settings.GetUnifiedProtocolName() == protocol {
  76. return settings.GetTypedSettings()
  77. }
  78. }
  79. return CreateTransportConfig(protocol)
  80. }
  81. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  82. for _, settings := range c.SecuritySettings {
  83. if settings.Type == c.SecurityType {
  84. return settings.GetInstance()
  85. }
  86. }
  87. return serial.GetInstance(c.SecurityType)
  88. }
  89. func (c *StreamConfig) HasSecuritySettings() bool {
  90. return len(c.SecurityType) > 0
  91. }
  92. func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
  93. features.PrintDeprecatedFeatureWarning("global transport settings")
  94. globalTransportSettings = settings
  95. return nil
  96. }
  97. func (c *ProxyConfig) HasTag() bool {
  98. return c != nil && len(c.Tag) > 0
  99. }
  100. func (m SocketConfig_TProxyMode) IsEnabled() bool {
  101. return m != SocketConfig_Off
  102. }