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 {
/**
* logger object
*/
// private static final Logger LOGGER = LogManager.getLogger(Whois.class);
/**
* whoisClient client from commons-net package to execute commands with
*/
WhoisClient whoisClient;
/**
* the complete output
*/
String whoisString = null;
/**
* logger object
*/
// private static final Logger LOGGER = LogManager.getLogger(Whois.class);
/**
* whoisClient client from commons-net package to execute commands with
*/
private WhoisClient whoisClient;
/**
* the complete output
*/
private String whoisString = null;
private static final List<String> WHOIS_SERVERS;
private static final List<String> WHOIS_SERVERS;
static {
WHOIS_SERVERS = new ArrayList<>();
WHOIS_SERVERS.add("whois.ripe.net");
WHOIS_SERVERS.add("whois.afrinic.net");
WHOIS_SERVERS.add("whois.apnic.net");
WHOIS_SERVERS.add("whois.arin.net");
static {
WHOIS_SERVERS = new ArrayList<>();
WHOIS_SERVERS.add("whois.ripe.net");
WHOIS_SERVERS.add("whois.afrinic.net");
WHOIS_SERVERS.add("whois.apnic.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;
}
}
/**
* creates a new instance of the whoisClient client
*/
public Whois() {
whoisClient = new WhoisClient();
whoisClient.setDefaultTimeout(60000);
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
*
* @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();
try {
info = execute(server, ip);
} catch (WhoisException ex) {
info = null;
}
}
if (info == null) {
throw new WhoisException("Cannot identify network");
} else {
return info;
}
}
public static WhoisInformation parse(String whoisInformation) throws WhoisException {
return parseWhoisInformation (whoisInformation);
}
private static WhoisInformation parseWhoisInformation (String whoisInformation) throws WhoisException {
// identify RIR and select correct parser
WhoisParser parser;
if (whoisInformation.toLowerCase(Locale.US).contains("arin.net")) {
parser = new ArinWhoisParser();
} else {
parser = new DefaultWhoisParser();
}
/**
* 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 {
return parser.parseWhoIsString(whoisInformation);
}
String parameter = "";
if (whoIsServer.equals("whois.arin.net")) {
parameter = "n + ";
}
/**
* returns the output received from the whois server
*
* @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);
}
}