frp 是一个专注于内网穿透的高性能反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议,且支持 P2P 通信。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。
项目地址:https://github.com/fatedier/frp
基本原理如下所示:
下面介绍FRP的两种部署方式,环境信息如下:
节点角色 | 节点类型 | OS | IP地址 |
---|---|---|---|
FRPS服务端 | 阿里云轻量服务器 | Ubuntu 22.04 LTS | 112.74.1.10 |
FRPC客户端 | 本地linux服务器 | Ubuntu 22.04 LTS | 192.168.72.16 |
项目地址:https://github.com/snowdreamtech/frp
服务端镜像地址:https://hub.docker.com/r/snowdreamtech/frps
客户端镜像地址:https://hub.docker.com/r/snowdreamtech/frpc
以下步骤在阿里云轻量服务器上执行,并确认已安装docker环境。
创建frps服务端配置文件,复制粘贴以下内容
mkdir -p /etc/frp
cat >/etc/frp/frps.toml<<EOF
bindPort = 7000
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "admin"
vhostHTTPPort = 8080
vhostHTTPSPort = 8443
EOF
配置参数说明:
注意:以上端口均需在防火墙放通。
使用docker 安装 frps 服务端
docker run -d --name frps --restart=always \
--network host \
-v /etc/frp/frps.toml:/etc/frp/frps.toml \
snowdreamtech/frps:0.52.3
查看容器运行状态
root@ubuntu:~# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a6abfba322a6 snowdreamtech/frps:0.52.3 "/bin/sh -c '/usr/bi…" About an hour ago Up About an hour frps
访问dashboard
http://112.74.1.10:7500/
首页如下:
以下步骤在本地linux服务器上执行,并确认已安装docker环境。
创建frpc客户端配置文件,复制粘贴以下内容,定义了两个proxies,分别为tcp穿透和http穿透示例。
mkdir -p /etc/frp
cat >/etc/frp/frpc.toml<<EOF
serverAddr = "112.74.1.10"
serverPort = 7000
webServer.addr = "0.0.0.0"
webServer.port = 7400
webServer.user = "admin"
webServer.password = "admin"
[[proxies]]
name = "test-tcp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000
[[proxies]]
name = "test-web"
type = "http"
localPort = 80
customDomains = ["112.74.1.10"]
EOF
参数说明:
使用 docker 部署 FRPC 客户端
docker run -d --name frpc --restart=always \
--network host \
-v /etc/frp/frpc.toml:/etc/frp/frpc.toml \
snowdreamtech/frpc:0.52.3
访问客户端 dashboard
http://192.168.72.16:7400/
首页如下:
测试基于TCP的SSH穿透
访问阿里云轻量服务器IP地址112.74.1.10
和6000
端口,实际会转发到本地服务器192.168.72.16
的22
端口。
测试是否能否通过ssh连接到客户端。
root@ubuntu:~# ssh -o Port=6000 root@112.74.1.10
root@112.74.1.10's password:
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-89-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Thu Dec 14 02:54:06 PM CST 2023
System load: 0.0
Usage of /: 75.9% of 196.89GB
Memory usage: 16%
Swap usage: 1%
Processes: 219
Users logged in: 1
IPv4 address for br-aeda5cc463d3: 172.18.0.1
IPv4 address for br-f87de9bc351f: 172.19.0.1
IPv4 address for docker0: 172.17.0.1
IPv4 address for ens33: 192.168.72.16
* Strictly confined Kubernetes makes edge and IoT secure. Learn how MicroK8s
just raised the bar for easy, resilient and secure K8s cluster deployment.
https://ubuntu.com/engage/secure-kubernetes-at-the-edge
Expanded Security Maintenance for Applications is not enabled.
64 updates can be applied immediately.
To see these additional updates run: apt list --upgradable
Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status
*** System restart required ***
Last login: Thu Dec 14 14:54:07 2023 from 127.0.0.1
root@ubuntu:~# hostname -I
192.168.72.16 172.17.0.1 172.18.0.1 172.19.0.1
root@ubuntu:~#
测试基于HTTP的WEB穿透
访问阿里云轻量服务器IP地址112.74.1.10
和8080
端口,实际会转发到本地服务器192.168.72.16
的80
端口。
本地使用docker运行一个示例nginx服务,用于监听80端口
docker run -d --name nginx -p 80:80 nginx
测试是否能否通过轻量服务器连接到本地nginx服务。
root@ubuntu:~# curl 112.74.1.10:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
推荐上面的docker方式部署服务端,如果没有docker环境,也可以使用systemd来部署和维护frp服务。
以下步骤在阿里云轻量服务器上执行。
在 Linux 系统下,使用 systemd
可以方便地控制 frps 服务端的启动、停止、配置后台运行以及开机自启动。
安装包下载地址:https://github.com/fatedier/frp/releases
下载frp安装包
wget https://github.com/fatedier/frp/releases/download/v0.52.3/frp_0.52.3_linux_amd64.tar.gz
tar -zxvf frp_0.52.3_linux_amd64.tar.gz
安装该fprs
二进制文件到/usr/local/bin目录下
cp frp_0.52.3_linux_amd64/frps /usr/local/bin/
创建frps配置文件
mkdir -p /etc/frp
cp frp_0.52.3_linux_amd64/frps.toml /etc/frp
修改frps配置文件
cat >/etc/frp/frps.toml<<EOF
bindPort = 7000
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "admin"
vhostHTTPPort = 8080
vhostHTTPSPort = 8443
EOF
创建 frps.service 服务启动文件
使用文本编辑器 (如 vim) 在 /etc/systemd/system
目录下创建一个 frps.service
文件,用于配置 frps 服务。
cat >/etc/systemd/system/frps.service<<EOF
[Unit]
Description = frp server
After = network.target syslog.target
Wants = network.target
[Service]
Type = simple
ExecStart = /usr/local/bin/frps -c /etc/frp/frps.toml
[Install]
WantedBy = multi-user.target
EOF
使用 systemd 命令启动 frps 服务端服务
systemctl enable --now frps
查看服务运行状态
root@node40:~# systemctl status frps
● frps.service - frp server
Loaded: loaded (/etc/systemd/system/frps.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-12-13 17:17:48 CST; 21h ago
Main PID: 906819 (frps)
Tasks: 7 (limit: 9387)
Memory: 7.5M
CPU: 5.643s
CGroup: /system.slice/frps.service
└─906819 /usr/local/bin/frps -c /etc/frp/frps.toml
Dec 13 17:18:08 node40 frps[906819]: 2023/12/13 17:18:08 [I] [dashboard_api.go:186] Http response [/api/proxy/tcp]: code [200]
Dec 13 17:18:10 node40 frps[906819]: 2023/12/13 17:18:10 [I] [dashboard_api.go:329] Http request: [/api/traffic/test-tcp]
Dec 13 17:18:10 node40 frps[906819]: 2023/12/13 17:18:10 [I] [dashboard_api.go:323] Http response [/api/traffic/test-tcp]: code [200]
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [control.go:334] [5b5fc91b4d176c56] control writer is closing
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [proxy.go:115] [5b5fc91b4d176c56] [test-tcp] proxy closing
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [W] [proxy.go:201] [5b5fc91b4d176c56] [test-tcp] listener is closed: accept tcp [::]:6000: use of closed network connec>
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [control.go:423] [5b5fc91b4d176c56] client exit success
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [service.go:533] [1036b9eb2691ec4e] client login info: ip [192.168.72.41:35800] version [0.52.3] hostname [] os [li>
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [tcp.go:82] [1036b9eb2691ec4e] [test-tcp] tcp proxy listen port [6000]
Dec 13 17:21:55 node40 frps[906819]: 2023/12/13 17:21:55 [I] [control.go:500] [1036b9eb2691ec4e] new proxy [test-tcp] type [tcp] success
服务启停命令
systemctl start frps
systemctl stop frps
systemctl restart frps
通过遵循上述步骤,您可以轻松地使用 systemd 来管理 frps 服务,实现启动、停止、自动运行和开机自启动。确保替换路径和配置文件名称以匹配您的实际安装。
以下步骤在本地linux服务器上执行。
在 Linux 系统下,使用 systemd
可以方便地控制 frps 服务端的启动、停止、配置后台运行以及开机自启动。
安装该fprc
二进制文件到/usr/local/bin目录下
cp frp_0.52.3_linux_amd64/frpc /usr/local/bin/
创建frps配置文件
mkdir -p /etc/frp
cp frp_0.52.3_linux_amd64/frpc.toml /etc/frp
修改客户端配置文件,指定服务端地址和端口
cat >/etc/frp/frpc.toml<<EOF
serverAddr = "112.74.1.10"
serverPort = 7000
webServer.addr = "0.0.0.0"
webServer.port = 7400
webServer.user = "admin"
webServer.password = "admin"
[[proxies]]
name = "test-tcp"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000
[[proxies]]
name = "test-web"
type = "http"
localPort = 80
customDomains = ["112.74.1.10"]
EOF
创建 frpc.service 服务启动文件
使用文本编辑器 (如 vim) 在 /etc/systemd/system
目录下创建一个 frpc.service
文件,用于配置 frpc 服务。
cat >/etc/systemd/system/frpc.service<<EOF
[Unit]
Description = frp client
After = network.target syslog.target
Wants = network.target
[Service]
Type = simple
ExecStart = /usr/local/bin/frpc -c /etc/frp/frpc.toml
[Install]
WantedBy = multi-user.target
EOF
使用 systemd 命令启动 frpc 客户端服务
systemctl enable --now frpc
查看服务运行状态
root@ubuntu:~# systemctl status frpc
● frpc.service - frp client
Loaded: loaded (/etc/systemd/system/frpc.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-12-13 17:21:55 CST; 21h ago
Main PID: 69350 (frpc)
Tasks: 5 (limit: 4556)
Memory: 3.9M
CPU: 12.045s
CGroup: /system.slice/frpc.service
└─69350 /usr/local/bin/frpc -c /etc/frp/frpc.toml
Dec 13 17:21:55 ubuntu systemd[1]: Started frp client.
Dec 13 17:21:55 ubuntu frpc[69350]: 2023/12/13 17:21:55 [I] [root.go:139] start frpc service for config file [/etc/frp/frpc.toml]
Dec 13 17:21:55 ubuntu frpc[69350]: 2023/12/13 17:21:55 [I] [service.go:299] [1036b9eb2691ec4e] login to server success, get run id [1036b9eb2691ec4e]
Dec 13 17:21:55 ubuntu frpc[69350]: 2023/12/13 17:21:55 [I] [proxy_manager.go:156] [1036b9eb2691ec4e] proxy added: [test-tcp]
Dec 13 17:21:55 ubuntu frpc[69350]: 2023/12/13 17:21:55 [I] [service.go:161] admin server listen on 0.0.0.0:7400
Dec 13 17:21:55 ubuntu frpc[69350]: 2023/12/13 17:21:55 [I] [control.go:173] [1036b9eb2691ec4e] [test-tcp] start proxy success
Dec 13 17:22:09 ubuntu frpc[69350]: 2023/12/13 17:22:09 [I] [admin_api.go:140] Http request [/api/status]
Dec 13 17:22:09 ubuntu frpc[69350]: 2023/12/13 17:22:09 [I] [admin_api.go:142] Http response [/api/status]
Dec 13 17:22:15 ubuntu frpc[69350]: 2023/12/13 17:22:15 [I] [admin_api.go:166] Http get request [/api/config]
Dec 13 17:22:15 ubuntu frpc[69350]: 2023/12/13 17:22:15 [I] [admin_api.go:168] Http get response [/api/config], code [200]