config_json.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // +build json
  2. package inbound
  3. import (
  4. "encoding/json"
  5. proto "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 = proto.UserLevel(jsonConfig.Level)
  44. return nil
  45. }
  46. func (this *Config) UnmarshalJSON(data []byte) error {
  47. type JsonConfig struct {
  48. Users []*proto.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
  59. this.Defaults = jsonConfig.Defaults
  60. if this.Defaults == nil {
  61. this.Defaults = &DefaultConfig{
  62. Level: proto.UserLevel(0),
  63. AlterIDs: 32,
  64. }
  65. }
  66. if this.Features != nil && this.DetourConfig == nil {
  67. this.DetourConfig = this.Features.Detour
  68. }
  69. return nil
  70. }
  71. func init() {
  72. config.RegisterInboundConfig("vmess",
  73. func(data []byte) (interface{}, error) {
  74. config := new(Config)
  75. err := json.Unmarshal(data, config)
  76. return config, err
  77. })
  78. }