convert.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package all
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/url"
  8. "os"
  9. "strings"
  10. "time"
  11. "github.com/xtls/xray-core/common"
  12. "github.com/xtls/xray-core/common/buf"
  13. "github.com/xtls/xray-core/infra/conf"
  14. "github.com/xtls/xray-core/infra/conf/serial"
  15. "github.com/xtls/xray-core/main/commands/base"
  16. "google.golang.org/protobuf/proto"
  17. )
  18. var cmdConvert = &base.Command{
  19. UsageLine: "{{.Exec}} convert [json file] [json file] ...",
  20. Short: "Convert multiple json config to protobuf",
  21. Long: `
  22. Convert multiple json config to protobuf.
  23. Examples:
  24. {{.Exec}} convert config.json c1.json c2.json <url>.json
  25. `,
  26. }
  27. func init() {
  28. cmdConvert.Run = executeConvert // break init loop
  29. }
  30. func executeConvert(cmd *base.Command, args []string) {
  31. unnamedArgs := cmdConvert.Flag.Args()
  32. if len(unnamedArgs) < 1 {
  33. base.Fatalf("empty config list")
  34. }
  35. conf := &conf.Config{}
  36. for _, arg := range unnamedArgs {
  37. fmt.Fprintf(os.Stderr, "Read config: %s", arg)
  38. r, err := loadArg(arg)
  39. common.Must(err)
  40. c, err := serial.DecodeJSONConfig(r)
  41. if err != nil {
  42. base.Fatalf(err.Error())
  43. }
  44. conf.Override(c, arg)
  45. }
  46. pbConfig, err := conf.Build()
  47. if err != nil {
  48. base.Fatalf(err.Error())
  49. }
  50. bytesConfig, err := proto.Marshal(pbConfig)
  51. if err != nil {
  52. base.Fatalf("failed to marshal proto config: %s", err)
  53. }
  54. if _, err := os.Stdout.Write(bytesConfig); err != nil {
  55. base.Fatalf("failed to write proto config: %s", err)
  56. }
  57. }
  58. // loadArg loads one arg, maybe an remote url, or local file path
  59. func loadArg(arg string) (out io.Reader, err error) {
  60. var data []byte
  61. switch {
  62. case strings.HasPrefix(arg, "http://"), strings.HasPrefix(arg, "https://"):
  63. data, err = FetchHTTPContent(arg)
  64. case arg == "stdin:":
  65. data, err = io.ReadAll(os.Stdin)
  66. default:
  67. data, err = os.ReadFile(arg)
  68. }
  69. if err != nil {
  70. return
  71. }
  72. out = bytes.NewBuffer(data)
  73. return
  74. }
  75. // FetchHTTPContent dials https for remote content
  76. func FetchHTTPContent(target string) ([]byte, error) {
  77. parsedTarget, err := url.Parse(target)
  78. if err != nil {
  79. return nil, newError("invalid URL: ", target).Base(err)
  80. }
  81. if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
  82. return nil, newError("invalid scheme: ", parsedTarget.Scheme)
  83. }
  84. client := &http.Client{
  85. Timeout: 30 * time.Second,
  86. }
  87. resp, err := client.Do(&http.Request{
  88. Method: "GET",
  89. URL: parsedTarget,
  90. Close: true,
  91. })
  92. if err != nil {
  93. return nil, newError("failed to dial to ", target).Base(err)
  94. }
  95. defer resp.Body.Close()
  96. if resp.StatusCode != 200 {
  97. return nil, newError("unexpected HTTP status code: ", resp.StatusCode)
  98. }
  99. content, err := buf.ReadAllToBytes(resp.Body)
  100. if err != nil {
  101. return nil, newError("failed to read HTTP response").Base(err)
  102. }
  103. return content, nil
  104. }