Apply JSON format to xray api commands' output

This commit is contained in:
RPRX 2020-12-04 14:03:07 +00:00 committed by GitHub
parent 0d772cd800
commit 0a9db2d3d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -104,16 +104,30 @@ func fetchHTTPContent(target string) ([]byte, error) {
} }
func showResponese(m proto.Message) { func showResponese(m proto.Message) {
if isNil(m) {
return
}
b := new(bytes.Buffer)
e := json.NewEncoder(b)
e.SetIndent("", " ")
e.SetEscapeHTML(false)
err := e.Encode(m)
msg := "" msg := ""
bs, err := proto.Marshal(m)
if err != nil { if err != nil {
msg = err.Error() msg = fmt.Sprintf("error: %s\n\n%v", err, m)
} else { } else {
msg = string(bs) msg = strings.TrimSpace(b.String())
msg = strings.TrimSpace(msg)
} }
if msg == "" { if msg == "" {
return return
} }
fmt.Println(msg) fmt.Println(msg)
} }
func isNil(i interface{}) bool {
vi := reflect.ValueOf(i)
if vi.Kind() == reflect.Ptr {
return vi.IsNil()
}
return i == nil
}