Path: blob/dev/pkg/protocols/common/helpers/responsehighlighter/response_highlighter.go
2073 views
package responsehighlighter12import (3"sort"4"strconv"5"strings"67"github.com/logrusorgru/aurora"89"github.com/projectdiscovery/nuclei/v3/pkg/operators"10)1112var colorFunction = aurora.Green1314func Highlight(operatorResult *operators.Result, response string, noColor, hexDump bool) string {15result := response16if operatorResult != nil && !noColor {17for _, currentMatch := range getSortedMatches(operatorResult) {18if hexDump {19highlightedHexDump, err := toHighLightedHexDump(result, currentMatch)20if err == nil {21result = highlightedHexDump.String()22}23} else {24result = highlightASCII(currentMatch, result)25}26}27}2829return result30}3132func highlightASCII(currentMatch string, result string) string {33var coloredMatchBuilder strings.Builder34for _, char := range currentMatch {35coloredMatchBuilder.WriteString(addColor(string(char)))36}3738return strings.ReplaceAll(result, currentMatch, coloredMatchBuilder.String())39}4041func getSortedMatches(operatorResult *operators.Result) []string {42sortedMatches := make([]string, 0, len(operatorResult.Matches))43for _, matches := range operatorResult.Matches {44sortedMatches = append(sortedMatches, matches...)45}4647sort.Slice(sortedMatches, func(i, j int) bool {48return len(sortedMatches[i]) > len(sortedMatches[j])49})50return sortedMatches51}5253func CreateStatusCodeSnippet(response string, statusCode int) string {54if strings.HasPrefix(response, "HTTP/") {55strStatusCode := strconv.Itoa(statusCode)56return response[:strings.Index(response, strStatusCode)+len(strStatusCode)]57}58return ""59}6061func addColor(value string) string {62return colorFunction(value).String()63}646566