This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
128 lines (114 loc) · 4.78 KB
/
server.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
from argparse import ArgumentParser
from threading import Thread
from HTTPServer.TCPServer import ThreadingServer
from HTTPServer.RequestHandle import *
import time, socket
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP, AES
from Crypto.Util.Padding import unpad, pad
parser = ArgumentParser()
parser.add_argument('--ip', '-i', type=str)
parser.add_argument('--port', '-p', type=int)
args = parser.parse_args()
PORT = args.port or 8080
IP = args.ip or ''
class RequestHandler:
def __init__(self, request, client_address, server):
self.request = request
self.client_address = client_address
self.server = server
self.cipher = None
self.decryptor = None
self.handle()
def handle(self):
con = self.request
first = True
if encrypt:
rsa_key = RSA.generate(2048)
con.sendall(rsa_key.public_key().export_key())
key = PKCS1_OAEP.new(rsa_key).decrypt(con.recv(1024))
iv = b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F'
self.cipher = AES.new(key, AES.MODE_CBC, iv)
self.decryptor = AES.new(key, AES.MODE_CBC, iv)
start_time = time.time()
print(con.getpeername())
while True:
# Receive data
recv_data = bytes()
try:
if timeout - time.time() + start_time <= 0:
raise socket.timeout
con.settimeout(timeout - time.time() + start_time)
while True:
data = con.recv(1024)
recv_data += data
if len(data) < 1024:
con.settimeout(None)
break
if recv_data == b'':
continue
except socket.timeout:
con.close()
return # Timeout
if encrypt:
recv_data = unpad(self.decryptor.decrypt(recv_data), 16, style='pkcs7')
print(recv_data.decode())
method, path, protocol, headers, msgdata = parse_request(recv_data.strip(b'\r\n'))
if protocol.upper() != http_version:
self.send(parse_header(headers, 505))
continue
# Send Icon
if path == '/favicon.ico':
self.send(process_icon(headers))
continue
# Authentication and Cookie
if first:
headers = authenticate(headers)
first = False
if headers.get('WWW-Authenticate'):
headers['Content-Length'] = 0
self.send(parse_header(headers, 401) + b'\r\n')
continue
# Process request
path, parameters = parse_path(path)
if path.strip('/') == command[0]:
if method.upper() != 'POST':
headers['Content-Length'] = 0
self.send(parse_header(headers, 405) + b'\r\n')
continue
if 'path' not in parameters:
headers['Content-Length'] = 0
self.send(parse_header(headers, 400) + b'\r\n')
continue
self.send(process_upload(parameters['path'], headers, msgdata))
elif path.strip('/') == command[1]:
if method.upper() != 'POST':
headers['Content-Length'] = 0
self.send(parse_header(headers, 405) + b'\r\n')
continue
if 'path' not in parameters:
headers['Content-Length'] = 0
self.send(parse_header(headers, 400) + b'\r\n')
continue
self.send(process_delete(parameters['path'], headers))
else:
if method.upper() == 'POST':
headers['Content-Length'] = 0
self.send(parse_header(headers, 405) + b'\r\n')
continue
sustech = 'SUSTech-HTTP' in parameters and parameters['SUSTech-HTTP'] == '1'
if 'chunked' in parameters:
headers['Chunked'] = parameters['chunked']
head = method.upper() == 'HEAD'
process_download(con, path.strip('/'), headers, sustech, head, self.cipher)
if headers['Connection'].lower() == 'close':
con.close()
return
def send(self, data):
if encrypt:
self.request.sendall(self.cipher.encrypt(pad(data, 16, style='pkcs7')))
else:
self.request.sendall(data)
if __name__ == "__main__":
server = ThreadingServer((IP, PORT), RequestHandler)
Thread(target=server.serve_forever).start()