added support for automatic whois server detection

This commit is contained in:
jomu
2016-08-28 15:55:18 +00:00
parent 5eb53f9eae
commit faff8cea8d
2 changed files with 47 additions and 9 deletions

View File

@ -1,5 +1,8 @@
package de.muehlencord.shared.network.whois; package de.muehlencord.shared.network.whois;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import org.apache.commons.net.whois.WhoisClient; import org.apache.commons.net.whois.WhoisClient;
@ -9,13 +12,26 @@ import org.apache.commons.net.whois.WhoisClient;
*/ */
public class Whois { public class Whois {
/** logger object */ /**
* logger object
*/
// private static final Logger LOGGER = LogManager.getLogger(Whois.class); // private static final Logger LOGGER = LogManager.getLogger(Whois.class);
/** whoisClient client from commons-net package to execute commands with */ /**
* whoisClient client from commons-net package to execute commands with
*/
WhoisClient whoisClient; WhoisClient whoisClient;
/** the complete output */ /**
* the complete output
*/
String whoisString = null; String whoisString = null;
private static final List<String> WHOIS_SERVERS;
static {
WHOIS_SERVERS = new ArrayList<>();
WHOIS_SERVERS.add("whois.ripe.net");
}
/** /**
* creates a new instance of the whoisClient client * creates a new instance of the whoisClient client
*/ */
@ -24,8 +40,30 @@ public class Whois {
whoisClient.setDefaultTimeout(60000); whoisClient.setDefaultTimeout(60000);
} }
public WhoisInformation execute(String ip) throws WhoisException {
Iterator<String> it = WHOIS_SERVERS.iterator();
WhoisInformation info = null;
while (it.hasNext() && ((info == null) || (info.getNetwork() == null))) {
String server = it.next();
try {
info = execute(server, ip);
} catch (WhoisException ex) {
info = null;
}
}
if (info == null) {
throw new WhoisException("Cannot identify network");
} else {
return info;
}
}
/** /**
* reads and parses the inforomation for the given ip address from the given whoisserver * reads and parses the inforomation for the given ip address from the given
* whoisserver
*
* @param whoIsServer the whoisClient server to use * @param whoIsServer the whoisClient server to use
* @param ip the ip to query whoisClient data for * @param ip the ip to query whoisClient data for
* @return whoisInformation parsed from whoisClient string * @return whoisInformation parsed from whoisClient string

View File

@ -94,7 +94,7 @@ public class WhoisTest extends BaseTest {
public void testStepWise() throws WhoisException { public void testStepWise() throws WhoisException {
Whois whoIsClient = new Whois(); Whois whoIsClient = new Whois();
// whoIsClient.execute ("whois.ripe.net", "88.198.181.181"); // whoIsClient.execute ("whois.ripe.net", "88.198.181.181");
WhoisInformation whoisInformation = whoIsClient.execute ("whois.ripe.net", "212.204.60.1"); WhoisInformation whoisInformation = whoIsClient.execute ("212.204.60.1");
assertTrue ("network", whoisInformation.getNetwork().contains("212.204.60.0/24")); assertTrue ("network", whoisInformation.getNetwork().contains("212.204.60.0/24"));
// whoIsClient.execute ("whois.ripe.net", "212.204.60.0"); // whoIsClient.execute ("whois.ripe.net", "212.204.60.0");
// whoIsClient.execute ("whois.ripe.net", "212.0.0.0/8"); // whoIsClient.execute ("whois.ripe.net", "212.0.0.0/8");