Enhance hosts
This commit is contained in:
11 files changed
+51
-56
No files matched your search
@@ -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<InetAddress> 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<Pair<String, String>> getAll() {
|
||||
List<Pair<String, String>> 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;
|
||||
}
|
||||
}
|
||||
@@ -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<String, InetAddress> builtInHosts;
|
||||
private static final Map<String, List<InetAddress>> builtInHosts;
|
||||
|
||||
static {
|
||||
Map<String, InetAddress> map = new HashMap<>();
|
||||
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.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<String, InetAddress> map, String host, String ip) {
|
||||
InetAddress address = Hosts.toInetAddress(host, ip);
|
||||
if (address != null) {
|
||||
map.put(host, address);
|
||||
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]);
|
||||
}
|
||||
map.put(host, Arrays.asList(addresses));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
|
||||
InetAddress inetAddress = hosts.get(hostname);
|
||||
if (inetAddress != null) {
|
||||
return Collections.singletonList(inetAddress);
|
||||
List<InetAddress> 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<InetAddress> 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) {
|
||||
|
||||
Reference in new issue
Block a user