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.
*/
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);

View File

@ -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();

View File

@ -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];

View File

@ -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);

View File

@ -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]);
}
}
}

View File

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