xinxin
This commit is contained in:
parent
d2c1625fb6
commit
2632d2d26b
88
tools.py
88
tools.py
@ -26,6 +26,7 @@ import requests
|
||||
import subprocess
|
||||
import time
|
||||
import logging
|
||||
from lxml import etree
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
@ -189,59 +190,72 @@ def connect_wifi(ssid: str = 'MaxEntropy', password: str = 'cskj12345678') -> bo
|
||||
except Exception as e:
|
||||
print(f"清理wifi连接的临时文件失败: {str(e)}")
|
||||
|
||||
def force_kill_proxifier(process_name="proxifier.exe"):
|
||||
"""强制终止Proxifier进程的可靠方法"""
|
||||
# 方法1:使用psutil终止所有同名进程
|
||||
killed = False
|
||||
for proc in psutil.process_iter(['name']):
|
||||
if proc.info['name'].lower() == process_name.lower():
|
||||
try:
|
||||
proc.kill() # 发送终止信号
|
||||
proc.wait(timeout=3) # 等待进程结束
|
||||
print(f"已终止进程: {proc.pid}")
|
||||
killed = True
|
||||
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.TimeoutExpired) as e:
|
||||
print(f"终止失败 ({proc.pid}): {type(e).__name__}")
|
||||
|
||||
# 设置代理
|
||||
def set_proxy(proxy_ip: str, proxy_port: str):
|
||||
"""一个函数完成所有功能:切换代理 -> 重启程序 -> 验证 -> 对比"""
|
||||
|
||||
# ========== 第一部分:加载代理配置文件 ==========
|
||||
# 修改配置文件,将代理IP和端口写入配置文件
|
||||
proxifier_path = r"C:\Program Files (x86)\Proxifier\Proxifier.exe"
|
||||
pyautogui.PAUSE = 0.2
|
||||
ppx_path = r"C:\Users\marui\AppData\Roaming\Proxifier4\Profiles\temp.ppx"
|
||||
|
||||
# ========== 第一部分:切换代理 ==========
|
||||
# 关闭现有程序
|
||||
# 读取XML文件
|
||||
with open(ppx_path, 'rb') as f:
|
||||
tree = etree.parse(f)
|
||||
|
||||
# 查找目标代理节点 (id="100")
|
||||
proxy_node = tree.find('.//Proxy[@id="100"]')
|
||||
if proxy_node is None:
|
||||
raise ValueError("未找到id=100的代理节点")
|
||||
|
||||
# 更新IP地址
|
||||
address = proxy_node.find('./Address')
|
||||
if address is not None:
|
||||
address.text = proxy_ip
|
||||
|
||||
# 更新端口
|
||||
port_node = proxy_node.find('./Port')
|
||||
if port_node is not None:
|
||||
port_node.text = str(proxy_port)
|
||||
|
||||
# 保存修改后的文件(保留原始XML格式)
|
||||
with open(ppx_path, 'wb') as f:
|
||||
f.write(b'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n')
|
||||
tree.write(f, encoding='utf-8', xml_declaration=False, pretty_print=True)
|
||||
|
||||
# ========== 第二部分:加载代理配置文件 ==========
|
||||
# 检测Proxifier是否已经运行
|
||||
try:
|
||||
force_kill_proxifier()
|
||||
# 尝试连接到已运行的Proxifier程序
|
||||
app = pywinauto.Application().connect(path=proxifier_path, timeout=2)
|
||||
|
||||
# 获取主窗口
|
||||
main_window = app.window(title_re='temp - Proxifier').wait("ready", timeout=20)
|
||||
main_window.set_focus()
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
pass
|
||||
print(f"Proxifier未运行或连接失败: {e}")
|
||||
# 如果连接失败,启动程序
|
||||
app = pywinauto.Application().start(proxifier_path)
|
||||
main_window = app.window(title_re='temp - Proxifier').wait("ready", timeout=20)
|
||||
print("Proxifier已启动")
|
||||
|
||||
# 启动程序
|
||||
app = pywinauto.Application().start(proxifier_path)
|
||||
app.window(title_re='temp - Proxifier').wait("exists", timeout=20)
|
||||
|
||||
# 切换代理(Alt -> Tab -> Enter -> Enter -> Enter -> 输入IP和端口 -> 确认)
|
||||
# 重新加载配置文件 - 使用快捷键方式
|
||||
pyautogui.PAUSE = 0.1
|
||||
logger.info("正在重新加载配置文件...")
|
||||
# 使用Alt+F快捷键打开文件菜单,然后选择重新加载
|
||||
pyautogui.keyDown('alt')
|
||||
pyautogui.keyUp('alt')
|
||||
pyautogui.press('enter')
|
||||
pyautogui.press('tab')
|
||||
pyautogui.press('enter')
|
||||
pyautogui.press('enter')
|
||||
pyautogui.press('enter')
|
||||
|
||||
pyautogui.typewrite(proxy_ip)
|
||||
pyautogui.press('tab')
|
||||
pyautogui.typewrite(str(proxy_port))
|
||||
pyautogui.press('enter')
|
||||
|
||||
# 确认代理设置
|
||||
for _ in range(6):
|
||||
pyautogui.press('tab')
|
||||
pyautogui.press('enter')
|
||||
|
||||
logger.info("代理切换完成,开始验证代理设置")
|
||||
|
||||
# ========== 第三部分:验证结果 ==========
|
||||
# 关闭可能的干扰界面
|
||||
pyautogui.press('escape')
|
||||
pyautogui.press('escape')
|
||||
@ -266,12 +280,14 @@ def set_proxy(proxy_ip: str, proxy_port: str):
|
||||
pyautogui.hotkey('ctrl', 'c')
|
||||
time.sleep(0.2)
|
||||
current_port = pyperclip.paste().strip()
|
||||
print(current_ip)
|
||||
print(current_port)
|
||||
logger.info(f"当前代理IP: {current_ip}")
|
||||
logger.info(f"当前代理端口: {current_port}")
|
||||
|
||||
# 关闭可能的干扰界面
|
||||
pyautogui.press('escape')
|
||||
pyautogui.press('escape')
|
||||
pyautogui.press('escape')
|
||||
pyautogui.press('escape')
|
||||
|
||||
# ========== 第三部分:验证结果 ==========
|
||||
if current_ip == proxy_ip and current_port == str(proxy_port):
|
||||
|
Loading…
x
Reference in New Issue
Block a user