Added hosts download (UI) & Fixed local resolver bugs

This commit is contained in:
PeratX 2017-04-27 21:39:17 +08:00
parent 06274e7787
commit 7e18e494a9
12 changed files with 159 additions and 15 deletions

View File

@ -17,6 +17,7 @@ import android.util.Log;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.DnsServer;
import org.itxtech.daedalus.util.HostsProvider;
import org.itxtech.daedalus.util.HostsResolver;
import java.util.ArrayList;
@ -46,6 +47,10 @@ public class Daedalus extends Application {
add(new DnsServer("3", "123.206.21.48", R.string.server_aixyz_south_china));
}};
public static final List<HostsProvider> HOSTS_PROVIDERS = new ArrayList<HostsProvider>() {{
add(new HostsProvider("racaljk", "https://coding.net/u/scaffrey/p/hosts/git/raw/master/hosts"));
}};
public static final String[] DEFAULT_TEST_DOMAINS = new String[]{
"google.com",
"twitter.com",

View File

@ -24,10 +24,7 @@ import android.widget.TextView;
import org.itxtech.daedalus.BuildConfig;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.AboutFragment;
import org.itxtech.daedalus.fragment.DnsTestFragment;
import org.itxtech.daedalus.fragment.MainFragment;
import org.itxtech.daedalus.fragment.SettingsFragment;
import org.itxtech.daedalus.fragment.*;
/**
* Daedalus Project
@ -54,6 +51,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public static final int FRAGMENT_DNS_TEST = 1;
public static final int FRAGMENT_SETTINGS = 2;
public static final int FRAGMENT_ABOUT = 3;
public static final int FRAGMENT_HOSTS = 4;
private static MainActivity instance = null;
@ -61,6 +59,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private DnsTestFragment mDnsTest;
private SettingsFragment mSettings;
private AboutFragment mAbout;
private HostsFragment mHosts;
private int currentFragment = FRAGMENT_NONE;
private MainFragment.MainFragmentHandler mHandler = null;
@ -160,6 +159,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
case FRAGMENT_ABOUT:
menu.findItem(R.id.nav_about).setChecked(true);
break;
case FRAGMENT_HOSTS:
menu.findItem(R.id.nav_hosts).setChecked(true);
break;
}
}
@ -178,6 +180,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
case FRAGMENT_ABOUT:
toolbar.setTitle(R.string.action_about);
break;
case FRAGMENT_HOSTS:
toolbar.setTitle(R.string.action_hosts);
break;
}
}
@ -218,6 +223,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
toolbar.setTitle(R.string.action_about);
currentFragment = FRAGMENT_ABOUT;
break;
case FRAGMENT_HOSTS:
if (mHosts == null) {
mHosts = new HostsFragment();
}
transaction.replace(R.id.id_content, mHosts);
toolbar.setTitle(R.string.action_hosts);
currentFragment = FRAGMENT_HOSTS;
break;
}
transaction.commit();
}
@ -244,6 +257,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
mDnsTest = null;
mSettings = null;
mAbout = null;
mHosts = null;
instance = null;
System.gc();
}
@ -299,6 +313,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
changeFragment(FRAGMENT_MAIN);
}
if (id == R.id.nav_hosts) {
changeFragment(FRAGMENT_HOSTS);
}
if (id == R.id.nav_github) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/iTXTech/Daedalus")));
}

View File

@ -0,0 +1,35 @@
package org.itxtech.daedalus.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.HostsProvider;
/**
* 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, version 3.
*/
public class HostsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_hosts, container, false);
Spinner spinnerHosts = (Spinner) view.findViewById(R.id.spinner_hosts);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(Daedalus.getInstance(), android.R.layout.simple_list_item_1, HostsProvider.getHostsProviderNames());
spinnerHosts.setAdapter(spinnerArrayAdapter);
spinnerHosts.setSelection(0);
return view;
}
}

View File

@ -36,6 +36,9 @@ public class MainFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = (new MainFragmentHandler()).setFragment(this);
MainActivity.getInstance().setMainFragmentHandler(mHandler);
}
@Override
@ -54,10 +57,6 @@ public class MainFragment extends Fragment {
}
});
updateUserInterface();
mHandler = (new MainFragmentHandler()).setFragment(this);
MainActivity.getInstance().setMainFragmentHandler(mHandler);
return view;
}

