-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy_pass.py
32 lines (27 loc) · 1.01 KB
/
proxy_pass.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
import re
import socket
PROXY_FILENAME = 'proxy_pass.txt'
BUFFER_SIZE = 1024
def replacement_proxy_pass(request: str,
client_connection: socket.socket) -> bool:
proxy = False
split_request = request.split(' ')
path = split_request[1].split('/')[1]
with open(PROXY_FILENAME) as file:
for line in file:
if path in line:
proxy = True
request = re.sub(r'/[a-zA-Z]*\d*', line.split()[4], request, 1)
if proxy:
server_connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_connection.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
server_connection.sendall(request.encode())
response = b''
while True:
data = server_connection.recv(BUFFER_SIZE)
response += data
if not data:
break
client_connection.sendall(response)
return proxy