IP returns the client IP address, checking proxy headers first.
()
| 545 | |
| 546 | // IP returns the client IP address, checking proxy headers first. |
| 547 | func (c *Ctx) IP() string { |
| 548 | if ip := c.Request.Header.Get("X-Real-Ip"); ip != "" { |
| 549 | return ip |
| 550 | } |
| 551 | if ip := c.Request.Header.Get("CF-Connecting-IP"); ip != "" { |
| 552 | return ip |
| 553 | } |
| 554 | if ip := c.Request.Header.Get("X-Forwarded-For"); ip != "" { |
| 555 | return strings.TrimSpace(strings.Split(ip, ",")[0]) |
| 556 | } |
| 557 | ip, _, err := net.SplitHostPort(c.Request.RemoteAddr) |
| 558 | if err != nil { |
| 559 | return c.Request.RemoteAddr |
| 560 | } |
| 561 | return ip |
| 562 | } |
| 563 | |
| 564 | // IPs returns all IP addresses from proxy headers. |
| 565 | func (c *Ctx) IPs() []string { |
no test coverage detected