FairEmail/app/src/main/java/eu/faircode/email/Whois.java

60 lines
2.1 KiB
Java
Raw Normal View History

2022-09-04 06:44:09 +00:00
package eu.faircode.email;
/*
This file is part of FairEmail.
FairEmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FairEmail is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
Copyright 2018-2022 by Marcel Bokhorst (M66B)
*/
import java.io.IOException;
2022-09-04 08:44:34 +00:00
import java.net.InetSocketAddress;
2022-09-04 06:44:09 +00:00
import java.net.Socket;
2022-09-04 08:09:05 +00:00
import java.net.UnknownHostException;
2022-09-04 06:44:09 +00:00
public class Whois {
2022-09-04 09:40:20 +00:00
// https://datatracker.ietf.org/doc/html/rfc812
2022-09-04 06:44:09 +00:00
private static final int WHOIS_PORT = 43;
2022-09-04 08:44:34 +00:00
private static final int WHOIS_TIMEOUT = 15 * 1000; // milliseconds
2022-09-04 06:44:09 +00:00
static String get(String domain) throws IOException {
2022-09-04 08:09:05 +00:00
return get(domain, getServer(domain), WHOIS_PORT);
2022-09-04 06:44:09 +00:00
}
static String get(String domain, String host) throws IOException {
return get(domain, host, WHOIS_PORT);
}
static String get(String domain, String host, int port) throws IOException {
2022-09-04 08:44:34 +00:00
Socket socket = new Socket();
socket.connect(new InetSocketAddress(host, port), WHOIS_TIMEOUT);
try {
2022-09-04 09:40:20 +00:00
socket.getOutputStream().write((domain + "\r\n").getBytes());
2022-09-04 08:09:05 +00:00
return host + ":" + port + "\n" + Helper.readStream(socket.getInputStream());
2022-09-04 08:44:34 +00:00
} finally {
socket.close();
2022-09-04 06:44:09 +00:00
}
}
2022-09-04 08:09:05 +00:00
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);
throw new UnknownHostException("whois server unknown " + domain);
}
2022-09-04 06:44:09 +00:00
}