config.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package internet
  2. import (
  3. "github.com/xtls/xray-core/common/errors"
  4. "github.com/xtls/xray-core/common/serial"
  5. "github.com/xtls/xray-core/features"
  6. )
  7. type ConfigCreator func() interface{}
  8. var (
  9. globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
  10. globalTransportSettings []*TransportConfig
  11. )
  12. var strategy = [][]byte{
  13. // name strategy, prefer, fallback
  14. {0, 0, 0}, // AsIs none, /, /
  15. {1, 0, 0}, // UseIP use, both, none
  16. {1, 4, 0}, // UseIPv4 use, 4, none
  17. {1, 6, 0}, // UseIPv6 use, 6, none
  18. {1, 4, 6}, // UseIPv4v6 use, 4, 6
  19. {1, 6, 4}, // UseIPv6v4 use, 6, 4
  20. {2, 0, 0}, // ForceIP force, both, none
  21. {2, 4, 0}, // ForceIPv4 force, 4, none
  22. {2, 6, 0}, // ForceIPv6 force, 6, none
  23. {2, 4, 6}, // ForceIPv4v6 force, 4, 6
  24. {2, 6, 4}, // ForceIPv6v4 force, 6, 4
  25. }
  26. const unknownProtocol = "unknown"
  27. func transportProtocolToString(protocol TransportProtocol) string {
  28. switch protocol {
  29. case TransportProtocol_TCP:
  30. return "tcp"
  31. case TransportProtocol_UDP:
  32. return "udp"
  33. case TransportProtocol_HTTP:
  34. return "http"
  35. case TransportProtocol_MKCP:
  36. return "mkcp"
  37. case TransportProtocol_WebSocket:
  38. return "websocket"
  39. case TransportProtocol_DomainSocket:
  40. return "domainsocket"
  41. case TransportProtocol_HTTPUpgrade:
  42. return "httpupgrade"
  43. default:
  44. return unknownProtocol
  45. }
  46. }
  47. func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
  48. if _, found := globalTransportConfigCreatorCache[name]; found {
  49. return errors.New("protocol ", name, " is already registered").AtError()
  50. }
  51. globalTransportConfigCreatorCache[name] = creator
  52. return nil
  53. }
  54. // Note: Each new transport needs to add init() func in transport/internet/xxx/config.go
  55. // Otherwise, it will cause #3244
  56. func CreateTransportConfig(name string) (interface{}, error) {
  57. creator, ok := globalTransportConfigCreatorCache[name]
  58. if !ok {
  59. return nil, errors.New("unknown transport protocol: ", name)
  60. }
  61. return creator(), nil
  62. }
  63. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  64. return c.Settings.GetInstance()
  65. }
  66. func (c *TransportConfig) GetUnifiedProtocolName() string {
  67. if len(c.ProtocolName) > 0 {
  68. return c.ProtocolName
  69. }
  70. return transportProtocolToString(c.Protocol)
  71. }
  72. func (c *StreamConfig) GetEffectiveProtocol() string {
  73. if c == nil {
  74. return "tcp"
  75. }
  76. if len(c.ProtocolName) > 0 {
  77. return c.ProtocolName
  78. }
  79. return transportProtocolToString(c.Protocol)
  80. }
  81. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  82. protocol := c.GetEffectiveProtocol()
  83. return c.GetTransportSettingsFor(protocol)
  84. }
  85. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  86. if c != nil {
  87. for _, settings := range c.TransportSettings {
  88. if settings.GetUnifiedProtocolName() == protocol {
  89. return settings.GetTypedSettings()
  90. }
  91. }
  92. }
  93. for _, settings := range globalTransportSettings {
  94. if settings.GetUnifiedProtocolName() == protocol {
  95. return settings.GetTypedSettings()
  96. }
  97. }
  98. return CreateTransportConfig(protocol)
  99. }
  100. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  101. for _, settings := range c.SecuritySettings {
  102. if settings.Type == c.SecurityType {
  103. return settings.GetInstance()
  104. }
  105. }
  106. return serial.GetInstance(c.SecurityType)
  107. }
  108. func (c *StreamConfig) HasSecuritySettings() bool {
  109. return len(c.SecurityType) > 0
  110. }
  111. func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
  112. features.PrintDeprecatedFeatureWarning("global transport settings")
  113. globalTransportSettings = settings
  114. return nil
  115. }
  116. func (c *ProxyConfig) HasTag() bool {
  117. return c != nil && len(c.Tag) > 0
  118. }
  119. func (m SocketConfig_TProxyMode) IsEnabled() bool {
  120. return m != SocketConfig_Off
  121. }
  122. func (s DomainStrategy) hasStrategy() bool {
  123. return strategy[s][0] != 0
  124. }
  125. func (s DomainStrategy) forceIP() bool {
  126. return strategy[s][0] == 2
  127. }
  128. func (s DomainStrategy) preferIP4() bool {
  129. return strategy[s][1] == 4 || strategy[s][1] == 0
  130. }
  131. func (s DomainStrategy) preferIP6() bool {
  132. return strategy[s][1] == 6 || strategy[s][1] == 0
  133. }
  134. func (s DomainStrategy) hasFallback() bool {
  135. return strategy[s][2] != 0
  136. }
  137. func (s DomainStrategy) fallbackIP4() bool {
  138. return strategy[s][2] == 4
  139. }
  140. func (s DomainStrategy) fallbackIP6() bool {
  141. return strategy[s][2] == 6
  142. }