Whois improvements

This commit is contained in:
M66B 2022-09-04 11:55:12 +02:00
parent d1dcdbfb67
commit 596e482f56
2 changed files with 15 additions and 9 deletions

View File

@ -461,7 +461,7 @@ public class FragmentDialogOpenLink extends FragmentDialogBase {
ibInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("https://en.wikipedia.org/wiki/WHOIS");
Uri uri = Uri.parse(Whois.WHOIS_INFO);
Helper.view(v.getContext(), uri, true);
}
});

View File

@ -23,11 +23,15 @@ import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
public class Whois {
// https://datatracker.ietf.org/doc/html/rfc812
// https://datatracker.ietf.org/doc/html/rfc3912
static final String WHOIS_INFO = "https://en.wikipedia.org/wiki/WHOIS";
private static final int WHOIS_PORT = 43;
private static final int WHOIS_TIMEOUT = 15 * 1000; // milliseconds
private static final String WHOIS_IANA = "whois.iana.org";
private static final String WHOIS_PREFIX = "whois:";
static String get(String domain) throws IOException {
return get(domain, getServer(domain), WHOIS_PORT);
@ -41,19 +45,21 @@ public class Whois {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), WHOIS_TIMEOUT);
try {
socket.getOutputStream().write((domain + "\r\n").getBytes());
return host + ":" + port + "\n" + Helper.readStream(socket.getInputStream());
byte[] request = (domain + "\r\n").getBytes(StandardCharsets.ISO_8859_1);
socket.getOutputStream().write(request);
String response = Helper.readStream(socket.getInputStream(), StandardCharsets.ISO_8859_1);
return host + ":" + port + "\n" + response;
} finally {
socket.close();
}
}
private static String getServer(String domain) throws IOException {
String who = get(domain, "whois.iana.org");
for (String w : who.split("\\r?\\n"))
if (w.startsWith("whois:"))
return w.substring(6).trim();
Log.w(who);
String iana = get(domain, WHOIS_IANA);
for (String line : iana.split("\\r?\\n"))
if (line.startsWith(WHOIS_PREFIX))
return line.substring(WHOIS_PREFIX.length()).trim();
Log.w(iana);
throw new UnknownHostException("whois server unknown " + domain);
}
}