|
|
@@ -3,7 +3,6 @@ package api
|
|
|
import (
|
|
|
"bytes"
|
|
|
"context"
|
|
|
- "encoding/json"
|
|
|
"fmt"
|
|
|
"io"
|
|
|
"net/http"
|
|
|
@@ -16,6 +15,7 @@ import (
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
|
"github.com/xtls/xray-core/main/commands/base"
|
|
|
"google.golang.org/grpc"
|
|
|
+ "google.golang.org/protobuf/encoding/protojson"
|
|
|
"google.golang.org/protobuf/proto"
|
|
|
)
|
|
|
|
|
|
@@ -103,25 +103,25 @@ func fetchHTTPContent(target string) ([]byte, error) {
|
|
|
return content, nil
|
|
|
}
|
|
|
|
|
|
-func showResponese(m proto.Message) {
|
|
|
+func protoToJSONString(m proto.Message, _, indent string) (string, error) {
|
|
|
+ ops := protojson.MarshalOptions{
|
|
|
+ Indent: indent,
|
|
|
+ EmitUnpopulated: true,
|
|
|
+ }
|
|
|
+ b, err := ops.Marshal(m)
|
|
|
+ return string(b), err
|
|
|
+}
|
|
|
+
|
|
|
+func showJSONResponse(m proto.Message) {
|
|
|
if isNil(m) {
|
|
|
return
|
|
|
}
|
|
|
- b := new(strings.Builder)
|
|
|
- e := json.NewEncoder(b)
|
|
|
- e.SetIndent("", " ")
|
|
|
- e.SetEscapeHTML(false)
|
|
|
- err := e.Encode(m)
|
|
|
- msg := ""
|
|
|
+ output, err := protoToJSONString(m, "", " ")
|
|
|
if err != nil {
|
|
|
- msg = fmt.Sprintf("error: %s\n\n%v", err, m)
|
|
|
- } else {
|
|
|
- msg = strings.TrimSpace(b.String())
|
|
|
- }
|
|
|
- if msg == "" {
|
|
|
- return
|
|
|
+ fmt.Fprintf(os.Stdout, "%v\n", m)
|
|
|
+ base.Fatalf("error encode json: %s", err)
|
|
|
}
|
|
|
- fmt.Println(msg)
|
|
|
+ fmt.Println(output)
|
|
|
}
|
|
|
|
|
|
func isNil(i interface{}) bool {
|