Optimized DNS Test method

This commit is contained in:
PeratX 2017-05-04 21:01:13 +08:00
parent 58cfcda2bb
commit 8ab3c0ad9a
3 changed files with 23 additions and 8 deletions

View File

@ -53,7 +53,7 @@ dependencies {
compile 'org.pcap4j:pcap4j-core:1.7.0'
compile 'org.pcap4j:pcap4j-packetfactory-propertiesbased:1.7.0'
compile 'de.measite.minidns:minidns-hla:0.2.1'
compile 'de.measite.minidns:minidns-core:0.2.1'
compile 'com.google.code.gson:gson:2.8.0'
testCompile 'junit:junit:4.12'

View File

@ -12,17 +12,18 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.*;
import de.measite.minidns.DNSClient;
import de.measite.minidns.DNSMessage;
import de.measite.minidns.Question;
import de.measite.minidns.Record;
import de.measite.minidns.record.A;
import de.measite.minidns.source.NetworkDataSource;
import de.measite.minidns.util.InetAddressUtil;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.util.DnsServerHelper;
import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.Arrays;
@ -78,9 +79,9 @@ public class DnsTestFragment extends Fragment {
addAll(Arrays.asList(servers.split(",")));
}
}};
DNSClient client = new DNSClient(null);
DNSQuery dnsQuery = new DNSQuery();
for (String dnsServer : dnsServers) {
testText = testServer(client, dnsServer, testDomain, testText);
testText = testServer(dnsQuery, dnsServer, testDomain, testText);
}
mHandler.obtainMessage(DnsTestHandler.MSG_TEST_DONE).sendToTarget();
} catch (Exception e) {
@ -88,7 +89,7 @@ public class DnsTestFragment extends Fragment {
}
}
private StringBuilder testServer(DNSClient client, String dnsServer, String testUrl, StringBuilder testText) {
private StringBuilder testServer(DNSQuery dnsQuery, String dnsServer, String testUrl, StringBuilder testText) {
Log.d(TAG, "Testing DNS " + dnsServer);
testText.append(getResources().getString(R.string.test_domain)).append(" ").append(testUrl).append("\n").append(getResources().getString(R.string.test_dns_server)).append(" ").append(dnsServer);
@ -103,7 +104,7 @@ public class DnsTestFragment extends Fragment {
try {
long startTime = System.currentTimeMillis();
DNSMessage responseMessage = client.query(message.build(), InetAddressUtil.ipv4From(dnsServer));
DNSMessage responseMessage = dnsQuery.query(message.build(), InetAddressUtil.ipv4From(dnsServer), 53);//Auto forward ports
long endTime = System.currentTimeMillis();
Set<A> answers = responseMessage.getAnswersFor(question);
@ -212,4 +213,10 @@ public class DnsTestFragment extends Fragment {
}
}
}
private class DNSQuery extends NetworkDataSource {
public DNSMessage query(DNSMessage message, InetAddress address, int port) throws IOException {
return queryUdp(message, address, port);
}
}
}

View File

@ -22,7 +22,15 @@ import java.util.Iterator;
import java.util.LinkedList;
/**
* @author PeratX
* Daedalus Project
*
* @author iTXTech
* @link https://itxtech.org
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
public class TcpDnsProvider extends UdpDnsProvider {
@ -125,7 +133,7 @@ public class TcpDnsProvider extends UdpDnsProvider {
// Packets to be sent to the real DNS server will need to be protected from the VPN
dnsSocket = SocketChannel.open().socket();
Log.d(TAG, String.valueOf(service.protect(dnsSocket)));
service.protect(dnsSocket);
SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DnsServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort()));
dnsSocket.connect(address, 5000);