/
output.go
183 lines (163 loc) · 3.6 KB
/
output.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package libgrequest
import (
"fmt"
"net/url"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
)
func parseurl(uarg string) string {
u, err := url.Parse(uarg)
if err != nil {
panic(err)
}
return u.Host + u.Path
}
// PrintChars :
// probably a better way to do this
func PrintChars(s *State, r *Result) {
if s.Filter.Contains(r.Chars) == s.Show {
if s.NoColor {
PrintNoColorFn(s, r)
} else {
PrintColorFn(s, r)
}
}
}
// PrintWords :
func PrintWords(s *State, r *Result) {
if s.Filter.Contains(r.Words) == s.Show {
if s.NoColor {
PrintNoColorFn(s, r)
} else {
PrintColorFn(s, r)
}
}
}
// PrintStatus :
func PrintStatus(s *State, r *Result) {
if s.Filter.Contains(r.Code) == s.Show { // issue nil
if s.NoColor {
PrintNoColorFn(s, r)
} else {
PrintColorFn(s, r)
}
}
}
// PrintLines :
func PrintLines(s *State, r *Result) {
if s.Filter.Contains(r.Lines) == s.Show {
if s.NoColor {
PrintNoColorFn(s, r)
} else {
PrintColorFn(s, r)
}
}
}
// ParsePrintFilterArgs :
func ParsePrintFilterArgs(s *State, fs string) {
m := regexp.MustCompile("(sl|sc|sw|sh|hc|hl|hh|hw)").FindString(fs)
if string(m[0]) == "s" {
s.Show = true
} else {
s.Show = false
}
switch m[1:] {
case "c":
s.Printer = PrintStatus
case "l":
s.Printer = PrintLines
case "w":
s.Printer = PrintWords
case "h":
s.Printer = PrintChars
}
}
// WriteToFile :
func WriteToFile(output string, s *State) {
_, err := s.OutputFile.WriteString(output)
if err != nil {
panic("[!] Unable to write to file " + s.OutputFileName)
}
}
// PrintNoColorFn : print page into to stdout
func PrintNoColorFn(s *State, r *Result) {
output := ""
if !s.NoStatus {
code := strconv.FormatInt(r.Code, 10)
output += fmt.Sprintf(" Code %-10s ", code)
}
if r.Chars >= int64(0) {
output += fmt.Sprintf(" C %-6.8s", res2string(r.Chars))
output += fmt.Sprintf(" W %-6.8s", res2string(r.Words))
output += fmt.Sprintf(" L %-6.8s", res2string(r.Lines))
}
output += fmt.Sprintf("%-30s", r.URL)
output += "\n"
if s.PrintBody == true {
output += "\n"
output += string(r.Body)
output += "\n"
}
if s.OutputFile != nil {
WriteToFile(output, s)
}
fmt.Printf(output)
}
// PrintColorFn : prints corized page info to stdout
func PrintColorFn(s *State, r *Result) {
if r == nil {
return
}
if s.NoColor == true {
PrintNoColorFn(s, r)
return
}
// color funcs
yellow := color.New(color.FgYellow).SprintFunc()
green := color.New(color.FgGreen).SprintFunc()
red := color.New(color.FgHiRed).SprintFunc()
cyan := color.New(color.FgHiCyan).SprintFunc()
// write output line
code := res2string(r.Code)
output := ""
if !s.NoStatus {
if strings.HasPrefix(code, "2") {
code = green(code)
}
if strings.HasPrefix(code, "3") {
code = yellow(code)
}
if strings.HasPrefix(code, "4") {
code = red(code)
}
if strings.HasPrefix(code, "5") {
code = cyan(code)
}
output += fmt.Sprintf(" Code %-10s ", code)
}
// print result struct data
if r.Chars >= int64(0) {
output += fmt.Sprintf("%s %-6.8s", " C", res2string(r.Chars)) // note to self: color for result int64s add about 10 spaces
output += fmt.Sprintf("%s %-5.5s", " W", res2string(r.Words))
output += fmt.Sprintf("%s %-5.5s", " L", res2string(r.Lines))
}
output += fmt.Sprintf("%-30s", r.URL)
output += "\n"
if s.PrintBody == true {
output += "\n"
output += yellow(string(r.Body))
output += "\n"
}
if s.OutputFile != nil {
WriteToFile(output, s)
}
// ignore initial url
re := regexp.MustCompile("FUZ(Z|[0-9]Z)")
match := re.FindString(output)
if len(match) == 0 {
// print one line output to stdout
fmt.Printf(output)
}
}