config_json.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // +build json
  2. package inbound
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/log"
  8. "v2ray.com/core/common/protocol"
  9. "v2ray.com/core/proxy/registry"
  10. "v2ray.com/core/proxy/vmess"
  11. "github.com/golang/protobuf/ptypes"
  12. )
  13. func (this *DetourConfig) UnmarshalJSON(data []byte) error {
  14. type JsonDetourConfig struct {
  15. ToTag string `json:"to"`
  16. }
  17. jsonConfig := new(JsonDetourConfig)
  18. if err := json.Unmarshal(data, jsonConfig); err != nil {
  19. return errors.New("VMess|Inbound: Failed to parse detour config: " + err.Error())
  20. }
  21. this.ToTag = jsonConfig.ToTag
  22. return nil
  23. }
  24. func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
  25. type JsonFeaturesConfig struct {
  26. Detour *DetourConfig `json:"detour"`
  27. }
  28. jsonConfig := new(JsonFeaturesConfig)
  29. if err := json.Unmarshal(data, jsonConfig); err != nil {
  30. return errors.New("VMess|Inbound: Failed to parse features config: " + err.Error())
  31. }
  32. this.Detour = jsonConfig.Detour
  33. return nil
  34. }
  35. func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
  36. type JsonDefaultConfig struct {
  37. AlterIDs uint16 `json:"alterId"`
  38. Level byte `json:"level"`
  39. }
  40. jsonConfig := new(JsonDefaultConfig)
  41. if err := json.Unmarshal(data, jsonConfig); err != nil {
  42. return errors.New("VMess|Inbound: Failed to parse default config: " + err.Error())
  43. }
  44. this.AlterIDs = jsonConfig.AlterIDs
  45. if this.AlterIDs == 0 {
  46. this.AlterIDs = 32
  47. }
  48. this.Level = uint32(jsonConfig.Level)
  49. return nil
  50. }
  51. func (this *Config) UnmarshalJSON(data []byte) error {
  52. type JsonConfig struct {
  53. Users []json.RawMessage `json:"clients"`
  54. Features *FeaturesConfig `json:"features"`
  55. Defaults *DefaultConfig `json:"default"`
  56. DetourConfig *DetourConfig `json:"detour"`
  57. }
  58. jsonConfig := new(JsonConfig)
  59. if err := json.Unmarshal(data, jsonConfig); err != nil {
  60. return errors.New("VMess|Inbound: Failed to parse config: " + err.Error())
  61. }
  62. this.Features = jsonConfig.Features // Backward compatibility
  63. this.Defaults = jsonConfig.Defaults
  64. if this.Defaults == nil {
  65. this.Defaults = &DefaultConfig{
  66. Level: 0,
  67. AlterIDs: 32,
  68. }
  69. }
  70. this.DetourConfig = jsonConfig.DetourConfig
  71. // Backward compatibility
  72. if this.Features != nil && this.DetourConfig == nil {
  73. this.DetourConfig = this.Features.Detour
  74. }
  75. this.AllowedUsers = make([]*protocol.User, len(jsonConfig.Users))
  76. for idx, rawData := range jsonConfig.Users {
  77. user := new(protocol.User)
  78. if err := json.Unmarshal(rawData, user); err != nil {
  79. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  80. }
  81. account := new(vmess.AccountPB)
  82. if err := json.Unmarshal(rawData, account); err != nil {
  83. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  84. }
  85. anyAccount, err := ptypes.MarshalAny(account)
  86. if err != nil {
  87. log.Error("VMess|Inbound: Failed to create account: ", err)
  88. return common.ErrBadConfiguration
  89. }
  90. user.Account = anyAccount
  91. this.AllowedUsers[idx] = user
  92. }
  93. return nil
  94. }
  95. func init() {
  96. registry.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
  97. }