-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
53 lines (44 loc) · 1.51 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
import socket
import sys
OWN_ADDR = ("0.0.0.0", 8000)
UPSTREAM_ADDR = ("127.0.0.1", 9000)
def log(message):
print(message, file=sys.stderr)
# create a socket interface where client can connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(OWN_ADDR)
s.listen()
log(f"Accepting new connection on ${OWN_ADDR}")
# accept a connecction froma accept queue (long running process)
while True:
client_con, client_addr = s.accept()
log(f"New connection from {client_addr}")
# receive packets from a client connection
data = client_con.recv(4096)
log(f"->* {len(data)}B")
try:
# create a socket with upstream server
upstream_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
upstream_sock.connect(UPSTREAM_ADDR)
log(f"Connected to {UPSTREAM_ADDR}")
#forward data received from client to upstream
upstream_sock.send(data)
log(f" *->{len(data)}B")
# receive the response back from the upstream
while True:
res = upstream_sock.recv(4096)
log(f" *<-{len(res)}B")
if not res:
break
# send the response received from the upstream to client
client_con.send(res)
log(f"<-* {len(res)}B")
except ConnectionRefusedError:
client_con.send(b'HTTP/1.1 502 BAD GATEWAY\r\n\r\n')
log("<-* BAD GATEWAY")
except OSError as err:
log(err)
finally:
upstream_sock.close()
client_con.close()
s.close()