2020/07/02
如今有很多学校、企业选用 EasyConnect 套端作为远程网络连接方案。但 EasyConnect 的客户端高度支持服务端自定义,其中服务端访问ip权限、ip映射可由服务端细致定义。与此形成对比的是客户端的透明度差,几乎所有配置由客户端为用户自动完成,且没有查看和更改选项。
在我使用 EasyConnect 远程访问学校校园网时,尴尬的问题来了:EasyConnect 默认做全局代理,而我只是想用 EasyConnect 访问知网或校园网资源,不想在其它的应用中被代理拖慢速度。想设置 EasyConnect ,它却根本不提供有用选项。那么有没有办法可以从底层限制 EasyConnect 的代理范围呢?
答案是肯定的。(以下适用 Windows 系统)
方法:修改路由表配置
在命令行中输入如下指令可以看到当前的路由表配置
route print
之后我发现,校园的 EasyConnect 对所有段的ip都做了代理。
既然 EasyConnect 内部不能修改,那我们可以在它做代理之后删除它的代理路由表!
因为我对cmd不熟悉(qwq),具体实现采用python夹带调用cmd的方式。以下代码实现了按从路由表中搜取172.29.*.*地址的代理,之后删除相关代理,最后添加 routes 地址到代理的功能。
import os
import re
routes = ['8.8.8.8',]
txt = os.popen('route print 172.29.*')
txt = txt.read()
result = re.search(r' 172.29.* (172.29.*) (172.29.*) ',txt)
if result:
gate = result.group(1).strip()
inter = result.group(2).strip()
print(f'网关:{gate} 接口:{inter}')
txt = os.popen('route print')
txt = txt.readlines()
for line in txt:
result = re.split('[ ]+', line.strip())
if len(result)==5 and result[2].strip()==gate and result[3].strip()==inter:
para1 = result[0].strip()
os.system(f'route delete {para1} {gate}')
for route in routes:
os.system(f'route add {route} mask 255.255.255.255 {gate}')
else:
print('如果重复运行该程序,因主要网关已被清理将无法找到VPN网关,请尝试优化程序或重启VPN。')
鉴于有命令需要管理员权限,我平时也爱写bat来一键运行py文件,下面再给出bat代码:
@echo off
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit
cd /d "%~dp0"
python vpn.py
pause
其中Python文件名为 vpn.py。
以上涉及大量Windows系统命令调用, Linux 平台需要对应做一些修改。