Created
July 16, 2013 11:00
-
-
Save ivarprudnikov/6007758 to your computer and use it in GitHub Desktop.
Grails check if remote is localhost
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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