config_json.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. )
  9. func (this *DetourConfig) UnmarshalJSON(data []byte) error {
  10. type JsonDetourConfig struct {
  11. ToTag string `json:"to"`
  12. }
  13. jsonConfig := new(JsonDetourConfig)
  14. if err := json.Unmarshal(data, jsonConfig); err != nil {
  15. return errors.New("VMessIn: Failed to parse detour config: " + err.Error())
  16. }
  17. this.ToTag = jsonConfig.ToTag
  18. return nil
  19. }
  20. func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
  21. type JsonFeaturesConfig struct {
  22. Detour *DetourConfig `json:"detour"`
  23. }
  24. jsonConfig := new(JsonFeaturesConfig)
  25. if err := json.Unmarshal(data, jsonConfig); err != nil {
  26. return errors.New("VMessIn: Failed to parse features config: " + err.Error())
  27. }
  28. this.Detour = jsonConfig.Detour
  29. return nil
  30. }
  31. func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
  32. type JsonDefaultConfig struct {
  33. AlterIDs uint16 `json:"alterId"`
  34. Level byte `json:"level"`
  35. }
  36. jsonConfig := new(JsonDefaultConfig)
  37. if err := json.Unmarshal(data, jsonConfig); err != nil {
  38. return errors.New("VMessIn: Failed to parse default config: " + err.Error())
  39. }
  40. this.AlterIDs = jsonConfig.AlterIDs
  41. if this.AlterIDs == 0 {
  42. this.AlterIDs = 32
  43. }
  44. this.Level = protocol.UserLevel(jsonConfig.Level)
  45. return nil
  46. }
  47. func (this *Config) UnmarshalJSON(data []byte) error {
  48. type JsonConfig struct {
  49. Users []*protocol.User `json:"clients"`
  50. Features *FeaturesConfig `json:"features"`
  51. Defaults *DefaultConfig `json:"default"`
  52. DetourConfig *DetourConfig `json:"detour"`
  53. }
  54. jsonConfig := new(JsonConfig)
  55. if err := json.Unmarshal(data, jsonConfig); err != nil {
  56. return errors.New("VMessIn: Failed to parse config: " + err.Error())
  57. }
  58. this.AllowedUsers = jsonConfig.Users
  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. return nil
  73. }
  74. func init() {
  75. internal.RegisterInboundConfig("vmess", func() interface{} { return new(Config) })
  76. }