1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-09 03:37:37 -05:00
v2fly/transport/internet/config.go

125 lines
2.9 KiB
Go
Raw Normal View History

2016-10-02 17:43:58 -04:00
package internet
import (
"v2ray.com/core/common/serial"
"v2ray.com/core/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
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) {
return c.Settings.GetInstance()
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 {
if settings.Type == c.SecurityType {
2016-10-16 08:22:21 -04:00
return settings.GetInstance()
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
}