config.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package internet
  2. import (
  3. "github.com/xtls/xray-core/common/errors"
  4. "github.com/xtls/xray-core/common/net"
  5. "github.com/xtls/xray-core/common/serial"
  6. )
  7. type ConfigCreator func() interface{}
  8. var (
  9. globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
  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 RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
  27. if _, found := globalTransportConfigCreatorCache[name]; found {
  28. return errors.New("protocol ", name, " is already registered").AtError()
  29. }
  30. globalTransportConfigCreatorCache[name] = creator
  31. return nil
  32. }
  33. // Note: Each new transport needs to add init() func in transport/internet/xxx/config.go
  34. // Otherwise, it will cause #3244
  35. func CreateTransportConfig(name string) (interface{}, error) {
  36. creator, ok := globalTransportConfigCreatorCache[name]
  37. if !ok {
  38. return nil, errors.New("unknown transport protocol: ", name)
  39. }
  40. return creator(), nil
  41. }
  42. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  43. return c.Settings.GetInstance()
  44. }
  45. func (c *TransportConfig) GetUnifiedProtocolName() string {
  46. return c.ProtocolName
  47. }
  48. func (c *StreamConfig) GetEffectiveProtocol() string {
  49. if c == nil || len(c.ProtocolName) == 0 {
  50. return "tcp"
  51. }
  52. return c.ProtocolName
  53. }
  54. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  55. protocol := c.GetEffectiveProtocol()
  56. return c.GetTransportSettingsFor(protocol)
  57. }
  58. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  59. if c != nil {
  60. for _, settings := range c.TransportSettings {
  61. if settings.GetUnifiedProtocolName() == protocol {
  62. return settings.GetTypedSettings()
  63. }
  64. }
  65. }
  66. return CreateTransportConfig(protocol)
  67. }
  68. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  69. for _, settings := range c.SecuritySettings {
  70. if settings.Type == c.SecurityType {
  71. return settings.GetInstance()
  72. }
  73. }
  74. return serial.GetInstance(c.SecurityType)
  75. }
  76. func (c *StreamConfig) HasSecuritySettings() bool {
  77. return len(c.SecurityType) > 0
  78. }
  79. func (c *ProxyConfig) HasTag() bool {
  80. return c != nil && len(c.Tag) > 0
  81. }
  82. func (m SocketConfig_TProxyMode) IsEnabled() bool {
  83. return m != SocketConfig_Off
  84. }
  85. func (s DomainStrategy) HasStrategy() bool {
  86. return strategy[s][0] != 0
  87. }
  88. func (s DomainStrategy) ForceIP() bool {
  89. return strategy[s][0] == 2
  90. }
  91. func (s DomainStrategy) PreferIP4() bool {
  92. return strategy[s][1] == 4 || strategy[s][1] == 0
  93. }
  94. func (s DomainStrategy) PreferIP6() bool {
  95. return strategy[s][1] == 6 || strategy[s][1] == 0
  96. }
  97. func (s DomainStrategy) HasFallback() bool {
  98. return strategy[s][2] != 0
  99. }
  100. func (s DomainStrategy) FallbackIP4() bool {
  101. return strategy[s][2] == 4
  102. }
  103. func (s DomainStrategy) FallbackIP6() bool {
  104. return strategy[s][2] == 6
  105. }
  106. func (s DomainStrategy) GetDynamicStrategy(addrFamily net.AddressFamily) DomainStrategy {
  107. if addrFamily.IsDomain(){
  108. return s
  109. }
  110. switch s {
  111. case DomainStrategy_USE_IP:
  112. if addrFamily.IsIPv4() {
  113. return DomainStrategy_USE_IP46
  114. } else if addrFamily.IsIPv6() {
  115. return DomainStrategy_USE_IP64
  116. }
  117. case DomainStrategy_FORCE_IP:
  118. if addrFamily.IsIPv4() {
  119. return DomainStrategy_FORCE_IP46
  120. } else if addrFamily.IsIPv6() {
  121. return DomainStrategy_FORCE_IP64
  122. }
  123. default:
  124. }
  125. return s
  126. }