Skip to content

Commit 20dc2d0

Browse files
committed
Autoblacklist now uses only the standard library
1 parent e7eeb28 commit 20dc2d0

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/main.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
import logging
77
import os
88
import sys
9-
import requests
9+
from urllib.request import urlopen, Request
10+
from urllib.error import URLError, HTTPError
11+
import ssl
1012
from datetime import datetime
1113
import time
1214
import traceback
@@ -302,12 +304,18 @@ async def handle_connection(self, reader, writer):
302304
if method == b"CONNECT" and self.auto_blacklist:
303305
try:
304306
if host not in self.blocked and host not in self.whitelist:
305-
requests.get('https://' + host.decode(), timeout=3)
306-
self.whitelist.append(host)
307-
except Exception: # pylint: disable=broad-except
308-
self.blocked.append(host)
309-
with open(self.blacklist, "a", encoding="utf-8") as f:
310-
f.write(host.decode() + "\n")
307+
req = Request(
308+
'https://' + host.decode(), headers={'User-Agent': 'Mozilla/5.0'})
309+
context = ssl._create_unverified_context() # pylint: disable=protected-access
310+
311+
with urlopen(req, timeout=4, context=context):
312+
self.whitelist.append(host)
313+
except URLError as e:
314+
reason = str(e.reason)
315+
if "handshake operation timed out" in reason:
316+
self.blocked.append(host)
317+
with open(self.blacklist, "a", encoding="utf-8") as f:
318+
f.write(host.decode() + "\n")
311319

312320
async with self.connections_lock:
313321
self.active_connections[conn_key] = conn_info

0 commit comments

Comments
 (0)