mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-27 01:13:01 +00:00
Use protojson for marshal api output
This commit is contained in:
parent
7d52ded2a3
commit
f956b142d8
@ -68,6 +68,6 @@ func executeAddInbounds(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to add inbound: %s", err)
|
base.Fatalf("failed to add inbound: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,6 @@ func executeRemoveInbounds(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to remove inbound: %s", err)
|
base.Fatalf("failed to remove inbound: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,5 +33,5 @@ func executeRestartLogger(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to restart logger: %s", err)
|
base.Fatalf("failed to restart logger: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,6 @@ func executeAddOutbounds(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to add outbound: %s", err)
|
base.Fatalf("failed to add outbound: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,6 +68,6 @@ func executeRemoveOutbounds(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to remove outbound: %s", err)
|
base.Fatalf("failed to remove outbound: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -16,6 +15,7 @@ import (
|
|||||||
"github.com/xtls/xray-core/common/buf"
|
"github.com/xtls/xray-core/common/buf"
|
||||||
"github.com/xtls/xray-core/main/commands/base"
|
"github.com/xtls/xray-core/main/commands/base"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"google.golang.org/protobuf/encoding/protojson"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -103,25 +103,25 @@ func fetchHTTPContent(target string) ([]byte, error) {
|
|||||||
return content, nil
|
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) {
|
if isNil(m) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
b := new(strings.Builder)
|
output, err := protoToJSONString(m, "", " ")
|
||||||
e := json.NewEncoder(b)
|
|
||||||
e.SetIndent("", " ")
|
|
||||||
e.SetEscapeHTML(false)
|
|
||||||
err := e.Encode(m)
|
|
||||||
msg := ""
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
msg = fmt.Sprintf("error: %s\n\n%v", err, m)
|
fmt.Fprintf(os.Stdout, "%v\n", m)
|
||||||
} else {
|
base.Fatalf("error encode json: %s", err)
|
||||||
msg = strings.TrimSpace(b.String())
|
|
||||||
}
|
}
|
||||||
if msg == "" {
|
fmt.Println(output)
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Println(msg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func isNil(i interface{}) bool {
|
func isNil(i interface{}) bool {
|
||||||
|
@ -44,5 +44,5 @@ func executeGetStats(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to get stats: %s", err)
|
base.Fatalf("failed to get stats: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
|
@ -44,5 +44,5 @@ func executeQueryStats(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to query stats: %s", err)
|
base.Fatalf("failed to query stats: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
|
@ -33,5 +33,5 @@ func executeSysStats(cmd *base.Command, args []string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
base.Fatalf("failed to get sys stats: %s", err)
|
base.Fatalf("failed to get sys stats: %s", err)
|
||||||
}
|
}
|
||||||
showResponese(resp)
|
showJSONResponse(resp)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user