This commit is contained in:
本间白猫 committed 2025-01-07 10:50:13 +08:00
1 parent 4b4e62ca51
commit 2e83336f97
5 files changed
+103 -20

No files matched your search

+14 -2
View File
@@ -1,4 +1,4 @@
import asyncio, logging, random, httpx, re, os
import asyncio, logging, random, httpx, re, os, time
from configparser import ConfigParser
from packaging import version
from colorama import Fore
@@ -270,7 +270,16 @@ def load_ip_list(file_path):
with open(file_path, 'r') as f:
return {line.strip() for line in f if line.strip()}
_proxy_check_cache = {}
_proxy_check_ttl = 10
async def check_proxy(proxy):
current_time = time.time()
if proxy in _proxy_check_cache:
cache_time, is_valid = _proxy_check_cache[proxy]
if current_time - cache_time < _proxy_check_ttl:
return is_valid
proxy_type = proxy.split('://')[0]
check_funcs = {
'http': check_http_proxy,
@@ -282,9 +291,12 @@ async def check_proxy(proxy):
return False
try:
return await check_funcs[proxy_type](proxy)
is_valid = await check_funcs[proxy_type](proxy)
_proxy_check_cache[proxy] = (current_time, is_valid)
return is_valid
except Exception as e:
logging.error(f"{proxy_type.upper()}代理 {proxy} 检测失败: {e}")
_proxy_check_cache[proxy] = (current_time, False)
return False
async def check_http_proxy(proxy):
+18 -4
View File
@@ -40,7 +40,10 @@ class AsyncProxyServer:
self.proxy_failed = False
self.proxy_fail_count = 0
self.max_fail_count = 2
self.semaphore = asyncio.Semaphore(10000)
self.semaphore = asyncio.Semaphore(20000)
self.buffer_size = 256 * 1024
self.proxy_cache = {}
self.proxy_cache_ttl = 10
async def get_next_proxy(self):
if self.mode == 'load_balance':
@@ -240,10 +243,9 @@ class AsyncProxyServer:
async def _pipe(self, reader, writer):
try:
buffer_size = 65536
while True:
try:
data = await asyncio.wait_for(reader.read(buffer_size), timeout=30)
data = await asyncio.wait_for(reader.read(self.buffer_size), timeout=30)
if not data:
break
writer.write(data)
@@ -470,7 +472,8 @@ class AsyncProxyServer:
writer.write(b'\r\n')
await writer.drain()
async for chunk in response.aiter_bytes(chunk_size=65536):
chunk_size = 256 * 1024
async for chunk in response.aiter_bytes(chunk_size=chunk_size):
if asyncio.current_task().cancelled():
raise asyncio.CancelledError
writer.write(f'{len(chunk):X}\r\n'.encode('utf-8', errors='ignore'))
@@ -515,3 +518,14 @@ class AsyncProxyServer:
await self.get_proxy()
self.last_switch_time = time.time()
logging.info(get_message('proxy_switched', self.language, old_proxy, self.current_proxy))
async def check_proxy(self, proxy):
current_time = time.time()
if proxy in self.proxy_cache:
cache_time, is_valid = self.proxy_cache[proxy]
if current_time - cache_time < self.proxy_cache_ttl:
return is_valid
is_valid = await self._check_proxy_impl(proxy)
self.proxy_cache[proxy] = (current_time, is_valid)
return is_valid