使用frp实现内网穿透
背景
公司要在供应商的生产车间部署一套自己的web系统,这套系统需要外部也能访问,但部分供应商的的网络设施比较落后,没法提供公网IP出来。所以要将本系统暴露到外网,不得不采用内网穿透的方式。
尝试
实现内网穿透的方式有很多,诸如ssh、ngrok都可以实现,但用下来,最稳定最方便的还属frp。笔者采用supervisor+frp的部署方式,运行了已经大半年了,效果还是很满意的。
安装步骤
要实现内网穿透,首先需要有能被外网访问的服务器,然后在这台服务器运行frp的服务端程序:
安装supervisor
xxxxxxxxxx$ sudo apt-get install supervisor下载frp
xxxxxxxxxx$ cd /opt$ wget https://github.com/fatedier/frp/releases/download/v0.28.2/frp_0.28.2_linux_amd64.tar.gz$ tar -zxvf frp_0.28.2_linux_amd64.tar.gz配置frps.ini
xxxxxxxxxx$ vim /opt/frp_0.28.2_linux_amd64/frps.inixxxxxxxxxx[common]bind_port = 7000dashboard_port = 7500# dashboard 用户名密码,默认都为 admindashboard_user = admindashboard_pwd = admin配置supervisor
xxxxxxxxxx$ sudo vim /etc/supervisor/conf.d/frp.confxxxxxxxxxx[program:frp]directory=/opt/frp_0.28.2_linux_amd64autostart = trueautorestart = truecommand = sh -c './frps -c ./frps.ini'stdout_logfile = /var/log/supervisor/stdout.logstderr_logfile = /var/log/supervisor/stderr.logstartretries = 3user = root启动&重启
xxxxxxxxxx# 首次启动$ sudo supervisorctl reload# 重启$ sudo supervisorctl restart frp验证是否成功
dashboard能够打开正常,说明启动成功。地址为:http://公网IP:7500, 用户名密码均为配置的admin。
要内网穿透的机器,需要运行frp的客户端程序,步骤如下:
安装supervisor
xxxxxxxxxx$ sudo apt-get install supervisor下载frp
xxxxxxxxxx$ cd /opt$ wget https://github.com/fatedier/frp/releases/download/v0.28.2/frp_0.28.2_linux_amd64.tar.gz$ tar -zxvf frp_0.28.2_linux_amd64.tar.gz配置frpc.ini
xxxxxxxxxx$ vim /opt/frp_0.28.2_linux_amd64/frpc.inixxxxxxxxxx[common]server_addr = 公网服务器IPserver_port = 7000[ssh-proxy]type = tcplocal_ip = 127.0.0.1local_port = 22remote_port = 2222[web-proxy]type = tcplocal_ip = 127.0.0.1local_port = 80remote_port = 8080此配置的意思是,将本机的22和80端口分别代理到公网服务器的2222端口和8080端口。
配置supervisor
xxxxxxxxxx$ sudo vim /etc/supervisor/conf.d/frp.confxxxxxxxxxx[program:frp]directory=/opt/frp_0.28.2_linux_amd64autostart = trueautorestart = truecommand = sh -c './frpc -c ./frpc.ini'stdout_logfile = /var/log/supervisor/stdout.logstderr_logfile = /var/log/supervisor/stderr.logstartretries = 3user = root启动&重启
xxxxxxxxxx# 首次启动$ sudo supervisorctl reload# 重启$ sudo supervisorctl restart frp验证
验证ssh是否正常
xxxxxxxxxxssh -p 2222 内网机用户@公网服务器IP验证web释放访问正常
xxxxxxxxxxhttp://公网服务器IP:8080
参考