编辑
2024-01-24
开发
00
请注意,本文编写于 184 天前,最后修改于 184 天前,其中某些信息可能已经过时。

目录

起因
frp是什么
安装frp
安装frps
frps配置文件
安装frpc
frpc配置文件
使用systemd配置frpc服务

起因

马上就要放寒假了,组里的服务器都没有配公网ip的(我甚至怀疑没有配固定ip),为了能够回家后在家里也能连上组里服务器跑代码,因此需要做一个内网穿透,让服务器的ssh服务暴露在公网上。

frp是什么

frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议,且支持 P2P 通信。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

github repo link:https://github.com/fatedier/frp/

文档:https://gofrp.org/zh-cn/docs/

安装frp

frp 主要由两个组件组成:客户端(frpc) 和 服务端(frps)。通常情况下,服务端部署在具有公网 IP 地址的机器上,而客户端部署在需要穿透的内网服务所在的机器上。

由于内网服务缺乏公网 IP 地址,因此无法直接被非局域网内的用户访问。用户通过访问服务端的 frps,frp 负责根据请求的端口或其他信息将请求路由到相应的内网机器,从而实现通信。

安装frps

在frp的中文文档中写到“frp 采用 Go 语言编写,支持跨平台,只需下载适用于您平台的二进制文件即可执行,无需额外依赖。”

但是,这里经过我自己的使用,我比较推荐使用github上的另一个叫做frps-onekey的repo提供的脚本 github repo link:https://github.com/MvsCode/frps-onekey

这个脚本的使用方式如下:

shell
# 从gitee下载 wget https://gitee.com/mvscode/frps-onekey/raw/master/install-frps.sh -O ./install-frps.sh # 从github下载 wget https://raw.githubusercontent.com/mvscode/frps-onekey/master/install-frps.sh -O ./install-frps.sh # 安装 chmod 700 ./install-frps.sh ./install-frps.sh install # 卸载 ./install-frps.sh uninstall # 更新 ./install-frps.sh update # 使用 frps {start|stop|restart|status|config|version}

经过测试,该脚本可能在Check updates for shell...阶段卡住,解决办法是注释掉shell_update这个函数。

frps配置文件

按照上述方式安装frps脚本后,配置文件的路径为/usr/local/frps/frps.ini,该配置文件参数如下:

ini
# [common] is integral section [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" bind_addr = 0.0.0.0 bind_port = 5443 # 这里填入frps监听的端口,frpc会向这个端口发送流量 # udp port used for kcp protocol, it can be same with 'bind_port' # if not set, kcp is disabled in frps kcp_bind_port = 5443 # 这里保持和bind_port一致 # if you want to configure or reload frps by dashboard, dashboard_port must be set dashboard_port = 6443 # 这里填入dashboard的端口号,配置后可以通过访问该端口查看frp的连接情况 # dashboard assets directory(only for debug mode) dashboard_user = xxxxxxxx # 访问dashboard所需的用户名(重要!) dashboard_pwd = xxxxxxxx # 访问dashboard所需的密码(重要!) # assets_dir = ./static vhost_http_port = 8080 # 这里填入http服务的转发端口,frpc的内网http服务会被转发到这里 vhost_https_port = 4443 # 这里填入https服务的转发端口 # console or real logFile path like ./frps.log log_file = ./frps.log # debug, info, warn, error log_level = info log_max_days = 3 # auth token token = xxxxxxxx # 校验密码(重要!),frpc需要在配置文件中填入一致的密码才会被frps接受 # It is convenient to use subdomain configure for http、https type when many people use one frps server together. subdomain_host = 175.178.244.215 # only allow frpc to bind ports you list, if you set nothing, there won't be any limit #allow_ports = 1-65535 # pool_count in each proxy will change to max_pool_count if they exceed the maximum value max_pool_count = 50 # if tcp stream multiplexing is used, default is true tcp_mux = true

更改好配置文件后,执行frps restart重新启动frps服务,执行frps status查看运行状况。 输出为Frps (pid xxxx) is running...则代表正常运行。

安装frpc

安装frpc则一般可以直接使用二进制包。从https://github.com/fatedier/frp/releases 页面下载.tar.gz格式的压缩包后解压,其中有frpcfrpc.tomlfrpsfrps.toml四个文件,我们只需要其中frpcfrpc.toml两个。

frpc配置文件

安装frpc时得到的frpc则为frpc的可执行二进制文件、frpc.toml则为其配置文件。

如果是要转发ssh服务的话,对应示例配置文件如下:

toml
serverAddr = "x.x.x.x" # 你的公网服务器地址 serverPort = 5443 # frps的bind_port auth.token = xxxx # frps的校验密码 [[proxies]] name = "mygpuserver_ssh" # 该服务的名称 type = "tcp" # 转发类型,这里填入tcp localIP = "127.0.0.1" # 本地ip,默认为127.0.0.1 localPort = 22 # ssh服务的端口号,默认为22 remotePort = 6000 # 访问公网服务器对应的端口

使用该配置的话,那么使用ssh连接内网服务器的命令为

shell
ssh -P 6000 user@{serverAddr}

如果是要转发http服务的话,对应示例配置文件如下:

toml
serverAddr = "x.x.x.x" # 你的公网服务器地址 serverPort = 5443 # frps的bind_port auth.token = xxxx # frps的校验密码 [[proxies]] name = "my_http_application1" type = "http" localPort = 80 customDomains = ["www.yourdomain.com"] [[proxies]] name = "my_http_application2" type = "http" localPort = 8080 customDomains = ["www.yourdomain2.com"]

修改完配置文件后,请别忘了给frpc添加可执行权限

chmod a+x frpc

使用systemd配置frpc服务

为了让frpc在后台运行,且可以开机自启,异常退出时自动恢复,我们有必要将frpc配置成systemd管理的服务。

shell
# 创建frpc服务的配置文件 sudo vim /etc/systemd/system/frpc.service

写入内容为:

[Unit] # 服务名称,可自定义 Description = frp server After = network.target syslog.target Wants = network.target [Service] Type = simple # 配置异常退出重新启动 Restart=on-failure # 或者always RestartSec=3 # 启动frpc的命令,需修改为您的frpc的安装路径 ExecStart = /path/to/frpc -c /path/to/frpc.toml [Install] WantedBy = multi-user.target

设置frpc开机自启动命令为

shell
sudo systemctl enable frpc

使用systemdfrpc进行管理的命令为

shell
# 启动frp sudo systemctl start frps # 停止frp sudo systemctl stop frps # 重启frp sudo systemctl restart frps # 查看frp状态 sudo systemctl status frps

本文作者:insomnia

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!