Clean up hosts implementation

This commit is contained in:
NekoInverter committed 2021-01-02 16:07:34 +08:00
1 parent 8513a78c01
commit 459d94a37f
2 files changed
+25 -31

No files matched your search

+10 -10
View File
@@ -35,7 +35,6 @@ import com.hippo.util.SqlUtils;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Hosts {
@@ -118,13 +117,14 @@ public class Hosts {
}
/**
* Returns true if the ip is valid.
* Returns true if ips are valid.
*/
public static boolean isValidIp(String ip_list) {
if (ip_list == null) {
public static boolean isValidIp(String ips) {
if (ips == null) {
return false;
}
for (String ip : ip_list.split("\\+")) {
String[] list = ips.split("\\+");
for (String ip : list) {
if (parseV4(ip) == null && parseV6(ip) == null) {
return false;
}
@@ -279,12 +279,12 @@ public class Hosts {
}
try (Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[]{host})) {
if (cursor.moveToNext()) {
String[] ip_l = SqlUtils.getString(cursor, COLUMN_IP, null).split("\\+");
InetAddress[] addr_l = new InetAddress[ip_l.length];
for (int i = 0; i < ip_l.length; i++) {
addr_l[i] = toInetAddress(host, ip_l[i]);
String[] ips = SqlUtils.getString(cursor, COLUMN_IP, null).split("\\+");
List<InetAddress> addresses = new ArrayList<>();
for (String ip: ips) {
addresses.add(toInetAddress(host, ip));
}
return Arrays.asList(addr_l);
return addresses;
} else {
return null;
}
@@ -30,7 +30,7 @@ import com.hippo.ehviewer.Settings;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -46,13 +46,15 @@ public class EhDns implements Dns {
static {
Map<String, List<InetAddress>> map = new HashMap<>();
put(map, "exhentai.org", "178.175.129.252+178.175.128.252+178.175.132.22+178.175.129.254");
put(map, "e-hentai.org", "104.20.26.25");
put(map, "repo.e-hentai.org", "94.100.28.57");
put(map, "e-hentai.org", "104.20.26.25", "104.20.26.25");
put(map, "exhentai.org", "178.175.128.252", "178.175.129.252", "178.175.129.254", "178.175.128.254",
"178.175.132.20", "178.175.132.22");
put(map, "repo.e-hentai.org", "94.100.28.57", "94.100.29.73");
put(map, "forums.e-hentai.org", "94.100.18.243");
put(map, "ehgt.org", "37.48.89.44+178.162.139.24+178.162.140.212+81.171.10.48");
put(map, "ul.ehgt.org", "94.100.24.82+94.100.24.72");
put(map, "raw.githubusercontent.com", "151.101.0.133+151.101.64.133+151.101.128.133+151.101.192.133");
put(map, "ehgt.org", "37.48.89.44", "81.171.10.48", "178.162.139.24", "178.162.140.212"
, "2001:1af8:4700:a062:8::47de", "2001:1af8:4700:a062:9::47de", "2001:1af8:4700:a0c9:4::47de", "2001:1af8:4700:a0c9:3::47de");
put(map, "ul.ehgt.org", "94.100.24.82", "94.100.24.72");
put(map, "raw.githubusercontent.com", "151.101.0.133", "151.101.64.133", "151.101.128.133", "151.101.192.133");
builtInHosts = map;
}
@@ -80,13 +82,12 @@ public class EhDns implements Dns {
dnsOverHttps = builder.post(true).build();
}
private static void put(Map<String, List<InetAddress>> map, String host, String ips) {
String[] ipList = ips.split("\\+");
InetAddress[] addresses = new InetAddress[ipList.length];
for (int i = 0; i < ipList.length; i++) {
addresses[i] = Hosts.toInetAddress(host, ipList[i]);
private static void put(Map<String, List<InetAddress>> map, String host, String... ips) {
List<InetAddress> addresses = new ArrayList<>();
for (String ip : ips) {
addresses.add(Hosts.toInetAddress(host, ip));
}
map.put(host, Arrays.asList(addresses));
map.put(host, addresses);
}
@NonNull
@@ -108,13 +109,6 @@ public class EhDns implements Dns {
return inetAddresses;
}
}
try {
return Arrays.asList(InetAddress.getAllByName(hostname));
} catch (NullPointerException e) {
UnknownHostException unknownHostException =
new UnknownHostException("Broken system behaviour for dns lookup of " + hostname);
unknownHostException.initCause(e);
throw unknownHostException;
}
return SYSTEM.lookup(hostname);
}
}