From e3829b0499f5ecb2f99e5d50c8d44d345c9e3ee5 Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Thu, 6 Jul 2017 15:29:07 +0800 Subject: [PATCH] Added support for IPv6 rules --- .../daedalus/provider/UdpProvider.java | 14 +++++---- .../itxtech/daedalus/util/RulesResolver.java | 30 +++++++++++++++---- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/org/itxtech/daedalus/provider/UdpProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/UdpProvider.java index 831be3f..933dd2b 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/UdpProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/UdpProvider.java @@ -10,6 +10,7 @@ import android.util.Log; import de.measite.minidns.DNSMessage; import de.measite.minidns.Record; import de.measite.minidns.record.A; +import de.measite.minidns.record.AAAA; import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.RulesResolver; @@ -347,11 +348,8 @@ public class UdpProvider extends Provider { String dnsQueryName = dnsMsg.getQuestion().name.toString(); try { - String response = null; - if (dnsMsg.getQuestion().type == Record.TYPE.A) { - response = RulesResolver.resolve(dnsQueryName); - } - if (response != null) { + String response = RulesResolver.resolve(dnsQueryName, dnsMsg.getQuestion().type); + if (response != null && dnsMsg.getQuestion().type == Record.TYPE.A) { Logger.info("Provider: Resolved " + dnsQueryName + " Local resolver response: " + response); DNSMessage.Builder builder = dnsMsg.asBuilder(); int[] ip = new int[4]; @@ -362,6 +360,12 @@ public class UdpProvider extends Provider { } builder.addAnswer(new Record<>(dnsQueryName, Record.TYPE.A, 1, 64, new A(ip[0], ip[1], ip[2], ip[3]))); handleDnsResponse(parsedPacket, builder.build().toArray()); + } else if (response != null && dnsMsg.getQuestion().type == Record.TYPE.AAAA) { + Logger.info("Provider: Resolved " + dnsQueryName + " Local resolver response: " + response); + DNSMessage.Builder builder = dnsMsg.asBuilder(); + builder.addAnswer(new Record<>(dnsQueryName, Record.TYPE.AAAA, 1, 64, + new AAAA(Inet6Address.getByName(response).getAddress()))); + handleDnsResponse(parsedPacket, builder.build().toArray()); } else { Logger.info("Provider: Resolving " + dnsQueryName + " Type: " + dnsMsg.getQuestion().type.name() + " Sending to " + destAddr); DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr, 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 8a31056..93da0f2 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java +++ b/app/src/main/java/org/itxtech/daedalus/util/RulesResolver.java @@ -1,5 +1,7 @@ package org.itxtech.daedalus.util; +import de.measite.minidns.Record; + import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -31,7 +33,8 @@ public class RulesResolver implements Runnable { private static int mode = MODE_HOSTS; private static String[] hostsFiles; private static String[] dnsmasqFiles; - private static HashMap rules; + private static HashMap rulesA; + private static HashMap rulesAAAA; private static boolean shutdown = false; public RulesResolver() { @@ -58,10 +61,16 @@ public class RulesResolver implements Runnable { } public static void clear() { - rules = null; + rulesA = null; } - public static String resolve(String hostname) { + public static String resolve(String hostname, Record.TYPE type) { + HashMap rules = null; + if (type == Record.TYPE.A) { + rules = rulesA; + } else if (type == Record.TYPE.AAAA) { + rules = rulesAAAA; + } if (rules == null) { return null; } @@ -90,7 +99,8 @@ public class RulesResolver implements Runnable { private void load() { try { status = STATUS_LOADING; - rules = new HashMap<>(); + rulesA = new HashMap<>(); + rulesAAAA = new HashMap<>(); if (mode == MODE_HOSTS) { for (String hostsFile : hostsFiles) { File file = new File(hostsFile); @@ -104,7 +114,11 @@ public class RulesResolver implements Runnable { while ((strLine = dataIO.readLine()) != null) { if (!strLine.equals("") && !strLine.startsWith("#")) { data = strLine.split("\\s+"); - rules.put(data[1], data[0]); + if (strLine.contains(":")) {//IPv6 + rulesAAAA.put(data[1], data[0]); + } else if (strLine.contains(".")) {//IPv4 + rulesA.put(data[1], data[0]); + } count++; } } @@ -132,7 +146,11 @@ public class RulesResolver implements Runnable { if (data[1].startsWith(".")) { data[1] = data[1].substring(1, data[1].length()); } - rules.put(data[1], data[2]); + if (strLine.contains(":")) {//IPv6 + rulesAAAA.put(data[1], data[2]); + } else if (strLine.contains(".")) {//IPv4 + rulesA.put(data[1], data[2]); + } count++; } }