config_json.go 902 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package json
  2. //go:generate errorgen
  3. import (
  4. "io"
  5. "v2ray.com/core"
  6. "v2ray.com/core/common"
  7. "v2ray.com/core/common/buf"
  8. "v2ray.com/core/common/cmdarg"
  9. "v2ray.com/core/common/platform/ctlcmd"
  10. "v2ray.com/core/infra/conf/serial"
  11. )
  12. func init() {
  13. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  14. Name: "JSON",
  15. Extension: []string{"json"},
  16. Loader: func(input interface{}) (*core.Config, error) {
  17. switch v := input.(type) {
  18. case cmdarg.Arg:
  19. jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), nil)
  20. if err != nil {
  21. return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
  22. }
  23. return core.LoadConfig("protobuf", "", &buf.MultiBufferContainer{
  24. MultiBuffer: jsonContent,
  25. })
  26. case io.Reader:
  27. return serial.LoadJSONConfig(v)
  28. default:
  29. return nil, newError("unknow type")
  30. }
  31. },
  32. }))
  33. }