config.go 2.3 KB

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