-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsleeper.py
49 lines (40 loc) · 1.41 KB
/
sleeper.py
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
import time
from typing import Optional
from mitmproxy import ctx
from mitmproxy import flowfilter
from mitmproxy.exceptions import OptionsError
from mitmproxy.flow import Flow
from mitmproxy.optmanager import OptManager
from mitmproxy.script import concurrent
matchall = flowfilter.parse(".")
class Sleeper:
def __init__(self):
self.filter: Optional[flowfilter.TFilter] = matchall
def load(self, loader: OptManager):
loader.add_option(
"sleep", Optional[int], None,
"Delay client requests (milliseconds)",
)
loader.add_option(
"sleep_filter", Optional[str], None,
"Apply delay to flows which match the filter"
)
def configure(self, updates: set[str]):
if "sleep" in updates:
sleep = ctx.options.sleep
if sleep and sleep < 0:
raise OptionsError("'sleep' must be >= 0")
if "sleep_filter" in updates:
filt_str = ctx.options.sleep_filter
filt = matchall if not filt_str else flowfilter.parse(filt_str)
if not filt:
raise OptionsError("Invalid filter expression: %s" % filt_str)
self.filter = filt
@concurrent
def request(self, flow: Flow):
delay = ctx.options.sleep
if delay and delay > 0 and flowfilter.match(self.filter, flow):
time.sleep(delay / 1000)
addons = [
Sleeper()
]