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/>.
|
|
|
|
|
2023-01-01 07:52:55 +00:00
|
|
|
Copyright 2018-2023 by Marcel Bokhorst (M66B)
|
2022-09-04 06:44:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
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 09:55:12 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2022-09-04 06:44:09 +00:00
|
|
|
|
|
|
|
public class Whois {
|
2022-09-04 09:55:12 +00:00
|
|
|
// https://datatracker.ietf.org/doc/html/rfc3912
|
|
|
|
static final String WHOIS_INFO = "https://en.wikipedia.org/wiki/WHOIS";
|
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 09:55:12 +00:00
|
|
|
private static final String WHOIS_IANA = "whois.iana.org";
|
|
|
|
private static final String WHOIS_PREFIX = "whois:";
|
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 {
|
2023-12-02 11:39:00 +00:00
|
|
|
Socket socket = ConnectionHelper.getSocket(host, port);
|
2022-09-04 08:44:34 +00:00
|
|
|
socket.connect(new InetSocketAddress(host, port), WHOIS_TIMEOUT);
|
|
|
|
try {
|
2022-12-02 17:12:09 +00:00
|
|
|
byte[] request = (domain + "\r\n").getBytes(StandardCharsets.UTF_8);
|
2022-09-04 09:55:12 +00:00
|
|
|
socket.getOutputStream().write(request);
|
2022-12-02 17:12:09 +00:00
|
|
|
String response = Helper.readStream(socket.getInputStream(), StandardCharsets.UTF_8);
|
2022-09-05 19:55:31 +00:00
|
|
|
return host + ":" + port + "\n\n" + response;
|
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 {
|
2022-09-04 09:55:12 +00:00
|
|
|
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);
|
2022-09-04 08:09:05 +00:00
|
|
|
throw new UnknownHostException("whois server unknown " + domain);
|
|
|
|
}
|
2022-09-04 06:44:09 +00:00
|
|
|
}
|