retrieve whois information and parse again without connection to server

This commit is contained in:
2020-04-22 08:27:10 +02:00
parent e12127f6dd
commit 0e21c9baf7

View File

@ -27,91 +27,108 @@ 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; private WhoisClient whoisClient;
/** /**
* the complete output * the complete output
*/ */
String whoisString = null; private String whoisString = null;
private static final List<String> WHOIS_SERVERS; private static final List<String> WHOIS_SERVERS;
static { static {
WHOIS_SERVERS = new ArrayList<>(); WHOIS_SERVERS = new ArrayList<>();
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"); WHOIS_SERVERS.add("whois.arin.net");
}
/**
* creates a new instance of the whoisClient client
*/
public Whois() {
whoisClient = new WhoisClient();
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) {
* creates a new instance of the whoisClient client throw new WhoisException("Cannot identify network");
*/ } else {
public Whois() { return info;
whoisClient = new WhoisClient(); }
whoisClient.setDefaultTimeout(60000); }
/**
* 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 + ";
} }
public WhoisInformation execute(String ip) throws WhoisException { // get whoisClient information
try {
whoisClient.connect(whoIsServer);
whoisString = whoisClient.query(parameter + ip);
whoisClient.disconnect();
} catch (Exception ex) {
throw new WhoisException("Error while reading whois data from " + whoIsServer + ". Reason: " + ex.getMessage(), ex);
}
return parseWhoisInformation (whoisString);
Iterator<String> it = WHOIS_SERVERS.iterator();
WhoisInformation info = null; }
while (it.hasNext() && ((info == null) || (info.getNetwork() == null))) {
String server = it.next(); public static WhoisInformation parse(String whoisInformation) throws WhoisException {
try { return parseWhoisInformation (whoisInformation);
info = execute(server, ip); }
} catch (WhoisException ex) {
info = null; private static WhoisInformation parseWhoisInformation (String whoisInformation) throws WhoisException {
} // identify RIR and select correct parser
} WhoisParser parser;
if (whoisInformation.toLowerCase(Locale.US).contains("arin.net")) {
if (info == null) { parser = new ArinWhoisParser();
throw new WhoisException("Cannot identify network"); } else {
} else { parser = new DefaultWhoisParser();
return info;
}
} }
/** return parser.parseWhoIsString(whoisInformation);
* 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")) { * returns the output received from the whois server
parameter = "n + "; *
} * @return the output received from the whois server
*/
public String getWhoisString() {
return whoisString;
}
// get whoisClient information
try {
whoisClient.connect(whoIsServer);
whoisString = whoisClient.query(parameter + ip);
whoisClient.disconnect();
} catch (Exception ex) {
throw new WhoisException("Error while reading whois data from " + whoIsServer + ". Reason: " + ex.getMessage(), ex);
}
// identify RIR and select correct parser
WhoisParser parser;
if (whoisString.toLowerCase(Locale.US).contains("arin.net")) {
parser = new ArinWhoisParser();
} else {
parser = new DefaultWhoisParser();
}
return parser.parseWhoIsString(whoisString);
}
} }