config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package internet
  2. import (
  3. "errors"
  4. "v2ray.com/core/common/log"
  5. v2net "v2ray.com/core/common/net"
  6. v2tls "v2ray.com/core/transport/internet/tls"
  7. "github.com/golang/protobuf/proto"
  8. "github.com/golang/protobuf/ptypes"
  9. )
  10. type NetworkConfigCreator func() proto.Message
  11. var (
  12. globalNetworkConfigCreatorCache = make(map[v2net.Network]NetworkConfigCreator)
  13. globalSecurityConfigCreatorCache = make(map[SecurityType]NetworkConfigCreator)
  14. globalNetworkSettings []*NetworkSettings
  15. ErrUnconfiguredNetwork = errors.New("Network config creator not set.")
  16. )
  17. func RegisterNetworkConfigCreator(network v2net.Network, creator NetworkConfigCreator) error {
  18. // TODO: check duplicate
  19. globalNetworkConfigCreatorCache[network] = creator
  20. return nil
  21. }
  22. func CreateNetworkConfig(network v2net.Network) (proto.Message, error) {
  23. creator, ok := globalNetworkConfigCreatorCache[network]
  24. if !ok {
  25. log.Warning("Internet: Network config creator not found: ", network)
  26. return nil, ErrUnconfiguredNetwork
  27. }
  28. return creator(), nil
  29. }
  30. func RegisterSecurityConfigCreator(securityType SecurityType, creator NetworkConfigCreator) error {
  31. globalSecurityConfigCreatorCache[securityType] = creator
  32. return nil
  33. }
  34. func CreateSecurityConfig(securityType SecurityType) (proto.Message, error) {
  35. creator, ok := globalSecurityConfigCreatorCache[securityType]
  36. if !ok {
  37. log.Warning("Internet: Security config creator not found: ", securityType)
  38. return nil, ErrUnconfiguredNetwork
  39. }
  40. return creator(), nil
  41. }
  42. func (this *NetworkSettings) GetTypedSettings() (interface{}, error) {
  43. message, err := CreateNetworkConfig(this.Network)
  44. if err != nil {
  45. return nil, err
  46. }
  47. if err := ptypes.UnmarshalAny(this.Settings, message); err != nil {
  48. return nil, err
  49. }
  50. return message, nil
  51. }
  52. func (this *SecuritySettings) GetTypeSettings() (interface{}, error) {
  53. message, err := CreateSecurityConfig(this.Type)
  54. if err != nil {
  55. return nil, err
  56. }
  57. if err := ptypes.UnmarshalAny(this.Settings, message); err != nil {
  58. return nil, err
  59. }
  60. return message, nil
  61. }
  62. func (this *StreamConfig) GetEffectiveNetworkSettings() (interface{}, error) {
  63. for _, settings := range this.NetworkSettings {
  64. if settings.Network == this.Network {
  65. return settings.GetTypedSettings()
  66. }
  67. }
  68. for _, settings := range globalNetworkSettings {
  69. if settings.Network == this.Network {
  70. return settings.GetTypedSettings()
  71. }
  72. }
  73. return CreateNetworkConfig(this.Network)
  74. }
  75. func (this *StreamConfig) GetEffectiveSecuritySettings() (interface{}, error) {
  76. for _, settings := range this.SecuritySettings {
  77. if settings.Type == this.SecurityType {
  78. return settings.GetTypeSettings()
  79. }
  80. }
  81. return CreateSecurityConfig(this.SecurityType)
  82. }
  83. func ApplyGlobalNetworkSettings(settings []*NetworkSettings) error {
  84. globalNetworkSettings = settings
  85. return nil
  86. }
  87. func init() {
  88. RegisterSecurityConfigCreator(SecurityType_TLS, func() proto.Message {
  89. return new(v2tls.Config)
  90. })
  91. }