一、使用原因

当处于大内网或者无公网IP的情况下,想要通过外网访问自己内网的设备,往往会很困难。特别是咩有公网IP时,DDNS也无法使用。

那么解决方案有很多种:

  1. FRP内网穿透【适用于有公网服务器的情况】

  2. TailScale

  3. ZeroTier

  4. 向日葵、节点小宝等第三方服务商

本篇主要对FRP进行介绍。

二、基本介绍

frp 是一款高性能的反向代理应用,专注于内网穿透。它支持多种协议,包括 TCP、UDP、HTTP、HTTPS 等,并且具备 P2P 通信功能。使用 frp,您可以安全、便捷地将内网服务暴露到公网,通过拥有公网 IP 的节点进行中转。

通过在具有公网 IP 的节点上部署 frp 服务端,您可以轻松地将内网服务穿透到公网,并享受以下专业特性:

  • 多种协议支持:客户端服务端通信支持 TCP、QUIC、KCP 和 Websocket 等多种协议。

  • TCP 连接流式复用:在单个连接上承载多个请求,减少连接建立时间,降低请求延迟。

  • 代理组间的负载均衡。

  • 端口复用:多个服务可以通过同一个服务端端口暴露。

  • P2P 通信:流量不必经过服务器中转,充分利用带宽资源。

  • 客户端插件:提供多个原生支持的客户端插件,如静态文件查看、HTTPS/HTTP 协议转换、HTTP、SOCKS5 代理等,以便满足各种需求。

  • 服务端插件系统:高度可扩展的服务端插件系统,便于根据自身需求进行功能扩展。

  • 用户友好的 UI 页面:提供服务端和客户端的用户界面,使配置和监控变得更加方便。

详情可见:FRP官方网站

三、安装部署

FRP分为FRPS与FRPC。

  • FRPS即指Frp server(服务)端,一般安装于有公网地址的服务器中。

  • FRPC即指Frp customer(客户)端,一般安装于需要进行内网穿透的机器上。

您可以从 GitHub 的 Release 页面中下载最新版本的客户端和服务器二进制文件。

本文以docker方式进行服务端与客户端安装教程的讲解。

3.1 Docker安装Frps

本文使用docker-compose安装,方便修改与管理端口,如果不习惯,可以使用compose转换工具进行docker命令安装。

docker-compose配置如下:

version: "3.3"
services:
  frps:
    image: snowdreamtech/frps:latest
    container_name: frps
    hostname: frps
    restart: always
    network_mode: host # 默认host配置,也可以指定端口
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./conf/frps.toml:/etc/frp/frps.toml:ro #存放服务端配置文件的位置
      - ./logs:/frp/logs  #存放log的位置

frps.toml服务端具体配置如下:

# 监听地址
bindAddr = "0.0.0.0" # 你的服务器内网地址
# 监听端口
bindPort = 7999      # 与客户端通信的端口号
# 监听 KCP 协议端口
kcpBindPort = 7999
# 监听 HTTP 协议端口
vhostHTTPPort = 8088 # 外网访问http服务的默认端口号
# 监听 HTTPS 协议端口
vhostHTTPSPort = 443 # 外网访问https服务的默认端口号
 
# 允许代理绑定的服务端端口
allowPorts = [
  {start = 5000, end = 6000}, # 与客户端通信时,允许外网访问的客户端的端口号
  { single = 8090 }
]
 
# 鉴权配置
## 鉴权方式
auth.method = "token"
## Token
auth.token = "xxxxxxxxxxxxx" #自行修改,与客户端一致即可
 
# 日志配置
log.to = "/frp/logs/frps.log"
log.level = "debug"
log.maxDays = 180
log.disablePrintColor = false
 
# subdomainHost = "xxxx.xxx"  #自行修改
 
# 仪表盘配置
webServer.addr = "0.0.0.0" # 你的服务器内网地址
webServer.port = 8998      # frp webui端口号
webServer.user = "xxxx"    # frp webui账户
webServer.password = "xxxxx"  # frp webui密码
 
# 是否提供 Prometheus 监控接口
enablePrometheus = true
 
# 允许客户端设置的最大连接池大小
transport.maxPoolCount = 1000

3.2 Docker安装Frpc

docker-compose配置如下:

version: '3.3'
services:
  frpc:
    image: snowdreamtech/frpc:latest
    container_name: frpc
    restart: always
    network_mode: host
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./conf:/etc/frp:ro #存放客户端配置文件的位置
      - ./logs:/frp/logs
      - /cert:/etc/ssl #证书路径自行配置,使用https域名访问时需要

frpc.toml服务端具体配置如下:

# 连接配置
## 连接服务端的地址
serverAddr = "x.x.x.x" #服务端IP
## 连接服务端的端口
serverPort = 7999
 
# 鉴权配置
## 鉴权方式
auth.method = "token"
## Token
auth.token = "xxxxxxxxxxxxx" #自行修改,与服务端一致即可
 
# 日志日志
## 日志路径
log.to = "frp/logs/frpc.log"
## 日志级别
log.level = "info"
## 日志文件最多保留天数
log.maxDays = 30
## 禁用标准输出中的日志颜色
log.disablePrintColor = false
 
includes = ["/etc/frp/confd/*.toml"]


# 以下为一个https服务的配置实例(本地http,转换为https)
[[proxies]]
name = "test_https"
type = "https"
customDomains = ["xxxx.xxx"] #自己的域名

[proxies.plugin]
type = "https2http"
localAddr = "127.29.0.1:8090" #本地的服务地址
# HTTPS 证书相关的配置
crtPath = "/etc/ssl/xxx.crt"
keyPath = "/etc/ssl/xxx.key"
hostHeaderRewrite = "127.29.0.1"
requestHeaders.set.x-from-where = "frp"

# 其他服务示例
# [[proxies]]
# name = "tcp"
# type = "tcp"
# localIP = "127.22.0.1"
# localPort  = 3000
# remotePort = 8978

# [[proxies]]
# name = "web"
# type = "http"
# localPort = 8090
# customDomains = ["xxx.xxx"]

frpc与frps启动即可。