2016-10-02 17:43:58 -04:00
|
|
|
package internet
|
|
|
|
|
2017-04-08 19:43:25 -04:00
|
|
|
import "v2ray.com/core/common/serial"
|
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)
|
2017-01-16 08:18:33 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:54:34 -05:00
|
|
|
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
|
2018-08-06 07:48:35 -04:00
|
|
|
name := transportProtocolToString(protocol)
|
|
|
|
if name == unknownProtocol {
|
|
|
|
return newError("protocol ", TransportProtocol_name[int32(protocol)], " is not supported").AtError()
|
2018-03-06 02:24:52 -05:00
|
|
|
}
|
2018-08-06 07:48:35 -04:00
|
|
|
return RegisterProtocolConfigCreatorByName(name, creator)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterProtocolConfigCreatorByName(name string, creator ConfigCreator) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-12 06:54:34 -05:00
|
|
|
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
|
2018-08-06 07:48:35 -04:00
|
|
|
name := transportProtocolToString(protocol)
|
|
|
|
if name == unknownProtocol {
|
|
|
|
return nil, newError("protocol ", TransportProtocol_name[int32(protocol)], " is not supported").AtError()
|
|
|
|
}
|
|
|
|
return CreateTransportConfigByName(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTransportConfigByName(name string) (interface{}, error) {
|
|
|
|
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
|
2017-01-12 06:54:34 -05:00
|
|
|
}
|
|
|
|
|
2018-08-06 07:48:35 -04:00
|
|
|
return transportProtocolToString(c.Protocol)
|
|
|
|
}
|
2017-01-12 06:54:34 -05:00
|
|
|
|
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
|
|
|
}
|
2017-01-12 06:54:34 -05: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) {
|
2017-01-26 14:46:44 -05:00
|
|
|
if c != nil {
|
|
|
|
for _, settings := range c.TransportSettings {
|
2018-08-06 07:48:35 -04:00
|
|
|
if settings.GetUnifiedProtocolName() == protocol {
|
2017-01-26 14:46:44 -05:00
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, settings := range globalTransportSettings {
|
2018-08-06 07:48:35 -04:00
|
|
|
if settings.GetUnifiedProtocolName() == protocol {
|
2017-01-26 14:46:44 -05:00
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-06 07:48:35 -04:00
|
|
|
return CreateTransportConfigByName(protocol)
|
2017-01-26 14:46:44 -05:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-16 08:18:33 -05:00
|
|
|
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
|
2017-01-12 06:54:34 -05:00
|
|
|
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
|
|
|
|
}
|