| 1 | package filter |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | |
| 9 | "github.com/ffuf/ffuf/v2/pkg/ffuf" |
| 10 | ) |
| 11 | |
| 12 | type TimeFilter struct { |
| 13 | ms int64 // milliseconds since first response byte |
| 14 | gt bool // filter if response time is greater than |
| 15 | lt bool // filter if response time is less than |
| 16 | valueRaw string |
| 17 | } |
| 18 | |
| 19 | func NewTimeFilter(value string) (ffuf.FilterProvider, error) { |
| 20 | var milliseconds int64 |
| 21 | gt, lt := false, false |
| 22 | |
| 23 | gt = strings.HasPrefix(value, ">") |
| 24 | lt = strings.HasPrefix(value, "<") |
| 25 | |
| 26 | if (!lt && !gt) || (lt && gt) { |
| 27 | return &TimeFilter{}, fmt.Errorf("Time filter or matcher (-ft / -mt): invalid value: %s", value) |
| 28 | } |
| 29 | |
| 30 | milliseconds, err := strconv.ParseInt(value[1:], 10, 64) |
| 31 | if err != nil { |
| 32 | return &TimeFilter{}, fmt.Errorf("Time filter or matcher (-ft / -mt): invalid value: %s", value) |
| 33 | } |
| 34 | return &TimeFilter{ms: milliseconds, gt: gt, lt: lt, valueRaw: value}, nil |
| 35 | } |
| 36 | |
| 37 | func (f *TimeFilter) MarshalJSON() ([]byte, error) { |
| 38 | return json.Marshal(&struct { |
| 39 | Value string `json:"value"` |
| 40 | }{ |
| 41 | Value: f.valueRaw, |
| 42 | }) |
| 43 | } |
| 44 | |
| 45 | func (f *TimeFilter) Filter(response *ffuf.Response) (bool, error) { |
| 46 | if f.gt { |
| 47 | if response.Duration.Milliseconds() > f.ms { |
| 48 | return true, nil |
| 49 | } |
| 50 | |
| 51 | } else if f.lt { |
| 52 | if response.Duration.Milliseconds() < f.ms { |
| 53 | return true, nil |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return false, nil |
| 58 | } |
| 59 | |
| 60 | func (f *TimeFilter) Repr() string { |
| 61 | return f.valueRaw |
| 62 | } |
| 63 | |
| 64 | func (f *TimeFilter) ReprVerbose() string { |
| 65 | return fmt.Sprintf("Response time: %s", f.Repr()) |
| 66 | } |