使用FRP进行内网穿透的最佳实践
转自我的个人博客https://blognas.hwb0307.com。欢迎关注!
前言
前不久我出过一期《Docker系列 通过FRP实现内网穿透》讲述怎么利用FRP进行内网穿透。不过,经过测试,很快我发现此文有相当大的局限性,列举如下:
- 仅限
x86
架构的机器/VPS可以使用 - 不是官方维护的镜像,很难获得开发者的支持
但是,frp开发者对于Docker镜像的态度比较随意,这对于使用Docker的用户来不太友好。其实fatedier/frps
的Docker镜像估计也是可以用的,不过我不准备折腾了。因为我感觉作者的主要精力还是放在原生frp程序里。所以我们干脆就用最官方的方法来使用frp!我看了一下Github的README文档,也不是很难。下面我们用新的方法再安装一次frp吧!
这个教程可以支持frp所支持的所有架构/系统类型,可以放心食用。如果你是高级用户,可以直接在Github Repo了解如何进行更加细致的设置。
原理
FRP的基本原理大致如下:
- 在一个有公网ip的VPS中搭建frp server(frps)服务。将域名
test.sample.com
解析到VPS里。 - 给frps开放某些特定的端口,比如
1234
- 在本地电脑安装frp customer(frpc)服务。通过
common
系列参数保证frps-frpc
的通信正常。 - 本地某服务上线,比如
localapp:6534
。 - frpc进行内部连接,比如
frpc:1234
- 穿透路线:
test.sample.com→VPS→frps:1234→frpc:1234→localapp:6534
按下面的步骤走,你就能深切地体会到原理对应的具体的命令行操作。
准备工作
如果想要降低失败率,你需要较好地掌握Linux基础,特别是与用户(组)相关的知识。如果你不太会Linux,你可以试一下本教程,有问题可加Telegram群或评论区提问。
域名解析
你需要提前将待用域名解析到VPS(frps端)的公网ip中。在本章的示范中,需要用到以下域名:
- frps.sample.com:用于访问服务器端的frps。
- httptest.sample.com:用于验证http内网穿透是否生效
如果你还需要https,还需要:
- httpstest.sample.com:用于验证https内网穿透的是否生效
提前申请并下载好httpstest.sample.com的SSL证书,即下文的httpstest.sample.com.pem
、httpstest.sample.com.key
。
frp版本
frp不同版本所使用的软件是不同的。在frp的Github Repo中,你可以看到最新的版本:
点进去后,你要根据自己的VPS/本地设备的架构/系统来下载软件:
如果你不知道自己的机子是什么架构,可以用uname -a
来查看。有问题加群或评论区提问。
服务端
配置ini文件
首先,创建工作目录:
work=~/Downloads/frp
创建并进入目录:
mkdir -p $work && cd $work
下载软件(不同CPU架构要选不同的版本)。或者你也可以在电脑下载,然后通过sftp上传到工作目录。
wget https://github.com/fatedier/frp/releases/download/v0.43.0/frp_0.43.0_linux_386.tar.gz
解压tar.gz文件:
tar zvfx frp_0.43.0_linux_386.tar.gz && mv frp_0.43.0_linux_386/* . && rm -r frp_0.43.0_linux_386/
此时文件夹的内容类似于:
$ ls -hl
-rw-rw-r-- 1 test_user test 8.8M May 27 16:35 frp_0.43.0_linux_386.tar.gz
-rwxr-xr-x 1 test_user test 9.8M May 27 16:31 frpc
-rw-r--r-- 1 test_user test 11K May 27 16:35 frpc_full.ini
-rw-r--r-- 1 test_user test 126 May 27 16:35 frpc.ini
-rwxr-xr-x 1 test_user test 13M May 27 16:31 frps
-rw-r--r-- 1 test_user test 5.5K May 27 16:35 frps_full.ini
-rw-r--r-- 1 test_user test 26 May 27 16:35 frps.ini
-rw-r--r-- 1 test_user test 12K May 27 16:35 LICENSE
修改frps.ini
的内容。主要是对端口号、帐号和密码、token进行一定改动。大家注意中文注释对应的参数。
vim ./frps.ini
填入以下内容。如果你是高级用户,你可以根据frps_full.ini
自己进行调整。小白用户不妨直接基于我写好的配置进行改动(注意中文标注的相应参数,推荐自定义,不要用我的默认值)。
[common]
# A literal address or host name for IPv6 must be enclosed
# in square brackets, as in "[::1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80"
# For single "bind_addr" field, no need square brackets, like "bind_addr = ::".
bind_addr = 0.0.0.0
# 按需修改。默认bind_port = 7000
bind_port = 6500 # udp port to help make udp hole to penetrate nat
# 按需修改。默认bind_udp_port = 7001
bind_udp_port = 6502# udp port used for kcp protocol, it can be same with 'bind_port'
# if not set, kcp is disabled in frps
# 按需修改。默认bind_port = 7000
kcp_bind_port = 6500# specify which address proxy will listen for, default value is same with bind_addr
# proxy_bind_addr = 127.0.0.1# if you want to support virtual host, you must set the http port for listening (optional)
# Note: http port and https port can be same with bind_port
# 按需修改。默认vhost_http_port = 80, vhost_https_port = 443。但一般VPS的80和443端口都要给Nginx之类的应用来托管。如果你的VPS专门只是用来反代的话,可以使用默认的80和443端口。
vhost_http_port = 6503
vhost_https_port = 6504# response header timeout(seconds) for vhost http server, default is 60s
# vhost_http_timeout = 60# tcpmux_httpconnect_port specifies the port that the server listens for TCP
# HTTP CONNECT requests. If the value is 0, the server will not multiplex TCP
# requests on one single port. If it's not - it will listen on this value for
# HTTP CONNECT requests. By default, this value is 0.
# tcpmux_httpconnect_port = 1337# If tcpmux_passthrough is true, frps won't do any update on traffic.
# tcpmux_passthrough = false# set dashboard_addr and dashboard_port to view dashboard of frps
# dashboard_addr's default value is same with bind_addr
# dashboard is available only if dashboard_port is set
dashboard_addr = 0.0.0.0
dashboard_port = 6501# dashboard user and passwd for basic auth protect
# frps后台的用户名和密码
dashboard_user = superman
dashboard_pwd = test0test# enable_prometheus will export prometheus metrics on {dashboard_addr}:{dashboard_port} in /metrics api.
enable_prometheus = true# dashboard assets directory(only for debug mode)
# assets_dir = ./static# console or real logFile path like ./frps.log
# log_file = ./frps.log# trace, debug, info, warn, error
log_level = infolog_max_days = 3# disable log colors when log_file is console, default is false
disable_log_color = false# DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true.
detailed_errors_to_client = true# authentication_method specifies what authentication method to use authenticate frpc with frps.
# If "token" is specified - token will be read into login message.
# If "oidc" is specified - OIDC (Open ID Connect) token will be issued using OIDC settings. By default, this value is "token".
authentication_method = token# authenticate_heartbeats specifies whether to include authentication token in heartbeats sent to frps. By default, this value is false.
authenticate_heartbeats = false# AuthenticateNewWorkConns specifies whether to include authentication token in new work connections sent to frps. By default, this value is false.
authenticate_new_work_conns = false# auth token
token = D3YhBv*sD3#*3LrvQX!%tA&r9xHN9N# oidc_issuer specifies the issuer to verify OIDC tokens with.
# By default, this value is "".
oidc_issuer =# oidc_audience specifies the audience OIDC tokens should contain when validated.
# By default, this value is "".
oidc_audience =# oidc_skip_expiry_check specifies whether to skip checking if the OIDC token is expired.
# By default, this value is false.
oidc_skip_expiry_check = false# oidc_skip_issuer_check specifies whether to skip checking if the OIDC token's issuer claim matches the issuer specified in OidcIssuer.
# By default, this value is false.
oidc_skip_issuer_check = false# heartbeat configure, it's not recommended to modify the default value
# the default value of heartbeat_timeout is 90. Set negative value to disable it.
# heartbeat_timeout = 90# user_conn_timeout configure, it's not recommended to modify the default value
# the default value of user_conn_timeout is 10
# user_conn_timeout = 10# only allow frpc to bind ports you list, if you set nothing, there won't be any limit
allow_ports = 3505-3510# pool_count in each proxy will change to max_pool_count if they exceed the maximum value
max_pool_count = 5# max ports can be used for each client, default value is 0 means no limit
max_ports_per_client = 0# tls_only specifies whether to only accept TLS-encrypted connections. By default, the value is false.
tls_only = false# tls_cert_file = server.crt
# tls_key_file = server.key
# tls_trusted_ca_file = ca.crt# if subdomain_host is not empty, you can set subdomain when type is http or https in frpc's configure file
# when subdomain is test, the host used by routing is test.frps.com
# 改成
subdomain_host = frps.sample.com# if tcp stream multiplexing is used, default is true
# tcp_mux = true# specify keep alive interval for tcp mux.
# only valid if tcp_mux is true.
# tcp_mux_keepalive_interval = 60# tcp_keepalive specifies the interval between keep-alive probes for an active network connection between frpc and frps.
# If negative, keep-alive probes are disabled.
# tcp_keepalive = 7200# custom 404 page for HTTP requests
# custom_404_page = /path/to/404.html# specify udp packet size, unit is byte. If not set, the default value is 1500.
# This parameter should be same between client and server.
# It affects the udp and sudp proxy.
udp_packet_size = 1500
如果你用了宝塔、ufw等软件,请打开防火墙的相应端口:6500-6504
、3505-3510
。
创建服务
为了可以比较方便地对这个软件进行管理,我们通过构建Linux Service的方式管理frp。我们创建frps
服务:
sudo vim /etc/systemd/system/frps.service
填入以下内容:
[Unit]
Description=frps service
Wants=network-online.target
After=network.target[Service]
Type=simple
ExecStart=/home/test_user/Downloads/frp/frps -c /home/test_user/Downloads/frp/frps.ini
# ExecStop=
Restart=on-failure # always
KillMode=process
TimeoutSec=120
RestartSec=60[Install]
WantedBy=multi-user.target
注意:
ExecStart
如果有引用文件,应该使用绝对路径ExecStart
中的/home/test_user
字段代表的是用户home目录。如果你有一个非root用户test_user
,一般地你可以使用本例中的/home/test_user
。如果你是root用户,要将/home/test_user
替换成/root
。
管理服务
启动服务:
sudo systemctl start frps.service
查看状态:
systemctl status frps.service
如果成功,有类似下面的日志(有success
字样):
最后,不要忘了设置开机自动启动:
sudo systemctl enable frps.service
其它常用的命令还包括:
- 重启服务:
sudo systemctl restart frps.service
- 停止服务:
sudo systemctl stop frps.service
当然,你也可以访问frps的dashboard:http://frps.sample.com:6501。帐户/密码是:superman/test0test
。
本地端
本地端的创建很多原则和服务端是一致的,比如用户目录的设置。大家注意按需修改
如果你用了宝塔、ufw等软件,请打开防火墙的相应端口:3505-3510
。
配置ini文件
设置工作目录:
work=~/Downloads/frpc
创建并进入目录:
mkdir -p $work && cd $work
下载软件:
wget https://github.com/fatedier/frp/releases/download/v0.43.0/frp_0.43.0_linux_386.tar.gz
解压文件:
tar zvfx frp_0.43.0_linux_386.tar.gz && mv frp_0.43.0_linux_386/* . && rm -r frp_0.43.0_linux_386/
此时文件夹的内容类似于:
$ ls -hl
-rw-rw-r-- 1 test_user test 8.8M May 27 16:35 frp_0.43.0_linux_386.tar.gz
-rwxr-xr-x 1 test_user test 9.8M May 27 16:31 frpc
-rw-r--r-- 1 test_user test 11K May 27 16:35 frpc_full.ini
-rw-r--r-- 1 test_user test 126 May 27 16:35 frpc.ini
-rwxr-xr-x 1 test_user test 13M May 27 16:31 frps
-rw-r--r-- 1 test_user test 5.5K May 27 16:35 frps_full.ini
-rw-r--r-- 1 test_user test 26 May 27 16:35 frps.ini
-rw-r--r-- 1 test_user test 12K May 27 16:35 LICENSE
修改frpc.ini
内容如下:
[common]
# 填写服务器实际IP
server_addr = frps.sample.com
# 填写服务器frps的主端口号,本示范中是6500
server_port = 6500
# 与frps.ini中的token要一一对应
token = 12345678
# 日志
log_level = info
log_max_days = 3# 日志
log_file = /home/test_user/Downloads/frpc/frpc.log
log_level = info
log_max_days = 3[ssh-3505]
# 将服务端的3505端口连接到本地的22端口进行ssh连接
type = tcp
# 本地ip
local_ip = 127.0.0.1
# 你的ssh端口。默认是22。如果有自定义ssh端口,就写那个自定义端口。
local_port = 22
# frps开放的端口之一。我随便写了个3505。
remote_port = 3505[http-httptest.sample.com]
# 测试http
type = http
# 本地ip
local_ip = 127.0.0.1
# 比如,某个Docker应用的端口号6533
local_port = 6533
custom_domains = httptest.sample.com# 如果你要https,还要加类似下面这种片段。
[https-httpstest.sample.com]
# 测试https
type = https
# 填写域名
custom_domains = httpstest.sample.com
plugin = https2http
plugin_local_addr = 127.0.0.1:6533
# HTTPS 证书相关的配置。路径用绝对路径。
plugin_crt_path = /home/test_user/Downloads/frpc/httpstest.sample.com.pem
plugin_key_path = /home/test_user/Downloads/frpc/httpstest.sample.com.key
plugin_host_header_rewrite = 127.0.0.1
plugin_header_X-From-Where = frp
创建服务
接着创建服务:
sudo vim /etc/systemd/system/frpc.service
填入以下内容:
[Unit]
Description=frpc service
Wants=network-online.target
After=network.target[Service]
Type=simple
ExecStart=/home/test_user/Downloads/frpc/frpc -c /home/test_user/Downloads/frpc/frpc.ini
# ExecStop=
Restart=on-failure # always
KillMode=process
TimeoutSec=120
RestartSec=60[Install]
WantedBy=multi-user.target
管理服务
启动服务:
sudo systemctl start frpc.service
查看状态:
systemctl status frpc.service
如果成功,有类似下面的日志(有success
字样):
最后,不要忘了设置开机自动启动:
sudo systemctl enable frpc.service
其它常用的命令还包括:
- 重启服务:
sudo systemctl restart frpc.service
- 停止服务:
sudo systemctl stop frpc.service
测试
这部份和之前的教程是一样的,没有区别。
SSH
你在自己的Shell终端里测试一下即可:
- 地址:frps.sample.com
- 端口号:3505
- 帐户:<自己的帐户>
- 密码:<自己的密码>
HTTP
访问http://httptest.sample.com:6503
可成功访问应用。
HTTPS
访问https://httpstest.sample.com:6504
可成功访问应用。
小结
这个教程还是蛮顺的,使用时注意一下用户名/用户组的设置就行了。另外,我觉得端口、用户名/密码、Token这些信息大家最好不要用默认的,自己改动一下,测试一下,以加深对frp的理解。FRP的内网穿透服务还是很稳定的,希望大伙们使用愉快喽!
参考
- Gihub: https://github.com/fatedier/frp
使用FRP进行内网穿透的最佳实践相关推荐
- 远程访问内网服务器使用FRP实现内网穿透
使用FRP实现内网穿透,远程访问内网服务器 frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持 ...
- 疫情之下,使用FRP实现内网穿透,远程连接公司电脑进行办公
当前情况下,经常会有需要到公司电脑进行一些操作,比如连接内网OA,数据库或者提交文档.为了减少外出,将使用frp进行内网穿透的方法进行一个说明. 前提条件 1. 一台拥有公网 IP 的设备(如果没有, ...
- 使用frp进行内网穿透的实例
目录 为什么需要内网穿透功能 frp是什么 工具下载地址 下载的软件可参考 准备工作 VPS相关 VPS上添加入站规则 服务端设置 服务端后台运行 客户端设置 自定义规则 客户端后台运行及开机自启 为 ...
- FRP搭建内网穿透(亲测有效)
FRP搭建内网穿透 1.概述: frp可以通过有公网IP的的服务器将内网的主机暴露给互联网,从而实现通过外网能直接访问到内网主机:frp有服务端和客户端,服务端需要装在有公网ip的服务器上,客户端装在 ...
- 【win11远程桌面访问--基于云端服务器(腾讯云为例)frp的内网穿透实现】
win11远程桌面访问--基于云端服务器(腾讯云为例)&frp的内网穿透实现 frp介绍 服务器账号配置 服务器安装ubuntu系统 服务器 ubuntu系统内配置frps开机自启动 下载服务 ...
- 【内网穿透服务器】利用云服务器+FRP实现内网穿透并远程连接服务器
应用目的:远程管理或在外网访问内网机器上的服务 这里我们是使用云服务器+FRP实现内网穿透,达到远程连接服务器的目的 准备工作 云服务器 这里我们租用的是腾讯云的服务器,类似于下图的这种,配置选择可以 ...
- 【内网穿透服务器】使用FRP实现内网穿透,远程访问内网服务器
使用FRP实现内网穿透,远程访问内网服务器 frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持 ...
- 记录阿里云 centOS FRP 树莓派 内网穿透
记录阿里云 centOS FRP 树莓派 内网穿透 我们知道windows上有一些穿透工具,花生壳向日葵这样的最近几年比较热门,几乎成了新的装机必备.我有一点嵌入式功底,在人工智能的背景下,除了嵌入式 ...
- 使用frp进行内网穿透
使用frp进行内网穿透 为何使用 概览 服务端部署 客户端部署 华硕路由器部署客户端 参考文档 为何使用 最近电信变更了固定IP的使用,原先一个电话打过去申请就可以解决的事情,现在它不行了,客服说要申 ...
最新文章
- win7x64注册表显卡渲染速度_Geek3D GpuTest GUI(显卡测试软件)下载-Geek3D GpuTest GUI(显卡测试软件)免费版下载v0.7.0...
- linux ant安装jar,ant打JAR、WAR包
- 第六天 选择与循环语句
- 2018-2019-1 20165226 《信息安全系统设计基础》第6周学习总结
- SQL:给查询添加一个合计行
- 升级 ubuntu_重要:Canonical 安全更新,请相关 Ubuntu 版本赶紧升级
- mysql 将时间戳_mysql 将时间戳直接转换成日期时间
- iptables 一些有用的规则
- matplotlib mysql_matplotlib简介
- Uphold:在最终被判定为证券前不会退市XRP
- 通用删除查询语句存储过程
- 【实用】MD5加密工具类及加密过程详解
- VirtualBox安装及网络配置
- STC8H开发(十): SPI驱动Nokia5110 LCD(PCD8544)
- Games104 gameplay系统笔记
- 完美修改证件照背景详细步骤,可以消除边界处的白边或者红边
- 模式识别:三层BP神经网络的设计与实现
- 我在帝都买房后,又开始思考怎样赚更多的钱了。
- NVIDIA Jetson之TF卡系统镜像备份与恢复
- 编码的秘密(python版)