Skip to content

Commit

Permalink
Fix several DoH issues and allow to specify the server
Browse files Browse the repository at this point in the history
  • Loading branch information
krlvm committed Apr 1, 2020
1 parent 33eaaf2 commit b9f9a23
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* Parts of code were derived from project
* dohjava (https://github.com/NUMtechnology/dohjava)
* licensed under the MIT license
* that licensed under the MIT license
*/
public class AndroidDohResolver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;
import android.view.MenuItem;

Expand All @@ -17,6 +19,7 @@
import ru.krlvm.powertunnel.android.R;
import tun.proxy.MyApplication;
import tun.proxy.preferences.fragments.PackageListPreferenceFragment;
import tun.proxy.preferences.preference.EditTextSummaryPreference;

import static android.preference.Preference.OnPreferenceChangeListener;
import static android.preference.Preference.OnPreferenceClickListener;
Expand All @@ -27,11 +30,18 @@ public class SimplePreferenceFragment extends PreferenceFragment implements OnPr
public static final String VPN_DISALLOWED_APPLICATION_LIST = "vpn_disallowed_application_list";
public static final String VPN_ALLOWED_APPLICATION_LIST = "vpn_allowed_application_list";
public static final String VPN_CLEAR_ALL_SELECTION = "vpn_clear_all_selection";
public static final String DNS_PROVIDER = "dns_provider";
public static final String SPECIFIED_DNS = "specified_dns_provider";

private ListPreference prefPackage;
private PreferenceScreen prefDisallow;
private PreferenceScreen prefAllow;

private ListPreference prefDns;
private EditTextSummaryPreference prefSpecDns;

private SharedPreferences prefs;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -47,6 +57,18 @@ public void onCreate(Bundle savedInstanceState) {
prefAllow.setOnPreferenceClickListener(this);
clearAllSelection.setOnPreferenceClickListener(this);

prefs = PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance());
prefSpecDns = ((EditTextSummaryPreference) findPreference(SPECIFIED_DNS));

prefDns = ((ListPreference) findPreference(DNS_PROVIDER));
prefDns.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
updateSpecDnsStatus(((String) newValue));
return true;
}
});

prefPackage.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
Expand All @@ -61,9 +83,14 @@ public boolean onPreferenceChange(Preference preference, Object value) {
}
});

updateSpecDnsStatus(prefs.getString(DNS_PROVIDER, "CLOUDFLARE"));
updateMenuItem();
}

private void updateSpecDnsStatus(String provider) {
prefSpecDns.setEnabled(provider.equals("SPECIFIED"));
}

@Override
public void onResume() {
super.onResume();
Expand Down
15 changes: 13 additions & 2 deletions app/src/main/java/tun/proxy/service/Tun2HttpVpnService.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,20 @@ private void start() {
PowerTunnel.DOH_ADDRESS = null;
if (prefs.getBoolean("override_dns", false) || DNS_SERVERS.isEmpty()) {
String provider = prefs.getString("dns_provider", "CLOUDFLARE");
DNS_SERVERS.clear();
DNS_OVERRIDE = true;
if(provider.equals("SPECIFIED")) {
String specifiedDnsProvider = prefs.getString("specified_dns_provider", "");
if(!specifiedDnsProvider.trim().isEmpty()) {
if(specifiedDnsProvider.startsWith("https://")) {

This comment has been minimized.

Copy link
@DRSDavidSoft

DRSDavidSoft Dec 18, 2020

This should be like this:

if(specifiedDnsProvider.startsWith("https://") || specifiedDnsProvider.startsWith("http://")) {

This is because the same DoH endpoints can be used over plain HTTP.

This comment has been minimized.

Copy link
@krlvm

krlvm Dec 18, 2020

Author Owner

It seems to me in this case all the advantages of DoH will be leveled, but of course I can add this

This comment has been minimized.

Copy link
@DRSDavidSoft

DRSDavidSoft Dec 18, 2020

@krlvm Your assumption is absolutely true, however please consider these two scenarios:

  1. the request is encrypted via another layer (e.g. local stunnel instance, or VPN, etc)
  2. the plain DoH server is on the local network (e.g. 192.168.x) and not on the public internet, so not exposed to DPI modifications by the ISP/government
PowerTunnel.DOH_ADDRESS = specifiedDnsProvider;
} else {

This comment has been minimized.

Copy link
@DRSDavidSoft

DRSDavidSoft Dec 18, 2020

I think an IP validator is required here:

// ...
InetAddressValidator validator = InetAddressValidator.getInstance();

if ( ... ) {
    // ...
} else if (validator.isValidInet4Address(specifiedDnsProvider) || validator.isValidInet6Address(specifiedDnsProvider)) {
    // ...
} else {
    // throw an error
}

It should display a toast message if an invalid address is provided. Otherwise, the user won't be notified of the issue.

DNS_SERVERS.clear();
DNS_SERVERS.add(specifiedDnsProvider);
}
}
}
if (!provider.contains("_DOH")) {
DNS_SERVERS.clear();
switch (provider) {
default:
case "CLOUDFLARE": {
Expand Down Expand Up @@ -139,7 +150,7 @@ private void start() {
}
}
}
Log.i(TAG, "Waiting for VPN server start...");
Log.i(TAG, "Waiting for VPN server to start...");
if (!PowerTunnel.isRunning()) {
try {
PowerTunnel.bootstrap();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-pl/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@
<string name="dns_fetch_error">Pobieranie listy serwerów DNS nie powiodło się</string>
<string name="error">Błąd</string>
<string name="download">Pobierz</string>
<string name="specify_dns_provider">Określ dostawcę DNS</string>
<string name="specified_dns_provider">Określony dostawca DNS</string>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@
<string name="dns_fetch_error">Ошибка при получении списка адресов DNS серверов</string>
<string name="error">Ошибка</string>
<string name="download">Скачать</string>
<string name="specify_dns_provider">Укажите поставщика DNS</string>
<string name="specified_dns_provider">Указанный поставщик DNS</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<item>AdGuard</item>
<item>AdGuard (DoH)</item>
<item>SecureDNS (DoH)</item>
<item>@string/specified_dns_provider</item>
</string-array>
<string-array name="pref_dns_provider_value" translatable="false">
<item>CLOUDFLARE</item>
Expand All @@ -63,6 +64,7 @@
<item>ADGUARD</item>
<item>ADGUARD_DOH</item>
<item>SECDNS_DOH</item>
<item>SPECIFIED</item>
</string-array>

<string name="pref_allowed_application_list">Allowed applications</string>
Expand Down Expand Up @@ -102,4 +104,6 @@
<string name="dns_fetch_error">Failed to fetch the DNS servers list</string>
<string name="error">Error</string>
<string name="download">Download</string>
<string name="specify_dns_provider">Specify DNS provider</string>
<string name="specified_dns_provider">Specified DNS provider</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
android:dependency="override_dns"
android:summary="%s"/>

<tun.proxy.preferences.preference.EditTextSummaryPreference
android:key="specified_dns_provider"
android:title="@string/specify_dns_provider"
android:summary="%s"
android:dependency="override_dns"
app:useSimpleSummaryProvider="true" />

<SwitchPreference
android:title="@string/pref_use_dns_sec"
android:key="use_dns_sec"
Expand Down

0 comments on commit b9f9a23

Please sign in to comment.