前言

现在越来越多的家宽没有公网IP
不要说固定的IP
甚至有一家连动态IP都不给
那我们现在还想远程连回家
那我们就试试用Frp

原理拓扑

借用大佬的拓扑图
我们可以看到
我们游戏服务器和朋友的主机
都位于各自运营商的大内网中
现在我们借助一台VPS做中转
只要我们都能访问到那台VPS
我们就可以一起愉快的玩游戏

Frp 文件下载

Frp 源码

1
https://github.com/fatedier/frp

Windows 下载

直接下载就好
咁都唔识,小儿麻痹咩你~
下载完就解压啦

Linux 下载

  1. 按照最新版本号更改下载链接
1
wget https://github.com/fatedier/frp/releases/download/v0.51.3/frp_0.51.3_linux_amd64.tar.gz
  1. 解压下载好的安装包
1
tar -zxvf frp_0.51.3_linux_amd64.tar.gz

删除无用配置

Frp Server 配置

1
2
sudo rm -f frpc*
sudo rm -f systemd/frpc*

Frp Client 配置

1
2
sudo rm -f frps*
sudo rm -f systemd/frps*

Frp Server 配置

修改 Frp Server 配置

1
sudo vim frps.ini

frps.ini 参考配置

可以参考进行修改

1
2
3
4
5
6
7
8
9
10
11
[common]
# 客户端与服务端的通信端口,可保持默认
bind_port = 7000
# Frp Server 管理后台端口,可保持默认
dashboard_port = 7500
# Frp Server 管理后台账号,自行设置
dashboard_user = xxx
# Frp Server 管理后台密码,自行设置
dashboard_pwd = xxx
# 客户端连接服务端的密码,自行设置
token = xxx

保持后台运行

1
2
3
4
# 创建 frp 后台
screen -S frp
# 启动 Frp Server 服务
./frps -c ./frps.ini

Screen 命令简介

1
2
3
4
5
6
7
8
9
10
11
12
# 新建名为 [xxx] 的 Session 
screen -S [xxx]
# 最小化 当前 Session
Ctrl + A + D
# 回到名为 [xxx] 的 Session
screen -r [xxx]
# 强制进入名为 [xxx] 的 Session
screen -D -r xxx
# 列出当前已运行 Session
screen -list
# 关闭名为 [xxx] 的session
screen -S [xxx] -X quit

注册系统服务

1
sudo vim /etc/systemd/system/frps.service

修改 Frp Server 服务文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
Description=Frp Server Daemon
After=network.target

[Service]
Type=simple
Restart=on-failure
RestartSec=5s
# 这里需要根据frps的安装路径,进行更改
ExecStart=/home/ecs-user/frp/frps -c /home/ecs-user/frp/frps.ini

[Install]
WantedBy=multi-user.target

Frp 系统命令

1
2
3
4
5
6
7
8
# 启动测试
sudo systemctl start frps.service
# 查看启动状态
sudo systemctl status frps.service
# 停止
sudo systemctl stop frps.service
# 开机自启
sudo systemctl enable frps.service

Frp Server 开放端口

  • 大家根据自己设置端口去各自云服务商开启

例如我就是开启了70007500端口

服务端搭建成功

系统服务

Frp 管理后台

Frp Client 配置

  • 这里我们用Windows作为客户端演示

修改 Frp Client 配置

修改 frpc.ini

frps.ini 参考配置

可以参考进行修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[common]
# 服务器公网IP
server_addr = xxx.xxx.xxx.xxx
# 服务端设置的bind_port
server_port = 7000
# 服务端设置的客户端连接密码
token = xxx
# 开机自动启动
autostart = true

# 起一个容易记的服务名
[xxx]
# 协议种类,默认 tcp 即可
type = tcp
# 需要接入服务的范围
local_ip = 127.0.0.1
# 内网穿透本地端口
local_port = xxx.xxx.xxx.xxx
# 内网穿透互联网端口
remote_port = xxx.xxx.xxx.xxx

客户端示例配置

  • 本段可以多次添加,同时进行多个端口的内网穿透
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# SSH
[SSH]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 22

# 远程桌面连接 [RDP]
[RDP]
type = tcp
local_ip = 127.0.0.1
local_port = 3389
remote_port = 3389

# Minecraft Java 版
[MC]
type = tcp
local_ip = 127.0.0.1
local_port = 25565
remote_port = 25565

# Minecraft 基岩版
[MCBE]
type = tcp
local_ip = 127.0.0.1
local_port = 19132
remote_port = 19132

# Terraria
[TR]
type = tcp
local_ip = 127.0.0.1
local_port = 7777
remote_port = 7777

开启 Frp Client 服务

1
./frpc -c ./frpc.ini

Frp Client 开机自启

  1. Win + R 打开 taskschd.msc
  1. 创建基本任务
  1. 选择 计算机启动时
  1. 选择 启动程序
  1. 配置自动执行脚本
  1. 完成配置
  1. 不管用户是否登录都要运行
  • 需要输入账户密码

Frp Client 开放端口

本地网关路由器

  • 大家根据自己设置端口去各自网关路由器开启

例如我就是开启了3389端口

云主机 VPS

  • 大家根据自己设置端口去各自云服务商开启

所以云主机还要开启3389端口

Frp 连接成功

参考 & 引用

https://www.zouht.com/727.html
https://learnku.com/articles/45769
https://cloud.tencent.com/developer/article/1700417
https://wp.gxnas.com/12153.html