Qilan2 commited on
Commit
3b65f97
·
verified ·
1 Parent(s): c2984e1

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +38 -28
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() # 解析为 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
- else:
450
- print("无法获取地理位置信息")
451
- return None
452
- else:
453
- print(f"请求失败, 状态码: {response.status_code}")
454
- return None
455
-
456
- def replace_ip_and_country_in_file(file_path):
 
 
 
 
 
 
 
 
 
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
- print(f"获取到地理信息: {geo_info}")
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
- # 使用正则表达式匹配 "IP[xxx] 国家 [yyy] IPS[zzz]" 的格式
473
- pattern = r'IP\[.*?\]\s*国家\s*\[.*?\]\s*IPS\[.*?\]'
474
- replacement = f'IP[{ip_address}] 国家 [{geo_info["country"]}] IPS[{geo_info["isp"]}]'
475
- new_content = re.sub(pattern, replacement, content)
476
 
477
- # print(f"替换后的内容: {new_content}")
 
 
 
478
 
479
- # 将修改后的内容写回文件
480
  with open(file_path, 'w', encoding='utf-8') as file:
481
- file.write(new_content)
482
 
483
- print(f"成功更新为: IP[{ip_address}] 国家 [{geo_info['country']}] IPS[{geo_info['isp']}]")
 
 
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"发生其他错误: {e}")
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