-
Notifications
You must be signed in to change notification settings - Fork 2
/
print.go
145 lines (121 loc) · 3 KB
/
print.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/binary"
"encoding/hex"
"log"
"strings"
"unicode"
"github.com/gookit/color"
)
func (p h2Packet) CanDecode() bool {
if len(p.Body) <= 5 || p.Body[0] != 0 {
return false
}
length := binary.BigEndian.Uint32(p.Body[1:5])
if length+5 != uint32(len(p.Body)) {
return false
}
if v, ok := p.Headers["content-type"]; ok {
return v[0] == "application/grpc" || v[0] == "application/grpc+proto"
}
if length >= 2 && p.Body[5] == '{' && (p.Body[6] == '"' || p.Body[6] == '}') {
return false
}
// other pass to decoder
return true
}
func (p h2Packet) CanJson() bool {
if len(p.Body) <= 5 || p.Body[0] != 0 {
return false
}
length := binary.BigEndian.Uint32(p.Body[1:5])
if v, ok := p.Headers["content-type"]; ok {
return v[0] == "application/grpc+json"
}
if length >= 2 && p.Body[5] == '{' && (p.Body[6] == '"' || p.Body[6] == '}') {
return true
}
return false
}
func (p h2Packet) CanPlain() bool {
// check 16 byte
n := 16
if n > len(p.Body) {
n = len(p.Body)
}
if n == 0 {
return false
}
for _, c := range p.Body[:n] {
if !unicode.IsPrint(rune(c)) {
return false
}
}
return true
}
func (p h2Packet) HeaderString() string {
var buf strings.Builder
// tcp 4 tuple
buf.WriteString(color.FgLightMagenta.Sprintf("%v\n", p.Tuple))
if p.Rst != 0 {
buf.WriteString(color.Sprintf("%s: %v\n", color.LightRed.Sprint("ERROR"), p.Rst))
}
// print headers
for k, v := range p.Headers {
// ignore some use-less
// if k == ":method" || k == ":scheme" || k == "te" || k == "user-agent" || k == "accept" || k == "content-length" {
// continue
// }
for _, vv := range v {
if k == ":path" || k == "<path" {
buf.WriteString(color.Sprintf("%s: %s\n", color.LightCyan.Sprint(k), color.LightYellow.Sprint(vv)))
} else {
buf.WriteString(color.Sprintf("%s: %s\n", color.LightCyan.Sprint(k), vv))
}
}
}
return buf.String()
}
func (p h2Packet) HeaderBodyString() string {
if len(p.Body) == 0 {
return p.HeaderString()
}
if p.CanJson() {
return p.HeaderString() + color.FgLightYellow.Sprintf("%s\n", p.Body[5:])
}
if p.CanPlain() {
return p.HeaderString() + color.FgLightYellow.Sprintf("%s\n", p.Body)
}
return p.HeaderString() + color.FgLightYellow.Sprintf("%s\n", hex.Dump(p.Body))
}
func (p h2Packet) Print(path string, isReq bool) {
// ignore reflection service call by myself
if path == "/grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo" {
return
}
if conf.PathFilter != nil && !conf.PathFilter.MatchString(path) {
return
}
if !p.CanDecode() {
p.DefPrint()
return
}
var host string
if isReq {
host = p.Tuple.Dst()
} else {
host = p.Tuple.Src()
}
ret, err := pbMgr.DecodeToJsonString(host, path, isReq, p.Body[5:])
if err != nil {
if conf.Verbose {
log.Println(p.Tuple, "decodeToJsonString failed", err)
}
p.DefPrint()
return
}
color.Fprint(lkout, p.HeaderString()+color.FgLightYellow.Sprintf("%s\n", ret))
}
func (p h2Packet) DefPrint() {
color.Fprint(lkout, p.HeaderBodyString())
}