Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -39,35 +39,98 @@ if not NOTDIAMOND_DOMAIN:
|
|
| 39 |
logger.error("NOTDIAMOND_DOMAIN environment variable is not set!")
|
| 40 |
raise ValueError("NOTDIAMOND_DOMAIN must be set")
|
| 41 |
|
| 42 |
-
#
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
ip_refresh_lock = threading.Lock()
|
| 45 |
|
| 46 |
-
def
|
| 47 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
with ip_refresh_lock:
|
| 50 |
if 'current_ip' not in ip_cache:
|
| 51 |
-
logger.info(f"
|
| 52 |
-
ip =
|
| 53 |
ip_cache['current_ip'] = ip
|
| 54 |
-
logger.info(f"
|
| 55 |
return ip_cache['current_ip']
|
| 56 |
-
except
|
| 57 |
-
logger.error(f"
|
| 58 |
-
raise
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
# 自定义连接函数
|
| 65 |
def patched_create_connection(address, *args, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
host, port = address
|
| 67 |
if host == NOTDIAMOND_DOMAIN:
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
return create_connection(address, *args, **kwargs)
|
| 72 |
|
| 73 |
# 替换 urllib3 的默认连接函数
|
|
|
|
| 39 |
logger.error("NOTDIAMOND_DOMAIN environment variable is not set!")
|
| 40 |
raise ValueError("NOTDIAMOND_DOMAIN must be set")
|
| 41 |
|
| 42 |
+
# IP缓存配置
|
| 43 |
+
IP_CACHE_TTL = 3600 # IP缓存1小时过期
|
| 44 |
+
MAX_IP_RETRIES = 3 # 最大重试次数
|
| 45 |
+
RETRY_DELAY = 5 # 重试延迟(秒)
|
| 46 |
+
|
| 47 |
+
# 动态IP缓存
|
| 48 |
+
ip_cache = TTLCache(maxsize=1, ttl=IP_CACHE_TTL)
|
| 49 |
ip_refresh_lock = threading.Lock()
|
| 50 |
|
| 51 |
+
def resolve_domain_ip(domain: str, retry_count: int = 0) -> str:
|
| 52 |
+
"""
|
| 53 |
+
解析域名对应的IP地址,带重试机制
|
| 54 |
+
|
| 55 |
+
Args:
|
| 56 |
+
domain: 要解析的域名
|
| 57 |
+
retry_count: 当前重试次数
|
| 58 |
+
|
| 59 |
+
Returns:
|
| 60 |
+
str: 解析得到的IP地址
|
| 61 |
+
|
| 62 |
+
Raises:
|
| 63 |
+
Exception: 当所有重试都失败时抛出异常
|
| 64 |
+
"""
|
| 65 |
+
try:
|
| 66 |
+
logger.info(f"Attempting to resolve IP for domain: {domain} (attempt {retry_count + 1})")
|
| 67 |
+
ip = socket.gethostbyname(domain)
|
| 68 |
+
logger.info(f"Successfully resolved IP: {ip} for domain: {domain}")
|
| 69 |
+
return ip
|
| 70 |
+
except socket.gaierror as e:
|
| 71 |
+
if retry_count < MAX_IP_RETRIES - 1:
|
| 72 |
+
logger.warning(f"DNS resolution failed for {domain} (attempt {retry_count + 1}): {e}")
|
| 73 |
+
time.sleep(RETRY_DELAY)
|
| 74 |
+
return resolve_domain_ip(domain, retry_count + 1)
|
| 75 |
+
else:
|
| 76 |
+
logger.error(f"DNS resolution failed after {MAX_IP_RETRIES} attempts for {domain}: {e}")
|
| 77 |
+
raise Exception(f"Failed to resolve IP for {domain} after {MAX_IP_RETRIES} attempts")
|
| 78 |
+
|
| 79 |
+
def get_current_ip() -> str:
|
| 80 |
+
"""
|
| 81 |
+
获取当前域名对应的IP地址,使用缓存机制
|
| 82 |
+
|
| 83 |
+
Returns:
|
| 84 |
+
str: 当前域名对应的IP地址
|
| 85 |
+
|
| 86 |
+
Raises:
|
| 87 |
+
Exception: 当IP解析失败时抛出异常
|
| 88 |
+
"""
|
| 89 |
try:
|
| 90 |
with ip_refresh_lock:
|
| 91 |
if 'current_ip' not in ip_cache:
|
| 92 |
+
logger.info(f"IP cache miss for domain: {NOTDIAMOND_DOMAIN}")
|
| 93 |
+
ip = resolve_domain_ip(NOTDIAMOND_DOMAIN)
|
| 94 |
ip_cache['current_ip'] = ip
|
| 95 |
+
logger.info(f"Updated IP cache with: {ip}")
|
| 96 |
return ip_cache['current_ip']
|
| 97 |
+
except Exception as e:
|
| 98 |
+
logger.error(f"Failed to get current IP: {e}")
|
| 99 |
+
raise
|
| 100 |
|
| 101 |
+
def refresh_ip_cache() -> None:
|
| 102 |
+
"""
|
| 103 |
+
强制刷新IP缓存
|
| 104 |
+
"""
|
| 105 |
+
try:
|
| 106 |
+
with ip_refresh_lock:
|
| 107 |
+
ip_cache.clear()
|
| 108 |
+
logger.info("IP cache cleared for refresh")
|
| 109 |
+
_ = get_current_ip() # 触发新的IP解析
|
| 110 |
+
logger.info("IP cache refreshed successfully")
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logger.error(f"Error refreshing IP cache: {e}")
|
| 113 |
+
raise
|
| 114 |
|
| 115 |
# 自定义连接函数
|
| 116 |
def patched_create_connection(address, *args, **kwargs):
|
| 117 |
+
"""
|
| 118 |
+
自定义连接函数,用于替换默认的连接函数
|
| 119 |
+
支持动态IP解析和自动重试
|
| 120 |
+
"""
|
| 121 |
host, port = address
|
| 122 |
if host == NOTDIAMOND_DOMAIN:
|
| 123 |
+
try:
|
| 124 |
+
current_ip = get_current_ip()
|
| 125 |
+
logger.info(f"Using resolved IP: {current_ip} for connection to {NOTDIAMOND_DOMAIN}")
|
| 126 |
+
return create_connection((current_ip, port), *args, **kwargs)
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(f"Connection failed using resolved IP: {e}")
|
| 129 |
+
# 如果连接失败,尝试刷新IP缓存并重试
|
| 130 |
+
refresh_ip_cache()
|
| 131 |
+
current_ip = get_current_ip()
|
| 132 |
+
logger.info(f"Retrying connection with fresh IP: {current_ip}")
|
| 133 |
+
return create_connection((current_ip, port), *args, **kwargs)
|
| 134 |
return create_connection(address, *args, **kwargs)
|
| 135 |
|
| 136 |
# 替换 urllib3 的默认连接函数
|