使用NPS进行内网穿透(微信/支付宝回调环境搭建)

开发过程中,接入第三方平台进行登陆或者支付的时候,经常会需要回调到本地服务器/开发环境,而如今由于 IPV4 的短缺,各电信运营商默认也不再提供公网 IP 了,对我们的开发调试造成了影响.所以,内网穿透是我们开发过程中必不可少的环节.这里就拿nps来搭建一个内网穿透的环境,让第三方平台的回调直接到我们的本机,方便进行功能开发和调试.

nps是 github的一个开源项目,可以让我们方便快速的搭建一个内网穿透的服务端和客户端.对于服务端,我们可以使用 docker 快速搭建起来,客户端则根据我们的实际情况进行搭建/运行.

docker 的安装这里不再赘述,大家可以自行 google/baidu

首先,我们需要下载 nps 的配置文件,下载链接点击此处进行下载,然后,上传配置文件到自己的公网服务器并进行解压,比如我们把配置文件放置在/mydata/nps/conf下,然后修改配置文件中的 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
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=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
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

#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=false

#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

通常情况下,我们只需要修改
web_username (登录 nps web管理界面的用户名)
web_password (登录 nps web管理界面的密码)

可选
web_port (web 管理界面的端口,默认为 8080,可以根据自己的情况进行修改)
public_vkey (客户端以配置文件进行启动时的密钥)

然后直接运行

docker run -d --name nps --net=host -v :/conf ffdfgdfg/nps

比如我们把配置文件放置在/mydata/nps/conf下,那么上面的命令就是

docker run -d --name nps --net=host -v /mydata/nps/conf:/conf ffdfgdfg/nps

容器启动以后,就可以访问服务器 IP:web_port,假如我的服务器地址是 103.72.132.110,上面配置的web_port=8080,那么就访问103.72.132.110:8080即可打开 nps 的管理界面


然后输入上面配置的web_username、和web_password即可登录 nps 管理界面了忽略我的客户端总数,因为我已经连接了一个客户端到此服务器

接下来,我们开始配置客户端,也就是我们自己的电脑,这里不建议使用 docker 进行搭建/运行,因为我们常用的开发环境是 win/mac,但是 docker 网络的 host模式,在 win/mac 上是有问题的,这里是 docker 官方的描述
The host networking driver only works on Linux hosts, and is not supported on Docker Desktop for Mac, Docker Desktop for Windows, or Docker EE for Windows Server.
所以,我们使用nps的 release 版本进行客户端的连接,大家可以点击下载链接,下载对应的版本,我这里是macOS,所以我下载了64 位版本,大家根据自己的平台进行下载,错误的版本会无法运行

下载完成后,我放在了/Users/sunyajie/Desktop/npc目录下

接下来,我们到服务端进行设置,登陆刚才的nps 管理界面,选择客户端,然后点击新增

在弹出的界面填写客户端的备注名称,唯一验证密钥可以留空,也可自定义,然后点击"新增",完成客户端的创建

新增完成后,记录下客户端的 ID,我这里是7

然后,点击7 旁边的 + 号,就会弹出客户端连接过来的命令,复制此命令

接下来,在本地电脑上,cd到下载的 npc目录,我这里是/Users/sunyajie/Desktop/npc,然后输入刚才复制的命令进行连接./npc -server=你的服务器IP:8024 -vkey=你的key -type=tcp,如图则表示连接成功

连接成功之后,打开nps 管理界面,可以看到客户端已经上线

到这里,我们的客户端和服务端都已经设置完毕,接下来大家可以针对自己的场景创建对应的连接.

比如我们需要让第三方回调返回内容到本地电脑,或者说让其他人可以通过公网访问自己电脑上的服务端,则可以这样操作

点击域名解析,然后点击新增,新增一个域名解析,配置如图:


配置完毕以后,我们就可以通过外网访问到本地的服务了,我本地运行了一个服务,端口号是 8888,当我访问localhost:8888的时候,结果如图

当我通过外网访问服务器 IP的时候,结果如图

此时就可以在第三方平台配置回调,回调到我们的服务器IP,就可以通过内网穿透直接回调到本地电脑,从而方便的进行开发和测试了,当然,部分第三方平台只支持域名回调,就需要将域名解析到我们自己的服务器,然后配置回调地址为域名即可.

以上内容转载请注明出处,同时也请大家不吝你的关注和下面的赞赏
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

你可能感兴趣的:(使用NPS进行内网穿透(微信/支付宝回调环境搭建))