概括:在家庭路由上安装 npc 和公网服务器上的 nps 通信,服务器上的 nps 创建 socks5 代理,Surge 连接 socks5 代理,实现安全地访问家庭内网。
服务器配置
购买轻量服务器
前往阿里云购买 云服务器 ECS,如果只是需要简单做个测试,可以选择 精选特惠服务器,价格低至 9¥/月。购买成功后进入控制台,点击左侧的实例,找到购买的服务器。在操作选项下,点击「更多 > 密码/密钥 > 重置实例密码」,修改 root 登录密码。

配置安全组规则
阿里云默认只开放了三个端口,在实际使用中这是不够的。所以需要配置安全组策略,打开需要的端口。此处可以直接开放所有端口,一步到位。进入服务器实例,点击「安全组 > 配置规则」。

点击手动添加规则,目的选择「全部 1/65535」,源选择「0.0.0.0/0」,点击保存。

配置 proxy
由于 nps 是托管在 GitHub 上的,如果觉得阿里云访问 GitHub 下载慢的话,可以参考 SSH 端口转发简明教程 服务器代理章节,将 macOS 上的 proxy 转发给阿里云服务器。在 macOS 上执行以下命令,将 macOS 上 8889 端口的 proxy 端口转发给服务器的 1234 端口。如果本机上也没有 proxy,这一步可以跳过。
ssh -fNTR 1234:localhost:8889 root@服务器IP
下载 nps
# 使用 curl 命令下载 nps 安装包,前置 socks5 proxy
curl -x socks5h://localhost:1234 -L 'https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_amd64_server.tar.gz' > linux_amd64_server.tar.gz
# 如果上个步骤没有配置 proxy 使用这条命令
curl -L 'https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_amd64_server.tar.gz' > linux_amd64_server.tar.gz
# 校验 md5
md5sum linux_amd64_server.tar.gz
fc9dfe6be2c5b7c0e120c3f2d0ddf472 linux_amd64_server.tar.gz
# 解压 tar.gz
tar -zxvf linux_amd64_server.tar.gz
# 将 nps 安装到服务器上
./nps install
配置 nps
vim /etc/nps/conf/nps.conf
更改 web 相关配置,主要是 username 和 password,以免被爆破。
#web
web_host=a.o.com
web_username=admin
web_password=123
web_port = 8080
web_ip=0.0.0.0
web_base_url=
web_open_ssl=false
web_cert_file=conf/server.pem
web_key_file=conf/server.key
# if web under proxy use sub path. like http://host/nps need this.
#web_base_url=/nps
配置完成后,启动 nps
nps start
Web 配置
通过 http://服务器IP:8080 访问 nps 控制面板,登录进去后,点击「客户端 > 新增」。
备注:任意
Basic 用户名、密码:一会在配置需要,请设置高强度密码,或是直接用 openssl 随机生成。
openssl rand -base64 18|md5sum
唯一验证密钥:可留空,由 nps 自动生成
开启压缩,开启加密,最后点击「新增」添加客户端。

回到客户端的主页面,刷新一下就可以看到新增的客户度。点击 ID 前面的「+」展开连接信息。可以看到有一个客户端命令,复制下来一会需要使用。

ER-X 配置
npc 下载
在家庭内网里,个人使用 ER-X 来配置 nps 客户端,读者可以根据自己的实际情况选择其他设备,由于每个人的设备不尽相同,授人以鱼,不如授之以渔,接下来就来看看如何选择合适的压缩包下载。登录 ER-X,通过 getconf LONG_BIT 和 lscpu 命令查看系统位数和字节序。根据返回的信息可以知道,ER-X 是 32 位系统,mips 构架,Little Endian 小端序。
ubnt@ER-X-Home:~$ getconf LONG_BIT
32
ubnt@ER-X-Home:~$ lscpu
Architecture: mips
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
在 nps 下载页面,提供了以下几种 mips 构架的压缩包。因为是 32 位系统,排除前四个,Little Endian 缩写是 le,排除后两个,再因为是要 client 客户端,而不是 server 服务端,所以最后选的是 linux_mipsle_client.tar.gz
linux_mips64le_client.tar.gz
linux_mips64le_server.tar.gz
linux_mips64_client.tar.gz
linux_mips64_server.tar.gz
linux_mipsle_client.tar.gz
linux_mipsle_server.tar.gz
linux_mips_client.tar.gz
linux_mips_server.tar.gz
选好了压缩包之后,复制链接在 ER-X 上下载。
# 下载 npc
curl -L 'https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_mipsle_client.tar.gz' > 'linux_mipsle_client.tar.gz'
# 解压 npc
tar -zxvf linux_mipsle_client.tar.gz
运行 npc
将刚才复制的客户端命令粘贴到 ER-X 的终端里,运行结果如下
ubnt@ER-X-Home:~$ ./npc -server=120.55.61.1:8024 -vkey=71wpokzrdh53tab4 -type=tcp
2022/02/17 06:04:39.134 [I] [npc.go:231] the version of client is 0.26.10, the core version of client is 0.26.0
2022/02/17 06:04:39.243 [I] [client.go:72] Successful connection with server 120.55.61.1:8024
如果看到 Successful connection with server 即代表连接成功,在 nps 的控制面板上,连接也会显示为「在线」

