mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 17:58:14 +08:00
Merge ec789274b1f5093f956d71d05b1cf88327e925d5 into d3ffc9985281dcf4d3bef604cce4e662b1a327a6
This commit is contained in:
commit
8d28cadce5
11
gin.go
11
gin.go
@ -486,6 +486,17 @@ func (engine *Engine) validateHeader(header string) (clientIP string, valid bool
|
|||||||
items := strings.Split(header, ",")
|
items := strings.Split(header, ",")
|
||||||
for i := len(items) - 1; i >= 0; i-- {
|
for i := len(items) - 1; i >= 0; i-- {
|
||||||
ipStr := strings.TrimSpace(items[i])
|
ipStr := strings.TrimSpace(items[i])
|
||||||
|
|
||||||
|
// Handle IPv6 with brackets and/or port: [::1], [::1]:8080, 192.168.1.1:8080
|
||||||
|
// net.SplitHostPort handles all these cases and strips brackets
|
||||||
|
if host, _, err := net.SplitHostPort(ipStr); err == nil {
|
||||||
|
ipStr = host
|
||||||
|
} else {
|
||||||
|
// No port present, just strip brackets if any (bare IPv6 like [::1])
|
||||||
|
ipStr = strings.TrimPrefix(ipStr, "[")
|
||||||
|
ipStr = strings.TrimSuffix(ipStr, "]")
|
||||||
|
}
|
||||||
|
|
||||||
ip := net.ParseIP(ipStr)
|
ip := net.ParseIP(ipStr)
|
||||||
if ip == nil {
|
if ip == nil {
|
||||||
break
|
break
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user