improved Whoi parsing

This commit is contained in:
jomu
2016-08-31 22:55:56 +00:00
parent 74d2ea5fe2
commit 393b768f4f
5 changed files with 55 additions and 28 deletions

View File

@ -13,16 +13,21 @@ import org.slf4j.LoggerFactory;
*/
public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser {
/** logger object */
/**
* logger object
*/
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
/** information to be returned */
/**
* information to be returned
*/
private WhoisInformation whoisInformation;
@Override
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
whoisInformation = new WhoisInformation();
whoisInformation.setResponseString(whoisString);
if (whoisString.contains("# start")) { // multiple values
String multipleWhoisString = whoisString;
while (multipleWhoisString.contains("# start")) {
@ -33,11 +38,9 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
throw new WhoisException("Error while reading whois part elements. Reason: " + ex.getMessage(), ex);
}
analyse(whoisPartString);
multipleWhoisString = multipleWhoisString.substring(multipleWhoisString.indexOf("# end")+4);
multipleWhoisString = multipleWhoisString.substring(multipleWhoisString.indexOf("# end") + 4);
}
} else {
analyse(whoisString);
}
@ -53,7 +56,7 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
blocks = whoisString.split("\r\n\r\n");
} else {
blocks = whoisString.split("\n\n");
}
}
for (String block : blocks) {
analyseBlock(block);
}
@ -72,10 +75,12 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
if (startString.equals("%")) {
startString = block.substring(0, block.indexOf(" ", 2));
}
while (startString.startsWith("\n")) {
startString = startString.substring(1, startString.length());
}
Map<String, String> valueMap = getValueMap(block);
switch (startString) {
case "NetRange:":
@ -86,13 +91,13 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
// FIXME add CDIR notation support
String startIpAddress = ipAddresses[0];
String endIPAddress = ipAddresses[2];
whoisInformation.getNetwork().addAll(rangeToCidrList(startIpAddress, endIPAddress));
whoisInformation.addNetwork(rangeToCidrList(startIpAddress, endIPAddress));
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
} else {
throw new WhoisException("Cannot identify netrange value");
}
break;
case "OrgName:":
String orgName = getValue("OrgName:", valueMap);
String orgId = getValue("OrgId:", valueMap);
@ -108,7 +113,6 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
LOGGER.info("Networkinformation:" + whoisInformation.getNetworkInformation().toString());
break;
case "OrgAbuseHandle:":
// TODO add abuse handler
LOGGER.info("Skipping OrgAbuseHandle block");

View File

@ -1,24 +1,29 @@
package de.muehlencord.shared.network.whois;
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
import java.util.ArrayList;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author jomu
*/
public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisParser {
/** logger object */
/**
* logger object
*/
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
/** information to be returned */
/**
* information to be returned
*/
private WhoisInformation whoisInformation;
@Override
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
whoisInformation = new WhoisInformation();
whoisInformation.setResponseString(whoisString);
// split by blank lines to identify blocks
String[] blocks;
if (whoisString.contains("\r\n")) {
@ -54,11 +59,11 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
switch (startString) {
case "inetnum:":
String inetnum = getValue("inetnum:", valueMap);
String country = getValue("country:", valueMap);
String netname = getValue("descr:", valueMap);
String descr = getValue("netname:", valueMap);
String parent = getValue("parent:", valueMap);
if ((inetnum == null) || (country == null)) {
throw new WhoisException("Cannot identify inetnum and/or country value");
@ -75,6 +80,14 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
whoisInformation.addNetwork(inetnum);
}
/*
if (parent != null) {
whoisInformation.addRootNetwork(parent);
LOGGER.info("Root network by parent" + whoisInformation.getRootNetwork().toString());
break;
}
*/
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
whoisInformation.setNetworkInformation(new NetworkInformation(netname, descr, country));
@ -82,18 +95,10 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
case "route:":
// get information on level-2 network
String route = getValue("route:", valueMap);
/*
try {
route = StringUtil.getValueBetweenKeywords(block, "route:", "descr:");
} catch (ParseException ex) {
throw new WhoisException ("Error while reading route information: "+ex.getMessage(), ex);
}
*/
whoisInformation.addRootNetwork(route);
LOGGER.info("Root network " + whoisInformation.getRootNetwork().toString());
break;
case "role:":
// admin company of network server belongs to
LOGGER.info("Skipping role block");

View File

@ -32,6 +32,7 @@ public class Whois {
WHOIS_SERVERS.add("whois.ripe.net");
WHOIS_SERVERS.add("whois.afrinic.net");
WHOIS_SERVERS.add("whois.apnic.net");
WHOIS_SERVERS.add("whois.arin.net");
}
/**

View File

@ -22,6 +22,10 @@ public class WhoisInformation {
* network information of host
*/
private NetworkInformation networkInformation;
/**
* the whois response string
*/
private String responseString;
public WhoisInformation() {
network = new LinkedList<>();
@ -70,9 +74,9 @@ public class WhoisInformation {
public void setRootNetwork(List<String> rootNetwork) {
this.rootNetwork = new ArrayList<>(rootNetwork);
}
public void addRootNetwork (String rootNetwork) {
this.rootNetwork.add (rootNetwork);
public void addRootNetwork(String rootNetwork) {
this.rootNetwork.add(rootNetwork);
}
/**
@ -89,12 +93,25 @@ public class WhoisInformation {
this.networkInformation = networkInformation;
}
public String getResponseString() {
return responseString;
}
public void setResponseString(String responseString) {
this.responseString = responseString;
}
void validate() throws WhoisException {
if (network.isEmpty()) {
if (network == null || network.isEmpty()) {
throw new WhoisException("no network information found");
}
if (networkInformation == null) {
throw new WhoisException("network information not set");
}
if (networkInformation.getDescription().contains("AFRINIC-CIDR-BLOCK")) {
throw new WhoisException("Deletgate to AFRINIC found");
}
}
}

View File

@ -78,7 +78,7 @@ public class WhoisTest extends BaseTest {
@Test
public void testGeneric() throws WhoisException {
Whois whoIsClient = new Whois();
WhoisInformation info = whoIsClient.execute( "41.190.8.21");
WhoisInformation info = whoIsClient.execute( "85.185.91.5");
assertNotNull (info);
assertNotNull (info.getNetwork());
assertFalse (info.getNetwork().isEmpty());