CentOS7 + frp远程访问内网Windows电脑

1.CentOS7.3 服务端

#下载server端
wget https://github.com/fatedier/frp/releases/download/v0.33.0/frp_0.33.0_linux_amd64.tar.gz
#解压
tar -zxvf frp_0.33.0_linux_amd64.tar.gz
#进入目录
mv frp_0.30.0_linux_amd64 frp
cd frp
#运行frp
./frps -c ./frps.ini

防火墙端口

#firewalld放行端口
firewall-cmd --zone=public --add-port=7000/tcp --permanent
firewall-cmd --zone=public --add-port=3389/tcp --permanent
firewall-cmd --reload

查看 frp 进程

ps -aux | grep frp

服务端配置

bind_port = 7000
# udp port to help make udp hole to penetrate nat
bind_udp_port = 6688

dashboard_port = 7500
# dashboard's username and password are both optional,if not set, default is admin.
dashboard_user = admin
dashboard_pwd = admin

# AuthenticationMethod 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

# AuthenticateHeartBeats 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 = 123
./frps -c ./frps.ini  
#前端开启,关闭就会失效,使用 nohup 后端运行
nohup ./frps -c ./frps.ini &

2.客户端配置

[common]
server_addr = xx.xx.xx.xx
server_port = 7000
# for authentication
token = 123

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

[mstsc]
type = tcp
local_ip = 127.0.0.1
local_port = 3389
remote_port = 3389

https://github.com/fatedier/frp
https://blog.csdn.net/qq920581171/article/details/100673337
https://www.jianshu.com/p/00c79df1aaf0

3.进入阿里云控制台,添加入站规则,否则客户端可能会连接失败
(报错:[service.go:97] login to server failed: dial tcp 47.XX.XX.62:7000: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
dial tcp 47.XX.XX.62:7000: connectex: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

4.IOS连接Windows10

https://www.cnblogs.com/Sungeek/p/10431149.html
https://www.feng.com/post/8557588
如果没找到,可以浏览搜索mstsc.exe

5.进阶

https://xinyuehtx.github.io/post/内网穿透神器frp之进阶配置.html

Frp的TCP模式问题
这里主要存在2个问题。

第一个是安全问题:

试想一下,frp的tcp模式相当于你的设备直接向公网暴露了一个tcp端口。任何设备都可以尝试连接这个端口。这里就会有很大的安全风险。

第二个问题是网络问题:

我的所有请求都需要进行frp的服务器进行中转,这里势必会造成比较大的网络延时。(尤其是我们大部分的vps是部署在国外)这对我们的服务响应速度会造成较大影响。

解决安全问题(stcp)模式
对于安全问题,frp的思路是,既然这些服务有可能被坏人攻击,那我们只要限制特定设备能够使用这个端口就好了。

那么问题来了,我怎么知道哪些设备是允许使用的呢?

服务端配置?那就又陷入了内网穿透的问题。

最简单的方法是使用密钥验证。这就是frp的Secret TCP(stcp)模式的思路。

如下图所示,frp客户端1需要暴露一个tcp端口。于是他在向服务端注册时,额外传了一个密钥。

所有其他设备期望访问这个端口,必须要先验证这个密钥。

这样一来,我们就需要在发起请求的设备上也配置一个frp客户端,通过这个客户端带着密钥发起请求。

如何配置
服务端:配置仍然同默认配置一致,直接运行即可
客户端1:配置需要将type改为stcp,并且增加一个sk字段。这里不需要远端端口,因为不公开

# frpc.ini
[common]
# 你的frp服务器的公网ip
server_addr = x.x.x.x
# 你的frp服务器的默认端口
server_port = 7000

[rdp]
type = stcp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
local_ip = 127.0.0.1
# 远程桌面的本地端口号
local_port = 3389

客户端2:

# frpc.ini
[common]
# 你的frp服务器的公网ip
server_addr = x.x.x.x
# 你的frp服务器的默认端口
server_port = 7000

[rdp_visitor]
type = stcp
# stcp 的访问者
role = visitor
# 要访问的 stcp 代理的名字
server_name = rdp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
# 绑定本地端口用于访问 远程桌面 服务
bind_addr = 127.0.0.1
bind_port = 6000

此时,你在客户端2,使用127.0.0.1:6000即可访问客户端1的远程服务。

解决网络问题(xtcp)模式
思考一下,我们的frp服务器主要目的是为了解决两台设备相互识别的情况。在正式运行时,其实并不需要服务端做什么事情。

frp客户端就好比两个相亲的对象,frp服务端是媒婆。媒婆介绍完之后,就应该有相亲对象自己聊天了。

这个就是点对点模式(p2p)。在frp中,这个可以通过设置xtcp实现。

如何配置
服务端:配置需要增加一个udp端口 7001,增加完之后就是如下

# frps.ini
[common]
bind_port = 7000
bind_udp_port = 7001

客户端1:配置需要将type改为xtcp即可

# frpc.ini
[common]
# 你的frp服务器的公网ip
server_addr = x.x.x.x
# 你的frp服务器的默认端口
server_port = 7000

[rdp]
type = xtcp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
local_ip = 127.0.0.1
# 远程桌面的本地端口号
local_port = 3389

客户端2:配置需要将type改为xtcp即可

# frpc.ini
[common]
# 你的frp服务器的公网ip
server_addr = x.x.x.x
# 你的frp服务器的默认端口
server_port = 7000

[rdp_visitor]
type = xtcp
# stcp 的访问者
role = visitor
# 要访问的 stcp 代理的名字
server_name = rdp
# 只有 sk 一致的用户才能访问到此服务
sk = abcdefg
# 绑定本地端口用于访问 远程桌面 服务
bind_addr = 127.0.0.1
bind_port = 6000

此时,你在客户端2,使用同样的方式,以127.0.0.1:6000即可访问客户端1的远程服务。

不过需要注意的是,目前frp的p2p服务还不完善,很多nat设备还是不能够穿透的。

6.参考

https://zhuanlan.zhihu.com/p/129076009

原文地址:https://www.cnblogs.com/TTonly/p/12933837.html