config_json.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. package json
  2. //go:generate go run github.com/xtls/xray-core/common/errors/errorgen
  3. import (
  4. "io"
  5. "os"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/cmdarg"
  8. core "github.com/xtls/xray-core/core"
  9. "github.com/xtls/xray-core/main/confloader"
  10. )
  11. func init() {
  12. common.Must(core.RegisterConfigLoader(&core.ConfigFormat{
  13. Name: "JSON",
  14. Extension: []string{"json"},
  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 xctl 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 xctl 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. }