v2fly/transport/internet/config.go

125 lines
3.0 KiB
Go
Raw Normal View History

2016-10-02 21:43:58 +00:00
package internet
import (
2021-02-16 20:31:50 +00:00
"github.com/v2fly/v2ray-core/v4/common/serial"
"github.com/v2fly/v2ray-core/v4/features"
)
2016-10-02 21:43:58 +00:00
2016-10-18 13:31:48 +00:00
type ConfigCreator func() interface{}
2016-10-02 21:43:58 +00:00
var (
2018-08-06 11:48:35 +00:00
globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
globalTransportSettings []*TransportConfig
2016-10-02 21:43:58 +00:00
)
2018-08-06 11:48:35 +00: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 19:33:58 +00:00
func RegisterProtocolConfigCreator(name string, creator ConfigCreator) error {
2018-08-06 11:48:35 +00:00
if _, found := globalTransportConfigCreatorCache[name]; found {
return newError("protocol ", name, " is already registered").AtError()
}
globalTransportConfigCreatorCache[name] = creator
2016-10-02 21:43:58 +00:00
return nil
}
2019-01-06 19:33:58 +00:00
func CreateTransportConfig(name string) (interface{}, error) {
2018-08-06 11:48:35 +00:00
creator, ok := globalTransportConfigCreatorCache[name]
2016-10-02 21:43:58 +00:00
if !ok {
2018-08-06 11:48:35 +00:00
return nil, newError("unknown transport protocol: ", name)
2016-10-02 21:43:58 +00:00
}
return creator(), nil
}
2017-10-24 18:25:45 +00:00
func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
2021-06-19 13:36:54 +00:00
return serial.GetInstanceOf(c.Settings)
2016-10-02 21:43:58 +00:00
}
2018-08-06 11:48:35 +00:00
func (c *TransportConfig) GetUnifiedProtocolName() string {
if len(c.ProtocolName) > 0 {
return c.ProtocolName
}
2018-08-06 11:48:35 +00:00
return transportProtocolToString(c.Protocol)
}
2018-08-06 11:48:35 +00:00
func (c *StreamConfig) GetEffectiveProtocol() string {
if c == nil {
return "tcp"
2016-10-02 21:43:58 +00:00
}
2018-08-06 11:48:35 +00:00
if len(c.ProtocolName) > 0 {
return c.ProtocolName
2016-10-02 21:43:58 +00:00
}
2018-08-06 11:48:35 +00:00
return transportProtocolToString(c.Protocol)
}
func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := c.GetEffectiveProtocol()
return c.GetTransportSettingsFor(protocol)
2016-10-02 21:43:58 +00:00
}
2018-08-06 11:48:35 +00:00
func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
if c != nil {
for _, settings := range c.TransportSettings {
2018-08-06 11:48:35 +00:00
if settings.GetUnifiedProtocolName() == protocol {
return settings.GetTypedSettings()
}
}
}
for _, settings := range globalTransportSettings {
2018-08-06 11:48:35 +00:00
if settings.GetUnifiedProtocolName() == protocol {
return settings.GetTypedSettings()
}
}
2019-01-06 19:33:58 +00:00
return CreateTransportConfig(protocol)
}
2017-10-24 18:25:45 +00:00
func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range c.SecuritySettings {
2021-06-19 13:36:54 +00:00
if serial.V2Type(settings) == c.SecurityType {
return serial.GetInstanceOf(settings)
2016-10-02 21:43:58 +00:00
}
}
2017-10-24 18:25:45 +00:00
return serial.GetInstance(c.SecurityType)
2016-10-16 12:22:21 +00:00
}
2017-10-24 18:25:45 +00:00
func (c *StreamConfig) HasSecuritySettings() bool {
return len(c.SecurityType) > 0
2016-10-02 21:43:58 +00:00
}
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
features.PrintDeprecatedFeatureWarning("global transport settings")
globalTransportSettings = settings
2016-10-02 21:43:58 +00:00
return nil
}
2016-11-10 22:41:28 +00:00
2017-10-24 18:25:45 +00:00
func (c *ProxyConfig) HasTag() bool {
return c != nil && len(c.Tag) > 0
2016-11-10 22:41:28 +00:00
}
2018-09-17 13:12:58 +00:00
func (m SocketConfig_TProxyMode) IsEnabled() bool {
return m != SocketConfig_Off
}