From cd676eabfcc7816fa568355314ad12f81b188bef Mon Sep 17 00:00:00 2001 From: tarsin Date: Tue, 22 Nov 2022 14:57:58 +0800 Subject: [PATCH] Mitigate CCP's sanctions --- .../java/com/hippo/ehviewer/client/EhDns.kt | 174 +++++++++++------- 1 file changed, 106 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhDns.kt b/app/src/main/java/com/hippo/ehviewer/client/EhDns.kt index e0356364f..779287088 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhDns.kt +++ b/app/src/main/java/com/hippo/ehviewer/client/EhDns.kt @@ -13,83 +13,121 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.hippo.ehviewer.client -package com.hippo.ehviewer.client; +import android.content.Context +import com.hippo.ehviewer.EhApplication +import com.hippo.ehviewer.Hosts +import com.hippo.ehviewer.Settings +import okhttp3.Dns +import java.net.InetAddress +import java.net.UnknownHostException -/* - * Created by Hippo on 2018/3/23. - */ +class EhDns(context: Context) : Dns { + private val hosts = EhApplication.getHosts(context) + private val builtInHosts: MutableMap> = mutableMapOf() -import android.content.Context; + init { + /* Pair(ip: String!, blockedByCCP: Boolean!) */ + val ehgtHosts = arrayOf( + Pair("37.48.89.44", false), + Pair("81.171.10.48", false), + Pair("178.162.139.24", false), + Pair("178.162.140.212", false), + Pair("2001:1af8:4700:a062:8::47de", false), + Pair("2001:1af8:4700:a062:9::47de", true), + Pair("2001:1af8:4700:a0c9:4::47de", false), + Pair("2001:1af8:4700:a0c9:3::47de", true), + ) -import androidx.annotation.NonNull; - -import com.hippo.ehviewer.EhApplication; -import com.hippo.ehviewer.Hosts; -import com.hippo.ehviewer.Settings; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import okhttp3.Dns; - -public class EhDns implements Dns { - - private static final Map> builtInHosts; - - static { - Map> map = new HashMap<>(); - put(map, "e-hentai.org", "104.20.134.21", "104.20.135.21", "172.67.0.127"); - 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", "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, "gt0.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, "gt1.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, "gt2.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, "gt3.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; + put( + builtInHosts, + "e-hentai.org", + Pair("104.20.134.21", true), + Pair("104.20.135.21", true), + Pair("172.67.0.127", false), + ) + put( + builtInHosts, + "exhentai.org", + Pair("178.175.128.252", false), + Pair("178.175.129.252", false), + Pair("178.175.129.254", false), + Pair("178.175.128.254", false), + Pair("178.175.132.20", false), + Pair("178.175.132.22", false), + ) + put( + builtInHosts, + "repo.e-hentai.org", + Pair("94.100.28.57", true), + Pair("94.100.29.73", true), + ) + put( + builtInHosts, + "forums.e-hentai.org", + Pair("94.100.18.243", false), + ) + put( + builtInHosts, + "ehgt.org", + *ehgtHosts + ) + put( + builtInHosts, + "gt0.ehgt.org", + *ehgtHosts + ) + put( + builtInHosts, + "gt1.ehgt.org", + *ehgtHosts + ) + put( + builtInHosts, + "gt2.ehgt.org", + *ehgtHosts + ) + put( + builtInHosts, + "gt3.ehgt.org", + *ehgtHosts + ) + put( + builtInHosts, + "ul.ehgt.org", + Pair("94.100.24.82", true), + Pair("94.100.24.72", true), + ) + put( + builtInHosts, + "raw.githubusercontent.com", + Pair("151.101.0.133", false), + Pair("151.101.64.133", false), + Pair("151.101.128.133", false), + Pair("151.101.192.133", false), + ) } - private final Hosts hosts; - - public EhDns(Context context) { - hosts = EhApplication.getHosts(context); - } - - private static void put(Map> map, String host, String... ips) { - List addresses = new ArrayList<>(); - for (String ip : ips) { - addresses.add(Hosts.toInetAddress(host, ip)); + private fun put( + map: MutableMap>, + host: String, + vararg ips: Pair + ) { + map[host] = ips.mapNotNull { + if (Settings.getDF() && it.second) null else Hosts.toInetAddress(host, it.first) } - map.put(host, addresses); } - @NonNull - @Override - public List lookup(@NonNull String hostname) throws UnknownHostException { - List inetAddresses = hosts.get(hostname); - if (inetAddresses != null) { - return inetAddresses; - } - if (Settings.getBuiltInHosts()) { - inetAddresses = builtInHosts.get(hostname); - if (inetAddresses != null) { - return inetAddresses; + @Throws(UnknownHostException::class) + override fun lookup(hostname: String): List { + var inetAddresses = hosts[hostname] + inetAddresses ?: run { + if (Settings.getBuiltInHosts()) { + inetAddresses = builtInHosts[hostname] } } - return SYSTEM.lookup(hostname); + inetAddresses ?: return Dns.SYSTEM.lookup(hostname) + return inetAddresses!! // Should never be null here } -} +} \ No newline at end of file