Skip to content

Commit db18d4c

Browse files
Added tls colorization, randomization of uuidi color
1 parent e96d6cd commit db18d4c

File tree

2 files changed

+168
-32
lines changed

2 files changed

+168
-32
lines changed

gohpts.go

Lines changed: 153 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,25 @@ var hopHeaders = []string{
7373
"Upgrade",
7474
}
7575

76+
var rColors = []func(string) *colors.Color{
77+
colors.Beige,
78+
colors.Blue,
79+
colors.Gray,
80+
colors.Green,
81+
colors.LightBlue,
82+
colors.Magenta,
83+
colors.Red,
84+
colors.Yellow,
85+
colors.BeigeBg,
86+
colors.BlueBg,
87+
colors.GrayBg,
88+
colors.GreenBg,
89+
colors.LightBlueBg,
90+
colors.MagentaBg,
91+
colors.RedBgDark,
92+
colors.YellowBg,
93+
}
94+
7695
func copyHeader(dst, src http.Header) {
7796
for k, vv := range src {
7897
for _, v := range vv {
@@ -366,6 +385,20 @@ func (p *proxyapp) doReq(w http.ResponseWriter, r *http.Request, sock *http.Clie
366385
return resp
367386
}
368387

388+
func randColor() func(string) *colors.Color {
389+
r := rand.New(rand.NewSource(time.Now().UnixNano()))
390+
randIndex := r.Intn(len(rColors))
391+
return rColors[randIndex]
392+
}
393+
394+
func (p *proxyapp) getId() string {
395+
id := uuid.New()
396+
if p.sniffnocolor {
397+
return fmt.Sprintf("%s", colors.WrapBrackets(id.String()))
398+
}
399+
return randColor()(fmt.Sprintf("%s", colors.WrapBrackets(id.String()))).String()
400+
}
401+
369402
func (p *proxyapp) colorizeStatus(code int, status string, bg bool) string {
370403
if bg {
371404
if code < 300 {
@@ -387,18 +420,21 @@ func (p *proxyapp) colorizeStatus(code int, status string, bg bool) string {
387420
return status
388421
}
389422

390-
func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodySaved, respBodySaved *[]byte, id uuid.UUID) string {
423+
func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodySaved, respBodySaved *[]byte, id string, ts bool) string {
391424
var sb strings.Builder
425+
if ts {
426+
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
427+
}
392428
if p.sniffnocolor {
393-
sb.WriteString(fmt.Sprintf("%s ", colors.WrapBrackets(id.String())))
394-
sb.WriteString(fmt.Sprintf("%s %s %s ", req.Method, req.URL, req.Proto))
429+
sb.WriteString(id)
430+
sb.WriteString(fmt.Sprintf(" %s %s %s ", req.Method, req.URL, req.Proto))
395431
if req.UserAgent() != "" {
396-
sb.WriteString(fmt.Sprintf("%s ", colors.WrapBrackets(req.UserAgent())))
432+
sb.WriteString(fmt.Sprintf("%s", colors.WrapBrackets(req.UserAgent())))
397433
}
398434
if req.ContentLength > 0 {
399-
sb.WriteString(fmt.Sprintf("Len: %d ", req.ContentLength))
435+
sb.WriteString(fmt.Sprintf(" Len: %d", req.ContentLength))
400436
}
401-
sb.WriteString("-> ")
437+
sb.WriteString(" -> ")
402438
sb.WriteString(fmt.Sprintf("%s %s ", resp.Proto, resp.Status))
403439
if resp.ContentLength > 0 {
404440
sb.WriteString(fmt.Sprintf("Len: %d", resp.ContentLength))
@@ -408,31 +444,31 @@ func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodyS
408444
if b != "" {
409445
sb.WriteString("\n")
410446
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
411-
sb.WriteString(fmt.Sprintf("%s ", colors.WrapBrackets(id.String())))
412-
sb.WriteString(fmt.Sprintf("req_body: %s", b))
447+
sb.WriteString(id)
448+
sb.WriteString(fmt.Sprintf(" req_body: %s", b))
413449
}
414450
}
415451
if p.body && len(*respBodySaved) > 0 {
416452
b := p.colorizeBody(respBodySaved)
417453
if b != "" {
418454
sb.WriteString("\n")
419455
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
420-
sb.WriteString(fmt.Sprintf("%s ", colors.WrapBrackets(id.String())))
421-
sb.WriteString(fmt.Sprintf("resp_body: %s", b))
456+
sb.WriteString(id)
457+
sb.WriteString(fmt.Sprintf(" resp_body: %s", b))
422458
}
423459
}
424460
} else {
425-
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", colors.WrapBrackets(id.String()))).String())
426-
sb.WriteString(colors.GreenBg(fmt.Sprintf("%s ", req.Method)).String())
461+
sb.WriteString(id)
462+
sb.WriteString(colors.Gray(fmt.Sprintf(" %s ", req.Method)).String())
427463
sb.WriteString(colors.YellowBg(fmt.Sprintf("%s ", req.URL)).String())
428-
sb.WriteString(colors.GreenBg(fmt.Sprintf("%s ", req.Proto)).String())
464+
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", req.Proto)).String())
429465
if req.UserAgent() != "" {
430-
sb.WriteString(colors.Gray(fmt.Sprintf("%s ", colors.WrapBrackets(req.UserAgent()))).String())
466+
sb.WriteString(colors.Gray(fmt.Sprintf("%s", colors.WrapBrackets(req.UserAgent()))).String())
431467
}
432468
if req.ContentLength > 0 {
433-
sb.WriteString(colors.BeigeBg(fmt.Sprintf("Len: %d ", req.ContentLength)).String())
469+
sb.WriteString(colors.BeigeBg(fmt.Sprintf(" Len: %d", req.ContentLength)).String())
434470
}
435-
sb.WriteString(colors.MagentaBg("-> ").String())
471+
sb.WriteString(colors.MagentaBg(" -> ").String())
436472
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", resp.Proto)).String())
437473
sb.WriteString(p.colorizeStatus(resp.StatusCode, fmt.Sprintf("%s ", resp.Status), true))
438474
if resp.ContentLength > 0 {
@@ -443,8 +479,8 @@ func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodyS
443479
if b != "" {
444480
sb.WriteString("\n")
445481
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
446-
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", colors.WrapBrackets(id.String()))).String())
447-
sb.WriteString(colors.GreenBg("req_body: ").String())
482+
sb.WriteString(id)
483+
sb.WriteString(colors.GreenBg(" req_body: ").String())
448484
sb.WriteString(b)
449485
}
450486
}
@@ -453,17 +489,85 @@ func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodyS
453489
if b != "" {
454490
sb.WriteString("\n")
455491
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
456-
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", colors.WrapBrackets(id.String()))).String())
457-
sb.WriteString(colors.GreenBg("resp_body: ").String())
492+
sb.WriteString(id)
493+
sb.WriteString(colors.GreenBg(" resp_body: ").String())
458494
sb.WriteString(b)
459495
}
460496
}
461497
}
462498
return sb.String()
463499
}
464500

465-
func (p *proxyapp) colorizeTLS(req *layers.TLSClientHello, resp *layers.TLSServerHello, id uuid.UUID) string {
466-
return "TODO:"
501+
func (p *proxyapp) colorizeTLS(req *layers.TLSClientHello, resp *layers.TLSServerHello, id string) string {
502+
var sb strings.Builder
503+
if p.sniffnocolor {
504+
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
505+
sb.WriteString(id)
506+
sb.WriteString(fmt.Sprintf(" %s:", req.TypeDesc))
507+
if req.Length > 0 {
508+
sb.WriteString(fmt.Sprintf(" Len: %d", req.Length))
509+
}
510+
if req.ServerName != nil && req.ServerName.SNName != "" {
511+
sb.WriteString(fmt.Sprintf(" SNI: %s", req.ServerName.SNName))
512+
}
513+
if req.Version != nil && req.Version.Desc != "" {
514+
sb.WriteString(fmt.Sprintf(" Ver: %s", req.Version.Desc))
515+
}
516+
if req.SessionID != "" {
517+
sb.WriteString(fmt.Sprintf(" SID: %s", req.SessionID))
518+
}
519+
if req.ALPN != nil {
520+
sb.WriteString(fmt.Sprintf(" ALPN: %v", req.ALPN))
521+
}
522+
sb.WriteString(" -> ")
523+
sb.WriteString(fmt.Sprintf("%s:", resp.TypeDesc))
524+
if resp.Length > 0 {
525+
sb.WriteString(fmt.Sprintf(" Len: %d", resp.Length))
526+
}
527+
if resp.CipherSuite != nil && resp.CipherSuite.Desc != "" {
528+
sb.WriteString(fmt.Sprintf(" CS: %s", resp.CipherSuite.Desc))
529+
}
530+
if resp.SupportedVersion != nil && resp.SupportedVersion.Desc != "" {
531+
sb.WriteString(fmt.Sprintf(" Ver: %s", resp.SupportedVersion.Desc))
532+
}
533+
if resp.ExtensionLength > 0 {
534+
sb.WriteString(fmt.Sprintf(" ExtLen: %d", resp.ExtensionLength))
535+
}
536+
} else {
537+
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
538+
sb.WriteString(id)
539+
sb.WriteString(colors.Magenta(fmt.Sprintf(" %s:", req.TypeDesc)).Bold())
540+
if req.Length > 0 {
541+
sb.WriteString(colors.BeigeBg(fmt.Sprintf(" Len: %d", req.Length)).String())
542+
}
543+
if req.ServerName != nil && req.ServerName.SNName != "" {
544+
sb.WriteString(colors.YellowBg(fmt.Sprintf(" SNI: %s", req.ServerName.SNName)).String())
545+
}
546+
if req.Version != nil && req.Version.Desc != "" {
547+
sb.WriteString(colors.GreenBg(fmt.Sprintf(" Ver: %s", req.Version.Desc)).String())
548+
}
549+
if req.SessionID != "" {
550+
sb.WriteString(colors.Gray(fmt.Sprintf(" SID: %s", req.SessionID)).String())
551+
}
552+
if req.ALPN != nil {
553+
sb.WriteString(colors.BlueBg(fmt.Sprintf(" ALPN: %v", req.ALPN)).String())
554+
}
555+
sb.WriteString(colors.MagentaBg(" -> ").String())
556+
sb.WriteString(colors.LightBlue(fmt.Sprintf("%s:", resp.TypeDesc)).Bold())
557+
if resp.Length > 0 {
558+
sb.WriteString(colors.BeigeBg(fmt.Sprintf(" Len: %d", resp.Length)).String())
559+
}
560+
if resp.CipherSuite != nil && resp.CipherSuite.Desc != "" {
561+
sb.WriteString(colors.Yellow(fmt.Sprintf(" CS: %s", resp.CipherSuite.Desc)).Bold())
562+
}
563+
if resp.SupportedVersion != nil && resp.SupportedVersion.Desc != "" {
564+
sb.WriteString(colors.GreenBg(fmt.Sprintf(" Ver: %s", resp.SupportedVersion.Desc)).String())
565+
}
566+
if resp.ExtensionLength > 0 {
567+
sb.WriteString(colors.BeigeBg(fmt.Sprintf(" ExtLen: %d", resp.ExtensionLength)).String())
568+
}
569+
}
570+
return sb.String()
467571
}
468572

469573
func (p *proxyapp) highlightPatterns(line string) (string, bool) {
@@ -587,8 +691,8 @@ func (p *proxyapp) handleForward(w http.ResponseWriter, r *http.Request) {
587691
}
588692
p.snifflogger.Log().Msg(fmt.Sprintf("[%s]", strings.Join(sniffheader, ",")))
589693
} else {
590-
id := uuid.New()
591-
p.snifflogger.Log().Msg(p.colorizeHTTP(req, resp, &reqBodySaved, &respBodySaved, id))
694+
id := p.getId()
695+
p.snifflogger.Log().Msg(p.colorizeHTTP(req, resp, &reqBodySaved, &respBodySaved, id, false))
592696
}
593697
}
594698
defer resp.Body.Close()
@@ -721,7 +825,7 @@ func (p *proxyapp) handleTunnel(w http.ResponseWriter, r *http.Request) {
721825
if p.sniff {
722826
wg.Add(1)
723827
sniffheader := make([]string, 0, 6)
724-
id := uuid.New()
828+
id := p.getId()
725829
if p.json {
726830
sniffheader = append(sniffheader, fmt.Sprintf("{\"connection\":{\"src_local\":%s,\"src_remote\":%s,\"dst_local\":%s,\"dst_remote\":%s}}",
727831
srcConn.LocalAddr(), srcConn.RemoteAddr(), dstConn.LocalAddr(), dstConn.RemoteAddr()))
@@ -730,14 +834,34 @@ func (p *proxyapp) handleTunnel(w http.ResponseWriter, r *http.Request) {
730834
sniffheader = append(sniffheader, string(j))
731835
}
732836
} else {
733-
// TODO:
837+
var sb strings.Builder
838+
if p.sniffnocolor {
839+
sb.WriteString(id)
840+
sb.WriteString(fmt.Sprintf(" Src: %s->%s -> Dst: %s->%s", srcConn.LocalAddr(), srcConn.RemoteAddr(), dstConn.LocalAddr(), dstConn.RemoteAddr()))
841+
sb.WriteString("\n")
842+
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
843+
sb.WriteString(id)
844+
sb.WriteString(fmt.Sprintf(" %s %s %s ", r.Method, r.URL, r.Proto))
845+
} else {
846+
sb.WriteString(id)
847+
sb.WriteString(colors.Green(fmt.Sprintf(" Src: %s->%s", srcConn.LocalAddr(), srcConn.RemoteAddr())).String())
848+
sb.WriteString(colors.Magenta(" -> ").String())
849+
sb.WriteString(colors.Blue(fmt.Sprintf("Dst: %s->%s", dstConn.LocalAddr(), dstConn.RemoteAddr())).String())
850+
sb.WriteString("\n")
851+
sb.WriteString(fmt.Sprintf("%s ", p.colorizeTimestamp()))
852+
sb.WriteString(id)
853+
sb.WriteString(colors.Gray(fmt.Sprintf(" %s ", r.Method)).String())
854+
sb.WriteString(colors.YellowBg(fmt.Sprintf("%s ", r.URL)).String())
855+
sb.WriteString(colors.BlueBg(fmt.Sprintf("%s ", r.Proto)).String())
856+
}
857+
sniffheader = append(sniffheader, sb.String())
734858
}
735859
go p.sniffreporter(&wg, &sniffheader, reqChan, respChan, id)
736860
}
737861
wg.Wait()
738862
}
739863

740-
func (p *proxyapp) colorizeReqResp(req, resp layers.Layer, sniffheader *[]string, id uuid.UUID) error {
864+
func (p *proxyapp) colorizeReqResp(req, resp layers.Layer, sniffheader *[]string, id string) error {
741865
switch reqt := req.(type) {
742866
case *layers.HTTPMessage:
743867
var reqBodySaved, respBodySaved []byte
@@ -765,7 +889,7 @@ func (p *proxyapp) colorizeReqResp(req, resp layers.Layer, sniffheader *[]string
765889
*sniffheader = append(*sniffheader, fmt.Sprintf("{\"resp_body\":%s}", respBodySaved))
766890
}
767891
} else {
768-
*sniffheader = append(*sniffheader, p.colorizeHTTP(reqt.Request, rest.Response, &reqBodySaved, &respBodySaved, id))
892+
*sniffheader = append(*sniffheader, p.colorizeHTTP(reqt.Request, rest.Response, &reqBodySaved, &respBodySaved, id, true))
769893
}
770894
case *layers.TLSMessage:
771895
var chs *layers.TLSClientHello
@@ -816,7 +940,7 @@ func (p *proxyapp) colorizeReqResp(req, resp layers.Layer, sniffheader *[]string
816940
return nil
817941
}
818942

819-
func (p *proxyapp) sniffreporter(wg *sync.WaitGroup, sniffheader *[]string, reqChan, respChan <-chan layers.Layer, id uuid.UUID) {
943+
func (p *proxyapp) sniffreporter(wg *sync.WaitGroup, sniffheader *[]string, reqChan, respChan <-chan layers.Layer, id string) {
820944
defer wg.Done()
821945
sniffheaderlen := len(*sniffheader)
822946
var reqQueue, respQueue []layers.Layer

tproxy_linux.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import (
99
"fmt"
1010
"net"
1111
"net/netip"
12+
"strings"
1213
"sync"
1314
"syscall"
1415
"time"
1516
"unsafe"
1617

17-
"github.com/google/uuid"
18+
"github.com/shadowy-pycoder/colors"
1819
"github.com/shadowy-pycoder/mshark/layers"
1920
"golang.org/x/net/proxy"
2021
"golang.org/x/sys/unix"
@@ -180,12 +181,23 @@ func (ts *tproxyServer) handleConnection(srcConn net.Conn) {
180181
if ts.pa.sniff {
181182
wg.Add(1)
182183
sniffheader := make([]string, 0, 6)
183-
id := uuid.New()
184+
id := ts.pa.getId()
184185
if ts.pa.json {
185186
sniffheader = append(sniffheader, fmt.Sprintf("{\"connection\":{\"tproxy_mode\":%s,\"src_local\":%s,\"src_remote\":%s,\"dst_local\":%s,\"dst_remote\":%s,\"original_dst\":%s}}",
186187
ts.pa.tproxyMode, srcConn.LocalAddr(), srcConn.RemoteAddr(), dstConn.LocalAddr(), dstConn.RemoteAddr(), dst))
187188
} else {
188-
// TODO:
189+
var sb strings.Builder
190+
if ts.pa.sniffnocolor {
191+
sb.WriteString(id)
192+
sb.WriteString(fmt.Sprintf(" Src: %s->%s -> Dst: %s->%s Orig: %s", srcConn.LocalAddr(), srcConn.RemoteAddr(), dstConn.LocalAddr(), dstConn.RemoteAddr(), dst))
193+
} else {
194+
sb.WriteString(id)
195+
sb.WriteString(colors.Green(fmt.Sprintf(" Src: %s->%s", srcConn.LocalAddr(), srcConn.RemoteAddr())).String())
196+
sb.WriteString(colors.Magenta(" -> ").String())
197+
sb.WriteString(colors.Blue(fmt.Sprintf("Dst: %s->%s ", dstConn.LocalAddr(), dstConn.RemoteAddr())).String())
198+
sb.WriteString(colors.BeigeBg(fmt.Sprintf("Orig Dst: %s", dst)).String())
199+
}
200+
sniffheader = append(sniffheader, sb.String())
189201
}
190202
go ts.pa.sniffreporter(&wg, &sniffheader, reqChan, respChan, id)
191203
}

0 commit comments

Comments
 (0)