-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (65 loc) · 2.98 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import urllib
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
import requests
# http://python.vic-tim.de/images/
# http://python.vic-tim.de/proxy/form.html
class MyHandler(BaseHTTPRequestHandler):
# replace images with fake images and manipulate title of webpage
def do_GET(self):
print("Path: " + self.path)
if self.path[-4:] == ".jpg":
self.send_response(200)
self.send_header("content-type", "image/jpeg")
self.end_headers()
with open("cat.jpg", "rb") as img:
self.wfile.write(img.read())
else:
with requests.get(self.path, stream=True) as res:
print(res.headers["Content-Type"])
if "text/html" in res.headers["Content-Type"]:
self.send_response(res.status_code)
self.send_header("content-type", "text/html")
self.end_headers()
content = str(res.content, "utf-8")
content = content.replace("Bilder", "Katzen")
self.wfile.write(content.encode())
else:
self.send_response(res.status_code)
for key, value in self.headers.items():
self.send_header(key, value)
self.end_headers()
self.wfile.write(res.raw.read())
# Methode will proxy the POST requests
# if the form is filled with the message top
# it will be replaced with flop and send to the server
# Original received response from server is changed
# that client will never now that top has changed to flop
def do_POST(self):
print(self.path)
print(self.headers)
print(self.headers["content-type"])
if self.headers["content-type"] == "application/x-www-form-urlencoded":
length = int(self.headers["content-length"])
form = str(self.rfile.read(length), "utf-8")
data = urllib.parse.parse_qs(form)
if "content" in data:
if type(data["content"]) == list:
if len(data["content"]) == 1:
data["content"][0] = data["content"][0].replace("top", "flop")
with requests.post(self.path, data=data, stream=True) as res:
self.send_response(res.status_code)
self.send_header("content-type", "text/html")
self.end_headers()
print(res.content)
content = str(res.content, "utf-8")
# Change back to original value so client will never now
# that the message was manipulated by the proxy
content.replace("flop", "top")
print(self.headers)
self.wfile.write(content.encode())
class ThreadingHTTPMixing(ThreadingMixIn, HTTPServer):
pass
address = ("127.0.0.1", 10080)
server = HTTPServer(address, MyHandler)
server.serve_forever()