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 (
|
2017-01-12 11:54:34 +00:00
|
|
|
globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
|
2017-01-16 13:18:33 +00:00
|
|
|
globalTransportSettings []*TransportConfig
|
2016-10-02 21:43:58 +00:00
|
|
|
)
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
|
2016-10-02 21:43:58 +00:00
|
|
|
// TODO: check duplicate
|
2017-01-12 11:54:34 +00:00
|
|
|
globalTransportConfigCreatorCache[protocol] = creator
|
2016-10-02 21:43:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-12 11:54:34 +00:00
|
|
|
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-01-16 13:18:33 +00:00
|
|
|
func (v *TransportConfig) GetTypedSettings() (interface{}, error) {
|
2016-11-27 20:39:09 +00:00
|
|
|
return v.Settings.GetInstance()
|
2016-10-02 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-12 11:54:34 +00: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 21:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-12 11:54:34 +00:00
|
|
|
|
|
|
|
for _, settings := range globalTransportSettings {
|
|
|
|
if settings.Protocol == protocol {
|
2016-10-02 21:43:58 +00:00
|
|
|
return settings.GetTypedSettings()
|
|
|
|
}
|
|
|
|
}
|
2017-01-12 11:54:34 +00:00
|
|
|
return CreateTransportConfig(protocol)
|
2016-10-02 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 19:46:44 +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)
|
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
|
|
|
|
for _, settings := range v.SecuritySettings {
|
|
|
|
if settings.Type == v.SecurityType {
|
2016-10-16 12:22:21 +00:00
|
|
|
return settings.GetInstance()
|
2016-10-02 21:43:58 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-15 10:51:09 +00:00
|
|
|
return serial.GetInstance(v.SecurityType)
|
2016-10-16 12:22:21 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *StreamConfig) HasSecuritySettings() bool {
|
|
|
|
return len(v.SecurityType) > 0
|
2016-10-02 21:43:58 +00:00
|
|
|
}
|
|
|
|
|
2017-01-16 13:18:33 +00:00
|
|
|
func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
|
2017-01-12 11:54:34 +00:00
|
|
|
globalTransportSettings = settings
|
2016-10-02 21:43:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-11-10 22:41:28 +00:00
|
|
|
|
2016-11-27 20:39:09 +00:00
|
|
|
func (v *ProxyConfig) HasTag() bool {
|
|
|
|
return v != nil && len(v.Tag) > 0
|
2016-11-10 22:41:28 +00:00
|
|
|
}
|