From faff8cea8d93c22f6caeb38515689737a1727d33 Mon Sep 17 00:00:00 2001 From: jomu Date: Sun, 28 Aug 2016 15:55:18 +0000 Subject: [PATCH] added support for automatic whois server detection --- .../shared/network/whois/Whois.java | 54 ++++++++++++++++--- .../shared/network/whois/WhoisTest.java | 2 +- 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/network/src/main/java/de/muehlencord/shared/network/whois/Whois.java b/network/src/main/java/de/muehlencord/shared/network/whois/Whois.java index 43bd46c..159da62 100644 --- a/network/src/main/java/de/muehlencord/shared/network/whois/Whois.java +++ b/network/src/main/java/de/muehlencord/shared/network/whois/Whois.java @@ -1,5 +1,8 @@ package de.muehlencord.shared.network.whois; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; import java.util.Locale; import org.apache.commons.net.whois.WhoisClient; @@ -9,30 +12,65 @@ import org.apache.commons.net.whois.WhoisClient; */ public class Whois { - /** logger object */ + /** + * logger object + */ // 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; - /** the complete output */ + /** + * the complete output + */ String whoisString = null; + private static final List 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 */ public Whois() { whoisClient = new WhoisClient(); whoisClient.setDefaultTimeout(60000); - } + } + + public WhoisInformation execute(String ip) throws WhoisException { + + Iterator 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 ip the ip to query whoisClient data for * @return whoisInformation parsed from whoisClient string * @throws WhoisException if the parsing fails */ public WhoisInformation execute(String whoIsServer, String ip) throws WhoisException { - + String parameter = ""; if (whoIsServer.equals("whois.arin.net")) { parameter = "n + "; @@ -56,6 +94,6 @@ public class Whois { } return parser.parseWhoIsString(whoisString); - + } } diff --git a/network/src/test/java/de/muehlencord/shared/network/whois/WhoisTest.java b/network/src/test/java/de/muehlencord/shared/network/whois/WhoisTest.java index dc07101..17028dc 100644 --- a/network/src/test/java/de/muehlencord/shared/network/whois/WhoisTest.java +++ b/network/src/test/java/de/muehlencord/shared/network/whois/WhoisTest.java @@ -94,7 +94,7 @@ public class WhoisTest extends BaseTest { public void testStepWise() throws WhoisException { Whois whoIsClient = new Whois(); // 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")); // whoIsClient.execute ("whois.ripe.net", "212.204.60.0"); // whoIsClient.execute ("whois.ripe.net", "212.0.0.0/8");