← 返回文章列表
分类: 未分类vpn

一种将校园vpn用于服务器的方法

摘要

本文适用于需要让Linux服务器长期访问校园内网资源,无法直接在服务器安装EasyConnect客户端的场景,仅适配使用EasyConnect 7.6.7版本的校园VPN

2026/04/21 · Chai_na 0 浏览

本文适用于需要让Linux服务器长期访问校园内网资源,无法直接在服务器安装EasyConnect客户端的场景,仅适配使用EasyConnect 7.6.7版本的校园VPN

感谢大佬们提供的docker运行方法 docker-easyconnect

1. 启动 EasyConnect 容器

docker run -d --name easyconnect --device /dev/net/tun --cap-add NET_ADMIN -e PASSWORD=xxxx -e URLWIN=1 -e DISABLE_PKG_VERSION_XML=1 -v $HOME/.ecdata:/root -p 127.0.0.1:5901:5901 -p 127.0.0.1:1080:1080 -p 127.0.0.1:8888:8888 --restart unless-stopped hagb/docker-easyconnect:7.6.7

2. 使用 VNC 连接 EasyConnect

VNC 连接信息:

  • 地址:127.0.0.1:5901

  • 密码:启动容器时 PASSWORD= 设置的值

先在本地做 SSH 转发:

ssh -L 5901:127.0.0.1:5901 root@你的服务器IP

然后本地 VNC 客户端连接:

127.0.0.1:5901

3. 测试 VPN 是否已经连通

测试 SOCKS5 代理

curl -v --socks5 127.0.0.1:1080 172.18.144.18

测试 HTTP 代理

curl -v -x http://127.0.0.1:8888 172.18.144.18

返回了

*   Trying 127.0.0.1:1080...
* SOCKS5 connect to IPv4 172.18.144.18:80 (locally resolved)
* SOCKS5 request granted.
* Connected to (nil) (127.0.0.1) port 1080 (#0)
> GET / HTTP/1.1
> Host: 172.18.144.18
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 308 Permanent Redirect
< Date: Tue, 21 Apr 2026 04:51:33 GMT
< Content-Type: text/html
< Content-Length: 164
< Connection: keep-alive
< Location: https://172.18.144.18
< 
<html>
<head><title>308 Permanent Redirect</title></head>
<body>
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx</center>
</body>
</html>
* Connection #0 to host (nil) left intact

说明跑通了

4. 目标:让服务器访问 172.18.144.18 时自动走代理

普通 ip route 不能把流量直接送进 SOCKS5 代理。

要实现“访问某个目标自动走代理”,需要:

  • redsocks

  • iptables


5. 安装 redsocks

apt install -y redsocks

6. 写入 redsocks 配置

cat >/etc/redsocks.conf <<'EOF'
base {
 log_debug = off;
 log_info = on;
 log = "file:/var/log/redsocks.log";
 daemon = on;
 redirector = iptables;
}

redsocks {
 local_ip = 127.0.0.1;
 local_port = 12345;

 ip = 127.0.0.1;
 port = 1080;
 type = socks5;
}
EOF

7. 启动 redsocks

pkill redsocks 2>/dev/null
redsocks -c /etc/redsocks.conf

检查监听:

ss -lntp | grep 12345

正常应看到 127.0.0.1:12345。


8. 配置 iptables,让 172.18.144.18 自动走代理

iptables -t nat -N REDSOCKS 2>/dev/null
iptables -t nat -F REDSOCKS
iptables -t nat -D OUTPUT -p tcp -j REDSOCKS 2>/dev/null
iptables -t nat -A REDSOCKS -d 172.18.144.18 -p tcp -j REDIRECT --to-ports 12345
iptables -t nat -A REDSOCKS -d 127.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 10.0.0.0/8 -j RETURN
iptables -t nat -A REDSOCKS -d 172.16.0.0/12 -j RETURN
iptables -t nat -A REDSOCKS -d 192.168.0.0/16 -j RETURN
iptables -t nat -A OUTPUT -p tcp -j REDSOCKS

查看规则:

iptables -t nat -L -n -v

9. 测试自动代理

直接访问:

curl -v --connect-timeout 10 http://172.18.144.18

可以访问成功,也能登录内网服务器

root@iZuf65de2ru7h673dw6wguZ:/# ssh -p 4001 [email protected]
Welcome to Ubuntu 20.04.5 LTS (GNU/Linux 5.15.0-139-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.
Last login: Tue Apr 21 11:49:18 2026 from 10.233.70.0
root@app-1190324114fw-416-deployment-598d778f86-t5lnl:~#