如果测试连接成功的话,可以使用 control+C 中断,将配置写入 conf/npc.conf 中。请注意此处最后一行的 auto_reconnection=true,必须要加上,可以保证在服务端出问题恢复之后自动重连。
ubnt@ER-X-Home:~$ cat conf/npc.conf
[common]
server_addr=120.55.61.1:8024
conn_type=tcp
vkey=71wpokzrdh53tab4
auto_reconnection=true
先使用 nohup 让 npc 后台运行
nohup /home/ubnt/npc >/dev/null 2>&1 &
再使用 crontab 设置每次重启之后自动运行
crontab -e@reboot /home/ubnt/npc >/dev/null 2>&1 &
新增 SOCKS5 代理
点击右边的「隧道 > 新增」,SOCKS 代理模式,服务端口自定义,如此处的 6666,点击新增添加。

软件配置
添加 Proxy Server
Home = socks5, 服务器IP, SOCKS5端口, Basic 用户名, Basic 密码
添加 Proxy Group
默认走 Home Proxy,蜂窝数据如果不指定的话,会自动使用 default 配置;如果 Wi-Fi SSID 是家里的 Ubnt AP,就直连。
HomeGroup = ssid, default = Home, "Ubnt AP" = DIRECT

添加 Rules
IP 网段匹配,如果是 192.168.99.0/24,走 HomeGroup 策略组,HomeGroup 再根据网络情况选择 Home Proxy 或者是直连。
IP-CIDR,192.168.99.0/24,HomeGroup,no-resolve
修改 skip-proxy
删除 192.168.0.0/16 网段,否则 Surge 不会处理本地请求。
skip-proxy = 127.0.0.1, 10.0.0.0/8, 172.16.0.0/12, 100.64.0.0/10, 17.0.0.0/8, localhost, *.local, *.crashlytics.com
添加网关服务器
在末尾添加 192.168.99.1 DNS 服务器
dns-server = 223.6.6.6,223.5.5.5,114.114.114.114,114.114.115.115,192.168.99.1网络连接测试
Ubnt AP Wi-Fi 下 HomeGroup 自动选择 DIRECT,TP-Link Wi-Fi 下 HomeGroup 自动选择 Home,蜂窝数据也是 Home

测试效果
测试外网访问内网



完善配置
将家庭路由上的 npc 移动到 /usr/bin 目录,使用 -config 参数指定配置文件路径,修改 crontab 配置,可以简化执行的命令。
@reboot npc -config $HOME/npc.conf网络错误情况模拟
测试出错自动重连
服务端重启 nps,模拟出错恢复后的情况
nps restart
查看客户端自动重连日志
2022/02/23 16:46:05 mux: read session unpack from connection err EOF
2022/02/23 16:46:05 close mux
2022/02/23 16:46:05.328 [W] [client.go:165] accpet error,the conn has closed
2022/02/23 16:46:05.331 [E] [client.go:92] Accept server data error read tcp 10.0.0.59:56281->10.0.0.60:8024: use of closed network connection, end this service
2022/02/23 16:46:05.332 [I] [control.go:102] Reconnecting...
2022/02/23 16:46:10.339 [E] [control.go:111] dial tcp 10.0.0.60:8024: connect: connection refused
2022/02/23 16:46:10.339 [I] [control.go:102] Reconnecting...
2022/02/23 16:46:15.346 [E] [control.go:111] dial tcp 10.0.0.60:8024: connect: connection refused
2022/02/23 16:46:15.347 [I] [control.go:102] Reconnecting...
2022/02/23 16:46:20.350 [E] [control.go:111] dial tcp 10.0.0.60:8024: connect: connection refused
2022/02/23 16:46:20.350 [I] [control.go:102] Reconnecting...
2022/02/23 16:46:25.376 [N] [control.go:174] web access login username:user password:qseebjmzl4eryw0u
2022/02/23 16:46:25.409 [I] [client.go:72] Successful connection with server 10.0.0.60:8024
家庭断电后自动重启
SSH 连接到 ER-X,使用 reboot 命令重启路由器。
ubnt@ubnt:~$ reboot
Proceed with reboot? [confirm]y
Broadcast message from ubnt@ubnt (pts/0) (Fri Feb 25 04:27:22 2022):
The system is going down for reboot NOW!
client_loop: send disconnect: Broken pipe稍等一会之后,路由器重新上线,使用 ps aux|grep npc 命令查看 npc 是否开机自动运行。
$ ssh ubnt@10.0.0.60
Welcome to EdgeOS
By logging in, accessing, or using the Ubiquiti product, you
acknowledge that you have read and understood the Ubiquiti
License Agreement (available in the Web UI at, by default,
http://192.168.1.1) and agree to be bound by its terms.
ubnt@10.0.0.60's password:
Linux ubnt 4.14.54-UBNT #1 SMP Thu Mar 5 16:54:37 UTC 2020 mips
Boot image can be upgraded to version [ e50_002_4c817 ].
Run "add system boot-image" to upgrade boot image.
ubnt@ubnt:~$ ps aux|grep npc
ubnt 237 0.9 2.0 673352 5280 ? Ssl 04:27 0:02 npc -config /home/ubnt/npc.conf
ubnt 3109 0.0 0.2 3024 544 pts/0 S+ 04:31 0:00 grep npc参考文章
