Skip to content

Commit 767579f

Browse files
added json marshalling for tls and http
1 parent e63e5cb commit 767579f

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

layers/http.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package layers
33
import (
44
"bufio"
55
"bytes"
6+
"encoding/json"
67
"fmt"
78
"net/http"
89
"net/http/httputil"
@@ -78,3 +79,47 @@ func (h *HTTPMessage) Parse(data []byte) error {
7879
}
7980

8081
func (h *HTTPMessage) NextLayer() (layer string, payload []byte) { return }
82+
83+
type HTTPRequestWrapper struct {
84+
Request HTTPRequest `json:"http_request"`
85+
}
86+
87+
type HTTPRequest struct {
88+
Host string `json:"host,omitempty"`
89+
URI string `json:"uri,omitempty"`
90+
Method string `json:"method,omitempty"`
91+
Proto string `json:"proto,omitempty"`
92+
ContentLength int `json:"content-length,omitempty"`
93+
Header http.Header `json:"header,omitempty"`
94+
}
95+
96+
type HTTPResponseWrapper struct {
97+
Response HTTPResponse `json:"http_response"`
98+
}
99+
100+
type HTTPResponse struct {
101+
Proto string `json:"proto,omitempty"`
102+
Status string `json:"status,omitempty"`
103+
ContentLength int `json:"content-length,omitempty"`
104+
Header http.Header `json:"header,omitempty"`
105+
}
106+
107+
func (h *HTTPMessage) MarshalJSON() ([]byte, error) {
108+
if h.Request != nil {
109+
return json.Marshal(&HTTPRequestWrapper{Request: HTTPRequest{
110+
Host: h.Request.Host,
111+
URI: h.Request.RequestURI,
112+
Method: h.Request.Method,
113+
Proto: h.Request.Proto,
114+
ContentLength: int(h.Request.ContentLength),
115+
Header: h.Request.Header,
116+
}})
117+
} else if h.Response != nil {
118+
return json.Marshal(&HTTPResponseWrapper{Response: HTTPResponse{
119+
Proto: h.Response.Proto,
120+
Status: h.Response.Status,
121+
ContentLength: int(h.Response.ContentLength),
122+
Header: h.Response.Header}})
123+
}
124+
return nil, fmt.Errorf("both request and response are empty")
125+
}

layers/tls.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package layers
33
import (
44
"encoding/binary"
55
"encoding/hex"
6+
"encoding/json"
67
"fmt"
78
"strings"
89
)
@@ -129,6 +130,47 @@ type TLSClientHello struct {
129130
ALPN []string
130131
}
131132

133+
type TLSClientHelloRequestWrapper struct {
134+
Request TLSClientHelloRequest `json:"tls_request"`
135+
}
136+
137+
type TLSClientHelloRequest struct {
138+
SNI string `json:"sni,omitempty"`
139+
Type string `json:"type,omitempty"`
140+
Version string `json:"version,omitempty"`
141+
SessionID string `json:"session_id,omitempty"`
142+
CipherSuites []string `json:"cipher_suites,omitempty"`
143+
Extensions []string `json:"extensions,omitempty"`
144+
ALPN []string `json:"alpn,omitempty"`
145+
}
146+
147+
func (tch *TLSClientHello) MarshalJSON() ([]byte, error) {
148+
cs := make([]string, 0, len(tch.CipherSuites))
149+
for _, c := range tch.CipherSuites {
150+
cs = append(cs, c.String())
151+
}
152+
es := make([]string, 0, len(tch.Extensions))
153+
for _, e := range tch.Extensions {
154+
es = append(es, e.String())
155+
}
156+
var ver, sn string
157+
if tch.Version != nil {
158+
ver = tch.Version.String()
159+
}
160+
if tch.ServerName != nil {
161+
sn = tch.ServerName.SNName
162+
}
163+
return json.Marshal(&TLSClientHelloRequestWrapper{Request: TLSClientHelloRequest{
164+
SNI: sn,
165+
Type: fmt.Sprintf("%s (%d)", tch.TypeDesc, tch.Type),
166+
Version: ver,
167+
SessionID: tch.SessionID,
168+
CipherSuites: cs,
169+
Extensions: es,
170+
ALPN: tch.ALPN,
171+
}})
172+
}
173+
132174
func (tch *TLSClientHello) String() string {
133175
return fmt.Sprintf(` - Type: %s (%d)
134176
- Length: %d
@@ -286,6 +328,44 @@ type TLSServerHello struct {
286328
SupportedVersion *TLSVersion
287329
}
288330

331+
type TLSServerHelloResponseWrapper struct {
332+
Response TLSServerHelloResponse `json:"tls_response"`
333+
}
334+
335+
type TLSServerHelloResponse struct {
336+
Type string `json:"type,omitempty"`
337+
Version string `json:"version,omitempty"`
338+
SessionID string `json:"session_id,omitempty"`
339+
CipherSuite string `json:"cipher_suite,omitempty"`
340+
Extensions []string `json:"extensions,omitempty"`
341+
SupportedVersion string `json:"supported_version,omitempty"`
342+
}
343+
344+
func (tsh *TLSServerHello) MarshalJSON() ([]byte, error) {
345+
es := make([]string, 0, len(tsh.Extensions))
346+
for _, e := range tsh.Extensions {
347+
es = append(es, e.String())
348+
}
349+
var ver, supver, cs string
350+
if tsh.Version != nil {
351+
ver = tsh.Version.String()
352+
}
353+
if tsh.SupportedVersion != nil {
354+
supver = tsh.SupportedVersion.String()
355+
}
356+
if tsh.CipherSuite != nil {
357+
cs = tsh.CipherSuite.String()
358+
}
359+
return json.Marshal(&TLSServerHelloResponseWrapper{Response: TLSServerHelloResponse{
360+
Type: fmt.Sprintf("%s (%d)", tsh.TypeDesc, tsh.Type),
361+
Version: ver,
362+
SessionID: tsh.SessionID,
363+
CipherSuite: cs,
364+
Extensions: es,
365+
SupportedVersion: supver,
366+
}})
367+
}
368+
289369
func (tsh *TLSServerHello) String() string {
290370
return fmt.Sprintf(` - Type: %s (%d)
291371
- Length: %d

0 commit comments

Comments
 (0)