|
6 | 6 | import logging |
7 | 7 | import os |
8 | 8 | import sys |
9 | | -import requests |
| 9 | +from urllib.request import urlopen, Request |
| 10 | +from urllib.error import URLError, HTTPError |
| 11 | +import ssl |
10 | 12 | from datetime import datetime |
11 | 13 | import time |
12 | 14 | import traceback |
@@ -302,12 +304,18 @@ async def handle_connection(self, reader, writer): |
302 | 304 | if method == b"CONNECT" and self.auto_blacklist: |
303 | 305 | try: |
304 | 306 | 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") |
311 | 319 |
|
312 | 320 | async with self.connections_lock: |
313 | 321 | self.active_connections[conn_key] = conn_info |
|
0 commit comments