IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    frp 多场景使用示例

    Teacher Du发表于 2024-11-01 03:51:26
    love 0

    发现很多小伙伴对 frp 感兴趣,但不知道如何使用,杜老师为小伙伴们分享一些示例。这里包括多个常见的使用场景和配置示例,小伙伴们可用来亲自部署和体验这些示例。

    通过 SSH 来访问内网机器

    这个示例通过简单配置 TCP 类型的代理让用户访问到内网的服务器。在具有公网 IP 机器上部署 frps,并修改 frps.ini 文件「这里使用了最简化配置」设置 frp 服务器用户接收客户端连接的端口:

    1
    2
    [common]
    bind_port = 7000

    在需被访问的内网机器上部署 frpc,并修改 frpc.ini 文件,假设 frps 所在服务器的公网 IP 为 x.x.x.x。local_ip 和 local_port 配置为本地需暴露到公网的服务地址和端口。remote_port 表示在 frp 服务端监听端口,访问此端口的流量将会被转发到本地服务对应端口:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [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

    多个 SSH 服务复用同一个端口

    这个示例通过 tcpmux 类型的代理,实现多个 SSH 服务通过同一个端口暴露。与此类似,只要是能够支持 HTTP 代理连接方式的客户端,都可以通过这种方式来实现对端口的复用。在具有公网 IP 机器上部署 frps,并修改 frps.ini 文件「这里使用最简化的配置」

    1
    2
    3
    [common]
    bind_port = 7000
    tcpmux_httpconnect_port = 5002

    在内网机器 A 上部署 frpc,配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [ssh1]
    type = tcpmux
    multiplexer = httpconnect
    custom_domains = machine-a.example.com
    local_ip = 127.0.0.1
    local_port = 22

    在内网机器 B 上部署另外一个 frpc,配置文件:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [ssh2]
    type = tcpmux
    multiplexer = httpconnect
    custom_domains = machine-b.example.com
    local_ip = 127.0.0.1
    local_port = 22

    通过 SSH 访问内网机器 A,假设用户名 test:

    1
    ssh -o 'proxycommand socat - PROXY:x.x.x.x:machine-a.example.com:22,proxyport=5002' test@machine-a

    通过自定义域名访问内网 Web 服务

    这个示例通过简单配置 HTTP 类型的代理让用户访问到内网的 Web 服务。HTTP 类型的代理相比于 TCP 类型,不仅在服务端只需要监听一个额外的端口 vhost_http_port 用于接收 HTTP 请求,还额外提供了基于 HTTP 协议的诸多功能。按照下面的代码修改 frps.ini 文件,设置监听 HTTP 请求的端口为 8080:

    1
    2
    3
    [common]
    bind_port = 7000
    vhost_http_port = 8080

    按照下面的代码修改 frpc.ini 文件,假设 frps 所在服务器的 IP 为 x.x.x.x,local_port 为本地机器上 Web 服务监听端口,绑定的自定义域名为 custom_domains:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [web]
    type = http
    local_port = 80
    custom_domains = www.yourdomain.com

    [web2]
    type = http
    local_port = 8080
    custom_domains = www.yourdomain2.com

    转发 DNS 的查询请求

    这个示例通过简单配置 UDP 类型代理转发 DNS 查询请求。DNS 查询请求通常使用 UDP 协议,frp 支持对内网 UDP 服务的穿透,配置方式和 TCP 基本一致。frps.ini 的内容如下:

    1
    2
    [common]
    bind_port = 7000

    frpc.ini 的内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [dns]
    type = udp
    local_ip = 8.8.8.8
    local_port = 53
    remote_port = 6000

    转发 Unix 的域套接字

    这个示例通过配置 Unix 域套接字客户端插件来通过 TCP 端口访问内网的 Unix 域套接字服务,如 Docker Daemon。frps.ini 的内容如下:

    1
    2
    [common]
    bind_port = 7000

    frpc.ini 的内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [unix_domain_socket]
    type = tcp
    remote_port = 6000
    plugin = unix_domain_socket
    plugin_unix_path = /var/run/docker.sock

    对外提供简单文件访问服务

    这个示例通过配置 static_file 客户端插件来将本地文件暴露在公网上供其他人访问。通过 static_file 插件可以对外提供一个简单的基于 HTTP 的文件访问服务。frps.ini 的内容如下:

    1
    2
    [common]
    bind_port = 7000

    frpc.ini 的内容如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [test_static_file]
    type = tcp
    remote_port = 6000
    plugin = static_file
    # 要对外暴露的文件目录
    plugin_local_path = /tmp/file
    # 用户访问 URL 会被去除的前缀,保留内容即为要访问的文件路径
    plugin_strip_prefix = static
    plugin_http_user = abc
    plugin_http_passwd = abc

    安全暴露内网服务

    这个示例将会创建一个只有自己能访问到的 SSH 服务代理。对于某些服务来说如果直接暴露于公网上将会存在安全隐患。使用 stcp 类型的代理可以避免让任何人都能访问到穿透的服务,但是访问者也需要运行另外一个 frpc 客户端。frps.ini 的内容如下:

    1
    2
    [common]
    bind_port = 7000

    在需要暴露到外网的机器上部署 frpc 且配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [secret_ssh]
    type = stcp
    # 只有 sk 一致的用户才能访问到此服务
    sk = abcdefg
    local_ip = 127.0.0.1
    local_port = 22

    在想要访问内网服务的机器上也部署 frpc 且配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [secret_ssh_visitor]
    type = stcp
    # stcp 访问者
    role = visitor
    # 要访问的 stcp 代理的名字
    server_name = secret_ssh
    sk = abcdefg
    # 绑定本地的端口用于访问 SSH 服务
    bind_addr = 127.0.0.1
    bind_port = 6000

    通过 SSH 来访问内网机器,假设用户名 test:

    1
    ssh -oPort=6000 test@127.0.0.1

    点对点的内网穿透

    这个示例将会演示一种不通过服务器中转流量的方式来访问内网服务。frp 提供了一种新的代理类型 xtcp 用于应对在希望传输大量数据且流量不经过服务器的场景。在需要暴露到外网的机器上部署 frpc 且配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [p2p_ssh]
    type = xtcp
    # 只有 sk 一致的用户才能访问到此服务
    sk = abcdefg
    local_ip = 127.0.0.1
    local_port = 22

    在想要访问内网服务的机器上也部署 frpc 且配置如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    [common]
    server_addr = x.x.x.x
    server_port = 7000

    [p2p_ssh_visitor]
    type = xtcp
    # xtcp 访问者
    role = visitor
    # 要访问的 xtcp 代理的名字
    server_name = p2p_ssh
    sk = abcdefg
    # 绑定本地的端口用于访问 SSH 服务
    bind_addr = 127.0.0.1
    bind_port = 6000
    # 当需要自动保持隧道打开时,设置为 true
    # keep_tunnel_open = false


沪ICP备19023445号-2号
友情链接