Skip to content

Commit e069771

Browse files
authored
Merge pull request #2293 from ayanrajpoot10/dev
Strip ANSI color codes from output file while keeping console colors
2 parents f94f72d + dfcc970 commit e069771

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

.github/auto_assign.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
addReviewers: true
2+
reviewers:
3+
- dogancanbakir
4+
- dwisiswant0
5+
- mzack9999
6+
7+
numberOfReviewers: 1
8+
skipKeywords:
9+
- '@dependabot'

runner/runner.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func (r *Runner) HTTPX() *httpx.HTTPX {
101101
// picked based on try-fail but it seems to close to one it's used https://www.hackerfactor.com/blog/index.php?/archives/432-Looks-Like-It.html#c1992
102102
var hammingDistanceThreshold int = 22
103103

104+
// regex for stripping ANSI codes
105+
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`)
106+
104107
type pHashCluster struct {
105108
BasePHash uint64 `json:"base_phash,omitempty" csv:"base_phash"`
106109
Hashes []pHashUrl `json:"hashes,omitempty" csv:"hashes"`
@@ -1180,7 +1183,7 @@ func (r *Runner) RunEnumeration() {
11801183

11811184
//nolint:errcheck // this method needs a small refactor to reduce complexity
11821185
if plainFile != nil {
1183-
plainFile.WriteString(resp.str + "\n")
1186+
plainFile.WriteString(handleStripAnsiCharacters(resp.str, r.options.NoColor) + "\n")
11841187
}
11851188

11861189
if len(r.options.ExcludeOutputFields) > 0 {
@@ -1345,6 +1348,13 @@ func (r *Runner) RunEnumeration() {
13451348
}
13461349
}
13471350

1351+
func handleStripAnsiCharacters(data string, skip bool) string {
1352+
if skip {
1353+
return data
1354+
}
1355+
return stripANSI(data)
1356+
}
1357+
13481358
func logFilteredErrorPage(fileName, url string) {
13491359
dir := filepath.Dir(fileName)
13501360
if !fileutil.FolderExists(dir) {
@@ -2699,3 +2709,8 @@ func isWebSocket(resp *httpx.Response) bool {
26992709
}
27002710
return false
27012711
}
2712+
2713+
// stripANSI removes ANSI color codes from a string using pre-compiled regex
2714+
func stripANSI(str string) string {
2715+
return ansiRegex.ReplaceAllString(str, "")
2716+
}

0 commit comments

Comments
 (0)