Skip to content

Commit

Permalink
Use protojson for marshal api output
Browse files Browse the repository at this point in the history
  • Loading branch information
yuhan6665 committed Jul 16, 2022
1 parent 7d52ded commit f956b14
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion main/commands/all/api/inbounds_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion main/commands/all/api/inbounds_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion main/commands/all/api/logger_restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion main/commands/all/api/outbounds_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion main/commands/all/api/outbounds_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
30 changes: 15 additions & 15 deletions main/commands/all/api/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion main/commands/all/api/stats_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion main/commands/all/api/stats_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion main/commands/all/api/stats_sys.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit f956b14

Please sign in to comment.