Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 76e259e

Browse files
committed
Add Dns Over Https switch. (Close #5)
1 parent f3a1e63 commit 76e259e

File tree

9 files changed

+60
-12
lines changed

9 files changed

+60
-12
lines changed

app/src/main/java/com/fox2code/mmm/MainApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ public static boolean isTextWrapEnabled() {
106106
return getSharedPreferences().getBoolean("pref_wrap_text", false);
107107
}
108108

109+
public static boolean isDohEnabled() {
110+
return getSharedPreferences().getBoolean("pref_dns_over_https", true);
111+
}
112+
109113
public static boolean isDeveloper() {
110114
return BuildConfig.DEBUG ||
111115
getSharedPreferences().getBoolean("developer", false);

app/src/main/java/com/fox2code/mmm/NotificationType.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
interface NotificationTypeCst {
2323
String TAG = "NotificationType";
24-
boolean ROOTLESS_TEST = true;
2524
}
2625

2726
public enum NotificationType implements NotificationTypeCst {
@@ -110,7 +109,7 @@ public boolean shouldRemove() {
110109
}, true) {
111110
@Override
112111
public boolean shouldRemove() {
113-
return (!(ROOTLESS_TEST && BuildConfig.DEBUG)) &&
112+
return !BuildConfig.DEBUG &&
114113
(MainApplication.isShowcaseMode() ||
115114
InstallerInitializer.peekMagiskPath() == null);
116115
}
@@ -135,11 +134,13 @@ public boolean shouldRemove() {
135134
this(textId, iconId, backgroundAttr, foregroundAttr, null);
136135
}
137136

138-
NotificationType(@StringRes int textId, int iconId, int backgroundAttr, int foregroundAttr, View.OnClickListener onClickListener) {
139-
this(textId, iconId, backgroundAttr, foregroundAttr, null, false);
137+
NotificationType(@StringRes int textId, int iconId, int backgroundAttr, int foregroundAttr,
138+
View.OnClickListener onClickListener) {
139+
this(textId, iconId, backgroundAttr, foregroundAttr, onClickListener, false);
140140
}
141141

142-
NotificationType(@StringRes int textId, int iconId, int backgroundAttr, int foregroundAttr, View.OnClickListener onClickListener, boolean special) {
142+
NotificationType(@StringRes int textId, int iconId, int backgroundAttr, int foregroundAttr,
143+
View.OnClickListener onClickListener, boolean special) {
143144
this.textId = textId;
144145
this.iconId = iconId;
145146
this.backgroundAttr = backgroundAttr;

app/src/main/java/com/fox2code/mmm/androidacy/AndroidacyRepoData.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
107107
for (int i = 0; i < len; i++) {
108108
jsonObject = jsonArray.getJSONObject(i);
109109
String moduleId = jsonObject.getString("codename");
110+
// Deny remote modules ids shorter than 3 chars
111+
if (moduleId.length() < 3) continue;
110112
long lastUpdate = jsonObject.getLong("updated_at") * 1000;
111113
lastLastUpdate = Math.max(lastLastUpdate, lastUpdate);
112114
RepoModule repoModule = this.moduleHashMap.get(moduleId);

app/src/main/java/com/fox2code/mmm/repo/RepoData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ protected List<RepoModule> populate(JSONObject jsonObject) throws JSONException
8585
for (int i = 0; i < len; i++) {
8686
JSONObject module = array.getJSONObject(i);
8787
String moduleId = module.getString("id");
88-
// Deny remote modules ids shorter than 3 chars long or that start with a digit
89-
if (moduleId.length() < 3 || Character.isDigit(moduleId.charAt(0))) continue;
88+
// Deny remote modules ids shorter than 3 chars
89+
if (moduleId.length() < 3) continue;
9090
long moduleLastUpdate = module.getLong("last_update");
9191
String moduleNotesUrl = module.getString("notes_url");
9292
String modulePropsUrl = module.getString("prop_url");

app/src/main/java/com/fox2code/mmm/settings/SettingsActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.fox2code.mmm.installer.InstallerInitializer;
2424
import com.fox2code.mmm.repo.RepoData;
2525
import com.fox2code.mmm.repo.RepoManager;
26+
import com.fox2code.mmm.utils.Http;
2627
import com.fox2code.mmm.utils.IntentHelper;
2728
import com.mikepenz.aboutlibraries.LibsBuilder;
2829
import com.topjohnwu.superuser.internal.UiThreadHandler;
@@ -83,6 +84,10 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
8384
}
8485
return true;
8586
});
87+
findPreference("pref_dns_over_https").setOnPreferenceChangeListener((p, v) -> {
88+
Http.setDoh((Boolean) v);
89+
return true;
90+
});
8691
if ("dark".equals(themePreference.getValue())) {
8792
findPreference("pref_force_dark_terminal").setEnabled(false);
8893
}

app/src/main/java/com/fox2code/mmm/utils/Http.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@
5252
public class Http {
5353
private static final String TAG = "Http";
5454
private static final OkHttpClient httpClient;
55+
private static final OkHttpClient httpClientDoH;
5556
private static final OkHttpClient httpClientWithCache;
57+
private static final OkHttpClient httpClientWithCacheDoH;
5658
private static final FallBackDNS fallbackDNS;
5759
private static final String androidacyUA;
60+
private static boolean doh;
5861

5962
static {
6063
MainApplication mainApplication = MainApplication.getINSTANCE();
@@ -131,26 +134,33 @@ public class Http {
131134
return chain.proceed(request.build());
132135
});
133136
// Fallback DNS cache responses in case request fail but already succeeded once in the past
134-
httpclientBuilder.dns(fallbackDNS = new FallBackDNS(mainApplication, dns,
137+
fallbackDNS = new FallBackDNS(mainApplication, dns,
135138
"github.com", "api.github.com", "raw.githubusercontent.com",
136139
"camo.githubusercontent.com", "user-images.githubusercontent.com",
137140
"cdn.jsdelivr.net", "img.shields.io", "magisk-modules-repo.github.io",
138-
"www.androidacy.com", "api.androidacy.com"));
141+
"www.androidacy.com", "api.androidacy.com");
139142
httpclientBuilder.cookieJar(new CDNCookieJar(true));
143+
httpclientBuilder.dns(Dns.SYSTEM);
140144
httpClient = httpclientBuilder.build();
145+
httpclientBuilder.dns(fallbackDNS);
146+
httpClientDoH = httpclientBuilder.build();
141147
httpclientBuilder.cache(new Cache(
142148
new File(mainApplication.getCacheDir(), "http_cache"),
143149
16L * 1024L * 1024L)); // 16Mib of cache
150+
httpclientBuilder.dns(Dns.SYSTEM);
144151
httpClientWithCache = httpclientBuilder.build();
152+
httpclientBuilder.dns(fallbackDNS);
153+
httpClientWithCacheDoH = httpclientBuilder.build();
145154
Log.i(TAG, "Initialized Http successfully!");
155+
doh = MainApplication.isDohEnabled();
146156
}
147157

148158
public static OkHttpClient getHttpClient() {
149-
return httpClient;
159+
return doh ? httpClientDoH : httpClient;
150160
}
151161

152162
public static OkHttpClient getHttpClientWithCache() {
153-
return httpClientWithCache;
163+
return doh ? httpClientWithCacheDoH : httpClientWithCache;
154164
}
155165

156166
public static byte[] doHttpGet(String url,boolean allowCache) throws IOException {
@@ -241,6 +251,11 @@ public static String getAndroidacyUA() {
241251
return androidacyUA;
242252
}
243253

254+
public static void setDoh(boolean doh) {
255+
Log.d(TAG, "DoH: " + Http.doh + " -> " + doh);
256+
Http.doh = doh;
257+
}
258+
244259
/**
245260
* Cookie jar that allow CDN cookies, reset on app relaunch
246261
* Note: An argument can be made that it allow tracking but
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24"
6+
android:tint="?attr/colorControlNormal">
7+
<path
8+
android:fillColor="@android:color/white"
9+
android:pathData="M12,1L3,5v6c0,5.55 3.84,10.74 9,12 5.16,-1.26 9,-6.45 9,-12L21,5l-9,-4zM12,11.99h7c-0.53,4.12 -3.28,7.79 -7,8.94L12,12L5,12L5,6.3l7,-3.11v8.8z"/>
10+
</vector>

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@
7474
and/or indicating poor module quality, disable at your own risk!
7575
</string>
7676
<string name="dns_over_https_pref">Dns over https</string>
77-
<string name="dns_over_https_desc">May fix connections issues in some cases.</string>
77+
<string name="dns_over_https_desc">
78+
May fix connections issues in some cases.
79+
(Does not apply to WebView)
80+
</string>
7881
<string name="disable_extensions_pref">Disable extensions</string>
7982
<string name="disable_extensions_desc">
8083
Disable Fox\'s Mmm extensions, this prevent modules from using

app/src/main/res/xml/root_preferences.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@
7171
app:title="@string/use_magisk_install_command_pref"
7272
app:summary="@string/use_magisk_install_command_desc"
7373
app:singleLineTitle="false" />
74+
75+
<SwitchPreferenceCompat
76+
app:defaultValue="true"
77+
app:key="pref_dns_over_https"
78+
app:icon="@drawable/ic_baseline_security_24"
79+
app:title="@string/dns_over_https_pref"
80+
app:summary="@string/dns_over_https_desc"
81+
app:singleLineTitle="false" />
7482
</PreferenceCategory>
7583

7684
<PreferenceCategory

0 commit comments

Comments
 (0)