forked from ffuf/ffuf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
size.go
68 lines (59 loc) · 1.52 KB
/
size.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
package filter
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"github.com/ffuf/ffuf/v2/pkg/ffuf"
)
type SizeFilter struct {
Value []ffuf.ValueRange
}
func NewSizeFilter(value string) (ffuf.FilterProvider, error) {
var intranges []ffuf.ValueRange
for _, sv := range strings.Split(value, ",") {
vr, err := ffuf.ValueRangeFromString(sv)
if err != nil {
return &SizeFilter{}, fmt.Errorf("Size filter or matcher (-fs / -ms): invalid value: %s", sv)
}
intranges = append(intranges, vr)
}
return &SizeFilter{Value: intranges}, nil
}
func (f *SizeFilter) MarshalJSON() ([]byte, error) {
value := make([]string, 0)
for _, v := range f.Value {
if v.Min == v.Max {
value = append(value, strconv.FormatInt(v.Min, 10))
} else {
value = append(value, fmt.Sprintf("%d-%d", v.Min, v.Max))
}
}
return json.Marshal(&struct {
Value string `json:"value"`
}{
Value: strings.Join(value, ","),
})
}
func (f *SizeFilter) Filter(response *ffuf.Response) (bool, error) {
for _, iv := range f.Value {
if iv.Min <= response.ContentLength && response.ContentLength <= iv.Max {
return true, nil
}
}
return false, nil
}
func (f *SizeFilter) Repr() string {
var strval []string
for _, iv := range f.Value {
if iv.Min == iv.Max {
strval = append(strval, strconv.Itoa(int(iv.Min)))
} else {
strval = append(strval, strconv.Itoa(int(iv.Min))+"-"+strconv.Itoa(int(iv.Max)))
}
}
return strings.Join(strval, ",")
}
func (f *SizeFilter) ReprVerbose() string {
return fmt.Sprintf("Response size: %s", f.Repr())
}