1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-03 06:30:42 +00:00
v2fly/transport/internet/config.go

97 lines
2.4 KiB
Go
Raw Normal View History

2016-10-02 21:43:58 +00:00
package internet
2017-04-08 23:43:25 +00:00
import "v2ray.com/core/common/serial"
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 (
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
globalTransportSettings []*TransportConfig
2016-10-02 21:43:58 +00:00
)
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
2018-03-06 07:24:52 +00:00
if _, found := globalTransportConfigCreatorCache[protocol]; found {
2018-03-08 20:07:01 +00:00
return newError("protocol ", TransportProtocol_name[int32(protocol)], " is already registered").AtError()
2018-03-06 07:24:52 +00:00
}
globalTransportConfigCreatorCache[protocol] = creator
2016-10-02 21:43:58 +00:00
return nil
}
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
creator, ok := globalTransportConfigCreatorCache[protocol]
2016-10-02 21:43:58 +00:00
if !ok {
2017-04-09 13:04:04 +00:00
return nil, newError("unknown transport protocol: ", protocol)
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) {
return c.Settings.GetInstance()
2016-10-02 21:43:58 +00:00
}
2017-10-24 18:25:45 +00:00
func (c *StreamConfig) GetEffectiveProtocol() TransportProtocol {
if c == nil {
return TransportProtocol_TCP
}
2017-10-24 18:25:45 +00:00
return c.Protocol
}
2017-10-24 18:25:45 +00:00
func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := c.GetEffectiveProtocol()
2017-10-24 18:25:45 +00:00
if c != nil {
for _, settings := range c.TransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
2016-10-02 21:43:58 +00:00
}
}
for _, settings := range globalTransportSettings {
if settings.Protocol == protocol {
2016-10-02 21:43:58 +00:00
return settings.GetTypedSettings()
}
}
return CreateTransportConfig(protocol)
2016-10-02 21:43:58 +00: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 18:25:45 +00:00
func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range c.SecuritySettings {
if settings.Type == c.SecurityType {
2016-10-16 12:22:21 +00:00
return settings.GetInstance()
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 {
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
}