Contents
内网穿透运用十分广泛,比如树莓派、nas、家用电脑等。有了内网穿透,我们可以在内网之外的任何网络访问自己处于内网(一般是路由器环境)的电脑等设备。几个场景:
- 外出旅行闲暇时间播放家中nas的美剧
- 上班时调用家中忘记拷贝到U盘的文件
- 需要macOS/Linux开发环境时使用ssh打开终端
- 在树莓派上搭建一个网站,可供外网访问
内网穿透的工具也有不少,e.g. 免费/付费的花生壳、frp、ngrok等。今天给大家介绍的工具是frp,搭建环境为带有公网ip的vps+macOS。
tg交流群:Newlearnerの水群
简介
frp 是一个可用于内网穿透的高性能的反向代理应用,支持 tcp, udp, http, https 协议,可部署在Windows、Linux和macOS这三个常见的桌面操作系统上。开启frp后,公网服务器和内网客户端建立连接,外网用户先去访问公网服务器指定端口,触发公网服务器和内网的互动,并返回信息给外网用户,这样就实现了内网的穿透。
其主要功能有:
利用处于内网或防火墙后的机器,对外网环境提供 http 或 https 服务。
对于 http, https 服务支持基于域名的虚拟主机,支持自定义域名绑定,使多个域名可以共用一个80端口。
利用处于内网或防火墙后的机器,对外网环境提供 tcp 和 udp 服务,例如在家里通过 ssh 访问处于公司内网环境内的主机。
frp下载包中含有客户端跟服务端两种配置文件,其目录结构如下:
frp├── frpc #客户端执行程序├── frpc.ini #客户端配置文件├── frpc_full.ini #客户端配置文件示例├── frps #服务端执行程序├── frps.ini #服务端配置文件└── frps_full.ini #服务端配置文件示例
因此frp根据执行程序和配置文件的类型区分客户端和服务端,在frp版本更新之后,若想要升级,请保持服务端和客户端版本一致。frp 目前(18.12.12)仍然处于前期开发阶段,未经充分测试与验证,不推荐用于生产环境。
部署与功能介绍
1、ssh功能
1 2 3 4 5 6 7 |
# frps.ini [common] #公网服务器与内网通信的端口 bind_port = 7000 #启动server端(于client前启动) ./frps -c ./frps.ini |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# frpc.ini [common] #你的公网ip server_addr = x.x.x.x #与server端的bind_port保持一致 server_port = 7000 [ssh] type = tcp local_ip = 127.0.0.1 #Mac的ssh端口,默认22即可 local_port = 22 #远程访问软件填写的端口 remote_port = 6000 #若内网防火墙禁止ssh,开启通信内容加密传输 use_encryption = true #若传输的报文长度较长,开启压缩节省流量、加快转发 use_compression = true #启动client端 ./frpc -c ./frpc.ini |
检查server端相关的端口是否开放,接着打开Mac设置-共享-远程登录。如果想要长期使用,也可以使用nohup命令挂载在后台运行。若Mac的终端和你的vps server端出现黄色或者红色字样,说明frp运行遇到了问题,请根据提示仔细检查和解决问题。
运行无误之后,打开远程连接ssh工具(xshell、finalshell、terminal等),ip地址填写上面的公网ip,端口为remote_port,用户名为Mac的管理员用户名,密码为管理员密码。e.g. ssh -p 6000 mac_user@xxxxxx
2、对外提供简单的文件访问服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# frps.ini [common] bind_port = 7000 # frpc.ini [common] #公网ip server_addr = x.x.x.x #与bind_port一致 server_port = 7000 [test_static_file] type = tcp #如果同时部署多项功能,请注意端口不要冲突 remote_port = 6000 plugin = static_file # 要对外暴露的文件目录 plugin_local_path = /目录 # 访问 url 中会被去除的前缀,保留的内容即为要访问的文件路径 plugin_strip_prefix = static plugin_http_user = username plugin_http_passwd = passwd |
启动客户端和服务端,打开浏览器,输入http://xxxxx:6000/static/
输入设置好的用户名和密码,即可访问指定目录下的文件,支持下载。
3、访问内网web 服务
1 2 3 4 5 6 7 |
# frps.ini [common] bind_port = 7000 #http服务的server端口 vhost_http_port = 8080 #https服务的server端口 vhost_https_port = 8080 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
# frpc.ini [common] #公网ip server_addr = x.x.x.x #和server端保持一致 server_port = 7000 [web] #根据实际情况选择 type = http/https ##根据实际情况选择 local_port = 80/443 #指定的域名 custom_domains = www.yourdomain.com |
需要将这个域名解析到公网ip上面,如果开启了全域名hts可能对http域名访问造成影响,请更改nginx设置。启动客户端与服务端,在浏览器上输入http(s)://www.yourdomain.com
即可访问内网设备的网站了。
4、Dashboard
作者为此专门写了一个单独的web面板,方便大家查看frp 的状态以及展示代理统计信息。
1 2 3 4 5 6 7 |
# frps.ini [common] #访问端口,注意不要和其他的remote_port冲突 dashboard_port = 7500 # dashboard 用户名密码 dashboard_user = username dashboard_pwd = passwd |
仅在server端部署即可,完成之后开启客户端和服务端,浏览器访问http://xxxxx:7500
,填写用户名和密码即可访问。
One more thing
-
设置开机启动
Linux下的开机启动较为容易配置,配置rc.local
文件即可。像debian9这样的系统自身不原生开启这个功能的,请谷歌。
1 2 3 4 |
cd /etc/rc.local #加入一行 /目录/frpc -c /目录/frpc.ini |
相比于Linux(通常用作server端),macOS(通常用作client端)设置开机启动就十分麻烦了,请参考:Mac设置开机启动
-
安全性
做了内网穿透之后我们就把设备暴露给外网了,因此当我们不用内网穿透的时候,注意采取关闭Mac的22端口,停止frp进程等措施保证安全。公网ip不要轻易泄露,设置的密码尽量复杂云云~
对于ssh这个功能,作者亦给出了自己保证安全性的措施:安全地暴露内网服务
完整配置
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 28 29 30 31 32 33 34 35 36 37 |
# frps.ini [common] bind_port = 7000 vhost_http_port = 8080 vhost_https_port = 8080 # frpc.ini [common] server_addr = x.x.x.x server_port = 7000 [ssh] type = tcp local_ip = 127.0.0.1 local_port = 22 remote_port = 6000 #use_encryption = true #use_compression = true [test_static_file] type = tcp remote_port = 6000 plugin = static_file plugin_local_path = /目录 plugin_strip_prefix = static plugin_http_user = username plugin_http_passwd = passwd [web1] type = http/https local_port = 80/443 custom_domains = www.yourdomain.com [web2] type = http/https local_port = 80/443 custom_domains = xxx.yourdomain.com |
本篇是围绕Mac来介绍,但以上配置对Windows和Linux也具有适用性,使用之前请注意releases对应的系统版本~
参考文章:Github-frp