内网穿透——NPC/NPC使用文档
1. 环境准备
- 服务端: 一台有公网IP的Linux服务器,已安装Docker。
- 客户端: 一台内网Linux/Windows机器。
- 程序: 从 https://github.com/yisier/nps 下载客户端
npc程序,要求与Docker运行nps的版本严格一致!
2. 服务端 (nps) 部署 - Docker
通过挂载配置文件来管理nps,便于维护和扩展。
Docker 镜像可以参考以下连接
https://github.com/dongyubin/DockerHub
2.1 创建并编辑配置文件
- 在服务端创建用于存放配置的目录:
sudo mkdir -p /etc/nps
- 创建并编辑nps的核心配置文件:
sudo nano /etc/nps/nps.conf
- 将以下基础配置写入文件,并将
your_strong_password替换为你的 Web 面板强密码:appname = nps #Boot mode(dev|pro) runmode = dev #HTTP(S) proxy port, no startup if empty http_proxy_ip=0.0.0.0 http_proxy_port=80 https_proxy_port=443 https_just_proxy=true #default https certificate setting https_default_cert_file=conf/server.pem https_default_key_file=conf/server.key ##bridge bridge_type=tcp bridge_port=8024 #桥接端口 bridge_ip=0.0.0.0 # Public password, which clients can use to connect to the server # After the connection, the server will be able to open relevant ports and parse related domain names according to its own configuration file. public_vkey=123 #Traffic data persistence interval(minute) #Ignorance means no persistence flow_store_interval=1 # log level LevelEmergency->0 LevelAlert->1 LevelCritical->2 LevelError->3 LevelWarning->4 LevelNotice->5 LevelInformational->6 LevelDebug->7 log_level=6 log_path=nps.log #Whether to restrict IP access, true or false or ignore #ip_limit=true #p2p #p2p_ip=127.0.0.1 #p2p_port=6000 #web web_host=a.o.com web_username=jynp #控制面板登录用户名 web_password=****** #控制面板登录密码 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 #Web API unauthenticated IP address(the len of auth_crypt_key must be 16) #Remove comments if needed #auth_key=test auth_key=123 #获取服务端authKey时的aes加密密钥,16位 auth_crypt_key =213 #allow_ports=9001-9009,10001,11000-12000 #Web management multi-user login allow_user_login=false allow_user_register=false allow_user_change_username=false #extension #流量限制 allow_flow_limit=true #带宽限制 allow_rate_limit=true #客户端最大隧道数限制 allow_tunnel_num_limit=true allow_local_proxy=false #客户端最大连接数 allow_connection_num_limit=true #每个隧道监听不同的服务端端口 allow_multi_ip=true system_info_display=true #获取用户真实ip http_add_origin_header=true #cache http_cache=false http_cache_length=100 #get origin ip #http_add_origin_header=false #pprof debug options #pprof_ip=0.0.0.0 #pprof_port=9999 #client disconnect timeout disconnect_timeout=60 #管理面板开启验证码校验 open_captcha=false # 是否开启tls tls_enable=true tls_bridge_port=8025
2.2 运行 Docker 容器
执行以下命令,自动加载已创建的配置文件:
docker run -d --restart=always --name nps --net=host -v <conf文件夹路径]>:/conf -v /etc/localtime:/etc/localtime:ro yisier1/nps2.3 配置防火墙
在服务端的防火墙(及云安全组)中,放行以下 TCP 端口:
- 8024: 客户端通信桥接端口
- 8080: Web 控制面板端口
2.4 获取客户端 VKey
- 访问
http://<服务器公网IP>:<控制面板端口>,使用控制面板登录用户名和控制面板登录密码登录
- 在 “客户端” 菜单中新增一个客户端,复
唯一验证密钥(vkey)
3. 客户端 (npc) 连接
3.1 下载运行客户端
在内网客户端机器上,执行以下命令连接到服务端:
./npc -server=<服务端公网IP>:<桥接端口> -vkey=<vkey>3.2 验证连接
- 客户端日志显示
Successful connection with server
- 服务端 Web 界面中,客户端状态显示为绿色的 “在线”
4. 创建 SSH 隧道 (示例)
4.1 添加隧道规则
在 Web 界面 “TCP 隧道” 中新增规则:
- 客户端 ID: 选择刚上线的客户端
- 服务端端口: (任何未被占用的端口)
- 目标: 127.0.0.1:22 (指向客户端本机的 SSH 服务)
4.2 为隧道开放防火墙
在服务端的防火墙(及云安全组)中,放行上一步设置的服务端端口
4.3 验证隧道
从任何外部电脑执行 SSH 命令,若能连接成功,即代表转发成功:
ssh <内网机器用户名>@<你的公网IP> -p <服务端端口>5. 进一步配置
5.2 将 npc 配置为 Systemd 服务 (Linux 客户端)
- 移动
npc程序到系统目录:sudo mv ./npc /usr/local/bin/npc
- 创建 Systemd 服务文件:
sudo vim /etc/systemd/system/npc.service
- 写入以下内容(替换
<你的公网IP>和<你的VKey>):[Unit] Description=NPS Client After=network.target [Service] ExecStart=/usr/local/bin/npc -server=<你的公网IP>:<桥接端口>-vkey=<vkey> Restart=always RestartSec=5 [Install] WantedBy=multi-user.target
- 启动并设置开机自启:
sudo systemctl daemon-reload sudo systemctl enable --now npc