json.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package json
  2. import (
  3. "context"
  4. "io"
  5. "github.com/xtls/xray-core/common"
  6. "github.com/xtls/xray-core/common/cmdarg"
  7. "github.com/xtls/xray-core/common/errors"
  8. "github.com/xtls/xray-core/core"
  9. "github.com/xtls/xray-core/infra/conf"
  10. "github.com/xtls/xray-core/infra/conf/serial"
  11. "github.com/xtls/xray-core/main/confloader"
  12. )
  13. func init() {
  14. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  15. Name: "JSON",
  16. Extension: []string{"json"},
  17. Loader: func(input interface{}) (*core.Config, error) {
  18. switch v := input.(type) {
  19. case cmdarg.Arg:
  20. cf := &conf.Config{}
  21. for i, arg := range v {
  22. errors.LogInfo(context.Background(), "Reading config: ", arg)
  23. r, err := confloader.LoadConfig(arg)
  24. if err != nil {
  25. return nil, errors.New("failed to read config: ", arg).Base(err)
  26. }
  27. c, err := serial.DecodeJSONConfig(r)
  28. if err != nil {
  29. return nil, errors.New("failed to decode config: ", arg).Base(err)
  30. }
  31. if i == 0 {
  32. // This ensure even if the muti-json parser do not support a setting,
  33. // It is still respected automatically for the first configure file
  34. *cf = *c
  35. continue
  36. }
  37. cf.Override(c, arg)
  38. }
  39. return cf.Build()
  40. case io.Reader:
  41. return serial.LoadJSONConfig(v)
  42. default:
  43. return nil, errors.New("unknown type")
  44. }
  45. },
  46. }))
  47. }