1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-30 15:56:28 -04:00
v2fly/transport/internet/config.go

80 lines
1.9 KiB
Go
Raw Normal View History

2016-10-02 17:43:58 -04:00
package internet
import (
2016-12-04 03:10:47 -05:00
"v2ray.com/core/common/errors"
2016-12-21 09:37:16 -05:00
"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 (
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
globalTransportSettings []*TransportSettings
2016-10-02 17:43:58 -04:00
)
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
2016-10-02 17:43:58 -04:00
// TODO: check duplicate
globalTransportConfigCreatorCache[protocol] = creator
2016-10-02 17:43:58 -04:00
return nil
}
func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
creator, ok := globalTransportConfigCreatorCache[protocol]
2016-10-02 17:43:58 -04:00
if !ok {
return nil, errors.New("Internet: Unknown transport protocol: ", protocol)
2016-10-02 17:43:58 -04:00
}
return creator(), nil
}
func (v *TransportSettings) GetTypedSettings() (interface{}, error) {
2016-11-27 15:39:09 -05:00
return v.Settings.GetInstance()
2016-10-02 17:43:58 -04:00
}
func (v *StreamConfig) GetEffectiveProtocol() TransportProtocol {
if v == nil {
return TransportProtocol_TCP
}
return v.Protocol
}
func (v *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
protocol := v.GetEffectiveProtocol()
if v != nil {
for _, settings := range v.TransportSettings {
if settings.Protocol == protocol {
return settings.GetTypedSettings()
}
2016-10-02 17:43:58 -04:00
}
}
for _, settings := range globalTransportSettings {
if settings.Protocol == protocol {
2016-10-02 17:43:58 -04:00
return settings.GetTypedSettings()
}
}
return CreateTransportConfig(protocol)
2016-10-02 17:43:58 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
for _, settings := range v.SecuritySettings {
if settings.Type == v.SecurityType {
2016-10-16 08:22:21 -04:00
return settings.GetInstance()
2016-10-02 17:43:58 -04:00
}
}
2016-12-15 05:51:09 -05:00
return serial.GetInstance(v.SecurityType)
2016-10-16 08:22:21 -04:00
}
2016-11-27 15:39:09 -05:00
func (v *StreamConfig) HasSecuritySettings() bool {
return len(v.SecurityType) > 0
2016-10-02 17:43:58 -04:00
}
func ApplyGlobalTransportSettings(settings []*TransportSettings) error {
globalTransportSettings = settings
2016-10-02 17:43:58 -04:00
return nil
}
2016-11-10 17:41:28 -05:00
2016-11-27 15:39:09 -05:00
func (v *ProxyConfig) HasTag() bool {
return v != nil && len(v.Tag) > 0
2016-11-10 17:41:28 -05:00
}