mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 13:22:09 +08:00
Merge 11763f132db6e5b491ac4ad4fa1a8fa6fcd27edc into 0397e5e0c0f8f8176c29f7edd8f1bff8e45df780
This commit is contained in:
commit
bee809f4a5
32
gin.go
32
gin.go
@ -17,6 +17,7 @@ import (
|
|||||||
|
|
||||||
"github.com/gin-gonic/gin/internal/bytesconv"
|
"github.com/gin-gonic/gin/internal/bytesconv"
|
||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
|
"github.com/gin-gonic/gin/utils"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
"golang.org/x/net/http2/h2c"
|
"golang.org/x/net/http2/h2c"
|
||||||
)
|
)
|
||||||
@ -401,24 +402,17 @@ func (engine *Engine) Run(addr ...string) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) {
|
func (engine *Engine) prepareTrustedCIDRs() ([]*net.IPNet, error) {
|
||||||
|
var err error
|
||||||
if engine.trustedProxies == nil {
|
if engine.trustedProxies == nil {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cidr := make([]*net.IPNet, 0, len(engine.trustedProxies))
|
cidr := make([]*net.IPNet, 0, len(engine.trustedProxies))
|
||||||
for _, trustedProxy := range engine.trustedProxies {
|
for _, trustedProxy := range engine.trustedProxies {
|
||||||
if !strings.Contains(trustedProxy, "/") {
|
trustedProxy, err = utils.MakeTrustIP(trustedProxy)
|
||||||
ip := parseIP(trustedProxy)
|
|
||||||
if ip == nil {
|
if err != nil {
|
||||||
return cidr, &net.ParseError{Type: "IP address", Text: trustedProxy}
|
return cidr, err
|
||||||
}
|
|
||||||
|
|
||||||
switch len(ip) {
|
|
||||||
case net.IPv4len:
|
|
||||||
trustedProxy += "/32"
|
|
||||||
case net.IPv6len:
|
|
||||||
trustedProxy += "/128"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_, cidrNet, err := net.ParseCIDR(trustedProxy)
|
_, cidrNet, err := net.ParseCIDR(trustedProxy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -489,20 +483,6 @@ func (engine *Engine) validateHeader(header string) (clientIP string, valid bool
|
|||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseIP parse a string representation of an IP and returns a net.IP with the
|
|
||||||
// minimum byte representation or nil if input is invalid.
|
|
||||||
func parseIP(ip string) net.IP {
|
|
||||||
parsedIP := net.ParseIP(ip)
|
|
||||||
|
|
||||||
if ipv4 := parsedIP.To4(); ipv4 != nil {
|
|
||||||
// return ip in a 4-byte representation
|
|
||||||
return ipv4
|
|
||||||
}
|
|
||||||
|
|
||||||
// return ip in a 16-byte representation or nil
|
|
||||||
return parsedIP
|
|
||||||
}
|
|
||||||
|
|
||||||
// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
// RunTLS attaches the router to a http.Server and starts listening and serving HTTPS (secure) requests.
|
||||||
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
// It is a shortcut for http.ListenAndServeTLS(addr, certFile, keyFile, router)
|
||||||
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
// Note: this method will block the calling goroutine indefinitely unless an error happens.
|
||||||
|
46
utils/ip.go
Normal file
46
utils/ip.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func parseIP(ip string) (net.IP, error) {
|
||||||
|
parsedIP := net.ParseIP(ip)
|
||||||
|
|
||||||
|
if ipv4 := parsedIP.To4(); ipv4 != nil {
|
||||||
|
return ipv4, nil
|
||||||
|
}
|
||||||
|
if parsedIP != nil{
|
||||||
|
return parsedIP, nil
|
||||||
|
}
|
||||||
|
return nil, &net.ParseError{Type: "IP address", Text: ip}
|
||||||
|
}
|
||||||
|
|
||||||
|
func MakeTrustIP(trustedIP string) (string, error) {
|
||||||
|
if strings.Contains(trustedIP, "/") {
|
||||||
|
return trustedIP, nil
|
||||||
|
}
|
||||||
|
ip, err := parseIP(trustedIP)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var mapRenderIP = map [int]func(trustIP string) string{
|
||||||
|
net.IPv4len: func(trustIP string) string{
|
||||||
|
return trustIP + "/32"
|
||||||
|
},
|
||||||
|
net.IPv6len: func(trustIP string) string{
|
||||||
|
return trustIP + "/32"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn, isExistKey := mapRenderIP[len(ip)]
|
||||||
|
|
||||||
|
if isExistKey != true{
|
||||||
|
return "", &net.ParseError{Type: "IP address", Text: trustedIP}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fn(trustedIP), nil
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user