View File

@ -521,8 +521,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
ip[i] = Integer.parseInt(block);
i++;
}
builder.setQuestions(null)
.addAnswer(new Record<>(dnsQueryName, Record.TYPE.getType(A.class), 1, 1, new A(ip[0], ip[1], ip[2], ip[3])));
builder.addAnswer(new Record<>(dnsQueryName, Record.TYPE.getType(A.class), 1, 64, new A(ip[0], ip[1], ip[2], ip[3])));
handleDnsResponse(parsedPacket, builder.build().toArray());
} else {
Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " , sending to " + destAddr);

View File

@ -0,0 +1,42 @@
package org.itxtech.daedalus.util;
import org.itxtech.daedalus.Daedalus;
import java.util.ArrayList;
/**
* 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, version 3.
*/
public class HostsProvider {
private String name;
private String downloadURL;
public HostsProvider(String name, String downloadURL) {
this.name = name;
this.downloadURL = downloadURL;
}
public String getName() {
return name;
}
public String getDownloadURL() {
return downloadURL;
}
public static String[] getHostsProviderNames() {
ArrayList<String> servers = new ArrayList<>(Daedalus.HOSTS_PROVIDERS.size());
for (HostsProvider hostsProvider : Daedalus.HOSTS_PROVIDERS) {
servers.add(hostsProvider.getName());
}
String[] stringServers = new String[Daedalus.HOSTS_PROVIDERS.size()];
return servers.toArray(stringServers);
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M14,2L6,2c-1.1,0 -1.99,0.9 -1.99,2L4,20c0,1.1 0.89,2 1.99,2L18,22c1.1,0 2,-0.9 2,-2L20,8l-6,-6zM16,18L8,18v-2h8v2zM16,14L8,14v-2h8v2zM13,9L13,3.5L18.5,9L13,9z"/>
</vector>

View File

@ -1,14 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView_about"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/fragment_about"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context="org.itxtech.daedalus.activity.AboutActivity">
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>

View File

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView_about"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="@+id/fragment_hosts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_hosts"
android:layout_alignParentTop="true"/>
<Button
android:text="@string/button_text_download_hosts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner_hosts"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"
android:id="@+id/button_download_hosts"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
</ScrollView>

View File

@ -11,6 +11,10 @@
android:title="@string/action_dns_test"
android:checked="false"
android:icon="@drawable/ic_verified_user_black_24dp"/>
<item android:id="@+id/nav_hosts"
android:title="@string/action_hosts"
android:checked="false"
android:icon="@drawable/ic_description_black_24dp"/>
</group>
<group android:checkableBehavior="single"

View File

@ -16,7 +16,7 @@
<string name="server_cutedns_north_china">CuteDNS! 华北</string>
<string name="secondary_server">备用 DNS 服务器</string>
<string name="notice_start_test">正在测试指定的 DNS 服务器……</string>
<string name="action_dns_test">DNS 测试</string>
<string name="action_dns_test">测试</string>
<string name="action_start_test">开始测试</string>
<string name="test_domain">测试域名:</string>
<string name="test_time_used">耗时:</string>
@ -49,4 +49,6 @@
<string name="settings_local_hosts_resolve">本地 hosts 解析</string>
<string name="notice_need_restart">重新启用 Daedalus 以应用设置。</string>
<string name="notice_need_storage_perm">本应用需要访问外部储存以实现本地 hosts 解析。</string>
<string name="action_hosts">Hosts</string>
<string name="button_text_download_hosts">下载 hosts</string>
</resources>

View File

@ -15,7 +15,7 @@
<string name="server_cutedns_north_china">CuteDNS! North China</string>
<string name="secondary_server">Secondary DNS</string>
<string name="notice_start_test">Testing specified DNS …</string>
<string name="action_dns_test">DNS test</string>
<string name="action_dns_test">Test</string>
<string name="action_start_test">Start test</string>
<string name="test_domain">Test domain:</string>
<string name="test_time_used">Time used:</string>
@ -50,4 +50,6 @@
<string name="notice_need_storage_perm">This application requires access to external storage for local host
resolution.
</string>
<string name="action_hosts">Hosts</string>
<string name="button_text_download_hosts">Download hosts</string>
</resources>