config.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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(name string, creator ConfigCreator) error {
  28. if _, found := globalTransportConfigCreatorCache[name]; found {
  29. return newError("protocol ", name, " is already registered").AtError()
  30. }
  31. globalTransportConfigCreatorCache[name] = creator
  32. return nil
  33. }
  34. func CreateTransportConfig(name string) (interface{}, error) {
  35. creator, ok := globalTransportConfigCreatorCache[name]
  36. if !ok {
  37. return nil, newError("unknown transport protocol: ", name)
  38. }
  39. return creator(), nil
  40. }
  41. func (c *TransportConfig) GetTypedSettings() (interface{}, error) {
  42. return c.Settings.GetInstance()
  43. }
  44. func (c *TransportConfig) GetUnifiedProtocolName() string {
  45. if len(c.ProtocolName) > 0 {
  46. return c.ProtocolName
  47. }
  48. return transportProtocolToString(c.Protocol)
  49. }
  50. func (c *StreamConfig) GetEffectiveProtocol() string {
  51. if c == nil {
  52. return "tcp"
  53. }
  54. if len(c.ProtocolName) > 0 {
  55. return c.ProtocolName
  56. }
  57. return transportProtocolToString(c.Protocol)
  58. }
  59. func (c *StreamConfig) GetEffectiveTransportSettings() (interface{}, error) {
  60. protocol := c.GetEffectiveProtocol()
  61. return c.GetTransportSettingsFor(protocol)
  62. }
  63. func (c *StreamConfig) GetTransportSettingsFor(protocol string) (interface{}, error) {
  64. if c != nil {
  65. for _, settings := range c.TransportSettings {
  66. if settings.GetUnifiedProtocolName() == protocol {
  67. return settings.GetTypedSettings()
  68. }
  69. }
  70. }
  71. for _, settings := range globalTransportSettings {
  72. if settings.GetUnifiedProtocolName() == protocol {
  73. return settings.GetTypedSettings()
  74. }
  75. }
  76. return CreateTransportConfig(protocol)
  77. }
  78. func (c *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  79. for _, settings := range c.SecuritySettings {
  80. if settings.Type == c.SecurityType {
  81. return settings.GetInstance()
  82. }
  83. }
  84. return serial.GetInstance(c.SecurityType)
  85. }
  86. func (c *StreamConfig) HasSecuritySettings() bool {
  87. return len(c.SecurityType) > 0
  88. }
  89. func ApplyGlobalTransportSettings(settings []*TransportConfig) error {
  90. globalTransportSettings = settings
  91. return nil
  92. }
  93. func (c *ProxyConfig) HasTag() bool {
  94. return c != nil && len(c.Tag) > 0
  95. }
  96. func (m SocketConfig_TProxyMode) IsEnabled() bool {
  97. return m != SocketConfig_Off
  98. }