Refactoring

This commit is contained in:
M66B 2022-09-04 08:41:43 +02:00
parent b6030ecd83
commit c44bac2238
2 changed files with 23 additions and 22 deletions

View File

@ -25,7 +25,6 @@ import android.net.Uri;
import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.core.net.MailTo;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -44,28 +43,18 @@ public class IPInfo {
private final static int FETCH_TIMEOUT = 15 * 1000; // milliseconds
static Pair<InetAddress, Organization> getOrganization(@NonNull Uri uri, Context context) throws IOException, ParseException {
if ("mailto".equalsIgnoreCase(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString());
String domain = UriHelper.getEmailDomain(email.getTo());
if (domain == null)
throw new UnknownHostException();
//InetAddress address = DnsHelper.lookupMx(context, domain);
//if (address == null)
// throw new UnknownHostException();
InetAddress address = InetAddress.getByName(domain);
return new Pair<>(address, getOrganization(address, context));
} else {
String host = uri.getHost();
if (host == null)
throw new UnknownHostException();
try {
host = IDN.toASCII(host, IDN.ALLOW_UNASSIGNED);
} catch (Throwable ex) {
Log.i(ex);
}
InetAddress address = InetAddress.getByName(host);
return new Pair<>(address, getOrganization(address, context));
String host = UriHelper.getHost(uri);
if (host == null)
throw new UnknownHostException();
try {
host = IDN.toASCII(host, IDN.ALLOW_UNASSIGNED);
} catch (Throwable ex) {
Log.i(ex);
}
InetAddress address = InetAddress.getByName(host);
return new Pair<>(address, getOrganization(address, context));
}
static Organization getOrganization(InetAddress address, Context context) throws IOException {

View File

@ -23,16 +23,20 @@ import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Base64;
import android.util.Pair;
import android.webkit.URLUtil;
import androidx.annotation.NonNull;
import androidx.core.net.MailTo;
import androidx.core.util.PatternsCompat;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.IDN;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
@ -422,6 +426,14 @@ public class UriHelper {
return uri;
}
static String getHost(Uri uri) {
if ("mailto".equalsIgnoreCase(uri.getScheme())) {
MailTo email = MailTo.parse(uri.toString());
return getEmailDomain(email.getTo());
} else
return uri.getHost();
}
static void test(Context context) {
String[] hosts = new String[]{
"child.parent.example.com", "parent.example.com", "example.com", "com",