使用 frp 的前提条件是有公网IP
的服务器,毕竟标题所讲的就是内网穿透。内网穿透,要么买服务,要么就自己搭,frp
就是一款高性能的反向代理应用,专注于内网穿透,主打一个免安装,Go
语言写的嘛,交叉静态编译就是香。
像微信以及一些服务,需要提供公网HTTP
地址甚至必须是HTTPS
服务才能申请或测试,频繁同步代码到服务器也不方便,而且如果是新的服务器还得搭环境,那还是做个内网穿透服务到本地比较舒服,非常适合个人做测试。
本文使用的是frp 0.61.2
,下载即可使用,解压后只有以下文件:
frpc
frpc.toml
frps
frps.toml
LICENSE
用户只需要关注frps.toml
和frpc.toml
,分别是服务端和客户端的配置。
服务端frps.toml
的bindPort
为穿透服务的端口,即frps
和frpc
通信的端口,而vhostHTTPPort
和vhostHTTPSPort
为服务器对外提供HTTP
和HTTPS
服务的端口。
而客户端frpc.toml
的serverAddr
和serverPort
显然就是客户端和服务端通信的地址和端口,例如我们通过example.com
的8080
端口为客户端提供穿透服务,则serverAddr
为example.com
,serverPort
为8080
;至于localIP="127.0.0.1"
、localPort=80
、customDomains=["example.com"]
就是需要暴露到公网的本地服务地址和端口以及自定义域名。
HTTP 服务
服务端frps.toml
配置:
bindPort=8080
vhostHTTPPort=8080
客户端frpc.toml
配置:
serverAddr="example.com"
serverPort=8080
[[proxies]]
name="web"
type="http"
localIP="127.0.0.1"
localPort=80
customDomains=["example.com"]
HTTPS 服务
vhostHTTPSPort
为服务器对外提供HTTPS
服务的端口。
服务端frps.toml
配置:
bindPort=8080
vhostHTTPSPort=443
客户端frpc.toml
配置:
serverAddr="example.com"
serverPort=8080
[[proxies]]
name="web"
type="https"
customDomains=["example.com"]
[proxies.plugin]
type="https2http"
localAddr="127.0.0.1:80"
crtPath="./example.com_public.crt"
keyPath="./example.com.key"
hostHeaderRewrite="127.0.0.1"
关键说明:
[[proxies]]
的type="https"
表示该转发是https
类型;[proxies.plugin]
的type="https2http"
是frp
将HTTP
服务转换为HTTPS
服务的扩展;[proxies.plugin]
的localAddr
为本地HTTPS
服务地址;[proxies.plugin]
的keyPath
为TLS
密钥文件路径;[proxies.plugin]
的crtPath
为TLS
证书文件路径;[proxies.plugin]
的hostHeaderRewrite
改写请求报文的Host
;