diff --git a/routebuildermdl/routebuilder_fasthttp.go b/routebuildermdl/routebuilder_fasthttp.go index b8b0e73a4e12ff841fa86cab797457106e70cec9..2a49e1e3d1a7b12d6686039239fbbe6467df584c 100644 --- a/routebuildermdl/routebuilder_fasthttp.go +++ b/routebuildermdl/routebuilder_fasthttp.go @@ -4,6 +4,7 @@ package routebuildermdl import ( "context" + "net" "strings" "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/authmdl/jwtmdl" @@ -203,8 +204,7 @@ func commonHandler(c *routing.Context, isRestricted, isRoleBased, heavyDataActiv func OpenHandler(c *routing.Context) error { c.Response.Header.Set("content-type", "application/json") principal := servicebuildermdl.Principal{} - - principal.ClientIP = c.RemoteIP().String() + principal.ClientIP = getClientIP(c) commonHandler(c, false, false, false, principal) return nil } @@ -219,7 +219,6 @@ func RestrictedHandler(c *routing.Context) error { c.SetStatusCode(412) return err } - // pricipalObj.ClientIP = c.RemoteIP().String() commonHandler(c, true, false, false, pricipalObj) return nil } @@ -234,7 +233,6 @@ func RoleBasedHandler(c *routing.Context) error { c.SetStatusCode(412) return err } - // pricipalObj.ClientIP = c.RemoteIP().String() commonHandler(c, true, true, false, pricipalObj) return nil } @@ -243,8 +241,7 @@ func RoleBasedHandler(c *routing.Context) error { func HeavyOpenHandler(c *routing.Context) error { c.Response.Header.Set("content-type", "application/json") principal := servicebuildermdl.Principal{} - - principal.ClientIP = c.RemoteIP().String() + principal.ClientIP = getClientIP(c) commonHandler(c, false, false, true, principal) return nil } @@ -259,7 +256,6 @@ func HeavyRestrictedHandler(c *routing.Context) error { c.SetStatusCode(412) return err } - // pricipalObj.ClientIP = c.RemoteIP().String() commonHandler(c, true, false, true, pricipalObj) return nil } @@ -274,7 +270,6 @@ func HeavyRoleBasedHandler(c *routing.Context) error { c.SetStatusCode(412) return err } - // pricipalObj.ClientIP = c.RemoteIP().String() commonHandler(c, true, true, true, pricipalObj) return nil } @@ -352,7 +347,19 @@ func getClientIP(c *routing.Context) string { clientIP = string(c.Request.Header.Peek("X-Forwarded-For")) } if clientIP == "" { - clientIP = c.RemoteIP().String() + clientIP, _, splitHostPortError := net.SplitHostPort(c.RemoteIP().String()) + if splitHostPortError == nil && isCorrectIP(clientIP) { + return clientIP + } + return "" + } + if isCorrectIP(clientIP) { + return clientIP } - return clientIP + return "" +} + +// isCorrectIP - return true if ip string is valid textual representation of an IP address, else returns false +func isCorrectIP(ip string) bool { + return net.ParseIP(ip) != nil } diff --git a/routebuildermdl/routebuilder_gin.go b/routebuildermdl/routebuilder_gin.go index f4ee41e6dd31596f1c5935e8b6206d151a936e82..528db5083682e7a51ca4359cfc4b692a17f7e29b 100644 --- a/routebuildermdl/routebuilder_gin.go +++ b/routebuildermdl/routebuilder_gin.go @@ -5,6 +5,7 @@ package routebuildermdl import ( "context" "io/ioutil" + "net" "net/http" "strings" @@ -192,8 +193,7 @@ func commonHandler(c *gin.Context, isRestricted, isRoleBased, heavyDataActivity // OpenHandler for /o func OpenHandler(c *gin.Context) { principal := servicebuildermdl.Principal{} - - // principal.ClientIP = c.Request.RemoteAddr + principal.ClientIP = getClientIP(c) commonHandler(c, false, false, false, principal) } @@ -205,7 +205,6 @@ func RestrictedHandler(c *gin.Context) { c.JSON(http.StatusExpectationFailed, extractError.Error()) return } - // pricipalObj.ClientIP = c.Request.RemoteAddr commonHandler(c, true, false, false, pricipalObj) } @@ -217,15 +216,13 @@ func RoleBasedHandler(c *gin.Context) { c.JSON(http.StatusExpectationFailed, extractError.Error()) return } - // pricipalObj.ClientIP = c.Request.RemoteAddr commonHandler(c, true, true, false, pricipalObj) } // HeavyOpenHandler for /o func HeavyOpenHandler(c *gin.Context) { principal := servicebuildermdl.Principal{} - - // principal.ClientIP = c.Request.RemoteAddr + principal.ClientIP = getClientIP(c) commonHandler(c, false, false, true, principal) } @@ -237,7 +234,6 @@ func HeavyRestrictedHandler(c *gin.Context) { c.JSON(http.StatusExpectationFailed, extractError.Error()) return } - // pricipalObj.ClientIP = c.Request.RemoteAddr commonHandler(c, true, false, true, pricipalObj) } @@ -249,7 +245,6 @@ func HeavyRoleBasedHandler(c *gin.Context) { c.JSON(http.StatusExpectationFailed, extractError.Error()) return } - // pricipalObj.ClientIP = c.Request.RemoteAddr commonHandler(c, true, true, true, pricipalObj) } @@ -286,7 +281,6 @@ func extractPricipalObject(c *gin.Context) (servicebuildermdl.Principal, error) } claim, decodeError := jwtmdl.DecodeToken(c.Request) if errormdl.CheckErr(decodeError) != nil { - // loggermdl.LogError(decodeError) return principal, errormdl.CheckErr(decodeError) } @@ -324,7 +318,19 @@ func getClientIP(c *gin.Context) string { clientIP = c.Request.Header.Get("X-Forwarded-For") } if clientIP == "" { - clientIP = c.Request.RemoteAddr + clientIP, _, splitHostPortError := net.SplitHostPort(c.Request.RemoteAddr) + if splitHostPortError == nil && isCorrectIP(clientIP) { + return clientIP + } + return "" + } + if isCorrectIP(clientIP) { + return clientIP } - return clientIP + return "" +} + +// isCorrectIP - return true if ip string is valid textual representation of an IP address, else returns false +func isCorrectIP(ip string) bool { + return net.ParseIP(ip) != nil }