config_json.go 913 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package json
  2. //go:generate errorgen
  3. import (
  4. "io"
  5. "os"
  6. "v2ray.com/core"
  7. "v2ray.com/core/common"
  8. "v2ray.com/core/common/buf"
  9. "v2ray.com/core/common/cmdarg"
  10. "v2ray.com/core/common/platform/ctlcmd"
  11. "v2ray.com/core/infra/conf/serial"
  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. jsonContent, err := ctlcmd.Run(append([]string{"config"}, v...), os.Stdin)
  21. if err != nil {
  22. return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
  23. }
  24. return core.LoadConfig("protobuf", "", &buf.MultiBufferContainer{
  25. MultiBuffer: jsonContent,
  26. })
  27. case io.Reader:
  28. return serial.LoadJSONConfig(v)
  29. default:
  30. return nil, newError("unknow type")
  31. }
  32. },
  33. }))
  34. }