config_json.go 2.8 KB

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