Skip to content

Instantly share code, notes, and snippets.

@ivarprudnikov
Created July 16, 2013 11:00
Show Gist options
  • Save ivarprudnikov/6007758 to your computer and use it in GitHub Desktop.
Save ivarprudnikov/6007758 to your computer and use it in GitHub Desktop.
Grails check if remote is localhost
def isLocalhost(String ip) {
def localIps = []
def networkInterfaces
try {
networkInterfaces = NetworkInterface.getNetworkInterfaces()
networkInterfaces = networkInterfaces?.toList()
} catch(SocketException e) {
}
if (networkInterfaces) {
networkInterfaces.each { ni ->
def interfaceIps = ni.getInetAddresses()
interfaceIps = interfaceIps?.toList()
if (interfaceIps)
interfaceIps.each { localIps << it.getHostAddress() }
}
}
// if 0:0:0:0:0:0:0:1%0
// https://issues.apache.org/jira/browse/TAP5-1836
if (ip == '0:0:0:0:0:0:0:1%0' || localIps.contains(ip))
return true
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment