From d63451c1553a2698b3d30d2050eccd3a42232db7 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Tue, 9 May 2017 21:08:49 +0800 Subject: [PATCH] Added support for multi dnsmasq configs --- .../java/org/itxtech/daedalus/Daedalus.java | 5 +- .../daedalus/service/DaedalusVpnService.java | 4 -- .../itxtech/daedalus/util/RulesResolver.java | 55 +++++++++++-------- 3 files changed, 36 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/org/itxtech/daedalus/Daedalus.java b/app/src/main/java/org/itxtech/daedalus/Daedalus.java index a72d875..ce1eacf 100644 --- a/app/src/main/java/org/itxtech/daedalus/Daedalus.java +++ b/app/src/main/java/org/itxtech/daedalus/Daedalus.java @@ -92,9 +92,12 @@ public class Daedalus extends Application { mHostsResolver.start(); hostsPath = getExternalFilesDir(null).getPath() + "/hosts"; - dnsmasqPath = getExternalFilesDir(null).getPath() + "/dnsmasq"; + dnsmasqPath = getExternalFilesDir(null).getPath() + "/dnsmasq/"; configPath = getExternalFilesDir(null).getPath() + "/config.json"; + File file = new File(dnsmasqPath); + Log.d(TAG, "mkdir result: " + file.mkdirs()); + initData(); instance = this; 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 4d64925..0a63492 100644 --- a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java +++ b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java @@ -132,10 +132,6 @@ public class DaedalusVpnService extends VpnService implements Runnable { if (mThread != null) { running = false; shouldRefresh = true; - provider.shutdown(); - mThread.interrupt(); - provider.stop(); - mThread = null; if (provider != null) { provider.shutdown(); mThread.interrupt(); 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 6cf8832..1dbf201 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java +++ b/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java @@ -33,14 +33,14 @@ public class RulesResolver implements Runnable { private static int status = STATUS_NOT_LOADED; private static int mode = MODE_HOSTS; private static String hostsFile; - private static String dnsmasqFile; + private static String dnsmasqPath; private static HashMap rules; private static boolean shutdown = false; public RulesResolver() { status = STATUS_NOT_LOADED; hostsFile = ""; - dnsmasqFile = ""; + dnsmasqPath = ""; shutdown = false; } @@ -59,9 +59,9 @@ public class RulesResolver implements Runnable { status = STATUS_PENDING_LOAD; } - public static void startLoadDnsmasq(String loadFile) { - Log.d(TAG, "Loading DNSMasq file " + loadFile); - dnsmasqFile = loadFile; + public static void startLoadDnsmasq(String loadPath) { + Log.d(TAG, "Loading DNSMasq file " + loadPath); + dnsmasqPath = loadPath; mode = MODE_DNSMASQ; status = STATUS_PENDING_LOAD; } @@ -118,30 +118,39 @@ public class RulesResolver implements Runnable { dataIO.close(); stream.close(); + } else { + status = STATUS_NOT_LOADED; + return; } } else if (mode == MODE_DNSMASQ) { - File file = new File(dnsmasqFile); - if (file.exists() && file.canRead()) { - FileInputStream stream = new FileInputStream(file); - BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream)); - String strLine; - String[] data; - while ((strLine = dataIO.readLine()) != null) { - //Log.d(TAG, strLine); - 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()); + File file = new File(dnsmasqPath); + if (!file.mkdirs() && !file.exists()) { + status = STATUS_NOT_LOADED; + return; + } + for (File conf : file.listFiles()) { + if (file.canRead()) { + Log.d(TAG, "load: Loading DNSMasq configuration " + conf.toString()); + FileInputStream stream = new FileInputStream(conf); + BufferedReader dataIO = new BufferedReader(new InputStreamReader(stream)); + String strLine; + String[] data; + while ((strLine = dataIO.readLine()) != null) { + 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[1] + " " + data[2]); } - rules.put(data[1], data[2]); - Log.d(TAG, "Putting " + data[1] + " " + data[2]); } } - } - dataIO.close(); - stream.close(); + dataIO.close(); + stream.close(); + } } } status = STATUS_LOADED;