config.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package internet
  2. import (
  3. "errors"
  4. "v2ray.com/core/common/loader"
  5. "v2ray.com/core/common/log"
  6. v2net "v2ray.com/core/common/net"
  7. )
  8. type ConfigCreator func() interface{}
  9. var (
  10. globalNetworkConfigCreatorCache = make(map[v2net.Network]ConfigCreator)
  11. globalNetworkSettings []*NetworkSettings
  12. ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
  13. )
  14. func RegisterNetworkConfigCreator(network v2net.Network, creator ConfigCreator) error {
  15. // TODO: check duplicate
  16. globalNetworkConfigCreatorCache[network] = creator
  17. return nil
  18. }
  19. func CreateNetworkConfig(network v2net.Network) (interface{}, error) {
  20. creator, ok := globalNetworkConfigCreatorCache[network]
  21. if !ok {
  22. log.Warning("Internet: Network config creator not found: ", network)
  23. return nil, ErrUnconfiguredNetwork
  24. }
  25. return creator(), nil
  26. }
  27. func (this *NetworkSettings) GetTypedSettings() (interface{}, error) {
  28. return this.Settings.GetInstance()
  29. }
  30. func (this *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
  31. for _, settings := range this.NetworkSettings {
  32. if settings.Network == this.Network {
  33. return settings.GetTypedSettings()
  34. }
  35. }
  36. for _, settings := range globalNetworkSettings {
  37. if settings.Network == this.Network {
  38. return settings.GetTypedSettings()
  39. }
  40. }
  41. return CreateNetworkConfig(this.Network)
  42. }
  43. func (this *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  44. for _, settings := range this.SecuritySettings {
  45. if settings.Type == this.SecurityType {
  46. return settings.GetInstance()
  47. }
  48. }
  49. return loader.GetInstance(this.SecurityType)
  50. }
  51. func (this *StreamConfig) HasSecuritySettings() bool {
  52. return len(this.SecurityType) > 0
  53. }
  54. func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
  55. globalNetworkSettings = settings
  56. return nil
  57. }
  58. func (this *ProxyConfig) HasTag() bool {
  59. return this != nil && len(this.Tag) > 0
  60. }