-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.py
60 lines (51 loc) · 1.67 KB
/
processor.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
50
51
52
53
54
55
56
57
58
59
60
from os.path import join, isdir
from os import listdir, remove
from json import dumps, loads
import argparse
import requests
import base64
from time import sleep
parser = argparse.ArgumentParser(description="Processes files on a certain disk.")
parser.add_argument("path", metavar="path", type=str, help="The path to the disk")
args = parser.parse_args()
PATH = args.path
def list_requests():
files = listdir(join(PATH, "req"))
return files
def parse_request(name):
f = open(join(PATH, "req", name), "r")
obj = loads(f.read())
f.close()
return obj
def make_request(obj):
r = requests.request(obj["method"], obj["url"], data=base64.b64decode(obj["data"]), headers=obj["headers"])
return r
def write_response(name, res: requests.Response):
f = open(join(PATH, "res", name), "w")
obj = {
"status": res.status_code,
"headers": dict(res.headers),
"body": base64.b64encode(res.content).decode()
}
f.write(dumps(obj))
f.close()
def delete_request(name):
remove(join(PATH, "req", name))
def main():
print("Hello! Insert the USB stick to write the responses to it!")
while True:
if isdir(join(PATH, "req")) and isdir(join(PATH, "res")):
reqs = list_requests()
print(f"Found {len(reqs)} requests.")
for i in reqs:
req = parse_request(i)
print(f"> {req['method']} {req['url']}")
res = make_request(req)
print(f"< {res.status_code}")
write_response(i, res)
delete_request(i)
else:
print("No valid disk found.")
sleep(0.5)
if __name__ == "__main__":
main()