原理
修改注册表让代{过}{滤}理设置自动化生效
注册表路径: HKEY_CURRENT_USER/Software\Microsoft\Windows\CurrentVersion\Internet Settings,通过修改注册表直接让代{过}{滤}理服务器生效
代码实现
from winreg import OpenKey, SetValueEx, QueryValueEx, CloseKey, HKEY_CURRENT_USER, KEY_ALL_ACCESS, REG_DWORD, REG_SZ
# 配置常量
PROXY_IP = '192.168.5.250'
PROXY_PORT = '7890'
BYPASS_LIST = ['<local>', '*.steamchina.com', 'csgo.wmsj.cn', 'dota2.wmsj.cn', 'wmsjsteam.com', 'dl.steam.clngaa.com', 'dl.steam.ksyna.com', 'st.dl.bscstorage.net', 'st.dl.eccdnx.com', 'st.dl.pinyuncloud.com', 'steampipe.steamcontent.tnkjmec.com', 'steampowered.com.8686c.com', 'steamstatic.com.8686c.com', 'xz.pphimalayanrt.com', 'steampipe-kr.akamaized.net', 'steampipe-partner.akamaized.net', 'steampipe.akamaized.net', '.steamcontent.com', 'localhost', '127.*', '10.*', '224.*', '239.*', '172.16-31.*', '192.168.*']
def toggle_proxy(proxy_ip, proxy_port, bypass_list):
# 打开Internet Settings注册表键
internet_settings = OpenKey(HKEY_CURRENT_USER,
r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
0, KEY_ALL_ACCESS)
print("打开Internet Settings注册表键")
# 检查代{过}{滤}理是否已启用
proxy_enabled = QueryValueEx(internet_settings, 'ProxyEnable')[0]
if proxy_enabled:
# 如果代{过}{滤}理已启用,禁用代{过}{滤}理
SetValueEx(internet_settings, 'ProxyEnable', 0, REG_DWORD, 0)
print("代{过}{滤}理已禁用")
else:
# 如果代{过}{滤}理未启用,启用代{过}{滤}理
SetValueEx(internet_settings, 'ProxyEnable', 0, REG_DWORD, 1)
print("代{过}{滤}理已启用")
# 设置代{过}{滤}理服务器和端口
proxy_server = f'{proxy_ip}:{proxy_port}'
SetValueEx(internet_settings, 'ProxyServer', 0, REG_SZ, proxy_server)
print(f"设置代{过}{滤}理服务器和端口为:{proxy_server}")
# 设置不使用代{过}{滤}理的地址
bypass = ';'.join(bypass_list)
SetValueEx(internet_settings, 'ProxyOverride', 0, REG_SZ, bypass)
print(f"设置不使用代{过}{滤}理的地址为:{bypass}")
# 关闭注册表键
CloseKey(internet_settings)
print("关闭Internet Settings注册表键")
# 使用
toggle_proxy(PROXY_IP, PROXY_PORT, BYPASS_LIST)