-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
287 lines (233 loc) · 9.34 KB
/
server.py
File metadata and controls
287 lines (233 loc) · 9.34 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import socket
import threading
import time
from datetime import datetime
HOST = "0.0.0.0"
PORT = 8080
client_semaphore = threading.Semaphore(5)
blocked_sites = {
"facebook.com",
"instagram.com"
}
CACHE = {}
CACHE_LOCK = threading.Lock()
CACHE_TTL = 60
LOG_FILE = "proxy.log"
LOG_LOCK = threading.Lock()
def log_request(client_ip, host, protocol):
with LOG_LOCK:
with open(LOG_FILE, "a") as f:
f.write(f"{datetime.now()} | {client_ip} | {host} | {protocol}\n")
def tunnel_data(source, destination):
try:
while True:
data = source.recv(4096)
if not data:
break
destination.sendall(data)
except:
pass
def handle_https(client_socket, addr, request_line):
remote_socket = None
try:
# Parse the CONNECT request
target = request_line.split(" ")[1]
# Handle both "host:port" and just "host" formats
if ":" in target:
host, port = target.split(":")
port = int(port)
else:
host = target
port = 443 # Default HTTPS port
print(f"[HTTPS] Request from {addr[0]} to {host}:{port}", flush=True)
# Check if site is blocked
if host in blocked_sites:
error_msg = f"[HTTPS] Blocked site: {host}"
print(error_msg, flush=True)
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\nSite blocked by proxy")
return
# Log the request
log_request(addr[0], host, "HTTPS")
# Try to resolve the hostname first
try:
import socket as sock_module
resolved_ip = sock_module.gethostbyname(host)
print(f"[HTTPS] Resolved {host} to {resolved_ip}", flush=True)
except sock_module.gaierror as dns_error:
error_msg = f"[HTTPS] DNS resolution failed for {host}: {dns_error}"
print(error_msg, flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nDNS resolution failed")
return
# Create socket and set timeout
remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_socket.settimeout(10) # 10 second timeout
print(f"[HTTPS] Connecting to {host}:{port}...", flush=True)
# Try to connect to the remote server
try:
remote_socket.connect((host, port))
print(f"[HTTPS] Successfully connected to {host}:{port}", flush=True)
except socket.timeout:
error_msg = f"[HTTPS] Connection timeout to {host}:{port}"
print(error_msg, flush=True)
client_socket.sendall(b"HTTP/1.1 504 Gateway Timeout\r\n\r\nConnection timeout")
return
except ConnectionRefusedError:
error_msg = f"[HTTPS] Connection refused by {host}:{port}"
print(error_msg, flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nConnection refused")
return
except OSError as conn_error:
error_msg = f"[HTTPS] Connection error to {host}:{port}: {conn_error}"
print(error_msg, flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nConnection failed")
return
# Send success response to client
client_socket.sendall(b"HTTP/1.1 200 Connection Established\r\n\r\n")
print(f"[HTTPS] Tunnel established for {host}:{port}", flush=True)
# Remove timeout for data transfer
remote_socket.settimeout(None)
# Create bidirectional tunnel
t1 = threading.Thread(target=tunnel_data, args=(client_socket, remote_socket))
t2 = threading.Thread(target=tunnel_data, args=(remote_socket, client_socket))
t1.start()
t2.start()
t1.join()
t2.join()
print(f"[HTTPS] Tunnel closed for {host}:{port}", flush=True)
except ValueError as parse_error:
error_msg = f"[HTTPS] Invalid CONNECT request format: {parse_error}"
print(error_msg, flush=True)
try:
client_socket.sendall(b"HTTP/1.1 400 Bad Request\r\n\r\nInvalid request format")
except:
pass
except Exception as e:
error_msg = f"[HTTPS] Unexpected error: {type(e).__name__}: {e}"
print(error_msg, flush=True)
try:
client_socket.sendall(b"HTTP/1.1 500 Internal Server Error\r\n\r\nProxy error")
except:
pass
finally:
# Clean up remote socket
if remote_socket:
try:
remote_socket.close()
except:
pass
def handle_http(client_socket, addr, request):
remote_socket = None
try:
request_str = request.decode(errors="ignore")
request_line = request_str.split("\r\n")[0]
cache_key = request_line
# Check cache first
with CACHE_LOCK:
if cache_key in CACHE:
response, timestamp = CACHE[cache_key]
if time.time() - timestamp < CACHE_TTL:
print(f"[HTTP] CACHE HIT: {cache_key}", flush=True)
client_socket.sendall(response)
return
else:
del CACHE[cache_key]
# Extract host from request
host = None
for line in request_str.split("\r\n"):
if line.lower().startswith("host:"):
host = line.split(":", 1)[1].strip()
break
if not host:
print(f"[HTTP] No host header found in request from {addr[0]}", flush=True)
client_socket.sendall(b"HTTP/1.1 400 Bad Request\r\n\r\nNo host header")
return
# Check if site is blocked
if host in blocked_sites:
print(f"[HTTP] Blocked site: {host}", flush=True)
client_socket.sendall(b"HTTP/1.1 403 Forbidden\r\n\r\nSite blocked by proxy")
return
log_request(addr[0], host, "HTTP")
print(f"[HTTP] Request from {addr[0]} to {host}", flush=True)
# Create socket and connect
remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
remote_socket.settimeout(15)
try:
remote_socket.connect((host, 80))
print(f"[HTTP] Connected to {host}:80", flush=True)
except socket.timeout:
print(f"[HTTP] Connection timeout to {host}:80", flush=True)
client_socket.sendall(b"HTTP/1.1 504 Gateway Timeout\r\n\r\nConnection timeout")
return
except socket.gaierror as dns_error:
print(f"[HTTP] DNS resolution failed for {host}: {dns_error}", flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nDNS resolution failed")
return
except ConnectionRefusedError:
print(f"[HTTP] Connection refused by {host}:80", flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nConnection refused")
return
except OSError as conn_error:
print(f"[HTTP] Connection error to {host}:80: {conn_error}", flush=True)
client_socket.sendall(b"HTTP/1.1 502 Bad Gateway\r\n\r\nConnection failed")
return
# Send request
remote_socket.sendall(request)
# Receive response
full_response = b""
while True:
data = remote_socket.recv(4096)
if not data:
break
full_response += data
remote_socket.close()
remote_socket = None
# Send response to client
if full_response:
client_socket.sendall(full_response)
# Cache the response
with CACHE_LOCK:
CACHE[cache_key] = (full_response, time.time())
print(f"[HTTP] CACHE STORED: {cache_key}", flush=True)
else:
print(f"[HTTP] Empty response from {host}", flush=True)
except Exception as e:
error_msg = f"[HTTP] Unexpected error: {type(e).__name__}: {e}"
print(error_msg, flush=True)
try:
client_socket.sendall(b"HTTP/1.1 500 Internal Server Error\r\n\r\nProxy error")
except:
pass
finally:
if remote_socket:
try:
remote_socket.close()
except:
pass
def handle_client(client_socket, addr):
print("Waiting for semaphore...", flush=True)
client_semaphore.acquire()
print("Entered critical section:", addr, flush=True)
try:
request = client_socket.recv(4096)
if not request:
return
request_line = request.decode(errors="ignore").split("\r\n")[0]
if request_line.startswith("CONNECT"):
handle_https(client_socket, addr, request_line)
else:
handle_http(client_socket, addr, request)
except Exception as e:
print("Client error:", e, flush=True)
finally:
client_socket.close()
client_semaphore.release()
print("Connection closed + semaphore released:", addr, flush=True)
proxy = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxy.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
proxy.bind((HOST, PORT))
proxy.listen(100)
print("HTTPS + HTTP Proxy running on port", PORT, flush=True)
while True:
client_socket, addr = proxy.accept()
thread = threading.Thread(target=handle_client, args=(client_socket, addr))
thread.start()