main.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # -*- coding: utf-8 -*-
  2. from urllib.parse import quote
  3. import subprocess
  4. import httpx
  5. import tkinter as tk
  6. nodes = [
  7. ["192.168.31.201", ['58001', '58002', '58003', '58004', '58005', '58006', '58007', '58008', '58009',
  8. '58010'], ],
  9. ["192.168.31.201", ['32001', '32002', '32003', '32004', '32005', '32006', '32007', '32008', '32009',
  10. '32010', '32011', '32012']],
  11. ["127.0.0.1", ['17888']]
  12. ]
  13. selected_nodes = nodes[0]
  14. output_messages = [] # 用于存储输出信息
  15. def patch_config(url_and_port):
  16. url = f"http://{url_and_port}"
  17. key = "/api/configs"
  18. full_url = url + key
  19. data = {"mode": "Global"}
  20. headers = {"Content-Type": "application/json"}
  21. try:
  22. response = httpx.patch(full_url, json=data, headers=headers)
  23. state = response.status_code
  24. if state == 204:
  25. pass
  26. # print(f"{url_and_port}: 切换全局代理 OK")
  27. else:
  28. raise Exception(f"请求失败: {response.status_code}")
  29. except httpx.HTTPError as exc:
  30. print(f"请求失败: {exc}")
  31. exit(1)
  32. def check_proxy(proxy_url, choose_proxy):
  33. encode_proxy_name = quote(choose_proxy, safe="")
  34. command = [
  35. "curl",
  36. "-X", "GET",
  37. f"{proxy_url}/api/proxies/{encode_proxy_name}/delay?timeout=5000&url=http:%2F%2Fwww.gstatic.com%2Fgenerate_204"
  38. ]
  39. try:
  40. result = subprocess.run(command, capture_output=True, text=True, check=True)
  41. # print("Output:", result.stdout)
  42. if 'Timeout' in result.stdout:
  43. return "Timeout"
  44. res = eval(result.stdout).get("meanDelay")
  45. return f"meanDelay: {res}"
  46. except subprocess.CalledProcessError as e:
  47. print("Error:", e.stderr)
  48. return str(e)
  49. def check_now_proxy(url_and_port):
  50. url = f"http://{url_and_port}/api/proxies"
  51. print(url)
  52. headers = {
  53. "Accept": "application/json, text/plain, */*",
  54. "Accept-Encoding": "gzip, deflate, br, zstd",
  55. "Accept-Language": "zh-CN,zh;q=0.8",
  56. "Connection": "keep-alive",
  57. "Host": url_and_port,
  58. "Referer": f"http://{url_and_port}/",
  59. "Sec-CH-UA": '"Chromium";v="134", "Not:A-Brand";v="24", "Brave";v="134"',
  60. "Sec-CH-UA-Mobile": "?0",
  61. "Sec-CH-UA-Platform": '"macOS"',
  62. "Sec-Fetch-Dest": "empty",
  63. "Sec-Fetch-Mode": "cors",
  64. "Sec-Fetch-Site": "same-origin",
  65. "Sec-GPC": "1",
  66. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36"
  67. }
  68. try:
  69. response = httpx.get(url, headers=headers)
  70. json_data = response.json()
  71. if not json_data or response.status_code != 200:
  72. print("JSON data is empty or request failed")
  73. return
  74. proxies = json_data.get("proxies")
  75. proxy_global = proxies.get("GLOBAL")
  76. now_proxy = proxy_global.get("now")
  77. return now_proxy
  78. except httpx.RequestError as e:
  79. print(f"Request failed: {e}")
  80. return False
  81. def run():
  82. ip = selected_nodes[0]
  83. for port in selected_nodes[1]:
  84. url_and_port = f"{ip}:{port}"
  85. # 切换全局代理
  86. patch_config(url_and_port)
  87. # 获取当前代理节点
  88. now_proxy = check_now_proxy(url_and_port)
  89. # 检测当前代理节点的延迟
  90. check_result = check_proxy(url_and_port, now_proxy)
  91. message = f"{url_and_port} --- {now_proxy} --- {check_result}\n{'*' * 88}\n\n"
  92. print(message)
  93. output_messages.append(message) # 将输出信息添加到列表中
  94. def display_output():
  95. run() # 执行主逻辑
  96. output_text.delete(1.0, tk.END) # 清空文本框
  97. for message in output_messages: # 将所有输出信息显示到文本框中
  98. output_text.insert(tk.END, message)
  99. # 创建主窗口
  100. root = tk.Tk()
  101. root.title("Proxy Checker")
  102. root.geometry("800x900")
  103. # 创建按钮
  104. check_button = tk.Button(root, text="Check Node", command=display_output)
  105. check_button.pack(pady=10)
  106. # 创建文本框(不带滚动条)
  107. output_text = tk.Text(root, width=100, height=60)
  108. output_text.pack(pady=10)
  109. # 启动主循环
  110. root.mainloop()