You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
676 B
27 lines
676 B
package router
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
func HttpRealIP() echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
if XForwardedFor := c.Request().Header.Get(http.CanonicalHeaderKey("X-Forwarded-For")); XForwardedFor != "" {
|
|
dataIndex := strings.Index(XForwardedFor, ", ")
|
|
if dataIndex == -1 {
|
|
dataIndex = len(XForwardedFor)
|
|
}
|
|
|
|
c.Request().RemoteAddr = XForwardedFor[:dataIndex]
|
|
} else if XRealIP := c.Request().Header.Get(http.CanonicalHeaderKey("X-Real-IP")); XRealIP != "" {
|
|
c.Request().RemoteAddr = XRealIP
|
|
}
|
|
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|