Spaces:
Paused
Paused
Upload app.py
Browse files
app.py
CHANGED
|
@@ -22,6 +22,7 @@ from time import sleep
|
|
| 22 |
from datetime import datetime, timedelta
|
| 23 |
import aiohttp
|
| 24 |
import asyncio
|
|
|
|
| 25 |
|
| 26 |
# 新增导入
|
| 27 |
import register_bot
|
|
@@ -780,53 +781,103 @@ def build_payload(request_data, model_id):
|
|
| 780 |
|
| 781 |
return payload
|
| 782 |
|
| 783 |
-
#
|
| 784 |
PROXY_API_BASE = "https://269900.xyz"
|
| 785 |
PROXY_CHECK_INTERVAL = 300 # 5分钟检查一次代理可用性
|
| 786 |
PROXY_MAX_LATENCY = 5000 # 最大延迟(毫秒)
|
| 787 |
MAX_PROXIES = 10 # 保持的最大代理数量
|
|
|
|
| 788 |
|
| 789 |
class ProxyManager:
|
| 790 |
def __init__(self):
|
| 791 |
-
self.proxies: List[Dict[str, Union[str, int, float]]] = []
|
| 792 |
self.last_update = 0
|
| 793 |
self.lock = threading.Lock()
|
|
|
|
| 794 |
|
| 795 |
async def fetch_proxies(self):
|
| 796 |
"""异步获取代理列表"""
|
| 797 |
try:
|
| 798 |
-
|
| 799 |
-
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 804 |
with self.lock:
|
| 805 |
-
self.proxies = [
|
| 806 |
-
|
| 807 |
-
|
| 808 |
-
|
| 809 |
-
|
| 810 |
-
|
| 811 |
-
for proxy in proxies
|
| 812 |
-
]
|
| 813 |
self.last_update = time.time()
|
| 814 |
logger.info(f"Successfully fetched {len(self.proxies)} proxies")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 815 |
except Exception as e:
|
| 816 |
-
logger.error(f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 817 |
|
| 818 |
def get_proxy(self) -> Optional[str]:
|
| 819 |
"""获取一个可用的代理"""
|
| 820 |
with self.lock:
|
| 821 |
current_time = time.time()
|
| 822 |
|
| 823 |
-
# 如果代理列表为空或者最后更新时间超过检查间隔,同步更新代理
|
| 824 |
if not self.proxies or (current_time - self.last_update) > PROXY_CHECK_INTERVAL:
|
| 825 |
-
#
|
| 826 |
loop = asyncio.new_event_loop()
|
| 827 |
asyncio.set_event_loop(loop)
|
| 828 |
try:
|
| 829 |
loop.run_until_complete(self.fetch_proxies())
|
|
|
|
|
|
|
| 830 |
finally:
|
| 831 |
loop.close()
|
| 832 |
|
|
@@ -836,31 +887,39 @@ class ProxyManager:
|
|
| 836 |
# 选择失败次数最少且最近最少使用的代理
|
| 837 |
valid_proxies = [p for p in self.proxies if p['fails'] < 3]
|
| 838 |
if not valid_proxies:
|
| 839 |
-
# 创建新的事件循环来运行异步函数
|
| 840 |
-
loop = asyncio.new_event_loop()
|
| 841 |
-
asyncio.set_event_loop(loop)
|
| 842 |
-
try:
|
| 843 |
-
loop.run_until_complete(self.fetch_proxies())
|
| 844 |
-
finally:
|
| 845 |
-
loop.close()
|
| 846 |
return None
|
| 847 |
|
| 848 |
proxy = min(valid_proxies, key=lambda x: (x['fails'], x['last_use']))
|
| 849 |
proxy['last_use'] = current_time
|
|
|
|
|
|
|
| 850 |
return proxy['url']
|
| 851 |
|
| 852 |
-
def
|
| 853 |
-
"""
|
| 854 |
-
|
| 855 |
-
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
|
| 859 |
-
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
-
|
| 863 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 864 |
|
| 865 |
def make_request(payload, auth_manager, model_id):
|
| 866 |
"""改进的请求函数,支持代理"""
|
|
@@ -898,6 +957,7 @@ def make_request(payload, auth_manager, model_id):
|
|
| 898 |
|
| 899 |
for attempt in range(max_retries):
|
| 900 |
# 获取代理
|
|
|
|
| 901 |
proxy = proxy_manager.get_proxy()
|
| 902 |
proxies = {'https': proxy} if proxy else None
|
| 903 |
|
|
@@ -907,22 +967,26 @@ def make_request(payload, auth_manager, model_id):
|
|
| 907 |
|
| 908 |
logger.info(f"使用代理: {proxy}" if proxy else "不使用代理")
|
| 909 |
|
| 910 |
-
|
| 911 |
-
|
| 912 |
-
|
| 913 |
-
|
| 914 |
-
|
|
|
|
|
|
|
|
|
|
| 915 |
stream=True,
|
| 916 |
proxies=proxies,
|
| 917 |
-
timeout=
|
| 918 |
-
)
|
| 919 |
|
| 920 |
-
if response.status_code == 200
|
| 921 |
-
logger.info(f"请求成功,使用账号 {auth_manager._email}")
|
| 922 |
return response
|
| 923 |
-
|
| 924 |
-
|
| 925 |
-
|
|
|
|
|
|
|
| 926 |
if response.status_code == 401:
|
| 927 |
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh")
|
| 928 |
if auth_manager.ensure_valid_token():
|
|
@@ -933,15 +997,9 @@ def make_request(payload, auth_manager, model_id):
|
|
| 933 |
auth_manager.set_model_unavailable(model_id)
|
| 934 |
break
|
| 935 |
|
| 936 |
-
if response.status_code == 429: # Rate limit
|
| 937 |
-
if proxy:
|
| 938 |
-
proxy_manager.mark_proxy_failed(proxy)
|
| 939 |
-
logger.warning(f"Rate limit hit, trying different proxy")
|
| 940 |
-
continue
|
| 941 |
-
|
| 942 |
logger.error(f"Request failed with status {response.status_code}")
|
| 943 |
|
| 944 |
-
except
|
| 945 |
logger.error(f"Request attempt {attempt + 1} failed: {e}")
|
| 946 |
if proxy:
|
| 947 |
proxy_manager.mark_proxy_failed(proxy)
|
|
@@ -1010,3 +1068,10 @@ if __name__ == "__main__":
|
|
| 1010 |
port = int(os.environ.get("PORT", 3000))
|
| 1011 |
app.run(debug=False, host='0.0.0.0', port=port, threaded=True)
|
| 1012 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
from datetime import datetime, timedelta
|
| 23 |
import aiohttp
|
| 24 |
import asyncio
|
| 25 |
+
import atexit
|
| 26 |
|
| 27 |
# 新增导入
|
| 28 |
import register_bot
|
|
|
|
| 781 |
|
| 782 |
return payload
|
| 783 |
|
| 784 |
+
# 修改代理相关的常量和配置
|
| 785 |
PROXY_API_BASE = "https://269900.xyz"
|
| 786 |
PROXY_CHECK_INTERVAL = 300 # 5分钟检查一次代理可用性
|
| 787 |
PROXY_MAX_LATENCY = 5000 # 最大延迟(毫秒)
|
| 788 |
MAX_PROXIES = 10 # 保持的最大代理数量
|
| 789 |
+
PROXY_TIMEOUT = 5 # 代理请求超时时间(秒)
|
| 790 |
|
| 791 |
class ProxyManager:
|
| 792 |
def __init__(self):
|
| 793 |
+
self.proxies: List[Dict[str, Union[str, int, float]]] = []
|
| 794 |
self.last_update = 0
|
| 795 |
self.lock = threading.Lock()
|
| 796 |
+
self.session = aiohttp.ClientSession()
|
| 797 |
|
| 798 |
async def fetch_proxies(self):
|
| 799 |
"""异步获取代理列表"""
|
| 800 |
try:
|
| 801 |
+
# 使用基本的 fetch_all 端点
|
| 802 |
+
endpoint = f"{PROXY_API_BASE}/fetch_all?region=cn&count=5"
|
| 803 |
+
|
| 804 |
+
async with self.session.get(endpoint, timeout=PROXY_TIMEOUT) as response:
|
| 805 |
+
if response.status == 200:
|
| 806 |
+
try:
|
| 807 |
+
# 获取文本响应
|
| 808 |
+
text = await response.text()
|
| 809 |
+
# 分割并解析代理列表
|
| 810 |
+
proxy_list = [p.strip() for p in text.split(',') if p.strip()]
|
| 811 |
+
|
| 812 |
with self.lock:
|
| 813 |
+
self.proxies = []
|
| 814 |
+
for proxy_str in proxy_list:
|
| 815 |
+
proxy_info = self._parse_proxy_string(proxy_str)
|
| 816 |
+
if proxy_info:
|
| 817 |
+
self.proxies.append(proxy_info)
|
| 818 |
+
|
|
|
|
|
|
|
| 819 |
self.last_update = time.time()
|
| 820 |
logger.info(f"Successfully fetched {len(self.proxies)} proxies")
|
| 821 |
+
|
| 822 |
+
except Exception as e:
|
| 823 |
+
logger.error(f"Error parsing proxy response: {e}")
|
| 824 |
+
else:
|
| 825 |
+
logger.error(f"Failed to fetch proxies, status: {response.status}")
|
| 826 |
+
|
| 827 |
except Exception as e:
|
| 828 |
+
logger.error(f"Error in fetch_proxies: {e}")
|
| 829 |
+
finally:
|
| 830 |
+
if not self.proxies:
|
| 831 |
+
logger.warning("No proxies available, will retry later")
|
| 832 |
+
|
| 833 |
+
def _parse_proxy_string(self, proxy_str: str) -> Optional[Dict[str, Union[str, int, float]]]:
|
| 834 |
+
"""解析代理字符串"""
|
| 835 |
+
try:
|
| 836 |
+
proxy_str = proxy_str.strip()
|
| 837 |
+
if not proxy_str:
|
| 838 |
+
return None
|
| 839 |
+
|
| 840 |
+
# 解析代理类型和地址
|
| 841 |
+
if proxy_str.startswith(('http://', 'https://', 'socks4://', 'socks5://')):
|
| 842 |
+
proxy_type = proxy_str.split('://')[0]
|
| 843 |
+
address = proxy_str.split('://')[1]
|
| 844 |
+
else:
|
| 845 |
+
proxy_type = 'http'
|
| 846 |
+
address = proxy_str
|
| 847 |
+
|
| 848 |
+
# 解析IP和端口
|
| 849 |
+
if ':' in address:
|
| 850 |
+
ip, port = address.split(':')
|
| 851 |
+
port = int(port)
|
| 852 |
+
else:
|
| 853 |
+
return None
|
| 854 |
+
|
| 855 |
+
return {
|
| 856 |
+
'url': proxy_str if proxy_str.startswith(('http://', 'https://', 'socks4://', 'socks5://'))
|
| 857 |
+
else f"http://{proxy_str}",
|
| 858 |
+
'type': proxy_type,
|
| 859 |
+
'ip': ip,
|
| 860 |
+
'port': port,
|
| 861 |
+
'last_use': 0,
|
| 862 |
+
'fails': 0
|
| 863 |
+
}
|
| 864 |
+
except Exception as e:
|
| 865 |
+
logger.error(f"Error parsing proxy string '{proxy_str}': {e}")
|
| 866 |
+
return None
|
| 867 |
|
| 868 |
def get_proxy(self) -> Optional[str]:
|
| 869 |
"""获取一个可用的代理"""
|
| 870 |
with self.lock:
|
| 871 |
current_time = time.time()
|
| 872 |
|
|
|
|
| 873 |
if not self.proxies or (current_time - self.last_update) > PROXY_CHECK_INTERVAL:
|
| 874 |
+
# 使用异步事件循环
|
| 875 |
loop = asyncio.new_event_loop()
|
| 876 |
asyncio.set_event_loop(loop)
|
| 877 |
try:
|
| 878 |
loop.run_until_complete(self.fetch_proxies())
|
| 879 |
+
except Exception as e:
|
| 880 |
+
logger.error(f"Error updating proxies: {e}")
|
| 881 |
finally:
|
| 882 |
loop.close()
|
| 883 |
|
|
|
|
| 887 |
# 选择失败次数最少且最近最少使用的代理
|
| 888 |
valid_proxies = [p for p in self.proxies if p['fails'] < 3]
|
| 889 |
if not valid_proxies:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 890 |
return None
|
| 891 |
|
| 892 |
proxy = min(valid_proxies, key=lambda x: (x['fails'], x['last_use']))
|
| 893 |
proxy['last_use'] = current_time
|
| 894 |
+
|
| 895 |
+
# 返回格式化的代理URL
|
| 896 |
return proxy['url']
|
| 897 |
|
| 898 |
+
async def check_proxy(self, proxy_url: str) -> bool:
|
| 899 |
+
"""检查代理是否可用"""
|
| 900 |
+
try:
|
| 901 |
+
async with self.session.get(
|
| 902 |
+
'https://www.google.com',
|
| 903 |
+
proxy=proxy_url,
|
| 904 |
+
timeout=PROXY_TIMEOUT
|
| 905 |
+
) as response:
|
| 906 |
+
return response.status == 200
|
| 907 |
+
except Exception:
|
| 908 |
+
return False
|
| 909 |
+
|
| 910 |
+
async def close(self):
|
| 911 |
+
"""关闭会话"""
|
| 912 |
+
await self.session.close()
|
| 913 |
+
|
| 914 |
+
# 修改代理管理器的创建方式
|
| 915 |
+
proxy_manager = None
|
| 916 |
|
| 917 |
+
def get_proxy_manager():
|
| 918 |
+
"""获取或创建代理管理器实例"""
|
| 919 |
+
global proxy_manager
|
| 920 |
+
if proxy_manager is None:
|
| 921 |
+
proxy_manager = ProxyManager()
|
| 922 |
+
return proxy_manager
|
| 923 |
|
| 924 |
def make_request(payload, auth_manager, model_id):
|
| 925 |
"""改进的请求函数,支持代理"""
|
|
|
|
| 957 |
|
| 958 |
for attempt in range(max_retries):
|
| 959 |
# 获取代理
|
| 960 |
+
proxy_manager = get_proxy_manager()
|
| 961 |
proxy = proxy_manager.get_proxy()
|
| 962 |
proxies = {'https': proxy} if proxy else None
|
| 963 |
|
|
|
|
| 967 |
|
| 968 |
logger.info(f"使用代理: {proxy}" if proxy else "不使用代理")
|
| 969 |
|
| 970 |
+
# 添加重试和超时设置
|
| 971 |
+
session = requests.Session()
|
| 972 |
+
session.mount('https://', CustomHTTPAdapter(max_retries=3))
|
| 973 |
+
|
| 974 |
+
response = session.post(
|
| 975 |
+
url,
|
| 976 |
+
headers=headers,
|
| 977 |
+
json=payload,
|
| 978 |
stream=True,
|
| 979 |
proxies=proxies,
|
| 980 |
+
timeout=(5, 30) # (连接超时, 读取超时)
|
| 981 |
+
)
|
| 982 |
|
| 983 |
+
if response.status_code == 200:
|
|
|
|
| 984 |
return response
|
| 985 |
+
|
| 986 |
+
if response.status_code == 429 and proxy: # 代理限流
|
| 987 |
+
proxy_manager.mark_proxy_failed(proxy)
|
| 988 |
+
continue
|
| 989 |
+
|
| 990 |
if response.status_code == 401:
|
| 991 |
logger.info(f"Token expired for account {auth_manager._email}, attempting refresh")
|
| 992 |
if auth_manager.ensure_valid_token():
|
|
|
|
| 997 |
auth_manager.set_model_unavailable(model_id)
|
| 998 |
break
|
| 999 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1000 |
logger.error(f"Request failed with status {response.status_code}")
|
| 1001 |
|
| 1002 |
+
except requests.exceptions.RequestException as e:
|
| 1003 |
logger.error(f"Request attempt {attempt + 1} failed: {e}")
|
| 1004 |
if proxy:
|
| 1005 |
proxy_manager.mark_proxy_failed(proxy)
|
|
|
|
| 1068 |
port = int(os.environ.get("PORT", 3000))
|
| 1069 |
app.run(debug=False, host='0.0.0.0', port=port, threaded=True)
|
| 1070 |
|
| 1071 |
+
@atexit.register
|
| 1072 |
+
def cleanup():
|
| 1073 |
+
"""清理资源"""
|
| 1074 |
+
if proxy_manager:
|
| 1075 |
+
loop = asyncio.get_event_loop()
|
| 1076 |
+
loop.run_until_complete(proxy_manager.close())
|
| 1077 |
+
|