文章

Openwrt 部署 nps 客户端(npc)记录

Openwrt 部署 nps 客户端(npc)记录

OpenWrt 部署 NPS 客户端(npc)记录

一、目标

在路由器上部署 npc 客户端,连接公网服务器 nps 服务端,实现:

  • 内网穿透
  • 远程桌面
  • SSH访问
  • NAS访问

系统:

  • 路由系统:OpenWrt
  • 穿透工具:NPS

二、网络架构

1
2
3
4
5
6
7
8
9
10
11
公网服务器
     │
     │ nps服务端
     │ 8024
     │
OpenWrt路由器
     │
     │ npc客户端
     │
家庭内网设备
(Windows / NAS / SSH)

访问路径:

1
外网 → 服务器 → npc → 内网设备

三、确认路由器架构

登录路由器:

1
ssh root@192.168.31.1

查看CPU架构:

1
uname -m

输出:

1
mips

说明:

1
MIPS架构CPU

四、下载 npc

进入程序目录:

1
cd /usr/bin

下载客户端:

1
wget https://github.com/ehang-io/nps/releases/download/v0.26.10/linux_mipsle_client.tar.gz

解压:

1
tar -zxvf linux_mipsle_client.tar.gz

赋权:

1
chmod +x npc

运行测试:

1
./npc

五、第一个坑:Illegal instruction

运行结果:

1
Illegal instruction

原因:

官方编译版本使用 硬浮点(hardfloat),而很多 MIPS 路由器不支持。

解决方法:

重新编译 npc:

1
GOMIPS=softfloat

六、使用 WSL 编译 npc

使用:

Windows Subsystem for Linux

安装 Go:

1
2
sudo apt update
sudo apt install golang -y

下载源码:

1
2
git clone https://github.com/ehang-io/nps.git
cd nps

编译:

1
GOOS=linux GOARCH=mipsle GOMIPS=softfloat go build -o npc ./cmd/npc

生成:

1
npc

七、上传到 OpenWrt

上传:

1
scp npc root@192.168.31.1:/usr/bin/

赋权:

1
chmod +x /usr/bin/npc

测试:

1
npc

成功输出:

1
the version of client is 0.26.10

八、配置 npc

创建配置目录:

1
mkdir -p /usr/bin/conf

编辑配置:

1
vi /usr/bin/conf/npc.conf

配置内容:

1
2
3
4
server_addr=服务器IP:8024
vkey=你的VKEY
conn_type=tcp
auto_reconnection=true

示例:

1
2
3
4
server_addr=8.162.8.67:8024
vkey=123456
conn_type=tcp
auto_reconnection=true

九、启动 npc

前台运行:

1
npc -config=/usr/bin/conf/npc.conf

成功日志:

1
Successful connection with server

十、后台运行 npc

方法1:简单后台运行

1
npc -config=/usr/bin/conf/npc.conf &

查看进程:

1
ps | grep npc

示例:

1
20603 root /usr/bin/npc -config=/usr/bin/conf/npc.conf

方法2:nohup后台运行

防止 SSH 断开:

1
nohup npc -config=/usr/bin/conf/npc.conf > /tmp/npc.log 2>&1 &

说明:

1
2
> /tmp/npc.log   保存日志
2>&1             合并错误输出

十一、查看 npc 日志

实时查看:

1
tail -f /tmp/npc.log

查看最后日志:

1
tail -n 50 /tmp/npc.log

查看全部日志:

1
cat /tmp/npc.log

十二、进程管理

查看 npc:

1
ps | grep npc

查看端口:

1
netstat -ntlp | grep npc

停止 npc:

1
killall npc

重启 npc:

1
2
killall npc
npc -config=/usr/bin/conf/npc.conf &

十三、开机自启配置

创建启动脚本:

1
vi /etc/init.d/npc

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh /etc/rc.common

START=99

start() {
    echo "Starting npc..."
    /usr/bin/npc -config=/usr/bin/conf/npc.conf > /tmp/npc.log 2>&1 &
}

stop() {
    echo "Stopping npc..."
    killall npc
}

赋权:

1
chmod +x /etc/init.d/npc

启用:

1
/etc/init.d/npc enable

启动:

1
/etc/init.d/npc start

停止:

1
/etc/init.d/npc stop

十四、NPS 面板配置

登录 Web 面板:

1
http://服务器IP:8080

新增隧道。

远程桌面示例:

参数示例
类型tcp
目标IP192.168.1.100
目标端口3389
服务器端口40001

十五、远程桌面连接

Windows 打开:

Remote Desktop Connection

输入:

1
服务器IP:40001

即可远程访问内网电脑。


十六、常见问题排查

1 npc 未连接服务器

检查:

1
2
server_addr
vkey

查看日志:

1
tail -f /tmp/npc.log

2 Illegal instruction

原因:

1
CPU架构不匹配

解决:

1
GOMIPS=softfloat

3 端口被占用

错误:

1
bind: address already in use

解决:

1
killall npc

4 默认连接 127.0.0.1

修改配置:

1
server_addr=公网服务器IP

十七、最终运行结构

1
2
3
4
5
6
7
8
9
10
公网服务器
    │
    │ nps
    │
    │ 8024
    │
OpenWrt (npc)
    │
    │
内网设备

实现:

  • 内网穿透
  • 远程桌面
  • NAS访问
  • SSH访问
本文由作者按照 CC BY 4.0 进行授权

© 🍍. 保留部分权利。

热门标签