-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.py
executable file
·114 lines (97 loc) · 3.46 KB
/
proxy.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/python
import SimpleHTTPServer
import SocketServer
import httplib
import logging
import subprocess
import sys
from pprint import pprint
from AcuLink import AcuLink
PORT = 80
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
logging.warning("======= GET STARTED =======")
logging.warning(self.headers)
url = "http://" + self.headers.getheader('host') + self.path
self.make_http_request("GET", url)
def do_POST(self):
# logging.warning("======= POST STARTED =======")
# logging.warning(self.headers)
length = int(self.headers.getheader('content-length'))
data = self.rfile.read(length)
# logging.warning("======= POST VALUES =======")
# logging.warning(data)
# logging.warning("\n")
url = "http://" + self.headers.getheader('host') + self.path
self.make_http_request("POST", url, data)
# SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def resolve_host(self, host):
out, error = self.exec_command("nslookup %s - 8.8.8.8" % host)
found = False
for line in out.split("\n"):
if found:
ip = line.split(" ")[1]
# print "returning ip", ip
return ip
if line.startswith(host) and "canonical name" in line:
host = line.split(" ")[-1][:-1]
if line.endswith(host):
found = True
print out
print "returning input", host
return host
def exec_command(self, command):
# print "====>", command
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(out, error) = proc.communicate()
if error:
print >> sys.stderr, error
return out, error
def make_http_request(self, method, url, data=None):
print "requesting {0}: {1}".format(method, url)
print "data: {0}".format(data)
parts = url.split("/")
host = parts[2].split(":")[0]
host = self.resolve_host(host)
print host
path = "/" + "/".join(parts[3:])
if url.startswith("https://"):
conn = httplib.HTTPSConnection(host)
else:
conn = httplib.HTTPConnection(host)
conn.request(method, path, data, self.headers.dict)
response = conn.getresponse()
print response.status, response.reason
content = response.read()
conn.close()
print "==> {0}".format(content[:256])
self.send_response(response.status)
for header in response.msg.headers:
self.wfile.write(header)
self.end_headers()
self.wfile.write(content)
self.wfile.close()
if aculink.can_handle(url, data):
pprint(aculink.handle(url, data))
class Server:
def __init__(self, port, handler):
self.port = port
self.handler = handler
self.httpd = SocketServer.TCPServer(("", self.port), self.handler)
def main_loop(self):
print "serving at port", self.port
self.httpd.serve_forever()
def shutdown(self):
self.httpd.shutdown()
if __name__ == '__main__':
aculink = AcuLink()
server_handler = ServerHandler
server = Server(PORT, server_handler)
try:
server.main_loop()
except KeyboardInterrupt:
print "Ctrl C - Stopping server"
server.shutdown()
sys.exit(1)
finally:
server.shutdown()