From 905f54d0fa6a8750dd3c90b29712010ab0655891 Mon Sep 17 00:00:00 2001 From: tehcneko <7764726+tehcneko@users.noreply.github.com> Date: Fri, 12 Feb 2021 23:10:47 +0800 Subject: [PATCH] [app] Add DoH (#122) --- app/build.gradle | 1 + .../java/io/github/lsposed/manager/App.java | 2 + .../github/lsposed/manager/util/DoHDNS.java | 70 +++++++++++++++++++ app/src/main/res/values/strings.xml | 3 + app/src/main/res/xml/prefs.xml | 14 ++++ 5 files changed, 90 insertions(+) create mode 100644 app/src/main/java/io/github/lsposed/manager/util/DoHDNS.java diff --git a/app/build.gradle b/app/build.gradle index 13e7358e..28e704e4 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -75,6 +75,7 @@ dependencies { implementation 'com.takisoft.preferencex:preferencex:1.1.0' implementation 'com.takisoft.preferencex:preferencex-colorpicker:1.1.0' implementation 'com.squareup.okhttp3:okhttp:4.9.0' + implementation 'com.squareup.okhttp3:okhttp-dnsoverhttps:4.9.0' final def markwon_version = '4.6.2' implementation "io.noties.markwon:core:$markwon_version" implementation "io.noties.markwon:ext-strikethrough:$markwon_version" diff --git a/app/src/main/java/io/github/lsposed/manager/App.java b/app/src/main/java/io/github/lsposed/manager/App.java index dd5e7214..c99a488e 100644 --- a/app/src/main/java/io/github/lsposed/manager/App.java +++ b/app/src/main/java/io/github/lsposed/manager/App.java @@ -16,6 +16,7 @@ import java.io.StringWriter; import io.github.lsposed.manager.repo.RepoLoader; import io.github.lsposed.manager.ui.activity.CrashReportActivity; +import io.github.lsposed.manager.util.DoHDNS; import okhttp3.Cache; import okhttp3.OkHttpClient; @@ -78,6 +79,7 @@ public class App extends Application { if (okHttpClient == null) { okHttpClient = new OkHttpClient.Builder() .cache(getOkHttpCache()) + .dns(new DoHDNS()) .build(); } return okHttpClient; diff --git a/app/src/main/java/io/github/lsposed/manager/util/DoHDNS.java b/app/src/main/java/io/github/lsposed/manager/util/DoHDNS.java new file mode 100644 index 00000000..cb37c58c --- /dev/null +++ b/app/src/main/java/io/github/lsposed/manager/util/DoHDNS.java @@ -0,0 +1,70 @@ +/* + * This file is part of LSPosed. + * + * LSPosed is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * LSPosed is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with LSPosed. If not, see . + * + * Copyright (C) 2020 EdXposed Contributors + * Copyright (C) 2021 LSPosed Contributors + */ + +package io.github.lsposed.manager.util; + +import androidx.annotation.NonNull; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.List; + +import io.github.lsposed.manager.App; +import okhttp3.Dns; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.dnsoverhttps.DnsOverHttps; + +public class DoHDNS implements Dns { + + private static DnsOverHttps dnsOverHttps; + + public DoHDNS() { + DnsOverHttps.Builder builder = new DnsOverHttps.Builder() + .client(new OkHttpClient.Builder().build()) + .url(HttpUrl.get("https://cloudflare-dns.com/dns-query")); + try { + builder.bootstrapDnsHosts(InetAddress.getByName("162.159.36.1"), + InetAddress.getByName("162.159.46.1"), + InetAddress.getByName("1.1.1.1"), + InetAddress.getByName("1.0.0.1"), + InetAddress.getByName("162.159.132.53"), + InetAddress.getByName("2606:4700:4700::1111"), + InetAddress.getByName("2606:4700:4700::1001"), + InetAddress.getByName("2606:4700:4700::0064"), + InetAddress.getByName("2606:4700:4700::6400")); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + dnsOverHttps = builder.post(true).build(); + } + + @NonNull + @Override + public List lookup(@NonNull String hostname) throws UnknownHostException { + if (App.getPreferences().getBoolean("doh", false)) { + List inetAddresses = dnsOverHttps.lookup(hostname); + if (inetAddresses.size() > 0) { + return inetAddresses; + } + } + return SYSTEM.lookup(hostname); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 98b08436..812aeebe 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -156,4 +156,7 @@ Collaborators Assets Open in browser + Network + DNS over HTTPS + Workaround DNS poisoning in some nations diff --git a/app/src/main/res/xml/prefs.xml b/app/src/main/res/xml/prefs.xml index c44518e8..92b71ab7 100644 --- a/app/src/main/res/xml/prefs.xml +++ b/app/src/main/res/xml/prefs.xml @@ -1,6 +1,20 @@ + + + + +