config_json.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // +build json
  2. package inbound
  3. import (
  4. "encoding/json"
  5. "errors"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/common/protocol"
  8. "v2ray.com/core/proxy/vmess"
  9. )
  10. func (this *DetourConfig) UnmarshalJSON(data []byte) error {
  11. type JsonDetourConfig struct {
  12. ToTag string `json:"to"`
  13. }
  14. jsonConfig := new(JsonDetourConfig)
  15. if err := json.Unmarshal(data, jsonConfig); err != nil {
  16. return errors.New("VMess|Inbound: Failed to parse detour config: " + err.Error())
  17. }
  18. this.To = jsonConfig.ToTag
  19. return nil
  20. }
  21. type FeaturesConfig struct {
  22. Detour *DetourConfig `json:"detour"`
  23. }
  24. func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
  25. type JsonDefaultConfig struct {
  26. AlterIDs uint16 `json:"alterId"`
  27. Level byte `json:"level"`
  28. }
  29. jsonConfig := new(JsonDefaultConfig)
  30. if err := json.Unmarshal(data, jsonConfig); err != nil {
  31. return errors.New("VMess|Inbound: Failed to parse default config: " + err.Error())
  32. }
  33. this.AlterId = uint32(jsonConfig.AlterIDs)
  34. if this.AlterId == 0 {
  35. this.AlterId = 32
  36. }
  37. this.Level = uint32(jsonConfig.Level)
  38. return nil
  39. }
  40. func (this *Config) UnmarshalJSON(data []byte) error {
  41. type JsonConfig struct {
  42. Users []json.RawMessage `json:"clients"`
  43. Features *FeaturesConfig `json:"features"`
  44. Defaults *DefaultConfig `json:"default"`
  45. DetourConfig *DetourConfig `json:"detour"`
  46. }
  47. jsonConfig := new(JsonConfig)
  48. if err := json.Unmarshal(data, jsonConfig); err != nil {
  49. return errors.New("VMess|Inbound: Failed to parse config: " + err.Error())
  50. }
  51. this.Default = jsonConfig.Defaults
  52. if this.Default == nil {
  53. this.Default = &DefaultConfig{
  54. Level: 0,
  55. AlterId: 32,
  56. }
  57. }
  58. this.Detour = jsonConfig.DetourConfig
  59. // Backward compatibility
  60. if jsonConfig.Features != nil && jsonConfig.DetourConfig == nil {
  61. this.Detour = jsonConfig.Features.Detour
  62. }
  63. this.User = make([]*protocol.User, len(jsonConfig.Users))
  64. for idx, rawData := range jsonConfig.Users {
  65. user := new(protocol.User)
  66. if err := json.Unmarshal(rawData, user); err != nil {
  67. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  68. }
  69. account := new(vmess.Account)
  70. if err := json.Unmarshal(rawData, account); err != nil {
  71. return errors.New("VMess|Inbound: Invalid user: " + err.Error())
  72. }
  73. user.Account = loader.NewTypedSettings(account)
  74. this.User[idx] = user
  75. }
  76. return nil
  77. }