config_json.go 782 B

123456789101112131415161718192021222324252627282930313233
  1. package json
  2. //go:generate errorgen
  3. import (
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "v2ray.com/core"
  8. "v2ray.com/core/common"
  9. "v2ray.com/core/common/buf"
  10. "v2ray.com/core/common/platform/ctlcmd"
  11. )
  12. func init() {
  13. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  14. Name: "JSON",
  15. Extension: []string{"json"},
  16. Loader: func(input io.Reader) (*core.Config, error) {
  17. fns := []string{}
  18. data, _ := ioutil.ReadAll(input)
  19. json.Unmarshal(data, &fns)
  20. jsonContent, err := ctlcmd.Run(append([]string{"config"}, fns...), nil)
  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. },
  28. }))
  29. }