improved Whoi parsing
This commit is contained in:
@ -13,14 +13,19 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser {
|
public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser {
|
||||||
|
|
||||||
/** logger object */
|
/**
|
||||||
|
* logger object
|
||||||
|
*/
|
||||||
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
|
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
|
||||||
/** information to be returned */
|
/**
|
||||||
|
* information to be returned
|
||||||
|
*/
|
||||||
private WhoisInformation whoisInformation;
|
private WhoisInformation whoisInformation;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
|
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
|
||||||
whoisInformation = new WhoisInformation();
|
whoisInformation = new WhoisInformation();
|
||||||
|
whoisInformation.setResponseString(whoisString);
|
||||||
if (whoisString.contains("# start")) { // multiple values
|
if (whoisString.contains("# start")) { // multiple values
|
||||||
|
|
||||||
String multipleWhoisString = whoisString;
|
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);
|
throw new WhoisException("Error while reading whois part elements. Reason: " + ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
analyse(whoisPartString);
|
analyse(whoisPartString);
|
||||||
multipleWhoisString = multipleWhoisString.substring(multipleWhoisString.indexOf("# end")+4);
|
multipleWhoisString = multipleWhoisString.substring(multipleWhoisString.indexOf("# end") + 4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
analyse(whoisString);
|
analyse(whoisString);
|
||||||
}
|
}
|
||||||
@ -72,10 +75,12 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
|
|||||||
if (startString.equals("%")) {
|
if (startString.equals("%")) {
|
||||||
startString = block.substring(0, block.indexOf(" ", 2));
|
startString = block.substring(0, block.indexOf(" ", 2));
|
||||||
}
|
}
|
||||||
|
while (startString.startsWith("\n")) {
|
||||||
|
startString = startString.substring(1, startString.length());
|
||||||
|
}
|
||||||
|
|
||||||
Map<String, String> valueMap = getValueMap(block);
|
Map<String, String> valueMap = getValueMap(block);
|
||||||
|
|
||||||
|
|
||||||
switch (startString) {
|
switch (startString) {
|
||||||
|
|
||||||
case "NetRange:":
|
case "NetRange:":
|
||||||
@ -86,7 +91,7 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
|
|||||||
// FIXME add CDIR notation support
|
// FIXME add CDIR notation support
|
||||||
String startIpAddress = ipAddresses[0];
|
String startIpAddress = ipAddresses[0];
|
||||||
String endIPAddress = ipAddresses[2];
|
String endIPAddress = ipAddresses[2];
|
||||||
whoisInformation.getNetwork().addAll(rangeToCidrList(startIpAddress, endIPAddress));
|
whoisInformation.addNetwork(rangeToCidrList(startIpAddress, endIPAddress));
|
||||||
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
|
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
|
||||||
} else {
|
} else {
|
||||||
throw new WhoisException("Cannot identify netrange value");
|
throw new WhoisException("Cannot identify netrange value");
|
||||||
@ -108,7 +113,6 @@ public class ArinWhoisParser extends AbstractWhoisParser implements WhoisParser
|
|||||||
LOGGER.info("Networkinformation:" + whoisInformation.getNetworkInformation().toString());
|
LOGGER.info("Networkinformation:" + whoisInformation.getNetworkInformation().toString());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "OrgAbuseHandle:":
|
case "OrgAbuseHandle:":
|
||||||
// TODO add abuse handler
|
// TODO add abuse handler
|
||||||
LOGGER.info("Skipping OrgAbuseHandle block");
|
LOGGER.info("Skipping OrgAbuseHandle block");
|
||||||
|
|||||||
@ -1,24 +1,29 @@
|
|||||||
package de.muehlencord.shared.network.whois;
|
package de.muehlencord.shared.network.whois;
|
||||||
|
|
||||||
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
|
import static de.muehlencord.shared.network.CidrTool.rangeToCidrList;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author jomu
|
* @author jomu
|
||||||
*/
|
*/
|
||||||
public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisParser {
|
public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisParser {
|
||||||
|
|
||||||
/** logger object */
|
/**
|
||||||
|
* logger object
|
||||||
|
*/
|
||||||
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
|
private final static Logger LOGGER = LoggerFactory.getLogger(DefaultWhoisParser.class);
|
||||||
/** information to be returned */
|
/**
|
||||||
|
* information to be returned
|
||||||
|
*/
|
||||||
private WhoisInformation whoisInformation;
|
private WhoisInformation whoisInformation;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
|
public WhoisInformation parseWhoIsString(String whoisString) throws WhoisException {
|
||||||
whoisInformation = new WhoisInformation();
|
whoisInformation = new WhoisInformation();
|
||||||
|
whoisInformation.setResponseString(whoisString);
|
||||||
// split by blank lines to identify blocks
|
// split by blank lines to identify blocks
|
||||||
String[] blocks;
|
String[] blocks;
|
||||||
if (whoisString.contains("\r\n")) {
|
if (whoisString.contains("\r\n")) {
|
||||||
@ -54,11 +59,11 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
|
|||||||
switch (startString) {
|
switch (startString) {
|
||||||
|
|
||||||
case "inetnum:":
|
case "inetnum:":
|
||||||
|
|
||||||
String inetnum = getValue("inetnum:", valueMap);
|
String inetnum = getValue("inetnum:", valueMap);
|
||||||
String country = getValue("country:", valueMap);
|
String country = getValue("country:", valueMap);
|
||||||
String netname = getValue("descr:", valueMap);
|
String netname = getValue("descr:", valueMap);
|
||||||
String descr = getValue("netname:", valueMap);
|
String descr = getValue("netname:", valueMap);
|
||||||
|
String parent = getValue("parent:", valueMap);
|
||||||
|
|
||||||
if ((inetnum == null) || (country == null)) {
|
if ((inetnum == null) || (country == null)) {
|
||||||
throw new WhoisException("Cannot identify inetnum and/or country value");
|
throw new WhoisException("Cannot identify inetnum and/or country value");
|
||||||
@ -75,6 +80,14 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
|
|||||||
whoisInformation.addNetwork(inetnum);
|
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());
|
LOGGER.info("Network:" + whoisInformation.getNetwork().toString());
|
||||||
whoisInformation.setNetworkInformation(new NetworkInformation(netname, descr, country));
|
whoisInformation.setNetworkInformation(new NetworkInformation(netname, descr, country));
|
||||||
|
|
||||||
@ -82,18 +95,10 @@ public class DefaultWhoisParser extends AbstractWhoisParser implements WhoisPars
|
|||||||
case "route:":
|
case "route:":
|
||||||
// get information on level-2 network
|
// get information on level-2 network
|
||||||
String route = getValue("route:", valueMap);
|
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);
|
whoisInformation.addRootNetwork(route);
|
||||||
LOGGER.info("Root network " + whoisInformation.getRootNetwork().toString());
|
LOGGER.info("Root network " + whoisInformation.getRootNetwork().toString());
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case "role:":
|
case "role:":
|
||||||
// admin company of network server belongs to
|
// admin company of network server belongs to
|
||||||
LOGGER.info("Skipping role block");
|
LOGGER.info("Skipping role block");
|
||||||
|
|||||||
@ -32,6 +32,7 @@ public class Whois {
|
|||||||
WHOIS_SERVERS.add("whois.ripe.net");
|
WHOIS_SERVERS.add("whois.ripe.net");
|
||||||
WHOIS_SERVERS.add("whois.afrinic.net");
|
WHOIS_SERVERS.add("whois.afrinic.net");
|
||||||
WHOIS_SERVERS.add("whois.apnic.net");
|
WHOIS_SERVERS.add("whois.apnic.net");
|
||||||
|
WHOIS_SERVERS.add("whois.arin.net");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -22,6 +22,10 @@ public class WhoisInformation {
|
|||||||
* network information of host
|
* network information of host
|
||||||
*/
|
*/
|
||||||
private NetworkInformation networkInformation;
|
private NetworkInformation networkInformation;
|
||||||
|
/**
|
||||||
|
* the whois response string
|
||||||
|
*/
|
||||||
|
private String responseString;
|
||||||
|
|
||||||
public WhoisInformation() {
|
public WhoisInformation() {
|
||||||
network = new LinkedList<>();
|
network = new LinkedList<>();
|
||||||
@ -71,8 +75,8 @@ public class WhoisInformation {
|
|||||||
this.rootNetwork = new ArrayList<>(rootNetwork);
|
this.rootNetwork = new ArrayList<>(rootNetwork);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addRootNetwork (String rootNetwork) {
|
public void addRootNetwork(String rootNetwork) {
|
||||||
this.rootNetwork.add (rootNetwork);
|
this.rootNetwork.add(rootNetwork);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -89,12 +93,25 @@ public class WhoisInformation {
|
|||||||
this.networkInformation = networkInformation;
|
this.networkInformation = networkInformation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getResponseString() {
|
||||||
|
return responseString;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setResponseString(String responseString) {
|
||||||
|
this.responseString = responseString;
|
||||||
|
}
|
||||||
|
|
||||||
void validate() throws WhoisException {
|
void validate() throws WhoisException {
|
||||||
if (network.isEmpty()) {
|
if (network == null || network.isEmpty()) {
|
||||||
throw new WhoisException("no network information found");
|
throw new WhoisException("no network information found");
|
||||||
}
|
}
|
||||||
if (networkInformation == null) {
|
if (networkInformation == null) {
|
||||||
throw new WhoisException("network information not set");
|
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
|
@Test
|
||||||
public void testGeneric() throws WhoisException {
|
public void testGeneric() throws WhoisException {
|
||||||
Whois whoIsClient = new Whois();
|
Whois whoIsClient = new Whois();
|
||||||
WhoisInformation info = whoIsClient.execute( "41.190.8.21");
|
WhoisInformation info = whoIsClient.execute( "85.185.91.5");
|
||||||
assertNotNull (info);
|
assertNotNull (info);
|
||||||
assertNotNull (info.getNetwork());
|
assertNotNull (info.getNetwork());
|
||||||
assertFalse (info.getNetwork().isEmpty());
|
assertFalse (info.getNetwork().isEmpty());
|
||||||
|
|||||||
Reference in New Issue
Block a user