org.ehoffman.resources.NetworkUtils Maven / Gradle / Ivy
The newest version!
package org.ehoffman.resources;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Hits external web sites to determine the executors public IP address.
*
* @author rex
*/
public final class NetworkUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(NetworkUtils.class);
private NetworkUtils() {
}
private static final String ALL_CHARACTERS = "(.*)";
private static final String GOOGLE_MY_IP = "http://www.google.com/search?q=my+ip";
static final Map URL_REGEX_FOR_IP = Collections.unmodifiableMap(new HashMap() {
private static final long serialVersionUID = 1L;
{
//put(GOOGLE_MY_IP, "(?:)(\\d{2,3}\\.\\d{2,3}\\.\\d{2,3}\\.\\d{2,3})(?:)");
put("http://checkip.amazonaws.com/", ALL_CHARACTERS);
put("http://ifconfig.me/ip", ALL_CHARACTERS);
put("http://wtfismyip.com/text", ALL_CHARACTERS);
}
});
private static Logger logger = LoggerFactory.getLogger(NetworkUtils.class);
/**
* Call {@link NetworkUtils#getMyPublicIp} instead
*
* @param urlString
* a string like http://www.google.com/search?q=my+ip or http://ifconfig.me/ip
* @param regex
* a regex matcher with the first matching grouping containing only the ip, like (?:
* )(\\d{2,3}\\.\\d{2,3}\\.\\d{2,3}\\.\\d{2,3})(?:)
* @return the ip address as a String
* @throws IOException
*/
public static String getIp(final String urlString, final String regex) throws IOException {
final Pattern pattern = Pattern.compile(regex);
final URL url = new URL(urlString);
final URLConnection connection = url.openConnection();
connection.addRequestProperty("Protocol", "Http/1.1");
connection.addRequestProperty("Referer", GOOGLE_MY_IP);
connection.addRequestProperty("Connection", "keep-alive");
connection.addRequestProperty("Keep-Alive", "5000");
connection.addRequestProperty("Accept-Language", "en-us,en");
connection.addRequestProperty("User-Agent",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:17.0) Gecko/20100101 Firefox/17.0.1");
try (InputStream stream = connection.getInputStream()) {
final String page = new String(ResourceUtils.toByteArray(stream), Charset.forName("utf8"));
logger.debug("Page contents are: " + page);
final Matcher matcher = pattern.matcher(page);
matcher.find();
try {
return matcher.group(1);
} catch (final IllegalStateException e) {
return null;
}
}
}
/**
* Uses two sources, google and ipconfig.me.... tries google first, falls back to ipconfig.me
*
* @return Your public ip address as a String, you must have an Internet connection
*/
public static String getMyPublicIp() {
for (final Map.Entry urlRegex : URL_REGEX_FOR_IP.entrySet()) {
try {
final String ip = getIp(urlRegex.getKey(), urlRegex.getValue()).trim();
if (ip != null && ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
LOGGER.debug("Pull ip from " + urlRegex.getKey());
return ip;
}
} catch (final IOException ioe) {
LOGGER.debug("Error trying to pull ip from " + urlRegex.getKey(), ioe);
}
}
return null;
}
/**
* Uses two sources, google and ipconfig.me.... tries google first, falls back to ipconfig.me
*
* @return Your public ip address as a String, you must have an Internet connection
*/
public static String getMyPublicIpAsCidr() {
final String output = getMyPublicIp();
return output + "/32";
}
public static boolean verifyAccess(final URL location) {
try (InputStream stream = location.openStream()) {
stream.read();
return true;
} catch (final IOException e) {
return false;
}
}
}