config_json.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. }
  52. jsonConfig := new(JsonConfig)
  53. if err := json.Unmarshal(data, jsonConfig); err != nil {
  54. return err
  55. }
  56. this.AllowedUsers = jsonConfig.Users
  57. this.Features = jsonConfig.Features
  58. this.Defaults = jsonConfig.Defaults
  59. if this.Defaults == nil {
  60. this.Defaults = &DefaultConfig{
  61. Level: proto.UserLevel(0),
  62. AlterIDs: 32,
  63. }
  64. }
  65. return nil
  66. }
  67. func init() {
  68. config.RegisterInboundConfig("vmess",
  69. func(data []byte) (interface{}, error) {
  70. config := new(Config)
  71. err := json.Unmarshal(data, config)
  72. return config, err
  73. })
  74. }