利用frp进行内网穿透,从外网访问内部服务器 (使用Oracle VM)

前言 & 简要介绍

我为什么突然要搞一个内网穿透呢,因为oracle有一个免费的机器,放着也没用,不如整一个frp做内网穿透好了。

什么是内网穿透,简单来说就是你家里有一个服务器,开放了一些服务想要给外面的人用。但是你没有公网ip,所以外面访问不了。

这个时候,就需要内网穿透。通过一个有公网地址的服务器,把流量转发过来。

接下来就是我如何配置内网穿透的过程了。

我的配置

首先你得有一个具有公网ip的服务器,一个域名(如果你没有http的流量需要转发的话就不需要)。公网的服务器用来安装frp的服务器端,一个域名用来对不同的web服务进行转发。

frp的代码开源在githubfrp,文档在文档。最基本的安装教程以及配置都可以在这两个链接中找到。

服务器设置(服务端)

安装&配置 frps

首先在Github的Release页面中下载对应操作系统架构的可执行文件,然后解压

1
2
3
wget https://github.com/fatedier/frp/releases/download/v0.44.0/frp_0.44.0_linux_amd64.tar.gz

tar -zxvf frp_0.44.0_linux_amd64.tar.gz

打开fprs.ini,记住是fprs.ini

修改内容为

1
2
3
4
5
6
7
8
9
10
11
[common]
bind_port = 7000 # 这个是服务器的端口
token=your_token # 改成你自己的token,随便取

dashboard_port = 7500 # 这个是管理界面的端口
dashboard_user = aynakeya # 这个是管理界面的账号
dashboard_pwd = your_password # 这个是管理界面的密码
enable_prometheus = true # 这个是开始管理界面

subdomain_host = aa.xxxx.com # 这个是http服务的自定义二级域名
vhost_http_port = 80 # 这个是http服务的端口,如果不需要转发http就不用写

关于自定义二级域名的跟多信息可以看自定义二级域名

将frps注册为系统服务并启动

创建frps.service文件

/etc/systemd/system/frps.service中填上

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
# 服务名称,可自定义
Description = frp server
After = network.target syslog.target
Wants = network.target

[Service]
Type = simple
# 启动frps的命令,需修改为frps的安装路径
ExecStart = /home/ubuntu/repo/frp/frps -c /home/ubuntu/repo/frp/frps.ini

[Install]
WantedBy = multi-user.target

然后启动并查看服务状态

1
2
sudo systemctl start frps.service
sudo systemctl status frps.service

打开允许程序通过服务器防火墙

这里以Oracle VM作为例子

打开Oracle VM Ubuntu系统instance的防火墙 reference

1
2
3
4
sudo iptables -P INPUT ACCEPT
sudo iptables -P OUTPUT ACCEPT
sudo iptables -P FORWARD ACCEPT
sudo iptables -F

找到Instance -> subnet -> security list -> 选一个security list

在security list中添加规则 Ingress Rule,把管理端口,程序端口,以及你要映射的端口都加上

图片

客户端设置 frpc

安装&配置 frpc

同样在Github的Release页面中下载对应操作系统架构的可执行文件,然后解压

1
2
3
wget https://github.com/fatedier/frp/releases/download/v0.44.0/frp_0.44.0_linux_amd64.tar.gz

tar -zxvf frp_0.44.0_linux_amd64.tar.gz

打开fprc.ini,这次是fprc.ini

填入如下内容,更多功能可以参考frp功能

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
[common]
server_addr = x.x.x.x # 服务器的地址
server_port = 7000
token=your_token # 在服务器设置里填的token

[frontpage]
type = http
local_ip = 127.0.0.1 # 本地的地址
local_port = 80 # 本地的端口
custom_domains = aa.xxxx.com # 用aa.xxxx.com访问

[calibre]
type = http
local_ip = 127.0.0.1
local_port = 8020
# 用calibre.<subdomain_host> 也就是 用calibre.aa.xxxx.com
subdomain = calibre

[jellyfin]
type = http
local_ip = 127.0.0.1
local_port = 8030
subdomain = jellyfin

[aria2web]
type = http
local_ip = 127.0.0.1
local_port = 6801
subdomain = aria2web

将frpc注册为系统服务并启动

创建frpc.service文件

/etc/systemd/user/frps.service中填上

1
2
3
4
5
6
7
8
9
10
11
[Unit]
Description = frp client
After = network.target syslog.target
Wants = network.target

[Service]
Type = simple
ExecStart = /home/aynakeya/repo/frp/frpc -c /home/aynakeya/repo/frp/frpc.ini

[Install]
WantedBy = multi-user.target

启动并检查

1
2
systemctl --user start frpc.service
systemctl --user status frpc.service

域名设置

打开dns记录,添加上两个记录,一个是次级域名的解析,一个是次级域名的次级域名的解析.

比如在这里就是aa.xxxx.com*.aa.xxxx.com

图片

完成

然后你就可以从外网访问你的内部服务器啦

效果展示

这个就不放了,效果和内网访问一样的。