shared.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package api
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "io"
  7. "net/http"
  8. "net/url"
  9. "os"
  10. "reflect"
  11. "strings"
  12. "time"
  13. "github.com/xtls/xray-core/common/buf"
  14. "github.com/xtls/xray-core/main/commands/base"
  15. "google.golang.org/grpc"
  16. "google.golang.org/protobuf/encoding/protojson"
  17. "google.golang.org/protobuf/proto"
  18. )
  19. type serviceHandler func(ctx context.Context, conn *grpc.ClientConn, cmd *base.Command, args []string) string
  20. var (
  21. apiServerAddrPtr string
  22. apiTimeout int
  23. )
  24. func setSharedFlags(cmd *base.Command) {
  25. cmd.Flag.StringVar(&apiServerAddrPtr, "s", "127.0.0.1:8080", "")
  26. cmd.Flag.StringVar(&apiServerAddrPtr, "server", "127.0.0.1:8080", "")
  27. cmd.Flag.IntVar(&apiTimeout, "t", 3, "")
  28. cmd.Flag.IntVar(&apiTimeout, "timeout", 3, "")
  29. }
  30. func dialAPIServer() (conn *grpc.ClientConn, ctx context.Context, close func()) {
  31. ctx, cancel := context.WithTimeout(context.Background(), time.Duration(apiTimeout)*time.Second)
  32. conn, err := grpc.DialContext(ctx, apiServerAddrPtr, grpc.WithInsecure(), grpc.WithBlock())
  33. if err != nil {
  34. base.Fatalf("failed to dial %s", apiServerAddrPtr)
  35. }
  36. close = func() {
  37. cancel()
  38. conn.Close()
  39. }
  40. return
  41. }
  42. // loadArg loads one arg, maybe an remote url, or local file path
  43. func loadArg(arg string) (out io.Reader, err error) {
  44. var data []byte
  45. switch {
  46. case strings.HasPrefix(arg, "http://"), strings.HasPrefix(arg, "https://"):
  47. data, err = fetchHTTPContent(arg)
  48. case arg == "stdin:":
  49. data, err = io.ReadAll(os.Stdin)
  50. default:
  51. data, err = os.ReadFile(arg)
  52. }
  53. if err != nil {
  54. return
  55. }
  56. out = bytes.NewBuffer(data)
  57. return
  58. }
  59. // fetchHTTPContent dials https for remote content
  60. func fetchHTTPContent(target string) ([]byte, error) {
  61. parsedTarget, err := url.Parse(target)
  62. if err != nil {
  63. return nil, err
  64. }
  65. if s := strings.ToLower(parsedTarget.Scheme); s != "http" && s != "https" {
  66. return nil, fmt.Errorf("invalid scheme: %s", parsedTarget.Scheme)
  67. }
  68. client := &http.Client{
  69. Timeout: 30 * time.Second,
  70. }
  71. resp, err := client.Do(&http.Request{
  72. Method: "GET",
  73. URL: parsedTarget,
  74. Close: true,
  75. })
  76. if err != nil {
  77. return nil, fmt.Errorf("failed to dial to %s", target)
  78. }
  79. defer resp.Body.Close()
  80. if resp.StatusCode != 200 {
  81. return nil, fmt.Errorf("unexpected HTTP status code: %d", resp.StatusCode)
  82. }
  83. content, err := buf.ReadAllToBytes(resp.Body)
  84. if err != nil {
  85. return nil, fmt.Errorf("failed to read HTTP response")
  86. }
  87. return content, nil
  88. }
  89. func protoToJSONString(m proto.Message, _, indent string) (string, error) {
  90. ops := protojson.MarshalOptions{
  91. Indent: indent,
  92. EmitUnpopulated: true,
  93. }
  94. b, err := ops.Marshal(m)
  95. return string(b), err
  96. }
  97. func showJSONResponse(m proto.Message) {
  98. if isNil(m) {
  99. return
  100. }
  101. output, err := protoToJSONString(m, "", " ")
  102. if err != nil {
  103. fmt.Fprintf(os.Stdout, "%v\n", m)
  104. base.Fatalf("error encode json: %s", err)
  105. }
  106. fmt.Println(output)
  107. }
  108. func isNil(i interface{}) bool {
  109. vi := reflect.ValueOf(i)
  110. if vi.Kind() == reflect.Ptr {
  111. return vi.IsNil()
  112. }
  113. return i == nil
  114. }