VPS配合Frp实现内网穿透使用windows远程桌面

windows远程桌面连接时,要么两台主机再同一个局域网,要么目标主机必须有外网IP。然而很多情况下,我们的主机都是没有外网IP的,因此我通过Frp和一个具有外网Ip的vps实现内网穿透进而进行远程桌面。

一些商家会提供免费vps,但我不是很放心,因此用购买的Vps搭建。
Vultr购买Vps的方式参考:https://blog.csdn.net/linlinlin96/article/details/79750867

首先将目标主机的远程桌面打开并配置防火墙,这里不再细说。

部署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客户端

下载windows版Frp:
https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_windows_amd64.zip
修改frpc.ini文件:

[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时遇到一些坑还没有解决,故暂且不用。

猜你喜欢

转载自blog.csdn.net/linlinlin96/article/details/79751316