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

Update server2.py

Browse files
Files changed (1) hide show
  1. server2.py +39 -28
server2.py CHANGED
@@ -93,6 +93,7 @@ def update():
93
  # os.system("apt-get install inotify-tools")
94
  os.system("pip install ruamel.yaml") # install ruamel.yaml to solve the import issue
95
  os.system("pip install pexpect")
 
96
  os.system('apt-get install pigz')
97
  os.system('apt-get install pv')
98
  print('-----------更新软件包列表结束-------------')
@@ -428,6 +429,9 @@ def frp(config_data1,config_file_path,backstage):
428
 
429
  import requests
430
  import re
 
 
 
431
 
432
  def get_current_ip():
433
  """获取当前公共 IP 地址"""
@@ -444,7 +448,7 @@ def get_geo_info(ip_address):
444
  response = requests.get(url, headers=headers)
445
 
446
  if response.status_code == 200:
447
- geo_info = response.json() # 解析为 JSON 格式
448
 
449
  if geo_info['code'] == 200:
450
  data = geo_info['data']
@@ -453,48 +457,55 @@ def get_geo_info(ip_address):
453
  'country': data['country'],
454
  'isp': data['isp']
455
  }
456
- else:
457
- print("无法获取地理位置信息")
458
- return None
459
- else:
460
- print(f"请求失败, 状态码: {response.status_code}")
461
- return None
462
-
463
- def replace_ip_and_country_in_file(file_path):
 
 
 
 
 
 
 
 
 
464
  try:
465
- # 获取当前 IP 地址
466
  ip_address = get_current_ip()
467
- print(f"获取到当前 IP: {ip_address}")
468
-
469
- # 获取地理位置信息
470
  geo_info = get_geo_info(ip_address)
471
- print(f"获取到地理信息: {geo_info}")
472
 
473
  if geo_info is not None:
474
  # 读取文件内容
475
  with open(file_path, 'r', encoding='utf-8') as file:
476
  content = file.read()
477
- # print(f"读取到的文件内容: {content}")
478
 
479
- # 使用正则表达式匹配 "IP[xxx] 国家 [yyy] IPS[zzz]" 的格式
480
- pattern = r'IP\[.*?\]\s*国家\s*\[.*?\]\s*IPS\[.*?\]'
481
- replacement = f'IP[{ip_address}] 国家 [{geo_info["country"]}] IPS[{geo_info["isp"]}]'
482
- new_content = re.sub(pattern, replacement, content)
483
 
484
- # print(f"替换后的内容: {new_content}")
 
 
 
485
 
486
- # 将修改后的内容写回文件
487
  with open(file_path, 'w', encoding='utf-8') as file:
488
- file.write(new_content)
489
 
490
- print(f"成功更新为: IP[{ip_address}] 国家 [{geo_info['country']}] IPS[{geo_info['isp']}]")
 
 
491
 
492
- except FileNotFoundError:
493
- print(f"错误: 文件 {file_path} 不存在。")
494
- except IOError as e:
495
- print(f"IO 错误: {e}")
496
  except Exception as e:
497
- print(f"发生其他错误: {e}")
 
498
 
499
  import socket
500
  import subprocess
 
93
  # os.system("apt-get install inotify-tools")
94
  os.system("pip install ruamel.yaml") # install ruamel.yaml to solve the import issue
95
  os.system("pip install pexpect")
96
+ os.system("pip install pytz")
97
  os.system('apt-get install pigz')
98
  os.system('apt-get install pv')
99
  print('-----------更新软件包列表结束-------------')
 
429
 
430
  import requests
431
  import re
432
+ from datetime import datetime, timedelta
433
+ import psutil # 用于获取系统启动时间
434
+ import pytz # 用于处理时区
435
 
436
  def get_current_ip():
437
  """获取当前公共 IP 地址"""
 
448
  response = requests.get(url, headers=headers)
449
 
450
  if response.status_code == 200:
451
+ geo_info = response.json()
452
 
453
  if geo_info['code'] == 200:
454
  data = geo_info['data']
 
457
  'country': data['country'],
458
  'isp': data['isp']
459
  }
460
+ return None
461
+
462
+ def get_next_relay_time():
463
+ """获取下一次接力时间(系统启动时间+12小时,转换为中国时区)"""
464
+ # 获取系统启动时间
465
+ boot_time = datetime.fromtimestamp(psutil.boot_time())
466
+ # 设置为UTC时区
467
+ utc = pytz.UTC
468
+ boot_time = utc.localize(boot_time)
469
+ # 转换为中国时区
470
+ china_tz = pytz.timezone('Asia/Shanghai')
471
+ boot_time_china = boot_time.astimezone(china_tz)
472
+ # 加上12小时
473
+ next_time = boot_time_china + timedelta(hours=12)
474
+ return next_time.strftime("%Y-%m-%d %H:%M:%S")
475
+
476
+ def replace_info_in_file(file_path):
477
  try:
478
+ # 获取当前 IP 地址和地理信息
479
  ip_address = get_current_ip()
 
 
 
480
  geo_info = get_geo_info(ip_address)
481
+ next_relay_time = get_next_relay_time()
482
 
483
  if geo_info is not None:
484
  # 读取文件内容
485
  with open(file_path, 'r', encoding='utf-8') as file:
486
  content = file.read()
 
487
 
488
+ # 替换接力时间
489
+ time_pattern = r'接力时间\[.*?\]'
490
+ time_replacement = f'接力时间[{next_relay_time}]'
491
+ content = re.sub(time_pattern, time_replacement, content)
492
 
493
+ # 替换 IP 信息
494
+ ip_pattern = r'IP\[.*?\]\s*国家\s*\[.*?\]\s*IPS\[.*?\]'
495
+ ip_replacement = f'IP[{ip_address}] 国家 [{geo_info["country"]}] IPS[{geo_info["isp"]}]'
496
+ content = re.sub(ip_pattern, ip_replacement, content)
497
 
498
+ # 写回文件
499
  with open(file_path, 'w', encoding='utf-8') as file:
500
+ file.write(content)
501
 
502
+ print(f"成功更新信息:")
503
+ print(f"接力时间: {next_relay_time}")
504
+ print(f"IP信息: IP[{ip_address}] 国家 [{geo_info['country']}] IPS[{geo_info['isp']}]")
505
 
 
 
 
 
506
  except Exception as e:
507
+ print(f"发生错误: {e}")
508
+
509
 
510
  import socket
511
  import subprocess