github.com/moqsien/xraycore@v1.8.5/transport/internet/config.go (about) 1 package internet 2 3 import ( 4 "github.com/moqsien/xraycore/common/serial" 5 "github.com/moqsien/xraycore/features" 6 ) 7 8 type ConfigCreator func() interface{} 9 10 var ( 11 globalTransportConfigCreatorCache = make(map[string]ConfigCreator) 12 globalTransportSettings []*TransportConfig 13 ) 14 15 const unknownProtocol = "unknown" 16 17 func transportProtocolToString(protocol TransportProtocol) string { 18 switch protocol { 19 case TransportProtocol_TCP: 20 return "tcp" 21 case TransportProtocol_UDP: 22 return "udp" 23 case TransportProtocol_HTTP: 24 return "http" 25 case TransportProtocol_MKCP: 26 return "mkcp" 27 case TransportProtocol_WebSocket: 28 return "websocket" 29 case TransportProtocol_DomainSocket: 30 return "domainsocket" 31 default: 32 return unknownProtocol 33 } 34 } 35 36 func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error { 37 if _, found := globalTransportConfigCreatorCache[name]; found { 38 return newError("protocol ", name, " is already registered").AtError() 39 } 40 globalTransportConfigCreatorCache[name] = creator 41 return nil 42 } 43 44 func CreateTransportConfig(name string) (interface{}, error) { 45 creator, ok := globalTransportConfigCreatorCache[name] 46 if !ok { 47 return nil, newError("unknown transport protocol: ", name) 48 } 49 return creator(), nil 50 } 51 52 func (c *TransportConfig) GetTypedSettings() (interface{}, error) { 53 return c.Settings.GetInstance() 54 } 55 56 func (c *TransportConfig) GetUnifiedProtocolName() string { 57 if len(c.ProtocolName) > 0 { 58 return c.ProtocolName 59 } 60 61 return transportProtocolToString(c.Protocol) 62 } 63 64 func (c *StreamConfig) GetEffectiveProtocol() string { 65 if c == nil { 66 return "tcp" 67 } 68 69 if len(c.ProtocolName) > 0 { 70 return c.ProtocolName 71 } 72 73 return transportProtocolToString(c.Protocol) 74 } 75 76 func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) { 77 protocol := c.GetEffectiveProtocol() 78 return c.GetTransportSettingsFor(protocol) 79 } 80 81 func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) { 82 if c != nil { 83 for _, settings := range c.TransportSettings { 84 if settings.GetUnifiedProtocolName() == protocol { 85 return settings.GetTypedSettings() 86 } 87 } 88 } 89 90 for _, settings := range globalTransportSettings { 91 if settings.GetUnifiedProtocolName() == protocol { 92 return settings.GetTypedSettings() 93 } 94 } 95 96 return CreateTransportConfig(protocol) 97 } 98 99 func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) { 100 for _, settings := range c.SecuritySettings { 101 if settings.Type == c.SecurityType { 102 return settings.GetInstance() 103 } 104 } 105 return serial.GetInstance(c.SecurityType) 106 } 107 108 func (c *StreamConfig) HasSecuritySettings() bool { 109 return len(c.SecurityType) > 0 110 } 111 112 func ApplyGlobalTransportSettings(settings []*TransportConfig) error { 113 features.PrintDeprecatedFeatureWarning("global transport settings") 114 globalTransportSettings = settings 115 return nil 116 } 117 118 func (c *ProxyConfig) HasTag() bool { 119 return c != nil && len(c.Tag) > 0 120 } 121 122 func (m SocketConfig_TProxyMode) IsEnabled() bool { 123 return m != SocketConfig_Off 124 }