-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
executable file
·148 lines (143 loc) · 5.55 KB
/
handler.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/python3.5
from http.server import BaseHTTPRequestHandler, HTTPServer
import threading, pickle, re, subprocess, urllib, os, pathlib, tools
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
# self.myLog()
f_type_map = {'.html': 'text/html', '.css': 'text/css', '.ico': 'image/x-icon', '.jpg': 'image/jpeg', '.png': 'image/png'}
t_type = re.compile('\/|(\.\w*)')
r_file = self.path.split('?')
requested_type = t_type.findall(self.path)
ex = requested_type[-1]
root = pathlib.PurePath('/app/files')
try:
cookies = [h.split('=') for h in self.headers['Cookie'].split(';')]
c = dict()
for x, y in cookies:
print(x, y)
c[x] = y
except:
cookies = None
c = None
tools.cookies(cookie=c, method='write')
if ex != '.py' and ex != '':
res = 200
fileToSend = None
hds = []
f = root.joinpath(r_file[0].strip('/'))
if not pathlib.Path(f).exists():
res = 404
elif pathlib.Path(f).is_dir():
if pathlib.Path(f / 'index.html').exists():
f = f / 'index.html'
with open(str(f), 'r') as file:
fileToSend = file.read()
hds.append(('content-type', 'text/html'))
else:
res = 404
else:
try:
with open(str(f), 'r') as file:
fileToSend = file.read()
hds.append(('content-type', f_type_map[ex]))
except UnicodeDecodeError:
with open(str(f), 'rb') as file:
fileToSend = file.read()
hds.append(('content-type', f_type_map[ex]))
if res == 200:
self.send_response(res)
else:
self.send_error(res)
for item in hds:
i1, i2 = item
self.send_header(i1, i2)
self.end_headers()
if fileToSend is not None:
try:
self.wfile.write(fileToSend.encode('utf-8'))
except AttributeError:
self.wfile.write(fileToSend)
return
else:
try:
with open('/app/files/session.dat', 'wb') as file:
d = dict()
params = dict()
for pair in list(r_file[1].split('&')):
key, value = pair.split('=')
params[key] = str(value)
d['params'] = params
pickle.dump(d, file)
except IndexError:
pass
self.send_response(200)
self.send_header('Content-Encoding', 'utf-8')
self.send_header('Content-Type', 'text/html')
file = subprocess.run(['python', '/app/files' + r_file[0]], stdout=subprocess.PIPE)
try:
with open('/app/files/hds.dat', 'rb') as f:
for key, value in pickle.load(f).items():
print(key, value)
self.send_header(key, value)
except FileNotFoundError:
pass
self.end_headers()
self.wfile.write(file.stdout)
def do_POST(self):
# self.myLog()
try:
res = 200
f = None
hds = [('Content-Encoding', 'utf-8')]
try:
with open('/home/jack/projects/server/cgi-bin%s'% r_file[0]) as file:
f = file.read()
#f = 'This is my secret message'
#self.wfile.write(bytes(f, 'utf8'))
except UnicodeDecodeError:
with open('/home/jack/projects/server/cgi-bin%s'% r_file[0], 'rb') as file:
f = file
#self.wfile.write(f)
except IOError:
res = 404
f = None
#self.send_error(404, 'File Not Found')
#self.wfile.write(bytes('404 file not found', 'utf8'))
except KeyError:
#self.send_response(200)
#self.send_header('Content-type', 'text/html')
#self.end_headers()
with open('/home/jack/projects/server/cgi-bin') as file:
#f = 'This is my secret message'
f = file
#self.wfile.write(bytes(f, 'utf8'))
if res == 200:
self.send_response(res)
else:
self.send_error(res)
for item in hds:
i1, i2 = item
self.send_header(i1, i2)
self.end_headers()
if f != None:
file = subprocess.run(['python', '/home/jack/projects/server/cgi-bin%s'% r_file[0]], stdout=subprocess.PIPE)
self.wfile.write(file.stdout.encode('utf-8'))
else:
pass
return
def do_OPTIONS(self):
methods = ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'CONNECT', 'OPTIONS', 'TRACE']
sup_methods = []
self.send_response(200)
for m in methods:
if hasattr(self, 'do_' + m):
sup_methods.append(m)
else:
pass
print(sup_methods)
meth = ', '.join(sup_methods)
self.send_header('Allow', meth)
self.end_headers()
def myLog(self):
with open('/home/jack/projects/.spy/data/ips.dat', 'ab') as f:
pickle.dump(self.client_address[0], f)