Update server.py
Browse files
server.py
CHANGED
|
@@ -91,6 +91,7 @@ def update():
|
|
| 91 |
# os.system("apt-get install inotify-tools")
|
| 92 |
os.system("pip install ruamel.yaml") # install ruamel.yaml to solve the import issue
|
| 93 |
os.system("pip install pexpect")
|
|
|
|
| 94 |
os.system('apt-get install pigz')
|
| 95 |
os.system('apt-get install pv')
|
| 96 |
print('-----------更新软件包列表结束-------------')
|
|
@@ -421,6 +422,9 @@ def frp(config_data1,config_file_path,backstage):
|
|
| 421 |
|
| 422 |
import requests
|
| 423 |
import re
|
|
|
|
|
|
|
|
|
|
| 424 |
|
| 425 |
def get_current_ip():
|
| 426 |
"""获取当前公共 IP 地址"""
|
|
@@ -437,7 +441,7 @@ def get_geo_info(ip_address):
|
|
| 437 |
response = requests.get(url, headers=headers)
|
| 438 |
|
| 439 |
if response.status_code == 200:
|
| 440 |
-
geo_info = response.json()
|
| 441 |
|
| 442 |
if geo_info['code'] == 200:
|
| 443 |
data = geo_info['data']
|
|
@@ -446,48 +450,54 @@ def get_geo_info(ip_address):
|
|
| 446 |
'country': data['country'],
|
| 447 |
'isp': data['isp']
|
| 448 |
}
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
try:
|
| 458 |
-
# 获取当前 IP
|
| 459 |
ip_address = get_current_ip()
|
| 460 |
-
print(f"获取到当前 IP: {ip_address}")
|
| 461 |
-
|
| 462 |
-
# 获取地理位置信息
|
| 463 |
geo_info = get_geo_info(ip_address)
|
| 464 |
-
|
| 465 |
|
| 466 |
if geo_info is not None:
|
| 467 |
# 读取文件内容
|
| 468 |
with open(file_path, 'r', encoding='utf-8') as file:
|
| 469 |
content = file.read()
|
| 470 |
-
# print(f"读取到的文件内容: {content}")
|
| 471 |
|
| 472 |
-
#
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
|
| 477 |
-
#
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
-
#
|
| 480 |
with open(file_path, 'w', encoding='utf-8') as file:
|
| 481 |
-
file.write(
|
| 482 |
|
| 483 |
-
print(f"
|
|
|
|
|
|
|
| 484 |
|
| 485 |
-
except FileNotFoundError:
|
| 486 |
-
print(f"错误: 文件 {file_path} 不存在。")
|
| 487 |
-
except IOError as e:
|
| 488 |
-
print(f"IO 错误: {e}")
|
| 489 |
except Exception as e:
|
| 490 |
-
print(f"
|
| 491 |
|
| 492 |
import socket
|
| 493 |
import subprocess
|
|
|
|
| 91 |
# os.system("apt-get install inotify-tools")
|
| 92 |
os.system("pip install ruamel.yaml") # install ruamel.yaml to solve the import issue
|
| 93 |
os.system("pip install pexpect")
|
| 94 |
+
os.system("pip install pytz")
|
| 95 |
os.system('apt-get install pigz')
|
| 96 |
os.system('apt-get install pv')
|
| 97 |
print('-----------更新软件包列表结束-------------')
|
|
|
|
| 422 |
|
| 423 |
import requests
|
| 424 |
import re
|
| 425 |
+
from datetime import datetime, timedelta
|
| 426 |
+
import psutil # 用于获取系统启动时间
|
| 427 |
+
import pytz # 用于处理时区
|
| 428 |
|
| 429 |
def get_current_ip():
|
| 430 |
"""获取当前公共 IP 地址"""
|
|
|
|
| 441 |
response = requests.get(url, headers=headers)
|
| 442 |
|
| 443 |
if response.status_code == 200:
|
| 444 |
+
geo_info = response.json()
|
| 445 |
|
| 446 |
if geo_info['code'] == 200:
|
| 447 |
data = geo_info['data']
|
|
|
|
| 450 |
'country': data['country'],
|
| 451 |
'isp': data['isp']
|
| 452 |
}
|
| 453 |
+
return None
|
| 454 |
+
|
| 455 |
+
def get_next_relay_time():
|
| 456 |
+
"""获取下一次接力时间(系统启动时间+12小时,转换为中国时区)"""
|
| 457 |
+
# 获取系统启动时间
|
| 458 |
+
boot_time = datetime.fromtimestamp(psutil.boot_time())
|
| 459 |
+
# 设置为UTC时区
|
| 460 |
+
utc = pytz.UTC
|
| 461 |
+
boot_time = utc.localize(boot_time)
|
| 462 |
+
# 转换为中国时区
|
| 463 |
+
china_tz = pytz.timezone('Asia/Shanghai')
|
| 464 |
+
boot_time_china = boot_time.astimezone(china_tz)
|
| 465 |
+
# 加上12小时
|
| 466 |
+
next_time = boot_time_china + timedelta(hours=12)
|
| 467 |
+
return next_time.strftime("%Y-%m-%d %H:%M:%S")
|
| 468 |
+
|
| 469 |
+
def replace_info_in_file(file_path):
|
| 470 |
try:
|
| 471 |
+
# 获取当前 IP 地址和地理信息
|
| 472 |
ip_address = get_current_ip()
|
|
|
|
|
|
|
|
|
|
| 473 |
geo_info = get_geo_info(ip_address)
|
| 474 |
+
next_relay_time = get_next_relay_time()
|
| 475 |
|
| 476 |
if geo_info is not None:
|
| 477 |
# 读取文件内容
|
| 478 |
with open(file_path, 'r', encoding='utf-8') as file:
|
| 479 |
content = file.read()
|
|
|
|
| 480 |
|
| 481 |
+
# 替换接力时间
|
| 482 |
+
time_pattern = r'接力时间\[.*?\]'
|
| 483 |
+
time_replacement = f'接力时间[{next_relay_time}]'
|
| 484 |
+
content = re.sub(time_pattern, time_replacement, content)
|
| 485 |
|
| 486 |
+
# 替换 IP 信息
|
| 487 |
+
ip_pattern = r'IP\[.*?\]\s*国家\s*\[.*?\]\s*IPS\[.*?\]'
|
| 488 |
+
ip_replacement = f'IP[{ip_address}] 国家 [{geo_info["country"]}] IPS[{geo_info["isp"]}]'
|
| 489 |
+
content = re.sub(ip_pattern, ip_replacement, content)
|
| 490 |
|
| 491 |
+
# 写回文件
|
| 492 |
with open(file_path, 'w', encoding='utf-8') as file:
|
| 493 |
+
file.write(content)
|
| 494 |
|
| 495 |
+
print(f"成功更新信息:")
|
| 496 |
+
print(f"接力时间: {next_relay_time}")
|
| 497 |
+
print(f"IP信息: IP[{ip_address}] 国家 [{geo_info['country']}] IPS[{geo_info['isp']}]")
|
| 498 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 499 |
except Exception as e:
|
| 500 |
+
print(f"发生错误: {e}")
|
| 501 |
|
| 502 |
import socket
|
| 503 |
import subprocess
|