config_json.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package json
  2. //go:generate go run github.com/v2fly/v2ray-core/v4/common/errors/errorgen
  3. import (
  4. "io"
  5. "os"
  6. core "github.com/v2fly/v2ray-core/v4"
  7. "github.com/v2fly/v2ray-core/v4/common"
  8. "github.com/v2fly/v2ray-core/v4/common/cmdarg"
  9. "github.com/v2fly/v2ray-core/v4/main/confloader"
  10. )
  11. func init() {
  12. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  13. Name: []string{"JSON"},
  14. Extension: []string{".json", ".jsonc"},
  15. Loader: func(input interface{}) (*core.Config, error) {
  16. switch v := input.(type) {
  17. case cmdarg.Arg:
  18. r, err := confloader.LoadExtConfig(v, os.Stdin)
  19. if err != nil {
  20. return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
  21. }
  22. return core.LoadConfig("protobuf", "", r)
  23. case io.Reader:
  24. r, err := confloader.LoadExtConfig([]string{"stdin:"}, os.Stdin)
  25. if err != nil {
  26. return nil, newError("failed to execute v2ctl to convert config file.").Base(err).AtWarning()
  27. }
  28. return core.LoadConfig("protobuf", "", r)
  29. default:
  30. return nil, newError("unknown type")
  31. }
  32. },
  33. }))
  34. }