config_json.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // +build json
  2. package inbound
  3. import (
  4. "encoding/json"
  5. "github.com/v2ray/v2ray-core/common/protocol"
  6. "github.com/v2ray/v2ray-core/proxy/internal/config"
  7. )
  8. func (this *DetourConfig) UnmarshalJSON(data []byte) error {
  9. type JsonDetourConfig struct {
  10. ToTag string `json:"to"`
  11. }
  12. jsonConfig := new(JsonDetourConfig)
  13. if err := json.Unmarshal(data, jsonConfig); err != nil {
  14. return err
  15. }
  16. this.ToTag = jsonConfig.ToTag
  17. return nil
  18. }
  19. func (this *FeaturesConfig) UnmarshalJSON(data []byte) error {
  20. type JsonFeaturesConfig struct {
  21. Detour *DetourConfig `json:"detour"`
  22. }
  23. jsonConfig := new(JsonFeaturesConfig)
  24. if err := json.Unmarshal(data, jsonConfig); err != nil {
  25. return err
  26. }
  27. this.Detour = jsonConfig.Detour
  28. return nil
  29. }
  30. func (this *DefaultConfig) UnmarshalJSON(data []byte) error {
  31. type JsonDefaultConfig struct {
  32. AlterIDs uint16 `json:"alterId"`
  33. Level byte `json:"level"`
  34. }
  35. jsonConfig := new(JsonDefaultConfig)
  36. if err := json.Unmarshal(data, jsonConfig); err != nil {
  37. return err
  38. }
  39. this.AlterIDs = jsonConfig.AlterIDs
  40. if this.AlterIDs == 0 {
  41. this.AlterIDs = 32
  42. }
  43. this.Level = protocol.UserLevel(jsonConfig.Level)
  44. return nil
  45. }
  46. func (this *Config) UnmarshalJSON(data []byte) error {
  47. type JsonConfig struct {
  48. Users []*protocol.User `json:"clients"`
  49. Features *FeaturesConfig `json:"features"`
  50. Defaults *DefaultConfig `json:"default"`
  51. DetourConfig *DetourConfig `json:"detour"`
  52. }
  53. jsonConfig := new(JsonConfig)
  54. if err := json.Unmarshal(data, jsonConfig); err != nil {
  55. return err
  56. }
  57. this.AllowedUsers = jsonConfig.Users
  58. this.Features = jsonConfig.Features // Backward compatibility
  59. this.Defaults = jsonConfig.Defaults
  60. if this.Defaults == nil {
  61. this.Defaults = &DefaultConfig{
  62. Level: protocol.UserLevel(0),
  63. AlterIDs: 32,
  64. }
  65. }
  66. this.DetourConfig = jsonConfig.DetourConfig
  67. // Backward compatibility
  68. if this.Features != nil && this.DetourConfig == nil {
  69. this.DetourConfig = this.Features.Detour
  70. }
  71. return nil
  72. }
  73. func init() {
  74. config.RegisterInboundConfig("vmess",
  75. func(data []byte) (interface{}, error) {
  76. config := new(Config)
  77. err := json.Unmarshal(data, config)
  78. return config, err
  79. })
  80. }