Windows指定网卡代理分流方案
Windows指定网卡代理分流方案(nic2proxy)
1. 环境准备
创建独立的 Python 虚拟环境并安装核心依赖库:
:: 创建虚拟环境
python -m venv G:\ProgramFiles\pythonvenv\wangka
:: 安装 nic2proxy
"G:\ProgramFiles\pythonvenv\wangka\Scripts\python.exe" -m pip install nic2proxy
2. 系统兼容性补丁(Windows 中文编码修复)
由于 Windows 中文系统默认使用 gbk 编码,而 nic2proxy 底层调用系统命令时默认使用 utf-8 解码,会导致字符解析报错。需修改 nic2proxy 源码进行适配:
- 打开文件:
G:\ProgramFiles\pythonvenv\wangka\Lib\site-packages\nic2proxy\__init__.py - 将代码中所有读取文件或解析输出的
utf-8/UTF-8编码替换为gbk。
可通过以下 Python 单行命令自动完成替换:
"G:\ProgramFiles\pythonvenv\wangka\Scripts\python.exe" -c "path = r'G:\ProgramFiles\pythonvenv\wangka\Lib\site-packages\nic2proxy\__init__.py'; content = open(path, 'r', encoding='utf-8', errors='ignore').read(); content = content.replace('utf-8', 'gbk').replace('UTF-8', 'gbk'); open(path, 'w', encoding='utf-8').write(content)"
3. 启动脚本编写
在 G:\ProgramFiles\pythonvenv\wangka\Scripts\ 目录下创建 run.py 脚本,内容如下:
from nic2proxy import MultiProxyServer
import os
# 键为本地监听地址及端口,值为需要绑定的目标网卡 IP
interfaces = {
"127.0.0.1:1081": "192.168.8.156"
}
# 日志目录配置
log_dir = r"G:\ProgramFiles\pythonvenv\wangka\logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
# 启动代理服务
server = MultiProxyServer(interfaces=interfaces, logfolder=log_dir)
server.start_proxy()
运行脚本启动服务:
"G:\ProgramFiles\pythonvenv\wangka\Scripts\python.exe" "G:\ProgramFiles\pythonvenv\wangka\Scripts\run.py"
4. 客户端配置
在应用(如比特浏览器)中配置代理服务器:
- 代理类型:
SOCKS5或HTTP - 代理主机:
127.0.0.1 - 代理端口:
1081
5. 进程管理与运维
检查端口占用及活跃连接:
netstat -ano | findstr 1081- 查看底层代理核心进程:
核心代理进程名称为s5light-windows-amd64-v3.exe。 强制关闭代理服务:
taskkill /F /IM s5light-windows-amd64-v3.exe
或者通过 PID 关闭:
taskkill /F /PID <PID数字>