From 5236e634c8b52a744386343645273b5868878ccd Mon Sep 17 00:00:00 2001 From: NekoInverter <7741672-NekoInverter@users.noreply.gitlab.com> Date: Fri, 18 Dec 2020 14:24:08 +0800 Subject: [PATCH] Enhance hosts --- .../main/java/com/hippo/ehviewer/Hosts.java | 49 +++++++++---------- .../java/com/hippo/ehviewer/client/EhDns.java | 40 +++++++-------- app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-ko/strings.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values-zh-rHK/strings.xml | 2 +- app/src/main/res/values-zh-rTW/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 11 files changed, 51 insertions(+), 56 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/Hosts.java b/app/src/main/java/com/hippo/ehviewer/Hosts.java index 880235921..58818a87e 100644 --- a/app/src/main/java/com/hippo/ehviewer/Hosts.java +++ b/app/src/main/java/com/hippo/ehviewer/Hosts.java @@ -35,6 +35,7 @@ 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 { @@ -46,11 +47,10 @@ public class Hosts { private static final int DB_VERSION = VERSION_1; - private final SQLiteOpenHelper helper; private final SQLiteDatabase db; public Hosts(Context context, String name) { - helper = new MSQLiteBuilder() + SQLiteOpenHelper helper = new MSQLiteBuilder() .version(VERSION_1) .createTable(TABLE_HOSTS) .insertColumn(TABLE_HOSTS, COLUMN_HOST, String.class) @@ -120,8 +120,16 @@ public class Hosts { /** * Returns true if the ip is valid. */ - public static boolean isValidIp(String ip) { - return ip != null && (parseV4(ip) != null || parseV6(ip) != null); + public static boolean isValidIp(String ip_list) { + if (ip_list == null) { + return false; + } + for (String ip: ip_list.split("\\+")) { + if (parseV4(ip) == null && parseV6(ip) == null) { + return false; + } + } + return true; } // org.xbill.DNS.Address.parseV4 @@ -265,30 +273,27 @@ public class Hosts { * Gets a InetAddress with the host. */ @Nullable - public InetAddress get(String host) { + public List get(String host) { if (!isValidHost(host)) { return null; } - - Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[]{host}); - try { + try (Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[]{host})) { if (cursor.moveToNext()) { - String ip = SqlUtils.getString(cursor, COLUMN_IP, null); - return toInetAddress(host, ip); + 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]); + } + return Arrays.asList(addr_l); } else { return null; } - } finally { - cursor.close(); } } private boolean contains(String host) { - Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[]{host}); - try { + try (Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[]{host})) { return cursor.moveToNext(); - } finally { - cursor.close(); } } @@ -326,23 +331,13 @@ public class Hosts { public List> getAll() { List> result = new ArrayList<>(); - Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); - try { + try (Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null)) { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); - - InetAddress inetAddress = toInetAddress(host, ip); - if (inetAddress == null) { - continue; - } - result.add(new Pair<>(host, ip)); } - } finally { - cursor.close(); } - return result; } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhDns.java b/app/src/main/java/com/hippo/ehviewer/client/EhDns.java index 2191b98ae..9e270003b 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhDns.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhDns.java @@ -31,7 +31,6 @@ import com.hippo.ehviewer.Settings; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Arrays; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -43,15 +42,16 @@ import okhttp3.dnsoverhttps.DnsOverHttps; public class EhDns implements Dns { - private static final Map builtInHosts; + private static final Map> builtInHosts; static { - Map map = new HashMap<>(); + Map> 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.29.73"); + put(map, "repo.e-hentai.org", "94.100.28.57"); put(map, "forums.e-hentai.org", "94.100.18.243"); - put(map, "ehgt.org", "178.162.139.24"); - put(map, "ul.ehgt.org", "94.100.24.82"); + 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"); builtInHosts = map; } @@ -79,34 +79,34 @@ public class EhDns implements Dns { dnsOverHttps = builder.post(true).build(); } - private static void put(Map map, String host, String ip) { - InetAddress address = Hosts.toInetAddress(host, ip); - if (address != null) { - map.put(host, address); + private static void put(Map> 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]); } + map.put(host, Arrays.asList(addresses)); } @NonNull @Override public List lookup(@NonNull String hostname) throws UnknownHostException { - InetAddress inetAddress = hosts.get(hostname); - if (inetAddress != null) { - return Collections.singletonList(inetAddress); + List inetAddresses = hosts.get(hostname); + if (inetAddresses != null) { + return inetAddresses; } - if (Settings.getBuiltInHosts()) { - inetAddress = builtInHosts.get(hostname); - if (inetAddress != null) { - return Collections.singletonList(inetAddress); + inetAddresses = builtInHosts.get(hostname); + if (inetAddresses != null) { + return inetAddresses; } } if (Settings.getDoH()) { - List inetAddresses = dnsOverHttps.lookup(hostname); - if (inetAddresses != null && inetAddresses.size() > 0) { + inetAddresses = dnsOverHttps.lookup(hostname); + if (inetAddresses.size() > 0) { return inetAddresses; } } - try { return Arrays.asList(InetAddress.getAllByName(hostname)); } catch (NullPointerException e) { diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 80f093e98..e040bc032 100755 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -293,7 +293,7 @@ Angepasstes hosts.txt hosts.txt Host - IP + IP1(+IP2+IP3...) Host hinzufügen Hinzufügen Host bearbeiten diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index 810de0d06..ac09975db 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -625,7 +625,7 @@ hosts.txt personalizado hosts.txt Anfitrión - IP + IP1(+IP2+IP3...) Añadir una anfitrión Añadir Anfitrión diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 8b4ee34e3..d8e7eab4a 100755 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -652,7 +652,7 @@ Personnalisé hosts.txt hosts.txt Hôte - IP + IP1(+IP2+IP3...) Ajouter hôte Ajouter Modifier hôte diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index b14989af8..f1797163e 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -652,7 +652,7 @@ カスタムhosts.txt hosts.txt ホスト - IP + IP1(+IP2+IP3...) ホストを追加 追加 ホストを編集 diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index a4d05787f..b1289d139 100755 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -336,7 +336,7 @@ 사용자 지정 hosts.txt hosts.txt 호스트 - IP + IP1(+IP2+IP3...) 호스트 추가 추가 호스트 편집 diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 449d85f84..0582070b7 100755 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -392,7 +392,7 @@ 自定义 hosts.txt hosts.txt Host - IP + IP1(+IP2+IP3...) 添加 host 添加 编辑 host diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 3f35aa8de..3d27c0f35 100755 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -387,7 +387,7 @@ 自定義 hosts.txt hosts.txt Host - IP + IP1(+IP2+IP3...) 添加 host 添加 編輯 host diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index 9c0729b64..015707c05 100755 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -386,7 +386,7 @@ 自訂hosts.txt hosts.txt Host - IP + IP1(+IP2+IP3...) 加入 host 加入 編輯 host diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9ef846679..822bf7b25 100755 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -438,7 +438,7 @@ Custom hosts.txt hosts.txt Host - IP + IP1(+IP2+IP3...) Add a host Add Edit the host