Skip to content

Commit 62df2ff

Browse files
committed
修复了自定义host不生效的问题
1 parent b51b10d commit 62df2ff

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

app/src/main/java/com/hippo/ehviewer/Hosts.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,31 @@ public InetAddress get(String host) {
7878
}
7979
}
8080

81+
/**
82+
* Gets a List<InetAddress> with the host.
83+
*/
84+
@Nullable
85+
public List<InetAddress> getList(String host) {
86+
if (!isValidHost(host)) {
87+
return null;
88+
}
89+
90+
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[] {host});
91+
List<InetAddress> list = new ArrayList<>();
92+
try {
93+
while (cursor.moveToNext()) {
94+
String ip = SqlUtils.getString(cursor, COLUMN_IP, null);
95+
InetAddress inetAddress = toInetAddress(host, ip);
96+
if (inetAddress != null) {
97+
list.add(inetAddress);
98+
}
99+
}
100+
return list;
101+
} finally {
102+
cursor.close();
103+
}
104+
}
105+
81106
private boolean contains(String host) {
82107
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + " WHERE " + COLUMN_HOST + " = ?;", new String[] {host});
83108
try {

app/src/main/java/com/hippo/ehviewer/client/EhHosts.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,21 +139,21 @@ private static void put(Map<String, List<InetAddress>> map, String host, String.
139139
@NonNull
140140
@Override
141141
public List<InetAddress> lookup(@NonNull String hostname) throws UnknownHostException {
142-
143-
List<InetAddress> inetAddresses = (List<InetAddress>) hosts.get(hostname);
144-
if (inetAddresses != null) {
145-
Collections.shuffle(inetAddresses, new Random(System.currentTimeMillis()));
146-
return inetAddresses;
142+
List<InetAddress> inetAddresses = hosts.getList(hostname);
143+
if(inetAddresses==null){
144+
inetAddresses = new ArrayList<>();
147145
}
146+
148147
if (Settings.getBuiltInHosts() || Settings.getBuiltEXHosts()) {
149-
inetAddresses = builtInHosts.get(hostname);
150-
if (inetAddresses != null) {
151-
Collections.shuffle(inetAddresses, new Random(System.currentTimeMillis()));
152-
return inetAddresses;
148+
List<InetAddress> appHosts = builtInHosts.get(hostname);
149+
if (appHosts != null) {
150+
inetAddresses.addAll(appHosts);
153151
}
152+
Collections.shuffle(inetAddresses, new Random(System.currentTimeMillis()));
153+
return inetAddresses;
154154
}
155155
if (Settings.getDoH()) {
156-
inetAddresses = dnsOverHttps.lookup(hostname);
156+
inetAddresses.addAll(dnsOverHttps.lookup(hostname));
157157
if (!inetAddresses.isEmpty()) {
158158
Collections.shuffle(inetAddresses, new Random(System.currentTimeMillis()));
159159
return inetAddresses;

0 commit comments

Comments
 (0)