【教程】Windows设置全局代理工具
【教程】Windows设置全局代理工具
小锋学长生活大爆炸

【教程】Windows设置全局代理工具

hualala
2023-09-04 / 0 评论 / 255 阅读
温馨提示:
本文最后更新于2023年09月05日,已超过442天没有更新,若内容或图片失效,请留言反馈。

有很多软件像CCProxy都是可以将本机设置带代理服务器,但就是没有一个比较好用的可以用来连接代理的软件,所以自己用Python写了个。

效果:

lm4myfti.png

代码:

import os
import winreg
import tkinter as tk
from tkinter import messagebox

# 默认代理服务器和端口
default_proxy_server = "202.38.247.229:7890"

# 初始代理服务器和端口为默认值
proxy_server = default_proxy_server

# 检查系统是否已开启代理
def check_proxy_enabled():
    try:
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings")
        proxy_enabled, _ = winreg.QueryValueEx(key, "ProxyEnable")
        winreg.CloseKey(key)
        return proxy_enabled == 1
    except Exception as e:
        return False

# 设置代理服务器和端口
def set_proxy():
    global proxy_server, proxy_port
    proxy_server = server_entry.get() or default_proxy_server

    try:
        # 打开注册表键
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", 0, winreg.KEY_WRITE)

        # 设置代理服务器和端口
        winreg.SetValueEx(key, "ProxyServer", 0, winreg.REG_SZ, proxy_server)

        # 启用代理
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 1)

        winreg.CloseKey(key)

        success_label.config(text="代理已设置", fg="green")
        messagebox.showinfo("成功", "代理已设置")
    except Exception as e:
        success_label.config(text="代理设置失败", fg="red")
        messagebox.showerror("错误", f"设置代理时出错: {str(e)}")

# 关闭代理
def disable_proxy():
    try:
        # 打开注册表键
        key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Internet Settings", 0, winreg.KEY_WRITE)

        # 禁用代理
        winreg.SetValueEx(key, "ProxyEnable", 0, winreg.REG_DWORD, 0)

        winreg.CloseKey(key)

        success_label.config(text="代理已禁用", fg="red")
        messagebox.showinfo("成功", "代理已禁用")
    except Exception as e:
        success_label.config(text="代理禁用失败", fg="yellow")
        messagebox.showerror("错误", f"禁用代理时出错: {str(e)}")

# 创建关闭窗口时的确认函数
def confirm_close():
    if check_proxy_enabled():
        result = messagebox.askquestion("确认退出", "退出软件将自动禁用代理。确定要退出吗?")
        if result == "yes":
            disable_proxy()
            window.quit()  # Exit the Tkinter mainloop and close the window
    else:
        window.quit()  # Exit the Tkinter mainloop and close the window


# 创建Tkinter窗口
window = tk.Tk()
window.title("全局代理设置")

# Calculate the screen width and height
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

# Calculate the x and y coordinates for the centered window
x = (screen_width - window.winfo_reqwidth()) / 2
y = (screen_height - window.winfo_reqheight()) / 2

# Set the window's geometry to be centered
window.geometry(f"+{int(x)}+{int(y)}")

# 标签和输入框
server_label = tk.Label(window, text="代理服务器:")
server_label.pack()
server_entry = tk.Entry(window)
server_entry.pack()
# 预填充默认代理服务器
server_entry.insert(0, default_proxy_server)

# 设置代理按钮
set_button = tk.Button(window, text="设置代理", command=set_proxy)
set_button.pack()

# 禁用代理按钮
disable_button = tk.Button(window, text="禁用代理", command=disable_proxy)
disable_button.pack()

# 代理设置结果标识
success_label = tk.Label(window, text="", fg="black")
success_label.pack()

# 检查代理设置并更新标识
if check_proxy_enabled():
    success_label.config(text="代理已开启", fg="green")
else:
    success_label.config(text="代理已禁用", fg="red")

# Register the confirm_close function to be called when the window is closed
window.protocol("WM_DELETE_WINDOW", confirm_close)

# 运行Tkinter主循环
window.mainloop()
0

评论 (0)

取消