1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-14 08:15:55 -05:00
v2fly/transport/internet/config.go

145 lines
3.6 KiB
Go
Raw Normal View History

2016-10-02 17:43:58 -04:00
package internet
import (
2021-09-05 05:18:47 -04:00
"context"
2021-10-28 06:34:19 -04:00
2021-09-07 11:53:09 -04:00
"github.com/golang/protobuf/proto"
2021-10-28 06:34:19 -04:00
"github.com/v2fly/v2ray-core/v5/common"
"github.com/v2fly/v2ray-core/v5/common/protoext"
"github.com/v2fly/v2ray-core/v5/common/serial"
"github.com/v2fly/v2ray-core/v5/features"
)
2016-10-02 17:43:58 -04:00
2016-10-18 09:31:48 -04:00
type ConfigCreator func() interface{}
2016-10-02 17:43:58 -04:00
var (
2018-08-06 07:48:35 -04:00
globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
globalTransportSettings []*TransportConfig
2016-10-02 17:43:58 -04:00
)
2018-08-06 07:48:35 -04:00
const unknownProtocol = "unknown"
func transportProtocolToString(protocol TransportProtocol) string {
switch protocol {
case TransportProtocol_TCP:
return "tcp"
case TransportProtocol_UDP:
return "udp"
case TransportProtocol_HTTP:
return "http"
case TransportProtocol_MKCP:
return "mkcp"
case TransportProtocol_WebSocket:
return "websocket"
case TransportProtocol_DomainSocket:
return "domainsocket"
default:
return unknownProtocol
}
}
2019-01-06 14:33:58 -05:00
func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
2018-08-06 07:48:35 -04:00
if _, found := globalTransportConfigCreatorCache[name]; found {
return newError("protocol ", name, " is already registered").AtError()
}
globalTransportConfigCreatorCache[name] = creator
2021-09-05 05:18:47 -04:00
common.RegisterConfig(creator(), func(ctx context.Context, config interface{}) (interface{}, error) {
return nil, newError("transport config should use CreateTransportConfig instead")
})
2016-10-02 17:43:58 -04:00
return nil
}
2019-01-06 14:33:58 -05:00
func CreateTransportConfig(name string) (interface{}, error) {
2018-08-06 07:48:35 -04:00
creator, ok := globalTransportConfigCreatorCache[name]
2016-10-02 17:43:58 -04:00
if !ok {
2018-08-06 07:48:35 -04:00
return nil, newError("unknown transport protocol: ", name)
2016-10-02 17:43:58 -04:00
}
return creator(), nil
}
2017-10-24 14:25:45 -04:00
func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
2021-06-19 09:36:54 -04:00
return serial.GetInstanceOf(c.Settings)
2016-10-02 17:43:58 -04:00
}
2018-08-06 07:48:35 -04:00
func (c *TransportConfig) GetUnifiedProtocolName() string {
if len(c.ProtocolName) > 0 {
return c.ProtocolName
}
2018-08-06 07:48:35 -04:00
return transportProtocolToString(c.Protocol)
}
2018-08-06 07:48:35 -04:00
func (c *StreamConfig) GetEffectiveProtocol() string {
if c == nil {
return "tcp"
2016-10-02 17:43:58 -04:00
}
2018-08-06 07:48:35 -04:00
if len(c.ProtocolName) > 0 {
return c.ProtocolName
2016-10-02 17:43:58 -04:00
}
2018-08-06 07:48:35 -04:00
return transportProtocolToString(c.Protocol)
}
func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := c.GetEffectiveProtocol()
return c.GetTransportSettingsFor(protocol)
2016-10-02 17:43:58 -04:00
}
2018-08-06 07:48:35 -04:00
func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
if c != nil {
for _, settings := range c.TransportSettings {
2018-08-06 07:48:35 -04:00
if settings.GetUnifiedProtocolName() == protocol {
return settings.GetTypedSettings()
}
}
}
for _, settings := range globalTransportSettings {
2018-08-06 07:48:35 -04:00
if settings.GetUnifiedProtocolName() == protocol {
return settings.GetTypedSettings()
}
}
2019-01-06 14:33:58 -05:00
return CreateTransportConfig(protocol)
}
2017-10-24 14:25:45 -04:00
func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range c.SecuritySettings {
2021-06-19 09:36:54 -04:00
if serial.V2Type(settings) == c.SecurityType {
return serial.GetInstanceOf(settings)
2016-10-02 17:43:58 -04:00
}
}
2017-10-24 14:25:45 -04:00
return serial.GetInstance(c.SecurityType)
2016-10-16 08:22:21 -04:00
}
2017-10-24 14:25:45 -04:00
func (c *StreamConfig) HasSecuritySettings() bool {
return len(c.SecurityType) > 0
2016-10-02 17:43:58 -04:00
}
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
features.PrintDeprecatedFeatureWarning("global transport settings")
globalTransportSettings = settings
2016-10-02 17:43:58 -04:00
return nil
}
2016-11-10 17:41:28 -05:00
2017-10-24 14:25:45 -04:00
func (c *ProxyConfig) HasTag() bool {
return c != nil && len(c.Tag) > 0
2016-11-10 17:41:28 -05:00
}
2018-09-17 09:12:58 -04:00
func (m SocketConfig_TProxyMode) IsEnabled() bool {
return m != SocketConfig_Off
}
2021-09-07 11:53:09 -04:00
func getOriginalMessageName(streamSettings *MemoryStreamConfig) string {
msgOpts, err := protoext.GetMessageOptions(proto.MessageV2(streamSettings.ProtocolSettings).ProtoReflect().Descriptor())
if err == nil {
if msgOpts.TransportOriginalName != "" {
return msgOpts.TransportOriginalName
}
}
return ""
}