Ip parse and port remove logic added

Merged Vikram Ingawale requested to merge vi_ipaddressFixes into devbranch
Compare and
2 files
+ 34
21
Preferences
File browser
Compare changes
@@ -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
}