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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM