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