config_json.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.To = jsonConfig.ToTag
  22. return nil
  23. }
  24. type FeaturesConfig struct {
  25. Detour *DetourConfig `json:"detour"`
  26. }
  27. func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
  28. type JsonDefaultConfig struct {
  29. AlterIDs uint16 `json:"alterId"`
  30. Level byte `json:"level"`
  31. }
  32. jsonConfig := new(JsonDefaultConfig)
  33. if err := json.Unmarshal(data, jsonConfig); err != nil {
  34. return errors.New("VMess|Inbound: Failed to parse default config: " + err.Error())
  35. }
  36. this.AlterId = uint32(jsonConfig.AlterIDs)
  37. if this.AlterId == 0 {
  38. this.AlterId = 32
  39. }
  40. this.Level = uint32(jsonConfig.Level)
  41. return nil
  42. }
  43. func (this *Config) UnmarshalJSON(data []byte) error {
  44. type JsonConfig struct {
  45. Users []json.RawMessage `json:"clients"`
  46. Features *FeaturesConfig `json:"features"`
  47. Defaults *DefaultConfig `json:"default"`
  48. DetourConfig *DetourConfig `json:"detour"`
  49. }
  50. jsonConfig := new(JsonConfig)
  51. if err := json.Unmarshal(data, jsonConfig); err != nil {
  52. return errors.New("VMess|Inbound: Failed to parse config: " + err.Error())
  53. }
  54. this.Default = jsonConfig.Defaults
  55. if this.Default == nil {
  56. this.Default = &DefaultConfig{
  57. Level: 0,
  58. AlterId: 32,
  59. }
  60. }
  61. this.Detour = jsonConfig.DetourConfig
  62. // Backward compatibility
  63. if jsonConfig.Features != nil && jsonConfig.DetourConfig == nil {
  64. this.Detour = jsonConfig.Features.Detour
  65. }
  66. this.User = make([]*protocol.User, len(jsonConfig.Users))
  67. for idx, rawData := range jsonConfig.Users {
  68. user := new(protocol.User)
  69. if err := json.Unmarshal(rawData, user); err != nil {
  70. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  71. }
  72. account := new(vmess.AccountPB)
  73. if err := json.Unmarshal(rawData, account); err != nil {
  74. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  75. }
  76. anyAccount, err := ptypes.MarshalAny(account)
  77. if err != nil {
  78. log.Error("VMess|Inbound: Failed to create account: ", err)
  79. return common.ErrBadConfiguration
  80. }
  81. user.Account = anyAccount
  82. this.User[idx] = user
  83. }
  84. return nil
  85. }
  86. func init() {
  87. registry.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
  88. }