Added support for IPv6 rules

This commit is contained in:
PeratX 2017-07-06 15:29:07 +08:00
parent 3cffe55387
commit e3829b0499
2 changed files with 33 additions and 11 deletions

View File

@ -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,

View File

@ -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<String, String> rules;
private static HashMap<String, String> rulesA;
private static HashMap<String, String> 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<String, String> 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++;
}
}