config.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package internet
  2. import "v2ray.com/core/common/serial"
  3. type ConfigCreator func() interface{}
  4. var (
  5. globalTransportConfigCreatorCache = make(map[string]ConfigCreator)
  6. globalTransportSettings []*TransportConfig
  7. )
  8. const unknownProtocol = "unknown"
  9. func transportProtocolToString(protocol TransportProtocol) string {
  10. switch protocol {
  11. case TransportProtocol_TCP:
  12. return "tcp"
  13. case TransportProtocol_UDP:
  14. return "udp"
  15. case TransportProtocol_HTTP:
  16. return "http"
  17. case TransportProtocol_MKCP:
  18. return "mkcp"
  19. case TransportProtocol_WebSocket:
  20. return "websocket"
  21. case TransportProtocol_DomainSocket:
  22. return "domainsocket"
  23. default:
  24. return unknownProtocol
  25. }
  26. }
  27. func RegisterProtocolConfigCreator(protocol TransportProtocol, creator ConfigCreator) error {
  28. name := transportProtocolToString(protocol)
  29. if name == unknownProtocol {
  30. return newError("protocol ", TransportProtocol_name[int32(protocol)], " is not supported").AtError()
  31. }
  32. return RegisterProtocolConfigCreatorByName(name, creator)
  33. }
  34. func RegisterProtocolConfigCreatorByName(name string, creator ConfigCreator) error {
  35. if _, found := globalTransportConfigCreatorCache[name]; found {
  36. return newError("protocol ", name, " is already registered").AtError()
  37. }
  38. globalTransportConfigCreatorCache[name] = creator
  39. return nil
  40. }
  41. func CreateTransportConfig(protocol TransportProtocol) (interface{}, error) {
  42. name := transportProtocolToString(protocol)
  43. if name == unknownProtocol {
  44. return nil, newError("protocol ", TransportProtocol_name[int32(protocol)], " is not supported").AtError()
  45. }
  46. return CreateTransportConfigByName(name)
  47. }
  48. func CreateTransportConfigByName(name string) (interface{}, error) {
  49. creator, ok := globalTransportConfigCreatorCache[name]
  50. if !ok {
  51. return nil, newError("unknown transport protocol: ", name)
  52. }
  53. return creator(), nil
  54. }
  55. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  56. return c.Settings.GetInstance()
  57. }
  58. func (c *TransportConfig) GetUnifiedProtocolName() string {
  59. if len(c.ProtocolName) > 0 {
  60. return c.ProtocolName
  61. }
  62. return transportProtocolToString(c.Protocol)
  63. }
  64. func (c *StreamConfig) GetEffectiveProtocol() string {
  65. if c == nil {
  66. return "tcp"
  67. }
  68. if len(c.ProtocolName) > 0 {
  69. return c.ProtocolName
  70. }
  71. return transportProtocolToString(c.Protocol)
  72. }
  73. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  74. protocol := c.GetEffectiveProtocol()
  75. return c.GetTransportSettingsFor(protocol)
  76. }
  77. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  78. if c != nil {
  79. for _, settings := range c.TransportSettings {
  80. if settings.GetUnifiedProtocolName() == protocol {
  81. return settings.GetTypedSettings()
  82. }
  83. }
  84. }
  85. for _, settings := range globalTransportSettings {
  86. if settings.GetUnifiedProtocolName() == protocol {
  87. return settings.GetTypedSettings()
  88. }
  89. }
  90. return CreateTransportConfigByName(protocol)
  91. }
  92. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  93. for _, settings := range c.SecuritySettings {
  94. if settings.Type == c.SecurityType {
  95. return settings.GetInstance()
  96. }
  97. }
  98. return serial.GetInstance(c.SecurityType)
  99. }
  100. func (c *StreamConfig) HasSecuritySettings() bool {
  101. return len(c.SecurityType) > 0
  102. }
  103. func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
  104. globalTransportSettings = settings
  105. return nil
  106. }
  107. func (c *ProxyConfig) HasTag() bool {
  108. return c != nil && len(c.Tag) > 0
  109. }