frp 实现内网穿透将内网 WebService 接口绑定自定义域名供外网访问

要将内网的 WebService 接口提供给外网访问,最简单又免费的方式就是通过 inconshreveable/ngrok: Introspected tunnels to localhost,ngrok 通过一条命令就可以实现内网穿透,并且不需要服务器。但是它有如下缺点:

  • 免费版本不支持自定义域名,每次执行命令后域名都是随机分配的。
  • 目前官方的已经升级到了2.x,已经不开源了。1.x 版本是开源的,但不再更新,目前最新的开源版本是 1.7.1。
  • ngrok 1.x 版本作者也说了有严重的内存、文件描述符泄漏等问题,国内的那些服务用的都是这个版本的客户端,稳定性很差。

我主要是是因为 ngrok 不能自定义域名,每次重启后重新执行命令生成的域名又变了,这样联调时对方又得更换接口地址,很尴尬的。

于是我找到一款和 ngrok 类似的软件 fatedier/frp: A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet.,它能满足我的需求,但是使用它没有服务器似乎不行(当然我也不清楚作者有没有提供仅使用客户端的平台化服务),当然自己待建服务端个性化更强也更安全。

环境

  • frp 服务端运行在阿里云的 CentOS 上。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    $ uname -a      
    Linux Msg.BJ.KTT 3.10.0-1160.24.1.el7.x86_64 #1 SMP Thu Apr 8 19:51:47 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

    # admin @ Msg in ~ [21:27:32]
    $ lsb_release -a
    LSB Version: :core-4.1-amd64:core-4.1-noarch
    Distributor ID: CentOS
    Description: CentOS Linux release 7.9.2009 (Core)
    Release: 7.9.2009
    Codename: Core

    因此服务端需使用 Releases · fatedier/frp 中的 linux_amd64 版本。

  • frp 客户端运行在 macOS 上。

    1
    2
    3
    4
    5
    6
    7
    8
    $ sw_vers 
    ProductName: macOS
    ProductVersion: 12.1
    BuildVersion: 21C52

    # ganzhixiong @ ganzhixiongdeMacBook-Pro-3 in ~/Downloads/frp_0.38.0_darwin_amd64 [21:28:29]
    $ uname -a
    Darwin ganzhixiongdeMacBook-Pro-3.local 21.2.0 Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:54 PST 2021; root:xnu-8019.61.5~1/RELEASE_X86_64 x86_64

    因此客户端需使用 Releases · fatedier/frp 中的 darwin_amd64 版本。

image-20220116215221801

可以看出 frp 的客户端和服务端的压缩包仅仅 8MB 多,很小了。
每个压缩包都包含客户端和服务端,客户端相关文件以 frpc 开头,服务端相关文件以 frps 开头。

下载服务端

  • 服务器能科学上网,可以直接用 wget 命令下载,但是直接从 GitHub 上下载经常不稳定,而且慢,几 KB 每秒能忍就慢慢等,不能就往下看。
  • 服务器不能科学上网,那就用能科学上网的电脑下载后,再将服务端需要使用的 frps 和 frps.ini 上传到服务器。

配置服务器

  1. 配置服务端。

    1
    vim frps.ini
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    [common]
    # 与客户端绑定的进行通信的端口。
    bind_port = 7000
    # 访问客户端 Web 服务自定义的端口号。
    #vhost_http_port = 80
    # 若 80 端口被 Nginx 或 httpd 占用,可换用其他端口,然后通过 Nginx 或 httpd 反向代理非 80 端口,是实现域名 80 端口访问。
    vhost_http_port = 8090

    # frp dashboard 通过浏览器查看 frp 的状态以及代理统计信息展示。
    # 默认没有开启,需要配置端口才能开启。
    dashboard_port = 7500
  2. 关闭阿里云 CentOS 防火墙。

    1
    2
    3
    4
    5
    6
    $ systemctl stop  firewalld.service    
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to manage system services or units.
    Authenticating as: root
    Password:
    ==== AUTHENTICATION COMPLETE ===

    不关闭此防火墙,则浏览器打开页面会提示:

    1
    2
    3
    4
    5
    6
    7
    The page you requested was not found.
    Sorry, the page you are looking for is currently unavailable.
    Please try again later.

    The server is powered by frp.

    Faithfully yours, frp.

    并且服务端也会输出错误信息。

启动服务端

1
$ ./frps -c ./frps.ini  

配置客户端

1
vim frp.ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[common]
# 公网服务器的 IP
server_addr = 47.95.123.137
# 服务端的 bind_port
server_port = 7000

[web]
# 访问协议
type = http
# WebService 接口所在的电脑 IP
local_ip = 10.211.55.21
# WebService 接口的端口号
local_port = 52748
# WebService 接口绑定的域名
custom_domains = xxx.ganzhixiong.com

启动客户端

1
$ ./frpc -c ./frpc.ini    

通过 Nginx 或 httpd 反向代理

httpd

以上步骤完成后,WebService 接口必须要在域名后面添加 8090 端口才能访问,对于最求完美的我来说,这是个瑕疵。

因为 80 端口已经被 httpd 使用,所以 frps 不能使用 80 端口,但可以通过 httpd 的反向代理实现。

1
2
# vim 用的少的,建议使用 VSCode 的 Remote SSH 编辑。
vim /etc/httpd/conf/httpd.conf

添加如下配置:

1
2
3
4
5
6
7
<VirtualHost *:80>
ServerName ganzhixiong.com
ServerAlias *.ganzhixiong.com
ProxyPreserveHost On
ProxyPass / http://127.0.0.1:8090/
ProxyPassReverse / http://127.0.0.1:8090/
</VirtualHost>

重启 httpd 服务

1
$ sudo service httpd restart

这样就可以不用加 8090 端口访问了,当然加也是可以访问的。

Nginx

参考 frp内网穿透技术分享 centos搭建frp服务器 群晖NAS内网穿透-毕嘉峰电脑技术网-实践才是真理,技术干货分享!

frp 实现内网穿透将内网 WebService 接口绑定自定义域名供外网访问

https://ganzhixiong.com/p/aa826b6d/

Author

干志雄

Posted on

2022-01-16

Updated on

2022-01-16

Licensed under

Comments