Implemented ServerTest

This commit is contained in:
PeratX 2017-04-05 22:48:42 +08:00
parent 9329ac3da6
commit c17631f8c1
9 changed files with 133 additions and 7 deletions

View File

@ -32,5 +32,6 @@ dependencies {
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile "de.measite.minidns:minidns-hla:0.2.1"
testCompile 'junit:junit:4.12'
}

View File

@ -23,4 +23,17 @@ public class DnsServers {
return "123.206.61.167";
}
}
public static String getDnsServerAddress(int id) {
switch (id) {
case R.string.server_east_china:
return "120.27.103.230";
case R.string.server_north_china:
return "113.107.249.56";
case R.string.server_south_china:
return "123.206.61.167";
default:
return "123.206.61.167";
}
}
}

View File

@ -1,19 +1,42 @@
package org.itxtech.daedalus;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
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.util.InetAddressUtil;
import java.net.InetAddress;
import java.util.*;
public class ServerTestActivity extends AppCompatActivity {
private static final int MSG_DISPLAY_STATUS = 0;
private static final int MSG_TEST_DONE = 1;
private boolean testing = false;
private Thread mThread = null;
private Handler mHandler = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_server_test);
final TextView textViewTestInfo = (TextView) findViewById(R.id.textView_test_info);
final Spinner spinnerServerChoice = (Spinner) findViewById(R.id.spinner_server_choice);
final Button startTestBut = (Button) findViewById(R.id.button_start_test);
startTestBut.setOnClickListener(new View.OnClickListener() {
@Override
@ -21,12 +44,88 @@ public class ServerTestActivity extends AppCompatActivity {
Snackbar.make(v, R.string.start_test, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
startTestBut.setVisibility(View.INVISIBLE);
if (mThread == null) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
String testUrl = "www.google.com";
String testText = "";
String[] dnsServers = {"8.8.8.8", "114.114.114.114", DnsServers.getDnsServerAddress(spinnerServerChoice.getSelectedItem().toString())};
DNSClient client = new DNSClient(null);
for (String dnsServer : dnsServers) {
testText = testServer(client, dnsServer, testUrl, testText);
}
mHandler.obtainMessage(MSG_TEST_DONE).sendToTarget();
} catch (Exception e) {
Log.e("DVpn", e.toString());
}
}
private String testServer(DNSClient client, String dnsServer, String testUrl, String testText) {
try {
Log.d("Dvpn", "Testing DNS " + dnsServer);
testText += getResources().getString(R.string.test_server_address) + testUrl + "\n"
+ getResources().getString(R.string.test_dns_server) + dnsServer;
long startTime = System.currentTimeMillis();
Question question = new Question(testUrl, Record.TYPE.getType(A.class));
DNSMessage.Builder message = DNSMessage.builder();
message.setQuestion(question);
message.setId((new Random()).nextInt());
message.setRecursionDesired(true);
message.getEdnsBuilder().setUdpPayloadSize(1024).setDnssecOk(false);
DNSMessage responseMessage = client.query(message.build(), InetAddressUtil.ipv4From(dnsServer));
long endTime = System.currentTimeMillis();
Set<A> answers = responseMessage.getAnswersFor(question);
for (A a : answers) {
InetAddress inetAddress = a.getInetAddress();
testText += "\n" + getResources().getString(R.string.test_resolved_address) + inetAddress.getHostAddress();
}
testText += "\n" + getResources().getString(R.string.test_time_used) + String.valueOf(endTime - startTime) + " ms\n\n";
mHandler.obtainMessage(MSG_DISPLAY_STATUS, testText).sendToTarget();
} catch (Exception e) {
Log.e("DVpn", e.toString());
}
return testText;
}
};
mThread = new Thread(runnable);
mThread.start();
}
}
});
mHandler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case MSG_DISPLAY_STATUS:
textViewTestInfo.setText((String) msg.obj);
break;
case MSG_TEST_DONE:
testing = false;
startTestBut.setVisibility(View.VISIBLE);
mThread = null;
break;
}
}
};
}
@Override
protected void onRestart() {
super.onRestart();
if (testing) {
final Button startTestBut = (Button) findViewById(R.id.button_start_test);
startTestBut.setVisibility(View.INVISIBLE);
}
}
}

View File

@ -24,7 +24,7 @@
android:text="@string/notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:id="@+id/textView_notice"
android:textSize="15sp"
android:layout_marginTop="100dp"
android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>

View File

@ -19,7 +19,7 @@
android:text="@string/about"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:id="@+id/textView_about"
android:textSize="20sp" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/>
</RelativeLayout>
</ScrollView>

View File

@ -14,12 +14,17 @@
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner" android:layout_alignParentTop="true"
android:id="@+id/spinner_server_choice" android:layout_alignParentTop="true"
android:entries="@array/dns_server_options"/>
<Button
android:text="@string/action_start_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/spinner"
android:layout_height="wrap_content" android:layout_below="@+id/spinner_server_choice"
android:layout_alignParentStart="true" android:layout_marginTop="20dp" android:id="@+id/button_start_test"
android:layout_alignParentEnd="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/button_start_test"
android:layout_alignParentStart="true" android:layout_marginTop="10dp" android:id="@+id/textView_test_info"
android:textSize="18sp"/>
</RelativeLayout>

View File

@ -24,7 +24,7 @@
android:text="@string/notice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:id="@+id/textView_notice"
android:textAppearance="@android:style/TextAppearance.Material" android:fontFamily="sans-serif"
android:layout_above="@+id/button_activate"
android:layout_centerHorizontal="true"

View File

@ -22,7 +22,11 @@
<string name="server_east_china">华东</string>
<string name="server_north_china">华北</string>
<string name="secondary_server">备用 DNS 服务器</string>
<string name="start_test">正在开始测试 DNS 服务器</string>
<string name="start_test">正在测试指定的 DNS 服务器</string>
<string name="action_server_test">测试服务器</string>
<string name="action_start_test">开始测试</string>
<string name="test_server_address">测试服务器:</string>
<string name="test_time_used">耗时:</string>
<string name="test_dns_server">DNS 服务器:</string>
<string name="test_resolved_address">解析的地址:</string>
</resources>

View File

@ -23,7 +23,11 @@
<string name="server_east_china">East China</string>
<string name="server_north_china">North China</string>
<string name="secondary_server">Secondary DNS server</string>
<string name="start_test">Starting to test DNS server</string>
<string name="start_test">Testing specified DNS server</string>
<string name="action_server_test">Server test</string>
<string name="action_start_test">Start test</string>
<string name="test_server_address">Test server address:</string>
<string name="test_time_used">Time used:</string>
<string name="test_dns_server">DNS server:</string>
<string name="test_resolved_address">Resolved address:</string>
</resources>