8

使用 Frp 内网穿透进行开发

 3 years ago
source link: https://blog.cugxuan.cn/2019/06/16/Proxy/expose-an-http-service-using-frp/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在有云服务器的情况(腾讯云或阿里云的学生机服务器了解一下)下,在写一些自己的接口和前端配合时,进行微信公众号功能开发时,经常会遇到这样的问题。

  1. 项目部署运行在自己的电脑,如何开发测试接口给前端对接?
  2. 微信公众号的接口需要部署到云服务,但是每次更新十分麻烦(暂未使用持续集成的情况下)

那么如何在本地就能重新更新服务呢?Frp 官方文档写的很详细,还是简单记录一下使用 frp 来进行内网穿透。

作用和效果

这次想先介绍一下作用和效果。如果你不清楚想达到的目的,对于配置文件的理解可能也不是那么直观。简单来说就是端口映射。

Server 运行在 localhost 的 8000 端口,但是 Server 不能被外网所访问。
通过 Frp 可以将 localhost 的端口“绑定”到腾讯云服务器的 8000 端口,意思就是你可以直接通过外网访问本地的服务。

network-model

先去 github 下载最新的 release 版本
https://github.com/fatedier/frp/releases

虽然官方文档中 通过自定义域名访问部署于内网的 web 服务 已经说的很清楚,我这里还是介绍一下简单用法

Server Config

监听服务器的 7000 端口,等待客户端 frp 的连接,将客户端的端口转发到服务器的 8000 端口

在 frps.ini 文件中填入以下内容

1
2
3
[common]
bind_port = 7000
vhost_http_port = 8000

Client Config

连接服务器的 7000 端口,将本地 8000 端口的服务转发到api.cugxuan.cn,custom_domains 也可以填 IP 地址。

如果 custom_domains 填的是域名地址,则访问
http://api.cugxuan.cn:8000/testapi
如果 custom_domains 填的是 IP 地址,则访问
http://x.x.x.x:8000/testapi

在 frpc.ini 文件中填入以下内容

1
2
3
4
5
6
7
8
[common]
server_addr = api.cugxuan.cn
server_port = 7000

[web]
type = http
local_port = 8000
custom_domains = api.cugxuan.cn

介绍两种方法进行部署

  1. 在本地修改好对应的内容,推送到 github 私有仓库,服务器从私有仓库 clone
  2. 服务器下载,然后直接使用 vim 编辑修改配置文件

git 存储

我使用的 github 中的私有项目来管理自己的 hooks 和 frp 的内容,直接在本地写好之后在服务器 git clone 即可,使用 scp 等方法也可以,能传到服务器就行

frp-folder.png

上传到服务器后,可使用 nohup 后台运行

1
2
$ cd frps_0.27.0_Linux/
$ nohup ./frps -c frps.ini >frp.log 2>&1 &

wget 下载

服务器端也可以使用 wget 进行下载 frp_0.27.0_linux_amd64.tar.gz,修改 frps.ini 之后就可以直接部署了

1
2
3
4
5
$ wget https://github.com/fatedier/frp/releases/download/v0.27.0/frp_0.27.0_linux_amd64.tar.gz
$ tar -zxvf frp_0.27.0_linux_amd64.tar.gz
$ cd frp_0.27.0_linux_amd64/
$ vi frps.ini
$ nohup ./frps -c frps.ini >frp.log 2>&1 &

客户端部署

我这里是 Mac 的电脑,如果是 Windows 去下载对应的版本然后在终端运行即可

1
2
3
4
5
$ cd frpc_0.27.0_Mac
$ ./frpc -c frpc.ini
测试一下是否正常
$ curl api.cugxuan.cn:8000/testapi
{"message":"test"}%

在这里只介绍了 frp 很简单的一个用法,还有很多强大的功能可以去看 Frp 官方文档
再介绍一个我们使用的例子,公网的服务器无法访问学校内网的一些服务(例如财务网站),我们通过搭建 frp 代理使公网服务器能够访问学校的内网,只需要几行配置文件即可解决。更厉害的是,frp 已经给出了代理池的解决方案,我们可以通过多个客户端代理来解决网络的不稳定性


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK