获取用户真实 IP

HTTP X-Forwarded-For

目前只有 HTTP 类型的代理支持这一功能,可以通过用户请求的 header 中的 X-Forwarded-For 来获取用户真实 IP,默认启用。

Proxy Protocol

frp 支持通过 Proxy Protocol 协议来传递经过 frp 代理的请求的真实 IP,此功能支持所有以 TCP 为底层协议的类型,不支持 UDP。

Proxy Protocol 功能启用后,frpc 在和本地服务建立连接后,会先发送一段 Proxy Protocol 的协议内容给本地服务,本地服务通过解析这一内容可以获得访问用户的真实 IP。所以不仅仅是 HTTP 服务,任何的 TCP 服务,只要支持这一协议,都可以获得用户的真实 IP 地址。

需要注意的是,在代理配置中如果要启用此功能,需要本地的服务能够支持 Proxy Protocol 这一协议,目前 nginx 和 haproxy 都能够很好的支持。

这里以 HTTPS 类型为例:

# frpc.toml
    [[proxies]]
    name = "web"
    type = "https"
    localPort = 443
    customDomains = ["test.yourdomain.com"]
    
    # 目前支持 v1 和 v2 两个版本的 proxy protocol 协议。
    transport.proxyProtocolVersion = "v2"
    

只需要在代理配置中增加一行 transport.proxyProtocolVersion = "v2" 即可开启此功能。

本地的 HTTPS 服务可以通过在 nginx 的配置中启用 Proxy Protocol 的解析并将结果设置在 X-Real-IP 这个 Header 中就可以在自己的 Web 服务中通过 X-Real-IP 获取到用户的真实 IP。

最后修改 November 15, 2023: fix 404 (#71) (626ce4d)

Get the truth of the user IP

HTTP X-Forwarded-For

Currently, only HTTPtype agents support this feature and can be accessed through user requests X-Forwarded Forin the header to get the truth of The user IP is enabled by default.

Proxy Protocol

FRP supports passing through FRP through theProxy Protocolprotocol The real IP address of the Agent's request, which supports all requests over TCP The type of underlying protocol does not support UDP.

Proxy ProtocolAfter enabling the function, frpc After establishing a connection with the local service, a segment will be sent first Proxy Protocol Provide the protocol content to the local service, and the local service can obtain the true identity of the visiting user by parsing this content IP. So it's not just HTTP services, any TCP Service, as long as this protocol is supported, can obtain the user's real IP address.

It should be noted that in the Agent To enable this feature in Configuration, local services need to be able to support it Proxy ProtocolThis protocol currently includes nginx and haproxy They can all be well supported.

TakingHTTPStype as an example:

# frpc.toml
    [[proxies]]
    name = "web"
    type = "https"
    localPort = 443
    customDomains = ["test.yourdomain.com"]
    
    # Two versions of proxy protocol, v1 and v2, are currently supported.
    transport.proxyProtocolVersion = "v2"
    

Just add one line in Agent Configuration transport. proxyProtocolVersion="a; V2"a This function can be activated.

The local HTTPS service can be enabled in nginx's Configuration Parse Proxy Protocol and set the result to X-Real IPThis header can be found on your own web Obtain the user's real IP address throughX-Real IPin the service.

Last modified November 15, 2023: fix 404 (#71) (626ce4d)