NAT穿透

百科:

https://zh.wikipedia.org/zh-hans/NAT%E7%A9%BF%E9%80%8F

https://baike.baidu.com/item/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F

下面以frp为例

frp server

下载

​ 从 https://github.com/fatedier/frp/releases 下载服务器系统对应的frp程序包

安装

​ 解压并进入文件夹 cd /home/frp_0.27.0_linux_amd64/

配置

​ 先删掉 frpc、frpc.ini 两个文件,然后再进行配置 vi ./frps.ini

​ frps.ini 表示 frp server 端配置文件

​ frpc.ini 表示 frp client 端配置文件

1
2
3
4
~]# vim frps.ini 
[common]
bind_port = 7000     #服务器需要监听的端口,用于和 frp 客户端连接。
vhost_http_port = 6081  #访问客户端web服务(http)自定义的端口号

注意:.ini配置文件中不可以有注释,下同。

后台运行

1
2
3
4
cd /home/frp_0.27.0_linux_amd64/ 
nohup ./frps -c ./frps.ini &
nohup ./frps -c ./frps.ini > /dev/null 2>&1 &
nohup ./frps -c ./frps.ini &> /home/frp_0.27.0_linux_amd64/frplog

frp client

下载

​ 从 https://github.com/fatedier/frp/releases 获取客户端系统对应的frp程序包

安装

​ 解压即可

配置

​ 删掉 frps、frps.ini 两个文件,配置 frpc.ini

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
[common]
server_addr = xxx.xxx.xxx.xxx #公网服务器的IP
server_port = 7000 #公网服务器监听的port,同上面bind_port的值


#公网通过 rdp 访问内部Windows服务器
[rdp]
type = tcp #连接协议
local_ip = 127.0.0.1 #内网服务器ip
local_port = 3389 #远程桌面的默认端口。当用户连接以下 frp server的5200端口时,会被转发到frp client的3389端口。
remote_port = 5200 #frpc在与frps建立连接后,server会监听于此端口用于被用户连接。需要打开frps的防火墙给此端口。
use_encryption = true
use_compression = true
----------------------------------------------------------------------------------------
#公网通过 ssh 访问内部Linux服务器
[ssh]
type = tcp #连接协议
local_ip = 127.0.0.1 #内网服务器ip
local_port = 22 #ssh默认端口号
remote_port = 6000 #自定义的访问内部ssh端口号
----------------------------------------------------------------------------------------
#公网访问内部web服务器以 http 方式
[web]
type = http #访问协议
local_port = 8080 #内网web服务的端口号
custom_domains = xxx.xxx.com #所绑定的公网服务器域名,一级、二级域名都可以
----------------------------------------------------------------------------------------

启动

​ 写一个 frp.bat 脚本文件用于启动 frp client,内容如下

1
2
3
4
5
6
7
8
9
10
11
@echo off  
start "C:\Windows\System32\cmd.exe"
cd C:\Users\Administrator\Desktop\frp_0.27.0_windows_amd64
frpc -c frpc.ini
exit

/*
* start "C:\Windows\System32\cmd.exe" 表示打开一个cmd命令行
* 命令段
* exit 退出打开的命令行
*/

Windows开机自启

添加服务

1
2
Windows下cmd运行
C:\Users\Administrator>sc create frp binPath=C:\Users\Administrator\Desktop\frp_0.27.0_windows_amd64\frp.bat start=auto