-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.py
84 lines (54 loc) · 1.67 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
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
import socket
import pickle
class Instructions:
url = ""
msg = ""
additional_param = ""
def redirect(data):
data = pickle.loads(data)
print(data.url)
dns = {
"encryption.com": {"192.168.18.2":4444},
"mylog.com": {"192.168.18.2":4443},
"givemerandom.com": {"192.168.18.2":4442},
"dadjokes.com": {"192.168.18.4":4441},
}
IP = {}
for x, y in dns.items():
if(x == data.url):
IP = y
if not IP:
msg = f"This site is unreachable. The server IP address with the {data.url} website could not be found."
return pickle.dumps(msg)
else:
sct_re = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
for x, y in IP.items():
ip = x
port = y
print("Connecting to: ", ip, " on port ", port)
sct_re.connect((ip, port))
sct_re.sendall(pickle.dumps(data))
msg = sct_re.recv(1024)
sct_re.close()
return msg
sct = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
IP = socket.gethostbyname(socket.gethostname())
port = 4445
sct.bind((IP, port))
sct.listen(5)
print(f"Proxy is running {IP} on port {port}")
try:
while True:
print("Waiting for clients..")
clientsocket, address = sct.accept()
print(f"Connection from {address} has been established")
data = clientsocket.recv(1024)
print("Data recived!")
new_data = redirect(data)
clientsocket.sendall(new_data)
clientsocket.shutdown(socket.SHUT_WR)
except KeyboardInterrupt:
print("Shutting down the server")
except Exception as exc:
print("Error ocurred: ", exc)
sct.close()