From f1c13b973e924081ebb81c567d47dce7a50839a2 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Sun, 7 May 2017 15:33:06 +0800 Subject: [PATCH] Properly support for DNSMasq (address=) --- .../daedalus/activity/MainActivity.java | 58 +++++++++---------- .../daedalus/fragment/SettingsFragment.java | 10 ---- .../daedalus/provider/UdpDnsProvider.java | 4 +- .../daedalus/service/DaedalusVpnService.java | 2 + .../itxtech/daedalus/util/RulesResolver.java | 45 +++++--------- .../main/res/menu/activity_main_drawer.xml | 2 +- 6 files changed, 47 insertions(+), 74 deletions(-) diff --git a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java index ef7e19f..f31a11b 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java @@ -38,15 +38,15 @@ import org.itxtech.daedalus.fragment.*; * (at your option) any later version. */ public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { + private static final String TAG = "DMainActivity"; + public static final String LAUNCH_ACTION = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_ACTION"; public static final int LAUNCH_ACTION_NONE = 0; public static final int LAUNCH_ACTION_ACTIVATE = 1; public static final int LAUNCH_ACTION_DEACTIVATE = 2; public static final int LAUNCH_ACTION_AFTER_DEACTIVATE = 3; + public static final String LAUNCH_FRAGMENT = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_FRAGMENT"; - - private static final String TAG = "DMainActivity"; - public static final int FRAGMENT_NONE = -1; public static final int FRAGMENT_MAIN = 0; public static final int FRAGMENT_DNS_TEST = 1; @@ -161,7 +161,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On menu.findItem(R.id.nav_about).setChecked(true); break; case FRAGMENT_RULES: - menu.findItem(R.id.nav_hosts).setChecked(true); + menu.findItem(R.id.nav_rules).setChecked(true); break; case FRAGMENT_DNS_SERVERS: menu.findItem(R.id.nav_dns_server).setChecked(true); @@ -294,8 +294,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On if (currentFragment == FRAGMENT_MAIN) { mHandler.obtainMessage(MainFragment.MainFragmentHandler.MSG_REFRESH).sendToTarget(); } - } else { - Daedalus.updateShortcut(this.getApplicationContext()); } int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE); @@ -310,32 +308,28 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On // Handle navigation view item clicks here. int id = item.getItemId(); - if (id == R.id.nav_settings) { - switchFragment(FRAGMENT_SETTINGS); - } - - if (id == R.id.nav_about) { - switchFragment(FRAGMENT_ABOUT); - } - - if (id == R.id.nav_dns_test) { - switchFragment(FRAGMENT_DNS_TEST); - } - - if (id == R.id.nav_home) { - switchFragment(FRAGMENT_MAIN); - } - - if (id == R.id.nav_hosts) { - switchFragment(FRAGMENT_RULES); - } - - if (id == R.id.nav_dns_server) { - switchFragment(FRAGMENT_DNS_SERVERS); - } - - if (id == R.id.nav_github) { - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus"))); + switch (id) { + case R.id.nav_about: + switchFragment(FRAGMENT_ABOUT); + break; + case R.id.nav_dns_server: + switchFragment(FRAGMENT_DNS_SERVERS); + break; + case R.id.nav_dns_test: + switchFragment(FRAGMENT_DNS_TEST); + break; + case R.id.nav_github: + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus"))); + break; + case R.id.nav_home: + switchFragment(FRAGMENT_MAIN); + break; + case R.id.nav_rules: + switchFragment(FRAGMENT_RULES); + break; + case R.id.nav_settings: + switchFragment(FRAGMENT_SETTINGS); + break; } DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java index 315fa56..f45fae2 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/SettingsFragment.java @@ -6,9 +6,7 @@ import android.os.Build; import android.os.Bundle; import android.preference.*; import android.support.design.widget.Snackbar; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; import org.itxtech.daedalus.util.DnsServerHelper; @@ -126,14 +124,6 @@ public class SettingsFragment extends PreferenceFragment { } } - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - view = super.onCreateView(inflater, container, savedInstanceState); - SwitchPreference localHostResolution = (SwitchPreference) findPreference("settings_local_rules_resolution"); - localHostResolution.setSummary(Daedalus.hostsPath); - return view; - } - @Override public void onStart() { super.onStart(); diff --git a/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java index bc5852b..950edb4 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java @@ -340,8 +340,8 @@ public class UdpDnsProvider extends DnsProvider { String dnsQueryName = dnsMsg.getQuestion().name.toString(); try { - if (RulesResolver.canResolve(dnsQueryName)) { - String response = RulesResolver.resolve(dnsQueryName); + String response; + if ((response = RulesResolver.resolve(dnsQueryName)) != null) { Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " address " + response + ", using local hosts to resolve."); DNSMessage.Builder builder = dnsMsg.asBuilder(); int[] ip = new int[4]; diff --git a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java index de8f413..0cc797f 100644 --- a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java +++ b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java @@ -121,6 +121,7 @@ public class DaedalusVpnService extends VpnService implements Runnable { @TargetApi(Build.VERSION_CODES.LOLLIPOP) private void stopThread() { + Log.d(TAG, "stopThread"); boolean shouldRefresh = false; try { if (mThread != null) { @@ -129,6 +130,7 @@ public class DaedalusVpnService extends VpnService implements Runnable { provider.shutdown(); mThread.interrupt(); provider.stop(); + mThread = null; } if (notification != null) { NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); diff --git a/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java b/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java index aad5916..6cf8832 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java +++ b/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java @@ -70,30 +70,9 @@ public class RulesResolver implements Runnable { rules = null; } - public static boolean canResolve(String hostname) { - if (rules == null) { - return false; - } - if (rules.containsKey(hostname)) { - return true; - } - if (mode == MODE_DNSMASQ) { - String[] pieces = hostname.split("\\."); - StringBuilder builder; - builder = new StringBuilder(); - for (int i = 1; i < pieces.length; i++) { - builder.append(".").append(pieces[i]); - } - if (rules.containsKey(builder.toString())) { - return true; - } - } - return false; - } - public static String resolve(String hostname) { if (rules == null) { - return ""; + return null; } if (rules.containsKey(hostname)) { return rules.get(hostname); @@ -101,15 +80,20 @@ public class RulesResolver implements Runnable { if (mode == MODE_DNSMASQ) { String[] pieces = hostname.split("\\."); StringBuilder builder; - builder = new StringBuilder(); for (int i = 1; i < pieces.length; i++) { - builder.append(".").append(pieces[i]); - } - if (rules.containsKey(builder.toString())) { - return rules.get(builder.toString()); + builder = new StringBuilder(); + for (int j = i; j < pieces.length; j++) { + builder.append(pieces[j]); + if (j < pieces.length - 1) { + builder.append("."); + } + } + if (rules.containsKey(builder.toString())) { + return rules.get(builder.toString()); + } } } - return ""; + return null; } private void load() { @@ -147,8 +131,11 @@ public class RulesResolver implements Runnable { if (!strLine.equals("") && !strLine.startsWith("#")) { data = strLine.split("/"); if (data.length == 3 && data[0].equals("address=")) { + if (data[1].startsWith(".")) { + data[1] = data[1].substring(1, data[1].length()); + } rules.put(data[1], data[2]); - Log.d(TAG, "Putting " + data[0] + " " + data[1]); + Log.d(TAG, "Putting " + data[1] + " " + data[2]); } } } diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/activity_main_drawer.xml index 58f8c5b..8906aa8 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/activity_main_drawer.xml @@ -20,7 +20,7 @@ -