-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
58 lines (49 loc) · 1.66 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
import asyncio
from asyncio import StreamReader, StreamWriter
import logging
from re import search
logging.basicConfig(level=logging.INFO)
# forward stream
async def pipe(r: StreamReader, w: StreamWriter):
try:
while not r.at_eof():
w.write(await r.read(4096))
finally:
w.close()
# handle every connection
async def conn_handler(lr: StreamReader, lw: StreamWriter):
data = (await lr.read(4096)).decode()
logging.debug(f'Got connection from {lw.get_extra_info("peername")[0]}')
logging.debug(data)
try:
# for HTTPS or any except HTTP
if data.startswith('CONNECT'):
host, port = data.splitlines()[0].split(' ')[1].split(':')
rr, rw = await asyncio.open_connection(host, port)
lw.write(b'HTTP/1.1 200 Connection Established\r\n\r\n')
await lw.drain()
await asyncio.gather(pipe(lr, rw), pipe(rr, lw))
# for HTTP
else:
host = search(r"Host: (.+)\r\n", data).group(1)
rr, rw = await asyncio.open_connection(host, 80)
rw.write(bytes(data, 'utf-8'))
await rw.drain()
await asyncio.gather(pipe(lr, rw), pipe(rr, lw))
except ConnectionResetError as e:
logging.error(e)
except Exception as e:
logging.error(e)
finally:
lw.close()
async def main():
server = await asyncio.start_server(conn_handler, '0.0.0.0', 5000)
logging.info(
f'Server ready listen at port {server.sockets[0].getsockname()[1]}')
await server.serve_forever()
try:
asyncio.run(main())
except KeyboardInterrupt:
exit(1)
except Exception as e:
logging.error(e)