zl程序教程

您现在的位置是:首页 >  Java

当前栏目

使用NPS服务器内网穿透——实现远程访问外网电脑

2023-02-18 16:26:28 时间

使用NPS进行内网穿透——实现远程访问外部网络电脑

参考文档:https://ehang-io.github.io/nps/#/?id=nps

内网穿透搭建教程

一、NPS简介和实现原理

1、NPS简介

nps是一款轻量级、高性能、功能强大的内网穿透代理服务器。目前支持tcp、udp流量转发,可支持任何tcp、udp上层协议(访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析等等……),此外还支持内网http代理、内网socks5代理、p2p等,并带有功能强大的web管理端。

2、NPS实现功能

1、做微信公众号开发、小程序开发等---- 域名代理模式 2、想在外网通过ssh连接内网的机器做云服务器到内网服务器端口的映射----tcp代理模式(本实验搭建,实现此功能) 3、在非内网环境下使用内网dns,或者需要通过udp访问内网机器等---- udp代理模式 4、在外网使用HTTP代理访问内网站点---- http代理模式 5、搭建一个内网穿透ss,在外网如同使用内网vpn一样访问内网资源或者设备---- socks5代理模式

3、nps特点

  • 全面的协议支持,与几乎所有常用协议兼容,例如tcp,udp,http(s),socks5,p2p,http代理等;
  • 全面的平台兼容性(Linux,Windows,MacOS,群晖等),仅支持作为系统服务进行安装。 全面控制,允许客户端和服务器控制;
  • Https集成,支持将后端代理和Web服务转换为https,并支持多个证书。 只需在Web ui上进行简单配置即可完成大多数要求;
  • 完整的信息显示,例如流量,系统信息,实时带宽,客户端版本等。 强大的扩展功能,一切可用(缓存,压缩,加密,流量限制,带宽限制,端口重用等);
  • 域名解析具有诸如自定义标题,404页面配置,主机修改,站点保护,URL路由和全景解析之类的功能。 服务器上的多用户和用户注册支持。

二、使用NPS安装前期准备

1、 提前下载好nps压缩包

下载链接:https://github.com/ehang-io/nps/releases

本实验的服务端使用的是centos 8 linux系统,所以下载对应的nps服务端的压缩包:linux_arm64_server.tar.gz;客户端使用的是Windows操作系统,对应下载nps客户端的压缩包:windows_amd64_client.tar.gz

使用者也可以根据自己实际使用场景进行相应压缩包的下载与安装搭建。

2、购买云服务器并查询其公网IP地址

查询购买云服务器的公网IP地址,这里选用的是华为鲲鹏云服务器,也可以购买阿里云、腾讯云等其他厂商的云服务器进行安装与搭建。

3、下载并安装好远程连接工具

这里使用的是xshell与xftp远程连接工具,也可以选择CRT、putty、Mobxterm等其他远程连接工具。

三、内网穿透步骤流程操作

【1】服务端配置

1、远程连接云服务器。如图所示

2、检查防火墙和SELINUX安全模式是否关闭

  • 关闭防火墙命令

systemctl stop firewalld systemctl disable firewalld systemctl status firewalld

  • 修改SELINUX安全模式

vim /etc/selinux/config 将此选项修改为SELINUX=disabled 按“esc”后,按“:wq”保存退出 reboot重启生效

3、新建nps文件夹,上传nps服务端的压缩包并解压。如图所示

mkdir nps tar -zxvf linux_arm64_server.tar.gz

4、进入conf文件夹内

进入nps.conf配置文件内进行编辑修改。如图所示。

cd nps/conf/ vim nps.conf

5、修改nps.conf配置文件的内容

修改部分:

#web
web_host=a.o.com  —— 云服务器公网IP地址
web_username=admin ——web控制台账户设置
web_password=123 ——web控制台密码设置
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

nps.conf配置内容如下: 【根据需要自行修改】

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 —— 连接协议为TCP协议
bridge_port=8024 —— 连接端口为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=7
#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  —— 云服务器公网IP地址
web_username=admin ——web控制台账户设置
web_password=123 ——web控制台密码设置
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_crypt_key =1234567812345678

#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=false
allow_rate_limit=false
allow_tunnel_num_limit=false
allow_local_proxy=false
allow_connection_num_limit=false
allow_multi_ip=false
system_info_display=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

修改完成后返回上一级目录,安装并开启nps服务。

cd … ./nps install ./nps start

6、登录 nps web后台管理

输入“公网IP:端口号”。注意:此端口需要在云服务器中的安全组设置被允许访问

7、添加客户端信息

8、设置TCP隧道信息

注意:设置的服务器端口需要在云服务器安全组中允许通过放行。

【2】客户端配置

1、解压nps客户端压缩包。如图所示

2、安装Git并启用

参考连接:Windows系统Git安装教程(详解Git安装过程) 启用方式: ① 直接打开Git Bash应用,输入cd A:/桌面文档/proxy/nps 【注意:斜杠“/”的书写方式】 进入nps压缩包解压后的路径(或目录)下

② 找到此目录,在此目录下右击打开即可。

3、运行客户端命令,连接服务器端

命令格式:./npc.exe -server=xx.xx.xx.xx:端口号 -vkey=密钥 -type=tcp

4、查看并验证是否成功

远程连接

至此,安装验证完成。

四、实验注意事项:

1、云服务器安全组端口号是否被允许放行

需要用到的端口号记得在云服务器安全组中设置允许放行,如客户端连接的端口、服务端的端口、web访问的端口等。如图所示。

2、web登录时提示账号密码错误问题

当输入正确的Web管理账号和密码时,依旧提示账号密码错误。如图所示。 解决办法如下:执行./nps uninstall然后删除/etc/nps目录,重新安装配置。

3、使用远程桌面连接出现如图报错

解决办法如下:

4、注意填写的最大端口号为65535

5、客户端需开启允许远程桌面访问

问题:远程桌面关闭,不允许远程连接到此电脑。

解决:开启客户端远程桌面。如图所示。