2016-10-02 23:43:58 +02:00
|
|
|
package internet
|
|
|
|
|
2017-04-09 01:43:25 +02:00
|
|
|
import "v2ray.com/core/common/serial"
|
2016-10-02 23:43:58 +02:00
|
|
|
|
2016-10-18 15:31:48 +02:00
|
|
|
type ConfigCreator func() interface{}
|
|
|
|
|
2016-10-02 23:43:58 +02:00
|
|
|
var (
|
2017-01-12 12:54:34 +01:00
|
|
|
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
|
2017-01-16 14:18:33 +01:00
|
|
|
globalTransportSettings []*TransportConfig
|
2016-10-02 23:43:58 +02:00
|
|
|
)
|
|
|
|
|
2017-01-12 12:54:34 +01:00
|
|
|
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
|
2018-03-06 15:24:52 +08:00
|
|
|
if _, found := globalTransportConfigCreatorCache[protocol]; found {
|
2018-03-08 21:07:01 +01:00
|
|
|
return newError("protocol ", TransportProtocol_name[int32(protocol)], " is already registered").AtError()
|
2018-03-06 15:24:52 +08:00
|
|
|
}
|
2017-01-12 12:54:34 +01:00
|
|
|
globalTransportConfigCreatorCache[protocol] = creator
|
2016-10-02 23:43:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 12:54:34 +01:00
|
|
|
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
|
|
|
|
creator, ok := globalTransportConfigCreatorCache[protocol]
|
2016-10-02 23:43:58 +02:00
|
|
|
if !ok {
|
2017-04-09 15:04:04 +02:00
|
|
|
return nil, newError("unknown transport protocol: ", protocol)
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
return creator(), nil
|
|
|
|
}
|
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
|
|
|
|
return c.Settings.GetInstance()
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *StreamConfig) GetEffectiveProtocol() TransportProtocol {
|
|
|
|
if c == nil {
|
2017-01-12 12:54:34 +01:00
|
|
|
return TransportProtocol_TCP
|
|
|
|
}
|
2017-10-24 20:25:45 +02:00
|
|
|
return c.Protocol
|
2017-01-12 12:54:34 +01:00
|
|
|
}
|
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
|
|
|
|
protocol := c.GetEffectiveProtocol()
|
2017-01-12 12:54:34 +01:00
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
if c != nil {
|
|
|
|
for _, settings := range c.TransportSettings {
|
2017-01-12 12:54:34 +01:00
|
|
|
if settings.Protocol == protocol {
|
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 12:54:34 +01:00
|
|
|
|
|
|
|
for _, settings := range globalTransportSettings {
|
|
|
|
if settings.Protocol == protocol {
|
2016-10-02 23:43:58 +02:00
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 12:54:34 +01:00
|
|
|
return CreateTransportConfig(protocol)
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:46:44 +01:00
|
|
|
func (c *StreamConfig) GetTransportSettingsFor(protocol TransportProtocol) (interface{}, error) {
|
|
|
|
if c != nil {
|
|
|
|
for _, settings := range c.TransportSettings {
|
|
|
|
if settings.Protocol == protocol {
|
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, settings := range globalTransportSettings {
|
|
|
|
if settings.Protocol == protocol {
|
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CreateTransportConfig(protocol)
|
|
|
|
}
|
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
|
|
|
|
for _, settings := range c.SecuritySettings {
|
|
|
|
if settings.Type == c.SecurityType {
|
2016-10-16 14:22:21 +02:00
|
|
|
return settings.GetInstance()
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 20:25:45 +02:00
|
|
|
return serial.GetInstance(c.SecurityType)
|
2016-10-16 14:22:21 +02:00
|
|
|
}
|
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *StreamConfig) HasSecuritySettings() bool {
|
|
|
|
return len(c.SecurityType) > 0
|
2016-10-02 23:43:58 +02:00
|
|
|
}
|
|
|
|
|
2017-01-16 14:18:33 +01:00
|
|
|
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
|
2017-01-12 12:54:34 +01:00
|
|
|
globalTransportSettings = settings
|
2016-10-02 23:43:58 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-10 23:41:28 +01:00
|
|
|
|
2017-10-24 20:25:45 +02:00
|
|
|
func (c *ProxyConfig) HasTag() bool {
|
|
|
|
return c != nil && len(c.Tag) > 0
|
2016-11-10 23:41:28 +01:00
|
|
|
}
|