Published on
· 4 min read

Frp内网穿透搭建

Authors
  • avatar
    Name
    felixDu
    Twitter
Table of Contents

Mac与Linux之Frp内网搭建

frp是什么

frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

有了内网穿透你能干什么?

  • 远程访问内网的 http/https 服务
  • 远程桌面(Windows/Mac)
  • 远程文件、 SSH
  • 小程序开发

​ ...

安装

下面介绍在linux服务器下配置

下载

注意修改版本号以及系统架构

wget https://github.com/fatedier/frp/releases/download/v0.xx.0/frp_0.xx.0_linux_amd64.tar.gz

解压

tar -xvf frp_0.xx.0_linux_amd64.tar.gz

移动至/usr/local

mkdir /usr/local/frp
mv frp_0.xx.0_linux_amd64/* /usr/local/frp/

其中frpc为客户端frps为服务端

配置systemctl来控制服务端运行

vi /usr/lib/systemd/system/frp.service
#---------------------------------------

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit

[Install]
WantedBy=multi-user.target


重新加载配置文件

systemctl daemon-reload

启动/停止/重启,查看状态,设置开机自启/关闭开机自启

systemctl start frp
systemctl stop frp
systemctl restart frp
systemctl status frp
systemctl enable frp
systemctl disable frp

配置和使用

服务端

[common] #必须设置
bind_port = 7000 #是自己设定的frp服务端端口
vhost_http_port = 80 #是自己设定的http访问端口
token = 123  #核实身份用,加了更安全

[ssh] #ssh反向代理(不是必须设置)
listen_port = 6000 是自己设定的ssh访问端口

[web] #http反向代理[]里的内容可以自己设定,但是客户端和服务端必须要对应(如[aaa],[bbb]);
type = http #为服务类型,可以设为http,https
custom_domains = test1.a.com #为要映射的域名,记得域名的A记录要解析到外网主机的IP。

[web2] #同上(可设置多个)

启动

./frps -c ./frps.ini

# 后台启动
nohup ./frps -c ./frps.ini &

客户端

[common]
server_addr = 远程frp服务器ip
server_port = 远程frp服务器端口
token = 远程frp服务器token

[http]
type = http
local_ip = 127.0.0.1
local_port = 本地端口号
remote_port = 远程frp服务器的http服务端口号
custom_domains = 自定义配置的域名
subdomain = 匹配服务端配置的subdomain_host

启动

./frpc -c ./frpc.ini

# 后台启动
nohup ./frpc -c ./frpc.ini &

这里我使用的为Mac所以设置了开机启动

sudo vi /Library/LaunchDaemons/frpc.plist

#------------------------------------------写入下面内容、记得去掉注释

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>frpc</string>
    <key>ProgramArguments</key>
    <array>
    <string>/usr/local/bin/frpc</string> # 修改为自己的地址
    <string>-c</string>
    <string>/etc/frpc.ini</string> # 修改为自己的地址
    </array>
  </dict>
</plist>

#---------------------------------------------加入启动项
sudo chown root /Library/LaunchDaemons/frpc.plist
sudo launchctl load -w /Library/LaunchDaemons/frpc.plist 

#---------------------------------------------取消自启动
sudo launchctl unload -w /Library/LaunchDaemons/frpc.plist