improved Whoi parsing
This commit is contained in:
@ -13,14 +13,19 @@ 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;
|
||||
@ -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);
|
||||
}
|
||||
@ -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,7 +91,7 @@ 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");
|
||||
@ -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");
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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<>();
|
||||
@ -71,8 +75,8 @@ public class WhoisInformation {
|
||||
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");
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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());
|
||||
|
||||
Reference in New Issue
Block a user