Properly support for DNSMasq (address=)

This commit is contained in:
PeratX 2017-05-07 15:33:06 +08:00
parent 311eb48c27
commit f1c13b973e
6 changed files with 47 additions and 74 deletions

View File

@ -38,15 +38,15 @@ import org.itxtech.daedalus.fragment.*;
* (at your option) any later version. * (at your option) any later version.
*/ */
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 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 String LAUNCH_ACTION = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_ACTION";
public static final int LAUNCH_ACTION_NONE = 0; public static final int LAUNCH_ACTION_NONE = 0;
public static final int LAUNCH_ACTION_ACTIVATE = 1; public static final int LAUNCH_ACTION_ACTIVATE = 1;
public static final int LAUNCH_ACTION_DEACTIVATE = 2; public static final int LAUNCH_ACTION_DEACTIVATE = 2;
public static final int LAUNCH_ACTION_AFTER_DEACTIVATE = 3; public static final int LAUNCH_ACTION_AFTER_DEACTIVATE = 3;
public static final String LAUNCH_FRAGMENT = "org.itxtech.daedalus.activity.MainActivity.LAUNCH_FRAGMENT"; 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_NONE = -1;
public static final int FRAGMENT_MAIN = 0; public static final int FRAGMENT_MAIN = 0;
public static final int FRAGMENT_DNS_TEST = 1; 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); menu.findItem(R.id.nav_about).setChecked(true);
break; break;
case FRAGMENT_RULES: case FRAGMENT_RULES:
menu.findItem(R.id.nav_hosts).setChecked(true); menu.findItem(R.id.nav_rules).setChecked(true);
break; break;
case FRAGMENT_DNS_SERVERS: case FRAGMENT_DNS_SERVERS:
menu.findItem(R.id.nav_dns_server).setChecked(true); 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) { if (currentFragment == FRAGMENT_MAIN) {
mHandler.obtainMessage(MainFragment.MainFragmentHandler.MSG_REFRESH).sendToTarget(); mHandler.obtainMessage(MainFragment.MainFragmentHandler.MSG_REFRESH).sendToTarget();
} }
} else {
Daedalus.updateShortcut(this.getApplicationContext());
} }
int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE); 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. // Handle navigation view item clicks here.
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.nav_settings) { switch (id) {
switchFragment(FRAGMENT_SETTINGS); case R.id.nav_about:
} switchFragment(FRAGMENT_ABOUT);
break;
if (id == R.id.nav_about) { case R.id.nav_dns_server:
switchFragment(FRAGMENT_ABOUT); switchFragment(FRAGMENT_DNS_SERVERS);
} break;
case R.id.nav_dns_test:
if (id == R.id.nav_dns_test) { switchFragment(FRAGMENT_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")));
if (id == R.id.nav_home) { break;
switchFragment(FRAGMENT_MAIN); case R.id.nav_home:
} switchFragment(FRAGMENT_MAIN);
break;
if (id == R.id.nav_hosts) { case R.id.nav_rules:
switchFragment(FRAGMENT_RULES); switchFragment(FRAGMENT_RULES);
} break;
case R.id.nav_settings:
if (id == R.id.nav_dns_server) { switchFragment(FRAGMENT_SETTINGS);
switchFragment(FRAGMENT_DNS_SERVERS); break;
}
if (id == R.id.nav_github) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus")));
} }
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout); DrawerLayout drawer = (DrawerLayout) findViewById(R.id.main_drawer_layout);

View File

@ -6,9 +6,7 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.*; import android.preference.*;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.DnsServerHelper; 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 @Override
public void onStart() { public void onStart() {
super.onStart(); super.onStart();

View File

@ -340,8 +340,8 @@ public class UdpDnsProvider extends DnsProvider {
String dnsQueryName = dnsMsg.getQuestion().name.toString(); String dnsQueryName = dnsMsg.getQuestion().name.toString();
try { try {
if (RulesResolver.canResolve(dnsQueryName)) { String response;
String response = RulesResolver.resolve(dnsQueryName); if ((response = RulesResolver.resolve(dnsQueryName)) != null) {
Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " address " + response + ", using local hosts to resolve."); Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " address " + response + ", using local hosts to resolve.");
DNSMessage.Builder builder = dnsMsg.asBuilder(); DNSMessage.Builder builder = dnsMsg.asBuilder();
int[] ip = new int[4]; int[] ip = new int[4];

View File

@ -121,6 +121,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
@TargetApi(Build.VERSION_CODES.LOLLIPOP) @TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void stopThread() { private void stopThread() {
Log.d(TAG, "stopThread");
boolean shouldRefresh = false; boolean shouldRefresh = false;
try { try {
if (mThread != null) { if (mThread != null) {
@ -129,6 +130,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
provider.shutdown(); provider.shutdown();
mThread.interrupt(); mThread.interrupt();
provider.stop(); provider.stop();
mThread = null;
} }
if (notification != null) { if (notification != null) {
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

View File

@ -70,30 +70,9 @@ public class RulesResolver implements Runnable {
rules = null; 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) { public static String resolve(String hostname) {
if (rules == null) { if (rules == null) {
return ""; return null;
} }
if (rules.containsKey(hostname)) { if (rules.containsKey(hostname)) {
return rules.get(hostname); return rules.get(hostname);
@ -101,15 +80,20 @@ public class RulesResolver implements Runnable {
if (mode == MODE_DNSMASQ) { if (mode == MODE_DNSMASQ) {
String[] pieces = hostname.split("\\."); String[] pieces = hostname.split("\\.");
StringBuilder builder; StringBuilder builder;
builder = new StringBuilder();
for (int i = 1; i < pieces.length; i++) { for (int i = 1; i < pieces.length; i++) {
builder.append(".").append(pieces[i]); builder = new StringBuilder();
} for (int j = i; j < pieces.length; j++) {
if (rules.containsKey(builder.toString())) { builder.append(pieces[j]);
return rules.get(builder.toString()); if (j < pieces.length - 1) {
builder.append(".");
}
}
if (rules.containsKey(builder.toString())) {
return rules.get(builder.toString());
}
} }
} }
return ""; return null;
} }
private void load() { private void load() {
@ -147,8 +131,11 @@ public class RulesResolver implements Runnable {
if (!strLine.equals("") && !strLine.startsWith("#")) { if (!strLine.equals("") && !strLine.startsWith("#")) {
data = strLine.split("/"); data = strLine.split("/");
if (data.length == 3 && data[0].equals("address=")) { 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]); rules.put(data[1], data[2]);
Log.d(TAG, "Putting " + data[0] + " " + data[1]); Log.d(TAG, "Putting " + data[1] + " " + data[2]);
} }
} }
} }

View File

@ -20,7 +20,7 @@
<item android:id="@+id/nav_dns_server" <item android:id="@+id/nav_dns_server"
android:title="@string/action_dns_servers" android:title="@string/action_dns_servers"
android:icon="@drawable/ic_device_hub"/> android:icon="@drawable/ic_device_hub"/>
<item android:id="@+id/nav_hosts" <item android:id="@+id/nav_rules"
android:title="@string/action_rules" android:title="@string/action_rules"
android:checked="false" android:checked="false"
android:icon="@drawable/ic_description"/> android:icon="@drawable/ic_description"/>