Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -34,12 +34,28 @@ CORS(app, resources={r"/*": {"origins": "*"}})
|
|
34 |
executor = ThreadPoolExecutor(max_workers=10)
|
35 |
|
36 |
proxy_url = os.getenv('PROXY_URL')
|
37 |
-
NOTDIAMOND_IP = os.getenv('NOTDIAMOND_IP')
|
38 |
NOTDIAMOND_DOMAIN = os.getenv('NOTDIAMOND_DOMAIN')
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
refresh_token_cache = TTLCache(maxsize=1000, ttl=3600)
|
45 |
headers_cache = TTLCache(maxsize=1, ttl=3600) # 1小时过期
|
@@ -49,8 +65,9 @@ token_refresh_lock = threading.Lock()
|
|
49 |
def patched_create_connection(address, *args, **kwargs):
|
50 |
host, port = address
|
51 |
if host == NOTDIAMOND_DOMAIN:
|
52 |
-
|
53 |
-
|
|
|
54 |
return create_connection(address, *args, **kwargs)
|
55 |
|
56 |
# 替换 urllib3 的默认连接函数
|
@@ -664,6 +681,15 @@ def health_check():
|
|
664 |
if current_time.tm_hour == 0 and current_time.tm_min == 0:
|
665 |
multi_auth_manager.reset_all_model_status()
|
666 |
logger.info("Reset model status for all accounts")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
667 |
except Exception as e:
|
668 |
logger.error(f"Health check error: {e}")
|
669 |
time.sleep(60) # 每分钟检查一次
|
|
|
34 |
executor = ThreadPoolExecutor(max_workers=10)
|
35 |
|
36 |
proxy_url = os.getenv('PROXY_URL')
|
|
|
37 |
NOTDIAMOND_DOMAIN = os.getenv('NOTDIAMOND_DOMAIN')
|
38 |
+
if not NOTDIAMOND_DOMAIN:
|
39 |
+
logger.error("NOTDIAMOND_DOMAIN environment variable is not set!")
|
40 |
+
raise ValueError("NOTDIAMOND_DOMAIN must be set")
|
41 |
|
42 |
+
# 动态IP缓存,设置1小时过期
|
43 |
+
ip_cache = TTLCache(maxsize=1, ttl=3600)
|
44 |
+
ip_refresh_lock = threading.Lock()
|
45 |
+
|
46 |
+
def get_current_ip():
|
47 |
+
"""动态获取当前域名对应的IP地址"""
|
48 |
+
try:
|
49 |
+
with ip_refresh_lock:
|
50 |
+
if 'current_ip' not in ip_cache:
|
51 |
+
logger.info(f"Resolving IP for domain: {NOTDIAMOND_DOMAIN}")
|
52 |
+
ip = socket.gethostbyname(NOTDIAMOND_DOMAIN)
|
53 |
+
ip_cache['current_ip'] = ip
|
54 |
+
logger.info(f"Successfully resolved IP: {ip}")
|
55 |
+
return ip_cache['current_ip']
|
56 |
+
except socket.gaierror as e:
|
57 |
+
logger.error(f"DNS resolution failed for {NOTDIAMOND_DOMAIN}: {e}")
|
58 |
+
raise Exception(f"Failed to resolve IP for {NOTDIAMOND_DOMAIN}")
|
59 |
|
60 |
refresh_token_cache = TTLCache(maxsize=1000, ttl=3600)
|
61 |
headers_cache = TTLCache(maxsize=1, ttl=3600) # 1小时过期
|
|
|
65 |
def patched_create_connection(address, *args, **kwargs):
|
66 |
host, port = address
|
67 |
if host == NOTDIAMOND_DOMAIN:
|
68 |
+
current_ip = get_current_ip()
|
69 |
+
logger.info(f"Connecting to {NOTDIAMOND_DOMAIN} using IP: {current_ip}")
|
70 |
+
return create_connection((current_ip, port), *args, **kwargs)
|
71 |
return create_connection(address, *args, **kwargs)
|
72 |
|
73 |
# 替换 urllib3 的默认连接函数
|
|
|
681 |
if current_time.tm_hour == 0 and current_time.tm_min == 0:
|
682 |
multi_auth_manager.reset_all_model_status()
|
683 |
logger.info("Reset model status for all accounts")
|
684 |
+
|
685 |
+
# 每小时刷新一次IP缓存
|
686 |
+
if current_time.tm_min == 0:
|
687 |
+
try:
|
688 |
+
ip_cache.clear()
|
689 |
+
logger.info("Cleared IP cache for periodic refresh")
|
690 |
+
except Exception as e:
|
691 |
+
logger.error(f"Error clearing IP cache: {e}")
|
692 |
+
|
693 |
except Exception as e:
|
694 |
logger.error(f"Health check error: {e}")
|
695 |
time.sleep(60) # 每分钟检查一次
|