From f956b142d8f3777e53b91e33e0d736bafcd6084f Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 15 Jul 2022 21:45:13 -0400 Subject: [PATCH] Use protojson for marshal api output --- main/commands/all/api/inbounds_add.go | 2 +- main/commands/all/api/inbounds_remove.go | 2 +- main/commands/all/api/logger_restart.go | 2 +- main/commands/all/api/outbounds_add.go | 2 +- main/commands/all/api/outbounds_remove.go | 2 +- main/commands/all/api/shared.go | 30 +++++++++++------------ main/commands/all/api/stats_get.go | 2 +- main/commands/all/api/stats_query.go | 2 +- main/commands/all/api/stats_sys.go | 2 +- 9 files changed, 23 insertions(+), 23 deletions(-) diff --git a/main/commands/all/api/inbounds_add.go b/main/commands/all/api/inbounds_add.go index 30c72a4a..dad8b0f9 100644 --- a/main/commands/all/api/inbounds_add.go +++ b/main/commands/all/api/inbounds_add.go @@ -68,6 +68,6 @@ func executeAddInbounds(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to add inbound: %s", err) } - showResponese(resp) + showJSONResponse(resp) } } diff --git a/main/commands/all/api/inbounds_remove.go b/main/commands/all/api/inbounds_remove.go index 1673943a..9ab83e20 100644 --- a/main/commands/all/api/inbounds_remove.go +++ b/main/commands/all/api/inbounds_remove.go @@ -69,6 +69,6 @@ func executeRemoveInbounds(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to remove inbound: %s", err) } - showResponese(resp) + showJSONResponse(resp) } } diff --git a/main/commands/all/api/logger_restart.go b/main/commands/all/api/logger_restart.go index 259ac962..7bd6f10d 100644 --- a/main/commands/all/api/logger_restart.go +++ b/main/commands/all/api/logger_restart.go @@ -33,5 +33,5 @@ func executeRestartLogger(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to restart logger: %s", err) } - showResponese(resp) + showJSONResponse(resp) } diff --git a/main/commands/all/api/outbounds_add.go b/main/commands/all/api/outbounds_add.go index 5d99de4f..5066e543 100644 --- a/main/commands/all/api/outbounds_add.go +++ b/main/commands/all/api/outbounds_add.go @@ -68,6 +68,6 @@ func executeAddOutbounds(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to add outbound: %s", err) } - showResponese(resp) + showJSONResponse(resp) } } diff --git a/main/commands/all/api/outbounds_remove.go b/main/commands/all/api/outbounds_remove.go index d9a8a945..9fdbc078 100644 --- a/main/commands/all/api/outbounds_remove.go +++ b/main/commands/all/api/outbounds_remove.go @@ -68,6 +68,6 @@ func executeRemoveOutbounds(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to remove outbound: %s", err) } - showResponese(resp) + showJSONResponse(resp) } } diff --git a/main/commands/all/api/shared.go b/main/commands/all/api/shared.go index b0990368..d3aaf0e1 100644 --- a/main/commands/all/api/shared.go +++ b/main/commands/all/api/shared.go @@ -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()) + fmt.Fprintf(os.Stdout, "%v\n", m) + base.Fatalf("error encode json: %s", err) } - if msg == "" { - return - } - fmt.Println(msg) + fmt.Println(output) } func isNil(i interface{}) bool { diff --git a/main/commands/all/api/stats_get.go b/main/commands/all/api/stats_get.go index 7e87cb98..c03fe5f8 100644 --- a/main/commands/all/api/stats_get.go +++ b/main/commands/all/api/stats_get.go @@ -44,5 +44,5 @@ func executeGetStats(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to get stats: %s", err) } - showResponese(resp) + showJSONResponse(resp) } diff --git a/main/commands/all/api/stats_query.go b/main/commands/all/api/stats_query.go index 725b506b..f3163199 100644 --- a/main/commands/all/api/stats_query.go +++ b/main/commands/all/api/stats_query.go @@ -44,5 +44,5 @@ func executeQueryStats(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to query stats: %s", err) } - showResponese(resp) + showJSONResponse(resp) } diff --git a/main/commands/all/api/stats_sys.go b/main/commands/all/api/stats_sys.go index 5f32d05b..de7a8ce2 100644 --- a/main/commands/all/api/stats_sys.go +++ b/main/commands/all/api/stats_sys.go @@ -33,5 +33,5 @@ func executeSysStats(cmd *base.Command, args []string) { if err != nil { base.Fatalf("failed to get sys stats: %s", err) } - showResponese(resp) + showJSONResponse(resp) }