11package gohpts
22
33import (
4+ "bufio"
45 "bytes"
56 "compress/gzip"
67 "context"
@@ -408,16 +409,22 @@ func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodyS
408409 sb .WriteString (fmt .Sprintf ("Len: %d" , resp .ContentLength ))
409410 }
410411 if p .body && len (* reqBodySaved ) > 0 {
411- sb .WriteString ("\n " )
412- sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
413- sb .WriteString (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ())))
414- sb .WriteString (fmt .Sprintf ("req_body: %s" , reqBodySaved ))
412+ b := p .colorizeBody (reqBodySaved )
413+ if b != "" {
414+ sb .WriteString ("\n " )
415+ sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
416+ sb .WriteString (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ())))
417+ sb .WriteString (fmt .Sprintf ("req_body: %s" , b ))
418+ }
415419 }
416420 if p .body && len (* respBodySaved ) > 0 {
417- sb .WriteString ("\n " )
418- sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
419- sb .WriteString (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ())))
420- sb .WriteString (fmt .Sprintf ("resp_body: %s" , respBodySaved ))
421+ b := p .colorizeBody (respBodySaved )
422+ if b != "" {
423+ sb .WriteString ("\n " )
424+ sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
425+ sb .WriteString (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ())))
426+ sb .WriteString (fmt .Sprintf ("resp_body: %s" , b ))
427+ }
421428 }
422429 } else {
423430 sb .WriteString (colors .BlueBg (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ()))).String ())
@@ -437,45 +444,63 @@ func (p *proxyapp) colorizeHTTP(req *http.Request, resp *http.Response, reqBodyS
437444 sb .WriteString (colors .BeigeBg (fmt .Sprintf ("Len: %d" , resp .ContentLength )).String ())
438445 }
439446 if p .body && len (* reqBodySaved ) > 0 {
440- sb .WriteString ("\n " )
441- sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
442- sb .WriteString (colors .BlueBg (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ()))).String ())
443- sb .WriteString (colors .GreenBg ("req_body: " ).String ())
444- sb .WriteString (p .colorizeBody (reqBodySaved ))
447+ b := p .colorizeBody (reqBodySaved )
448+ if b != "" {
449+ sb .WriteString ("\n " )
450+ sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
451+ sb .WriteString (colors .BlueBg (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ()))).String ())
452+ sb .WriteString (colors .GreenBg ("req_body: " ).String ())
453+ sb .WriteString (b )
454+ }
445455 }
446456 if p .body && len (* respBodySaved ) > 0 {
447- sb .WriteString ("\n " )
448- sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
449- sb .WriteString (colors .BlueBg (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ()))).String ())
450- sb .WriteString (colors .GreenBg ("resp_body: " ).String ())
451- sb .WriteString (p .colorizeBody (respBodySaved ))
457+ b := p .colorizeBody (respBodySaved )
458+ if b != "" {
459+ sb .WriteString ("\n " )
460+ sb .WriteString (fmt .Sprintf ("%s " , p .colorizeTimestamp ()))
461+ sb .WriteString (colors .BlueBg (fmt .Sprintf ("%s " , colors .WrapBrackets (id .String ()))).String ())
462+ sb .WriteString (colors .GreenBg ("resp_body: " ).String ())
463+ sb .WriteString (b )
464+ }
452465 }
453466 }
454467 p .snifflogger .Log ().Msg (sb .String ())
455468}
456469
457- func (p * proxyapp ) colorizeBody (b * []byte ) string {
458- s := fmt .Sprintf ("%s" , * b )
459- if p .sniffnocolor || ! p .body {
460- return s
470+ func (p * proxyapp ) highlightPatterns (line string ) (string , bool ) {
471+ matched := false
472+
473+ // line, matched = p.replace(line, ipPortPattern, colors.YellowBg, matched)
474+ // line, matched = p.replace(line, domainPattern, colors.YellowBg, matched)
475+ line , matched = p .replace (line , jwtPattern , colors .Magenta , matched )
476+ line , matched = p .replace (line , authPattern , colors .Magenta , matched )
477+ line , matched = p .replace (line , credsPattern , colors .GreenBg , matched )
478+
479+ return line , matched
480+ }
481+
482+ func (p * proxyapp ) replace (line string , re * regexp.Regexp , color func (string ) * colors.Color , matched bool ) (string , bool ) {
483+ if re .MatchString (line ) {
484+ matched = true
485+ if ! p .sniffnocolor {
486+ line = re .ReplaceAllStringFunc (line , func (s string ) string {
487+ return color (s ).String ()
488+ })
489+ }
461490 }
462- result := ipPortPattern .ReplaceAllStringFunc (s , func (match string ) string {
463- return colors .YellowBg (match ).String ()
464- })
465- result = domainPattern .ReplaceAllStringFunc (result , func (match string ) string {
466- return colors .YellowBg (match ).String ()
467- })
491+ return line , matched
492+ }
468493
469- result = jwtPattern . ReplaceAllStringFunc ( result , func ( match string ) string {
470- return colors . Magenta ( match ). String ( )
471- } )
472- result = authPattern . ReplaceAllStringFunc ( result , func ( match string ) string {
473- return colors . Magenta ( match ). String ()
474- })
475- result = credsPattern . ReplaceAllStringFunc ( result , func ( match string ) string {
476- return colors . GreenBg ( match ). String ()
477- })
478- return result
494+ func ( p * proxyapp ) colorizeBody ( b * [] byte ) string {
495+ matches := make ([] string , 0 , 3 )
496+ scanner := bufio . NewScanner ( bytes . NewReader ( * b ) )
497+ for scanner . Scan () {
498+ line := scanner . Text ()
499+ if highlighted , ok := p . highlightPatterns ( line ); ok {
500+ matches = append ( matches , strings . Trim ( highlighted , " \r \n \t " ))
501+ }
502+ }
503+ return strings . Join ( matches , " \n " )
479504}
480505
481506func (p * proxyapp ) colorizeTimestamp () string {
@@ -535,11 +560,11 @@ func (p *proxyapp) handleForward(w http.ResponseWriter, r *http.Request) {
535560 respBodySaved , _ = io .ReadAll (io .LimitReader (resp .Body , maxBodySize ))
536561 resp .Body = io .NopCloser (io .MultiReader (bytes .NewReader (respBodySaved ), resp .Body ))
537562 }
538- }
539- if resp . Header . Get ( "Content-Encoding" ) == " gzip" {
540- gzr , err := gzip . NewReader ( bytes . NewReader ( respBodySaved ))
541- if err == nil {
542- respBodySaved , _ = io . ReadAll ( gzr )
563+ if resp . Header . Get ( "Content-Encoding" ) == "gzip" {
564+ gzr , err := gzip . NewReader ( bytes . NewReader ( respBodySaved ))
565+ if err == nil {
566+ respBodySaved , _ = io . ReadAll ( gzr )
567+ }
543568 }
544569 }
545570 if p .json {
0 commit comments