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