config.go 4.2 KB

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