Skip to content

Commit 766b673

Browse files
authored
docs: update clientIp description (#796)
1 parent 3d59b54 commit 766b673

File tree

2 files changed

+50
-26
lines changed
  • content
    • en/docs/hertz/tutorials/basic-feature/context
    • zh/docs/hertz/tutorials/basic-feature/context

2 files changed

+50
-26
lines changed

content/en/docs/hertz/tutorials/basic-feature/context/request.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@ func (ctx *RequestContext) SetClientIPFunc(f ClientIP)
13301330

13311331
Obtain the address of the client IP.
13321332

1333+
The default behavior of this function: If there is an ip in the `X-Forwarded-For` or `X-Real-IP` headers, read the ip from these two headers and return it (priority `X-Forwarded-For` greater than `X-Real-IP`), otherwise return remote address.
1334+
13331335
Function Signature:
13341336

13351337
```go
@@ -1339,16 +1341,20 @@ func (ctx *RequestContext) ClientIP() string
13391341
Example Code:
13401342

13411343
```go
1344+
// X-Forwarded-For: 20.20.20.20, 30.30.30.30
1345+
// X-Real-IP: 10.10.10.10
13421346
h.Use(func(c context.Context, ctx *app.RequestContext) {
1343-
ip := ctx.ClientIP() // example: 127.0.0.1
1347+
ip := ctx.ClientIP() // 20.20.20.20
13441348
})
13451349
```
13461350

13471351
### SetClientIPFunc
13481352

13491353
If the default method provided by the [ClientIP](#clientip) function does not meet the requirements, users can use this function to customize the way to obtain the client ip.
13501354

1351-
This function can be used in scenarios where you want to obtain an ip from the `X-Forwarded-For` or `X-Real-IP` header even if a remote ip exists (multiple proxies, want to obtain the initial ip from the `X-Forwarded-For` or `X-Real-IP` header).
1355+
Users can implement custom functions themselves or by setting `app.ClientIPOptions`.
1356+
1357+
> Note: When setting `app.ClientIPOptions`, `TrustedCIDRs` requires user customization(if not set, fixed return to remote address), representing trusted routes. If the remote address is within the trusted routing range, it will choose to obtain the ip from `RemoteIPHeaders`, otherwise it will return the remote address.
13521358
13531359
Function Signature:
13541360

@@ -1360,18 +1366,24 @@ Example Code:
13601366

13611367
```go
13621368
// POST http://example.com/user
1363-
// X-Forwarded-For: 203.0.113.195
1369+
// X-Forwarded-For: 30.30.30.30
13641370
h.POST("/user", func(c context.Context, ctx *app.RequestContext) {
1365-
ip := ctx.ClientIP() // ip == "127.0.0.1"
1366-
1367-
opts := app.ClientIPOptions{
1368-
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
1369-
TrustedProxies: map[string]bool{ip: true},
1370-
}
1371-
ctx.SetClientIPFunc(app.ClientIPWithOption(opts))
1372-
1373-
ip = ctx.ClientIP() // ip == "203.0.113.195"
1374-
ctx.String(consts.StatusOK, ip)
1371+
// method 1
1372+
customClientIPFunc := func(ctx *app.RequestContext) string {
1373+
return "127.0.0.1"
1374+
}
1375+
ctx.SetClientIPFunc(customClientIPFunc)
1376+
ip := ctx.ClientIP() // ip == "127.0.0.1"
1377+
1378+
// method 2
1379+
_, cidr, _ := net.ParseCIDR("127.0.0.1/32")
1380+
opts := app.ClientIPOptions{
1381+
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
1382+
TrustedCIDRs: []*net.IPNet{cidr},
1383+
}
1384+
ctx.SetClientIPFunc(app.ClientIPWithOption(opts))
1385+
1386+
ip = ctx.ClientIP() // ip == "30.30.30.30"
13751387
})
13761388
```
13771389

content/zh/docs/hertz/tutorials/basic-feature/context/request.md

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,8 @@ func (ctx *RequestContext) SetClientIPFunc(f ClientIP)
13301330

13311331
获取客户端 IP 的地址。
13321332

1333+
该函数的默认行为:若 `X-Forwarded-For` 或 `X-Real-IP` Header 中存在 ip,则从这两个 Header 中读 ip 并返回(优先级 `X-Forwarded-For` 大于 `X-Real-IP`),否则返回 remote address。
1334+
13331335
函数签名:
13341336

13351337
```go
@@ -1339,16 +1341,20 @@ func (ctx *RequestContext) ClientIP() string
13391341
示例:
13401342

13411343
```go
1344+
// X-Forwarded-For: 20.20.20.20, 30.30.30.30
1345+
// X-Real-IP: 10.10.10.10
13421346
h.Use(func(c context.Context, ctx *app.RequestContext) {
1343-
ip := ctx.ClientIP() // example: 127.0.0.1
1347+
ip := ctx.ClientIP() // 20.20.20.20
13441348
})
13451349
```
13461350

13471351
### SetClientIPFunc
13481352

13491353
[ClientIP](#clientip) 函数提供的默认方式不满足需求,用户可以使用该函数自定义获取客户端 ip 的方式。
13501354

1351-
该函数可用于即使 remote ip 存在,也希望从 `X-Forwarded-For``X-Real-IP` Header 获取 ip 的场景(多重代理,想从 `X-Forwarded-For``X-Real-IP` Header 获得最初的 ip)。
1355+
用户可以自己实现自定义函数,也可以通过设置 `app.ClientIPOptions` 实现。
1356+
1357+
> 注意:在设置 `app.ClientIPOptions` 时,`TrustedCIDRs` 需用户自定义(若不设置则固定返回 remote address),代表可信任的路由。若 remote address 位于可信任的路由范围内,则会选择从 `RemoteIPHeaders` 中获取 ip,否则返回 remote address。
13521358
13531359
函数签名:
13541360

@@ -1360,18 +1366,24 @@ func (ctx *RequestContext) SetClientIPFunc(f ClientIP)
13601366

13611367
```go
13621368
// POST http://example.com/user
1363-
// X-Forwarded-For: 203.0.113.195
1369+
// X-Forwarded-For: 30.30.30.30
13641370
h.POST("/user", func(c context.Context, ctx *app.RequestContext) {
1365-
ip := ctx.ClientIP() // ip == "127.0.0.1"
1366-
1367-
opts := app.ClientIPOptions{
1368-
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
1369-
TrustedProxies: map[string]bool{ip: true},
1370-
}
1371-
ctx.SetClientIPFunc(app.ClientIPWithOption(opts))
1372-
1373-
ip = ctx.ClientIP() // ip == "203.0.113.195"
1374-
ctx.String(consts.StatusOK, ip)
1371+
// method 1
1372+
customClientIPFunc := func(ctx *app.RequestContext) string {
1373+
return "127.0.0.1"
1374+
}
1375+
ctx.SetClientIPFunc(customClientIPFunc)
1376+
ip := ctx.ClientIP() // ip == "127.0.0.1"
1377+
1378+
// method 2
1379+
_, cidr, _ := net.ParseCIDR("127.0.0.1/32")
1380+
opts := app.ClientIPOptions{
1381+
RemoteIPHeaders: []string{"X-Forwarded-For", "X-Real-IP"},
1382+
TrustedCIDRs: []*net.IPNet{cidr},
1383+
}
1384+
ctx.SetClientIPFunc(app.ClientIPWithOption(opts))
1385+
1386+
ip = ctx.ClientIP() // ip == "30.30.30.30"
13751387
})
13761388
```
13771389

0 commit comments

Comments
 (0)