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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import re
|
| 3 |
+
from datetime import datetime, timedelta
|
| 4 |
+
import psutil # 用于获取系统启动时间
|
| 5 |
+
import pytz # 用于处理时区
|
| 6 |
+
import os # 用于检查文件是否存在
|
| 7 |
+
import traceback # 用于获取详细错误信息
|
| 8 |
+
|
| 9 |
+
def get_current_ip():
|
| 10 |
+
"""获取当前公共 IP 地址"""
|
| 11 |
+
try:
|
| 12 |
+
response = requests.get("https://ipinfo.io/ip")
|
| 13 |
+
return response.text.strip()
|
| 14 |
+
except Exception as e:
|
| 15 |
+
print(f"获取IP地址时出错: {e}")
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
# 添加英文到中文的国家名称映射
|
| 19 |
+
def get_country_name_in_chinese(english_name):
|
| 20 |
+
"""将英文国家名称转换为中文"""
|
| 21 |
+
country_map = {
|
| 22 |
+
"United States": "美国",
|
| 23 |
+
"United Kingdom": "英国",
|
| 24 |
+
"Russia": "俄罗斯",
|
| 25 |
+
"Japan": "日本",
|
| 26 |
+
"China": "中国",
|
| 27 |
+
"Germany": "德国",
|
| 28 |
+
"France": "法国",
|
| 29 |
+
"Italy": "意大利",
|
| 30 |
+
"Canada": "加拿大",
|
| 31 |
+
"Australia": "澳大利亚",
|
| 32 |
+
"Brazil": "巴西",
|
| 33 |
+
"India": "印度",
|
| 34 |
+
"Singapore": "新加坡",
|
| 35 |
+
"Netherlands": "荷兰",
|
| 36 |
+
"South Korea": "韩国",
|
| 37 |
+
"Hong Kong": "香港",
|
| 38 |
+
"Taiwan": "台湾",
|
| 39 |
+
"Spain": "西班牙",
|
| 40 |
+
"Sweden": "瑞典",
|
| 41 |
+
"Switzerland": "瑞士",
|
| 42 |
+
"Norway": "挪威",
|
| 43 |
+
"Denmark": "丹麦",
|
| 44 |
+
"Finland": "芬兰",
|
| 45 |
+
"Ireland": "爱尔兰",
|
| 46 |
+
"Belgium": "比利时",
|
| 47 |
+
"Poland": "波兰",
|
| 48 |
+
"Austria": "奥地利",
|
| 49 |
+
"Israel": "以色列",
|
| 50 |
+
"Ukraine": "乌克兰",
|
| 51 |
+
"Turkey": "土耳其",
|
| 52 |
+
"Thailand": "泰国",
|
| 53 |
+
"Vietnam": "越南",
|
| 54 |
+
"Malaysia": "马来西亚",
|
| 55 |
+
"Indonesia": "印度尼西亚",
|
| 56 |
+
"Philippines": "菲律宾",
|
| 57 |
+
"Mexico": "墨西哥",
|
| 58 |
+
"Argentina": "阿根廷",
|
| 59 |
+
"Chile": "智利",
|
| 60 |
+
"South Africa": "南非"
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
# 如果没有找到对应的中文名称,返回原始名称
|
| 64 |
+
return country_map.get(english_name, english_name)
|
| 65 |
+
|
| 66 |
+
def get_isp_name_in_chinese(english_name):
|
| 67 |
+
"""将常见ISP名称转换为中文"""
|
| 68 |
+
isp_map = {
|
| 69 |
+
"GOOGLE": "谷歌",
|
| 70 |
+
"GOOGLE-CLOUD-PLATFORM": "谷歌云平台",
|
| 71 |
+
"GOOGLE CLOUD": "谷歌云",
|
| 72 |
+
"GOOGLE LLC": "谷歌公司",
|
| 73 |
+
"MICROSOFT": "微软",
|
| 74 |
+
"MICROSOFT CORPORATION": "微软公司",
|
| 75 |
+
"AMAZON": "亚马逊",
|
| 76 |
+
"AMAZON.COM": "亚马逊",
|
| 77 |
+
"AMAZON WEB SERVICES": "亚马逊云服务",
|
| 78 |
+
"AWS": "亚马逊云服务",
|
| 79 |
+
"CLOUDFLARE": "Cloudflare",
|
| 80 |
+
"ALIBABA": "阿里巴巴",
|
| 81 |
+
"ALIYUN": "阿里云",
|
| 82 |
+
"TENCENT": "腾讯",
|
| 83 |
+
"TENCENT CLOUD": "腾讯云",
|
| 84 |
+
"BAIDU": "百度",
|
| 85 |
+
"ORACLE": "甲骨文",
|
| 86 |
+
"IBM": "IBM",
|
| 87 |
+
"DIGITAL OCEAN": "Digital Ocean",
|
| 88 |
+
"LINODE": "Linode",
|
| 89 |
+
"CHINA TELECOM": "中国电信",
|
| 90 |
+
"CHINA UNICOM": "中国联通",
|
| 91 |
+
"CHINA MOBILE": "中国移动",
|
| 92 |
+
"CHINANET": "中国电信",
|
| 93 |
+
"CHINA UNICOM CLOUD": "中国联通云",
|
| 94 |
+
"AT&T": "AT&T",
|
| 95 |
+
"VERIZON": "威瑞森",
|
| 96 |
+
"COMCAST": "康卡斯特",
|
| 97 |
+
"LEVEL3": "Level 3",
|
| 98 |
+
"COGENT": "Cogent",
|
| 99 |
+
"NTT": "NTT",
|
| 100 |
+
"SOFTLAYER": "SoftLayer",
|
| 101 |
+
"RACKSPACE": "Rackspace",
|
| 102 |
+
"HETZNER": "Hetzner"
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
# 如果没有找到对应的中文名称,返回原始名称
|
| 106 |
+
# 将英文名称转为大写进行匹配,增加匹配成功率
|
| 107 |
+
return isp_map.get(english_name.upper(), english_name)
|
| 108 |
+
|
| 109 |
+
def get_geo_info(ip_address):
|
| 110 |
+
"""根据 IP 地址获取地理位置信息"""
|
| 111 |
+
if not ip_address:
|
| 112 |
+
print("无法获取地理信息: IP地址为空")
|
| 113 |
+
return None
|
| 114 |
+
|
| 115 |
+
# 首先尝试使用可能返回中文的API
|
| 116 |
+
try:
|
| 117 |
+
print(f"尝试使用ip.taobao.com获取IP {ip_address} 的地理信息...")
|
| 118 |
+
url = f"http://ip.taobao.com/outGetIpInfo?ip={ip_address}&accessKey=alibaba-inc"
|
| 119 |
+
headers = {
|
| 120 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 124 |
+
|
| 125 |
+
if response.status_code == 200:
|
| 126 |
+
try:
|
| 127 |
+
data = response.json()
|
| 128 |
+
if data.get('code') == 0:
|
| 129 |
+
data = data.get('data', {})
|
| 130 |
+
country = data.get('country', '未知')
|
| 131 |
+
isp = data.get('isp', '未知')
|
| 132 |
+
print(f"成功获取中文地理信息: {country}, {isp}")
|
| 133 |
+
return {
|
| 134 |
+
'ip': ip_address,
|
| 135 |
+
'country': country,
|
| 136 |
+
'isp': isp
|
| 137 |
+
}
|
| 138 |
+
except ValueError as e:
|
| 139 |
+
print(f"解析API响应JSON时出错: {e}")
|
| 140 |
+
else:
|
| 141 |
+
print(f"ip.taobao.com API请求失败,状态码: {response.status_code}")
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print(f"使用ip.taobao.com获取地理信息时出错: {e}")
|
| 144 |
+
|
| 145 |
+
# 尝试使用ipapi.co API(英文,可靠)
|
| 146 |
+
try:
|
| 147 |
+
print(f"尝试使用ipapi.co获取IP {ip_address} 的地理信息...")
|
| 148 |
+
url = f"https://ipapi.co/{ip_address}/json/"
|
| 149 |
+
headers = {
|
| 150 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 154 |
+
|
| 155 |
+
if response.status_code == 200:
|
| 156 |
+
try:
|
| 157 |
+
data = response.json()
|
| 158 |
+
english_country = data.get('country_name', '未知')
|
| 159 |
+
english_isp = data.get('org', '未知')
|
| 160 |
+
|
| 161 |
+
# 转换为中文
|
| 162 |
+
chinese_country = get_country_name_in_chinese(english_country)
|
| 163 |
+
chinese_isp = get_isp_name_in_chinese(english_isp)
|
| 164 |
+
|
| 165 |
+
print(f"成功获取并转换地理信息: {english_country} -> {chinese_country}, {english_isp} -> {chinese_isp}")
|
| 166 |
+
return {
|
| 167 |
+
'ip': ip_address,
|
| 168 |
+
'country': chinese_country,
|
| 169 |
+
'isp': chinese_isp
|
| 170 |
+
}
|
| 171 |
+
except ValueError as e:
|
| 172 |
+
print(f"解析API响应JSON时出错: {e}")
|
| 173 |
+
else:
|
| 174 |
+
print(f"ipapi.co API请求失败,状态码: {response.status_code}")
|
| 175 |
+
except Exception as e:
|
| 176 |
+
print(f"使用ipapi.co获取地理信息时出错: {e}")
|
| 177 |
+
|
| 178 |
+
# 如果API调用失败,尝试使用另一个备用API: ip-api.com
|
| 179 |
+
try:
|
| 180 |
+
print(f"尝试使用ip-api.com获取IP {ip_address} 的地理信息...")
|
| 181 |
+
# 指定lang=zh-CN获取中文结果
|
| 182 |
+
url = f"http://ip-api.com/json/{ip_address}?lang=zh-CN"
|
| 183 |
+
response = requests.get(url, timeout=10)
|
| 184 |
+
|
| 185 |
+
if response.status_code == 200:
|
| 186 |
+
try:
|
| 187 |
+
data = response.json()
|
| 188 |
+
if data.get('status') == 'success':
|
| 189 |
+
country = data.get('country', '未知')
|
| 190 |
+
isp = data.get('isp', '未知')
|
| 191 |
+
print(f"成功获取中文地理信息: {country}, {isp}")
|
| 192 |
+
return {
|
| 193 |
+
'ip': ip_address,
|
| 194 |
+
'country': country,
|
| 195 |
+
'isp': isp
|
| 196 |
+
}
|
| 197 |
+
except ValueError as e:
|
| 198 |
+
print(f"解析API响应JSON时出错: {e}")
|
| 199 |
+
else:
|
| 200 |
+
print(f"ip-api.com API请求失败,状态码: {response.status_code}")
|
| 201 |
+
except Exception as e:
|
| 202 |
+
print(f"使用ip-api.com获取地理信息时出错: {e}")
|
| 203 |
+
|
| 204 |
+
# 如果所有API都失败,则使用模拟数据
|
| 205 |
+
print("所有API请求失败,使用模拟数据")
|
| 206 |
+
# 根据IP地址的第一个数字粗略判断国家(仅作为备选方案)
|
| 207 |
+
first_octet = int(ip_address.split('.')[0])
|
| 208 |
+
|
| 209 |
+
if first_octet < 50:
|
| 210 |
+
country = "美国"
|
| 211 |
+
isp = "未知ISP"
|
| 212 |
+
elif first_octet < 100:
|
| 213 |
+
country = "欧洲"
|
| 214 |
+
isp = "未知ISP"
|
| 215 |
+
elif first_octet < 150:
|
| 216 |
+
country = "亚洲"
|
| 217 |
+
isp = "未知ISP"
|
| 218 |
+
else:
|
| 219 |
+
country = "其他地区"
|
| 220 |
+
isp = "未知ISP"
|
| 221 |
+
|
| 222 |
+
print(f"使用模拟数据: 国家[{country}], ISP[{isp}]")
|
| 223 |
+
return {
|
| 224 |
+
'ip': ip_address,
|
| 225 |
+
'country': country,
|
| 226 |
+
'isp': isp
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
def get_next_relay_time():
|
| 230 |
+
"""获取下一次接力时间(系统启动时间+12小时,转换为中国时区)"""
|
| 231 |
+
try:
|
| 232 |
+
# 获取系统启动时间
|
| 233 |
+
boot_time = datetime.fromtimestamp(psutil.boot_time())
|
| 234 |
+
# 设置为UTC时区
|
| 235 |
+
utc = pytz.UTC
|
| 236 |
+
boot_time = utc.localize(boot_time)
|
| 237 |
+
# 转换为中国时区
|
| 238 |
+
china_tz = pytz.timezone('Asia/Shanghai')
|
| 239 |
+
boot_time_china = boot_time.astimezone(china_tz)
|
| 240 |
+
# 加上12小时
|
| 241 |
+
next_time = boot_time_china + timedelta(hours=12)
|
| 242 |
+
return next_time.strftime("%Y-%m-%d %H:%M:%S")
|
| 243 |
+
except Exception as e:
|
| 244 |
+
print(f"计算接力时间时出错: {e}")
|
| 245 |
+
return None
|
| 246 |
+
|
| 247 |
+
def replace_info_in_file(file_path):
|
| 248 |
+
# 检查文件是否存在
|
| 249 |
+
if not os.path.exists(file_path):
|
| 250 |
+
print(f"错误: 文件不存在 - {file_path}")
|
| 251 |
+
return False
|
| 252 |
+
|
| 253 |
+
try:
|
| 254 |
+
# 获取当前 IP 地址和地理信息
|
| 255 |
+
print("正在获取IP地址...")
|
| 256 |
+
ip_address = get_current_ip()
|
| 257 |
+
if not ip_address:
|
| 258 |
+
print("无法获取IP地址,终止操作")
|
| 259 |
+
return False
|
| 260 |
+
|
| 261 |
+
print(f"成功获取IP: {ip_address}")
|
| 262 |
+
print("正在获取地理信息...")
|
| 263 |
+
geo_info = get_geo_info(ip_address)
|
| 264 |
+
if not geo_info:
|
| 265 |
+
print("无法获取地理信息,终止操作")
|
| 266 |
+
return False
|
| 267 |
+
|
| 268 |
+
print("正在计算下一次接力时间...")
|
| 269 |
+
next_relay_time = get_next_relay_time()
|
| 270 |
+
if not next_relay_time:
|
| 271 |
+
print("无法计算接力时间,���止操作")
|
| 272 |
+
return False
|
| 273 |
+
|
| 274 |
+
print("正在读取文件...")
|
| 275 |
+
# 读取文件内容
|
| 276 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 277 |
+
content = file.read()
|
| 278 |
+
|
| 279 |
+
# 将文件内容输出到控制台,用于调试
|
| 280 |
+
print("文件原始内容片段:")
|
| 281 |
+
# print(content[:500] + "..." if len(content) > 500 else content)
|
| 282 |
+
|
| 283 |
+
# 接力时间模式匹配更灵活
|
| 284 |
+
print("尝试查找并替换接力时间...")
|
| 285 |
+
time_patterns = [
|
| 286 |
+
r'接力时间\[.*?\]',
|
| 287 |
+
r'接力时间\s*\[.*?\]',
|
| 288 |
+
r'接力时间[(\(].*?[)\)]'
|
| 289 |
+
]
|
| 290 |
+
|
| 291 |
+
time_replaced = False
|
| 292 |
+
for pattern in time_patterns:
|
| 293 |
+
try:
|
| 294 |
+
before_replace = content
|
| 295 |
+
content = re.sub(pattern, f'接力时间[{next_relay_time}]', content)
|
| 296 |
+
if before_replace != content:
|
| 297 |
+
time_replaced = True
|
| 298 |
+
print(f"成功使用模式 '{pattern}' 替换接力时间")
|
| 299 |
+
break
|
| 300 |
+
except Exception as e:
|
| 301 |
+
print(f"使用模式 '{pattern}' 替换时出错: {e}")
|
| 302 |
+
|
| 303 |
+
if not time_replaced:
|
| 304 |
+
print("使用模式匹配未能替换接力时间,尝试直接查找和替换...")
|
| 305 |
+
# 尝试查找包含"接力时间"的行并整行替换
|
| 306 |
+
lines = content.split('\n')
|
| 307 |
+
for i, line in enumerate(lines):
|
| 308 |
+
if "接力时间" in line:
|
| 309 |
+
# print(f"找到包含接力时间的行: {line}")
|
| 310 |
+
# lines[i] = f'接力时间[{next_relay_time}]'
|
| 311 |
+
# print(f"替换为: {lines[i]}")
|
| 312 |
+
# content = '\n'.join(lines)
|
| 313 |
+
# time_replaced = True
|
| 314 |
+
break
|
| 315 |
+
|
| 316 |
+
if not time_replaced:
|
| 317 |
+
print("警告: 无法替换接力时间")
|
| 318 |
+
|
| 319 |
+
# IP信息匹配更灵活
|
| 320 |
+
print("尝试查找并替换IP信息...")
|
| 321 |
+
# 首先尝试直接查找当前显示的IP
|
| 322 |
+
ip_pattern_raw = r'IP\[([\d\.]+)\]'
|
| 323 |
+
ip_matches = re.findall(ip_pattern_raw, content)
|
| 324 |
+
|
| 325 |
+
ip_replaced = False
|
| 326 |
+
if ip_matches:
|
| 327 |
+
current_ip = ip_matches[0]
|
| 328 |
+
print(f"找到当前显示的IP: {current_ip}")
|
| 329 |
+
|
| 330 |
+
# 尝试整行替换
|
| 331 |
+
lines = content.split('\n')
|
| 332 |
+
for i, line in enumerate(lines):
|
| 333 |
+
if current_ip in line:
|
| 334 |
+
print(f"找到包含当前IP的行: {line}")
|
| 335 |
+
|
| 336 |
+
# 替换IP
|
| 337 |
+
new_line = line.replace(f"IP[{current_ip}]", f"IP[{ip_address}]")
|
| 338 |
+
|
| 339 |
+
# 替换国家信息 - 处理可能的空格变体
|
| 340 |
+
country_patterns = [r'国家\[(.*?)\]', r'国家\s+\[(.*?)\]']
|
| 341 |
+
for pattern in country_patterns:
|
| 342 |
+
country_match = re.search(pattern, new_line)
|
| 343 |
+
if country_match:
|
| 344 |
+
# 保持原始空格格式
|
| 345 |
+
space = ' ' if '国家 [' in new_line else ''
|
| 346 |
+
new_line = re.sub(pattern, f'国家{space}[{geo_info["country"]}]', new_line)
|
| 347 |
+
break
|
| 348 |
+
|
| 349 |
+
# 替换ISP信息 - 同时处理ISP和IPS两种写法
|
| 350 |
+
isp_patterns = [r'ISP\[(.*?)\]', r'ISP\s+\[(.*?)\]', r'IPS\[(.*?)\]', r'IPS\s+\[(.*?)\]']
|
| 351 |
+
for pattern in isp_patterns:
|
| 352 |
+
isp_match = re.search(pattern, new_line)
|
| 353 |
+
if isp_match:
|
| 354 |
+
# 确定使用ISP还是IPS,并保持空格格式
|
| 355 |
+
isp_tag = 'IPS' if 'IPS' in new_line else 'ISP'
|
| 356 |
+
space = ' ' if f'{isp_tag} [' in new_line else ''
|
| 357 |
+
new_line = re.sub(pattern, f'{isp_tag}{space}[{geo_info["isp"]}]', new_line)
|
| 358 |
+
break
|
| 359 |
+
|
| 360 |
+
lines[i] = new_line
|
| 361 |
+
content = '\n'.join(lines)
|
| 362 |
+
ip_replaced = True
|
| 363 |
+
print(f"替换为新行: {new_line}")
|
| 364 |
+
break
|
| 365 |
+
|
| 366 |
+
if not ip_replaced:
|
| 367 |
+
print("警告: 无法替换IP信息")
|
| 368 |
+
# 尝试在文件中查找任何IP模式来帮助诊断
|
| 369 |
+
all_ips = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', content)
|
| 370 |
+
if all_ips:
|
| 371 |
+
print(f"文件中找到的所有IP: {all_ips[:5]}")
|
| 372 |
+
|
| 373 |
+
# 尝试找出任何可能的IP行格式
|
| 374 |
+
lines = content.split('\n')
|
| 375 |
+
for line in lines:
|
| 376 |
+
if re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', line):
|
| 377 |
+
print(f"包含IP的行: {line}")
|
| 378 |
+
|
| 379 |
+
# 直接搜索和尝试改变特定的HTML或文本内容
|
| 380 |
+
print("尝试直接搜索和替换特定内容...")
|
| 381 |
+
|
| 382 |
+
# 尝试查找并替换HTML中的特定元素
|
| 383 |
+
# 例如 <div>IP[...] 国家[...] ISP[...]</div>
|
| 384 |
+
html_pattern = r'<[^>]*>([^<]*IP\[[\d\.]+\][^<]*国家\s*\[[^\]]*\][^<]*(?:ISP|IPS)\s*\[[^\]]*\][^<]*)<[^>]*>'
|
| 385 |
+
html_matches = re.findall(html_pattern, content)
|
| 386 |
+
|
| 387 |
+
if html_matches:
|
| 388 |
+
print(f"找到HTML元素中的IP信息: {html_matches[0]}")
|
| 389 |
+
for match in html_matches:
|
| 390 |
+
replacement = match
|
| 391 |
+
# 替换IP
|
| 392 |
+
replacement = re.sub(r'IP\[[\d\.]+\]', f'IP[{ip_address}]', replacement)
|
| 393 |
+
# 替换国家
|
| 394 |
+
replacement = re.sub(r'国家\s*\[[^\]]*\]', f'国家[{geo_info["country"]}]', replacement)
|
| 395 |
+
# 替换ISP/IPS
|
| 396 |
+
if 'ISP' in replacement:
|
| 397 |
+
replacement = re.sub(r'ISP\s*\[[^\]]*\]', f'ISP[{geo_info["isp"]}]', replacement)
|
| 398 |
+
else:
|
| 399 |
+
replacement = re.sub(r'IPS\s*\[[^\]]*\]', f'IPS[{geo_info["isp"]}]', replacement)
|
| 400 |
+
|
| 401 |
+
content = content.replace(match, replacement)
|
| 402 |
+
print(f"替换HTML元素内容: {match} -> {replacement}")
|
| 403 |
+
|
| 404 |
+
print("正在写入更新后的内容...")
|
| 405 |
+
# 写回文件
|
| 406 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 407 |
+
file.write(content)
|
| 408 |
+
|
| 409 |
+
print(f"成功更新信息:")
|
| 410 |
+
print(f"接力时间: {next_relay_time}")
|
| 411 |
+
print(f"IP信息: IP[{ip_address}] 国家[{geo_info['country']}] ISP[{geo_info['isp']}]")
|
| 412 |
+
|
| 413 |
+
# 添加直接编辑HTML的备选方式
|
| 414 |
+
print("尝试使用更直接的方式修改文件...")
|
| 415 |
+
try:
|
| 416 |
+
# 使用简单的字符串替换
|
| 417 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
| 418 |
+
html_content = file.read()
|
| 419 |
+
|
| 420 |
+
# 1. 查找任何IP地址并替换为新IP
|
| 421 |
+
old_ips = set(re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', html_content))
|
| 422 |
+
for old_ip in old_ips:
|
| 423 |
+
if old_ip != ip_address: # 避免替换已经是新IP的情况
|
| 424 |
+
print(f"直接替换IP: {old_ip} -> {ip_address}")
|
| 425 |
+
html_content = html_content.replace(old_ip, ip_address)
|
| 426 |
+
|
| 427 |
+
# 2. 直接替换国家名称
|
| 428 |
+
# 找出当前显示的国家名
|
| 429 |
+
country_matches = re.findall(r'国家\s*\[([^\]]+)\]', html_content)
|
| 430 |
+
if country_matches:
|
| 431 |
+
for country in country_matches:
|
| 432 |
+
if country != geo_info["country"]:
|
| 433 |
+
print(f"直接替换国家: {country} -> {geo_info['country']}")
|
| 434 |
+
html_content = html_content.replace(f"国家[{country}]", f"国家[{geo_info['country']}]")
|
| 435 |
+
html_content = html_content.replace(f"国家 [{country}]", f"国家 [{geo_info['country']}]")
|
| 436 |
+
|
| 437 |
+
# 3. 直接替换ISP名称
|
| 438 |
+
isp_matches = re.findall(r'ISP\s*\[([^\]]+)\]', html_content)
|
| 439 |
+
if isp_matches:
|
| 440 |
+
for isp in isp_matches:
|
| 441 |
+
if isp != geo_info["isp"]:
|
| 442 |
+
print(f"直接替换ISP: {isp} -> {geo_info['isp']}")
|
| 443 |
+
html_content = html_content.replace(f"ISP[{isp}]", f"ISP[{geo_info['isp']}]")
|
| 444 |
+
html_content = html_content.replace(f"ISP [{isp}]", f"ISP [{geo_info['isp']}]")
|
| 445 |
+
|
| 446 |
+
# IPS变体
|
| 447 |
+
ips_matches = re.findall(r'IPS\s*\[([^\]]+)\]', html_content)
|
| 448 |
+
if ips_matches:
|
| 449 |
+
for isp in ips_matches:
|
| 450 |
+
if isp != geo_info["isp"]:
|
| 451 |
+
print(f"直接替换IPS: {isp} -> {geo_info['isp']}")
|
| 452 |
+
html_content = html_content.replace(f"IPS[{isp}]", f"IPS[{geo_info['isp']}]")
|
| 453 |
+
html_content = html_content.replace(f"IPS [{isp}]", f"IPS [{geo_info['isp']}]")
|
| 454 |
+
|
| 455 |
+
# 写回文件
|
| 456 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 457 |
+
file.write(html_content)
|
| 458 |
+
|
| 459 |
+
print("完成直接字符串替换")
|
| 460 |
+
except Exception as e:
|
| 461 |
+
print(f"直接替换方法出错: {e}")
|
| 462 |
+
|
| 463 |
+
return True
|
| 464 |
+
|
| 465 |
+
except Exception as e:
|
| 466 |
+
print(f"发生错误: {str(e)}")
|
| 467 |
+
print("详细错误信息:")
|
| 468 |
+
traceback.print_exc()
|
| 469 |
+
return False
|
| 470 |
+
|
| 471 |
+
# 修改文件路径
|
| 472 |
+
file_path = '/data/SillyTavern/public/login.html'
|
| 473 |
+
print(f"准备修改文件: {file_path}")
|
| 474 |
+
success = replace_info_in_file(file_path)
|
| 475 |
+
if not success:
|
| 476 |
+
print("文件更新失败")
|
| 477 |
+
else:
|
| 478 |
+
print("文件更新成功")
|