VPS配合Frp實現內網穿透使用windows遠端桌面
windows遠端桌面連線時,要麼兩臺主機再同一個區域網,要麼目標主機必須有外網IP。然而很多情況下,我們的主機都是沒有外網IP的,因此我通過Frp和一個具有外網Ip的vps實現內網穿透進而進行遠端桌面。
首先將目標主機的遠端桌面開啟並配置防火牆,這裡不再細說。
部署Frp伺服器端:
首先使用如下命令從官方github下載伺服器端frp並解壓:
wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_amd64.tar.gz
tar -zxvf frp_0.16.1_linux_amd64.tar .gz
進入frp所在的目錄:
cd frp_0.16.1_linux_amd64
使用命令vi frps.ini
修改frps.ini
檔案:
[common]
bind_port = 7000
privilege_token = xxxxxx
dashboard_port = 7001
dashboard_user = user
dashboard_pwd = 123456
privilege_token
是特權模式下與客戶端連線的密碼
dashboard_port
是web控制頁面也可以不設定。
設定完成,後臺執行frps並將日誌寫入logs檔案:
./frps -c ./frps. ini -L logs &
可以使用如下命令實時檢視日誌檔案:
tail -f logs
伺服器端配置完成!
安裝Frp客戶端
[common]
server_addr = xx.xx.xx.xx
server_port = 7000
privilege_token = xxxxxx
[RemoteDesktop]
type = tcp
local_addr = 127.0.0.1
local_port = 3389
remote_port = 7002
其中:
server_addr
是外網vps的ip地址server_port
要與設定伺服器端時的bind_port一致,privilege_token
與伺服器端一致remote_port
注意不要衝突
然後執行frpc.exe
。
注意:
這裡有一個坑,如果卡在以上介面,一段時間後報錯,並且伺服器logs沒有任何響應:
[W] [control.go:109] login to server failed: dial tcp 108.61.23.7: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 108.61.23.7: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.
這時說明伺服器的防火牆沒有開啟該埠,參考Vultr文件:
https://www.vultr.com/docs/install-wordpress-with-apache-php-and-mysql-automated-startup-script
注意到以下關於防火牆的命令:
##### Open firewall for http and SSL
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
/etc/init.d/iptables save
/etc/init.d/iptables restart
我們需要將7000(伺服器端中的bind_port)埠放行,再vps中輸入以下命令:
iptables -F
iptables -A INPUT -i lo -j ACCEPT
iptables -A INPUT -p tcp -m tcp --dport 7000 -j ACCEPT
然後重新連線windows客戶端
連線成功!
現在就可以用vps的ip:埠號
連線遠端桌面了(本文為xx.xx.xx.xx:7002
)。
最後推薦lanproxy,是github上的一個專案,用於內網穿透,使用很方便。我因為僅使用遠端桌面,frp夠用,再加上配置lanproxy時遇到一些坑還沒有解決,故暫且不用。