2015-05-02 04:17:00 +00:00
|
|
|
import time
|
2016-10-19 22:27:05 +00:00
|
|
|
from mitmproxy import exceptions
|
2015-05-02 04:17:00 +00:00
|
|
|
|
|
|
|
BLOCKSIZE = 1024
|
2015-06-26 22:46:12 +00:00
|
|
|
# It's not clear what the upper limit for time.sleep is. It's lower than the
|
|
|
|
# maximum int or float. 1 year should do.
|
|
|
|
FOREVER = 60 * 60 * 24 * 365
|
2015-05-02 04:17:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_chunk(fp, val, blocksize, start, end):
|
|
|
|
"""
|
|
|
|
(start, end): Inclusive lower bound, exclusive upper bound.
|
|
|
|
"""
|
|
|
|
for i in range(start, end, blocksize):
|
|
|
|
fp.write(
|
|
|
|
val[i:min(i + blocksize, end)]
|
|
|
|
)
|
|
|
|
return end - start
|
|
|
|
|
|
|
|
|
|
|
|
def write_values(fp, vals, actions, sofar=0, blocksize=BLOCKSIZE):
|
|
|
|
"""
|
|
|
|
vals: A list of values, which may be strings or Value objects.
|
|
|
|
|
2016-06-04 09:30:31 +00:00
|
|
|
actions: A list of (offset, action, arg) tuples. Action may be "inject",
|
|
|
|
"pause" or "disconnect".
|
2015-05-02 04:17:00 +00:00
|
|
|
|
|
|
|
Both vals and actions are in reverse order, with the first items last.
|
|
|
|
|
|
|
|
Return True if connection should disconnect.
|
|
|
|
"""
|
|
|
|
sofar = 0
|
|
|
|
try:
|
|
|
|
while vals:
|
|
|
|
v = vals.pop()
|
|
|
|
offset = 0
|
|
|
|
while actions and actions[-1][0] < (sofar + len(v)):
|
|
|
|
a = actions.pop()
|
|
|
|
offset += send_chunk(
|
|
|
|
fp,
|
|
|
|
v,
|
|
|
|
blocksize,
|
|
|
|
offset,
|
|
|
|
a[0] - sofar - offset
|
|
|
|
)
|
|
|
|
if a[1] == "pause":
|
2015-06-26 22:46:12 +00:00
|
|
|
time.sleep(
|
|
|
|
FOREVER if a[2] == "f" else a[2]
|
|
|
|
)
|
2015-05-02 04:17:00 +00:00
|
|
|
elif a[1] == "disconnect":
|
|
|
|
return True
|
|
|
|
elif a[1] == "inject":
|
|
|
|
send_chunk(fp, a[2], blocksize, 0, len(a[2]))
|
|
|
|
send_chunk(fp, v, blocksize, offset, len(v))
|
|
|
|
sofar += len(v)
|
|
|
|
# Remainders
|
|
|
|
while actions:
|
|
|
|
a = actions.pop()
|
|
|
|
if a[1] == "pause":
|
|
|
|
time.sleep(a[2])
|
|
|
|
elif a[1] == "disconnect":
|
|
|
|
return True
|
|
|
|
elif a[1] == "inject":
|
|
|
|
send_chunk(fp, a[2], blocksize, 0, len(a[2]))
|
2016-10-19 22:27:05 +00:00
|
|
|
except exceptions.TcpDisconnect: # pragma: no cover
|
2015-05-02 04:17:00 +00:00
|
|
|
return True
|