config.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package internet
  2. import (
  3. "v2ray.com/core/common/errors"
  4. "v2ray.com/core/common/serial"
  5. )
  6. type ConfigCreator func() interface{}
  7. var (
  8. globalTransportConfigCreatorCache = make(map[TransportProtocol]ConfigCreator)
  9. globalTransportSettings []*TransportConfig
  10. )
  11. func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
  12. // TODO: check duplicate
  13. globalTransportConfigCreatorCache[protocol] = creator
  14. return nil
  15. }
  16. func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
  17. creator, ok := globalTransportConfigCreatorCache[protocol]
  18. if !ok {
  19. return nil, errors.New("Internet: Unknown transport protocol: ", protocol)
  20. }
  21. return creator(), nil
  22. }
  23. func (v *TransportConfig) GetTypedSettings() (interface{}, error) {
  24. return v.Settings.GetInstance()
  25. }
  26. func (v *StreamConfig) GetEffectiveProtocol() TransportProtocol {
  27. if v == nil {
  28. return TransportProtocol_TCP
  29. }
  30. return v.Protocol
  31. }
  32. func (v *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  33. protocol := v.GetEffectiveProtocol()
  34. if v != nil {
  35. for _, settings := range v.TransportSettings {
  36. if settings.Protocol == protocol {
  37. return settings.GetTypedSettings()
  38. }
  39. }
  40. }
  41. for _, settings := range globalTransportSettings {
  42. if settings.Protocol == protocol {
  43. return settings.GetTypedSettings()
  44. }
  45. }
  46. return CreateTransportConfig(protocol)
  47. }
  48. func (c *StreamConfig) GetTransportSettingsFor(protocol TransportProtocol) (interface{}, error) {
  49. if c != nil {
  50. for _, settings := range c.TransportSettings {
  51. if settings.Protocol == protocol {
  52. return settings.GetTypedSettings()
  53. }
  54. }
  55. }
  56. for _, settings := range globalTransportSettings {
  57. if settings.Protocol == protocol {
  58. return settings.GetTypedSettings()
  59. }
  60. }
  61. return CreateTransportConfig(protocol)
  62. }
  63. func (v *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  64. for _, settings := range v.SecuritySettings {
  65. if settings.Type == v.SecurityType {
  66. return settings.GetInstance()
  67. }
  68. }
  69. return serial.GetInstance(v.SecurityType)
  70. }
  71. func (v *StreamConfig) HasSecuritySettings() bool {
  72. return len(v.SecurityType) > 0
  73. }
  74. func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
  75. globalTransportSettings = settings
  76. return nil
  77. }
  78. func (v *ProxyConfig) HasTag() bool {
  79. return v != nil && len(v.Tag) > 0
  80. }