config.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package internet
  2. import (
  3. "github.com/xtls/xray-core/common/errors"
  4. "github.com/xtls/xray-core/common/serial"
  5. )
  6. type ConfigCreator func() interface{}
  7. var (
  8. globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
  9. )
  10. var strategy = [][]byte{
  11. // name strategy, prefer, fallback
  12. {0, 0, 0}, // AsIs none, /, /
  13. {1, 0, 0}, // UseIP use, both, none
  14. {1, 4, 0}, // UseIPv4 use, 4, none
  15. {1, 6, 0}, // UseIPv6 use, 6, none
  16. {1, 4, 6}, // UseIPv4v6 use, 4, 6
  17. {1, 6, 4}, // UseIPv6v4 use, 6, 4
  18. {2, 0, 0}, // ForceIP force, both, none
  19. {2, 4, 0}, // ForceIPv4 force, 4, none
  20. {2, 6, 0}, // ForceIPv6 force, 6, none
  21. {2, 4, 6}, // ForceIPv4v6 force, 4, 6
  22. {2, 6, 4}, // ForceIPv6v4 force, 6, 4
  23. }
  24. const unknownProtocol = "unknown"
  25. func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
  26. if _, found := globalTransportConfigCreatorCache[name]; found {
  27. return errors.New("protocol ", name, " is already registered").AtError()
  28. }
  29. globalTransportConfigCreatorCache[name] = creator
  30. return nil
  31. }
  32. // Note: Each new transport needs to add init() func in transport/internet/xxx/config.go
  33. // Otherwise, it will cause #3244
  34. func CreateTransportConfig(name string) (interface{}, error) {
  35. creator, ok := globalTransportConfigCreatorCache[name]
  36. if !ok {
  37. return nil, errors.New("unknown transport protocol: ", name)
  38. }
  39. return creator(), nil
  40. }
  41. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  42. return c.Settings.GetInstance()
  43. }
  44. func (c *TransportConfig) GetUnifiedProtocolName() string {
  45. return c.ProtocolName
  46. }
  47. func (c *StreamConfig) GetEffectiveProtocol() string {
  48. if c == nil || len(c.ProtocolName) == 0 {
  49. return "tcp"
  50. }
  51. return c.ProtocolName
  52. }
  53. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  54. protocol := c.GetEffectiveProtocol()
  55. return c.GetTransportSettingsFor(protocol)
  56. }
  57. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  58. if c != nil {
  59. for _, settings := range c.TransportSettings {
  60. if settings.GetUnifiedProtocolName() == protocol {
  61. return settings.GetTypedSettings()
  62. }
  63. }
  64. }
  65. return CreateTransportConfig(protocol)
  66. }
  67. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  68. for _, settings := range c.SecuritySettings {
  69. if settings.Type == c.SecurityType {
  70. return settings.GetInstance()
  71. }
  72. }
  73. return serial.GetInstance(c.SecurityType)
  74. }
  75. func (c *StreamConfig) HasSecuritySettings() bool {
  76. return len(c.SecurityType) > 0
  77. }
  78. func (c *ProxyConfig) HasTag() bool {
  79. return c != nil && len(c.Tag) > 0
  80. }
  81. func (m SocketConfig_TProxyMode) IsEnabled() bool {
  82. return m != SocketConfig_Off
  83. }
  84. func (s DomainStrategy) hasStrategy() bool {
  85. return strategy[s][0] != 0
  86. }
  87. func (s DomainStrategy) forceIP() bool {
  88. return strategy[s][0] == 2
  89. }
  90. func (s DomainStrategy) preferIP4() bool {
  91. return strategy[s][1] == 4 || strategy[s][1] == 0
  92. }
  93. func (s DomainStrategy) preferIP6() bool {
  94. return strategy[s][1] == 6 || strategy[s][1] == 0
  95. }
  96. func (s DomainStrategy) hasFallback() bool {
  97. return strategy[s][2] != 0
  98. }
  99. func (s DomainStrategy) fallbackIP4() bool {
  100. return strategy[s][2] == 4
  101. }
  102. func (s DomainStrategy) fallbackIP6() bool {
  103. return strategy[s][2] == 6
  104. }