Added custom configuration storage for Custom DNS servers
This commit is contained in:
parent
42b445d0be
commit
5bdfeb9a60
@ -53,6 +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 'com.google.code.gson:gson:2.8.0'
|
||||
|
||||
testCompile 'junit:junit:4.12'
|
||||
}
|
||||
|
@ -14,12 +14,18 @@ import android.os.Build;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.util.Log;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import org.itxtech.daedalus.activity.MainActivity;
|
||||
import org.itxtech.daedalus.service.DaedalusVpnService;
|
||||
import org.itxtech.daedalus.util.Configurations;
|
||||
import org.itxtech.daedalus.util.DnsServer;
|
||||
import org.itxtech.daedalus.util.HostsProvider;
|
||||
import org.itxtech.daedalus.util.HostsResolver;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@ -37,14 +43,16 @@ import java.util.List;
|
||||
public class Daedalus extends Application {
|
||||
private static final String SHORTCUT_ID_ACTIVATE = "shortcut_activate";
|
||||
|
||||
private static final String TAG = "Daedalus";
|
||||
|
||||
public static final List<DnsServer> DNS_SERVERS = new ArrayList<DnsServer>() {{
|
||||
/*add(new DnsServer("0", "113.107.249.56", R.string.server_cutedns_north_china));
|
||||
add(new DnsServer("1", "120.27.103.230", R.string.server_cutedns_east_china));
|
||||
add(new DnsServer("2", "123.206.61.167", R.string.server_cutedns_south_china));*/
|
||||
add(new DnsServer("0", "115.159.220.214", R.string.server_puredns_east_china));
|
||||
add(new DnsServer("1", "123.207.137.88", R.string.server_puredns_north_china));
|
||||
add(new DnsServer("2", "115.159.146.99", R.string.server_aixyz_east_china));
|
||||
add(new DnsServer("3", "123.206.21.48", R.string.server_aixyz_south_china));
|
||||
add(new DnsServer("115.159.220.214", R.string.server_puredns_east_china));
|
||||
add(new DnsServer("123.207.137.88", R.string.server_puredns_north_china));
|
||||
add(new DnsServer("115.159.146.99", R.string.server_aixyz_east_china));
|
||||
add(new DnsServer("123.206.21.48", R.string.server_aixyz_south_china));
|
||||
}};
|
||||
|
||||
public static final List<HostsProvider> HOSTS_PROVIDERS = new ArrayList<HostsProvider>() {{
|
||||
@ -61,7 +69,10 @@ public class Daedalus extends Application {
|
||||
"wikipedia.org"
|
||||
};
|
||||
|
||||
public static Configurations configurations;
|
||||
|
||||
public static String hostsPath;
|
||||
public static String configPath;
|
||||
|
||||
private static Daedalus instance = null;
|
||||
private static SharedPreferences prefs;
|
||||
@ -71,18 +82,28 @@ public class Daedalus extends Application {
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
initConfig();
|
||||
mHostsResolver = new Thread(new HostsResolver());
|
||||
mHostsResolver.start();
|
||||
|
||||
hostsPath = getExternalFilesDir(null).getPath() + "/hosts";
|
||||
configPath = getExternalFilesDir(null).getPath() + "/config.json";
|
||||
|
||||
initData();
|
||||
|
||||
instance = this;
|
||||
}
|
||||
|
||||
private void initConfig() {
|
||||
private void initData() {
|
||||
PreferenceManager.setDefaultValues(this, R.xml.perf_settings, false);
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
|
||||
configurations = Configurations.load(new File(configPath));
|
||||
}
|
||||
|
||||
public static <T> T parseJson(Class<T> beanClass, JsonReader reader) throws JsonParseException {
|
||||
GsonBuilder builder = new GsonBuilder();
|
||||
Gson gson = builder.create();
|
||||
return gson.fromJson(reader, beanClass);
|
||||
}
|
||||
|
||||
public static final int REQUEST_EXTERNAL_STORAGE = 1;
|
||||
@ -113,6 +134,7 @@ public class Daedalus extends Application {
|
||||
|
||||
@Override
|
||||
public void onTerminate() {
|
||||
Log.d("Daedalus", "onTerminate");
|
||||
super.onTerminate();
|
||||
|
||||
instance = null;
|
||||
|
@ -52,6 +52,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
public static final int FRAGMENT_SETTINGS = 2;
|
||||
public static final int FRAGMENT_ABOUT = 3;
|
||||
public static final int FRAGMENT_HOSTS = 4;
|
||||
public static final int FRAGMENT_DNS_SERVERS = 5;
|
||||
|
||||
private static MainActivity instance = null;
|
||||
|
||||
@ -60,6 +61,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
private SettingsFragment mSettings;
|
||||
private AboutFragment mAbout;
|
||||
private HostsFragment mHosts;
|
||||
private DnsServersFragment mDnsServers;
|
||||
private int currentFragment = FRAGMENT_NONE;
|
||||
|
||||
private MainFragment.MainFragmentHandler mHandler = null;
|
||||
@ -96,13 +98,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
((TextView) navigationView.getHeaderView(0).findViewById(R.id.textView_nav_git_commit)).setText(getString(R.string.nav_git_commit) + " " + BuildConfig.GIT_COMMIT);
|
||||
|
||||
if (getIntent().getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE) == FRAGMENT_NONE) {
|
||||
FragmentManager fm = getFragmentManager();
|
||||
FragmentTransaction transaction = fm.beginTransaction();
|
||||
if (mMain == null) {
|
||||
mMain = new MainFragment();
|
||||
}
|
||||
transaction.replace(R.id.id_content, mMain).commit();
|
||||
currentFragment = FRAGMENT_MAIN;
|
||||
switchFragment(FRAGMENT_MAIN);
|
||||
}
|
||||
|
||||
updateUserInterface(getIntent());
|
||||
@ -162,6 +158,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
case FRAGMENT_HOSTS:
|
||||
menu.findItem(R.id.nav_hosts).setChecked(true);
|
||||
break;
|
||||
case FRAGMENT_DNS_SERVERS:
|
||||
menu.findItem(R.id.nav_dns_server).setChecked(true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -183,10 +181,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
case FRAGMENT_HOSTS:
|
||||
toolbar.setTitle(R.string.action_hosts);
|
||||
break;
|
||||
case FRAGMENT_DNS_SERVERS:
|
||||
toolbar.setTitle(R.string.action_dns_servers);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void changeFragment(int fragment) {
|
||||
private void switchFragment(int fragment) {
|
||||
FragmentManager fm = getFragmentManager();
|
||||
FragmentTransaction transaction = fm.beginTransaction();
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
@ -231,6 +232,14 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
toolbar.setTitle(R.string.action_hosts);
|
||||
currentFragment = FRAGMENT_HOSTS;
|
||||
break;
|
||||
case FRAGMENT_DNS_SERVERS:
|
||||
if (mDnsServers == null) {
|
||||
mDnsServers = new DnsServersFragment();
|
||||
}
|
||||
transaction.replace(R.id.id_content, mDnsServers);
|
||||
toolbar.setTitle(R.string.action_dns_servers);
|
||||
currentFragment = FRAGMENT_DNS_SERVERS;
|
||||
break;
|
||||
}
|
||||
transaction.commit();
|
||||
}
|
||||
@ -241,7 +250,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
if (drawer.isDrawerOpen(GravityCompat.START)) {
|
||||
drawer.closeDrawer(GravityCompat.START);
|
||||
} else if (currentFragment != FRAGMENT_MAIN) {
|
||||
changeFragment(FRAGMENT_MAIN);
|
||||
switchFragment(FRAGMENT_MAIN);
|
||||
updateNavigationMenu();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
@ -287,7 +296,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
|
||||
int fragment = intent.getIntExtra(LAUNCH_FRAGMENT, FRAGMENT_NONE);
|
||||
if (fragment != FRAGMENT_NONE) {
|
||||
changeFragment(fragment);
|
||||
switchFragment(fragment);
|
||||
updateNavigationMenu();
|
||||
}
|
||||
}
|
||||
@ -298,23 +307,27 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
int id = item.getItemId();
|
||||
|
||||
if (id == R.id.nav_settings) {
|
||||
changeFragment(FRAGMENT_SETTINGS);
|
||||
switchFragment(FRAGMENT_SETTINGS);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_about) {
|
||||
changeFragment(FRAGMENT_ABOUT);
|
||||
switchFragment(FRAGMENT_ABOUT);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_dns_test) {
|
||||
changeFragment(FRAGMENT_DNS_TEST);
|
||||
switchFragment(FRAGMENT_DNS_TEST);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_home) {
|
||||
changeFragment(FRAGMENT_MAIN);
|
||||
switchFragment(FRAGMENT_MAIN);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_hosts) {
|
||||
changeFragment(FRAGMENT_HOSTS);
|
||||
switchFragment(FRAGMENT_HOSTS);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_dns_server) {
|
||||
switchFragment(FRAGMENT_DNS_SERVERS);
|
||||
}
|
||||
|
||||
if (id == R.id.nav_github) {
|
||||
|
@ -0,0 +1,77 @@
|
||||
package org.itxtech.daedalus.fragment;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.v7.widget.LinearLayoutManager;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
import org.itxtech.daedalus.R;
|
||||
import org.itxtech.daedalus.util.CustomDnsServer;
|
||||
|
||||
/**
|
||||
* 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 DnsServersFragment extends Fragment {
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_dns_servers, container, false);
|
||||
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView_dns_servers);
|
||||
LinearLayoutManager manager = new LinearLayoutManager(getActivity());
|
||||
recyclerView.setLayoutManager(manager);
|
||||
DnsServerAdapter adapter = new DnsServerAdapter();
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab_add_server);
|
||||
return view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
|
||||
Daedalus.configurations.save();
|
||||
}
|
||||
|
||||
private class DnsServerAdapter extends RecyclerView.Adapter<ViewHolder> {
|
||||
@Override
|
||||
public void onBindViewHolder(ViewHolder holder, int position) {
|
||||
CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(position);
|
||||
holder.textViewName.setText(server.getName());
|
||||
holder.textViewAddress.setText(server.getAddress() + ":" + server.getPort());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return Daedalus.configurations.getCustomDnsServers().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_dns_servers, parent, false);
|
||||
return new ViewHolder(view);
|
||||
}
|
||||
}
|
||||
|
||||
private static class ViewHolder extends RecyclerView.ViewHolder {
|
||||
private final TextView textViewName;
|
||||
private final TextView textViewAddress;
|
||||
|
||||
ViewHolder(View view) {
|
||||
super(view);
|
||||
textViewName = (TextView) view.findViewById(R.id.textView_custom_dns_name);
|
||||
textViewAddress = (TextView) view.findViewById(R.id.textView_custom_dns_address);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package org.itxtech.daedalus.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import org.itxtech.daedalus.Daedalus;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author PeratX
|
||||
*/
|
||||
public class Configurations {
|
||||
private static File file;
|
||||
|
||||
private ArrayList<CustomDnsServer> customDnsServers;
|
||||
|
||||
public ArrayList<CustomDnsServer> getCustomDnsServers() {
|
||||
if (customDnsServers == null) {
|
||||
customDnsServers = new ArrayList<>();
|
||||
}
|
||||
return customDnsServers;
|
||||
}
|
||||
|
||||
public static Configurations load(File file) {
|
||||
Configurations.file = file;
|
||||
Configurations config = null;
|
||||
try {
|
||||
config = Daedalus.parseJson(Configurations.class, new JsonReader(new FileReader(file)));
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
|
||||
if (config == null) {
|
||||
config = new Configurations();
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
public void save() {
|
||||
try {
|
||||
FileWriter writer = new FileWriter(file);
|
||||
new Gson().toJson(this, writer);
|
||||
writer.flush();
|
||||
writer.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package org.itxtech.daedalus.util;
|
||||
|
||||
/**
|
||||
* 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 CustomDnsServer {
|
||||
private String name;
|
||||
private String address;
|
||||
private int port;
|
||||
|
||||
public CustomDnsServer(String name, String address, int port) {
|
||||
this.name = name;
|
||||
this.address = address;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
@ -16,12 +16,14 @@ import java.util.ArrayList;
|
||||
* the Free Software Foundation, version 3.
|
||||
*/
|
||||
public class DnsServer {
|
||||
private static int totalId = 0;
|
||||
|
||||
private String id;
|
||||
private String address;
|
||||
private int description;
|
||||
private int description = 0;
|
||||
|
||||
public DnsServer(String id, String address, int description) {
|
||||
this.id = id;
|
||||
public DnsServer(String address, int description) {
|
||||
this.id = String.valueOf(totalId++);
|
||||
this.address = address;
|
||||
this.description = description;
|
||||
}
|
||||
|
9
app/src/main/res/drawable/ic_device_hub_black_24dp.xml
Normal file
9
app/src/main/res/drawable/ic_device_hub_black_24dp.xml
Normal 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="M17,16l-4,-4V8.82C14.16,8.4 15,7.3 15,6c0,-1.66 -1.34,-3 -3,-3S9,4.34 9,6c0,1.3 0.84,2.4 2,2.82V12l-4,4H3v5h5v-3.05l4,-4.2 4,4.2V21h5v-5h-4z"/>
|
||||
</vector>
|
@ -5,8 +5,7 @@
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true"
|
||||
tools:context="org.itxtech.daedalus.activity.MainActivity">
|
||||
android:fitsSystemWindows="true">
|
||||
|
||||
<android.support.design.widget.AppBarLayout
|
||||
android:layout_height="wrap_content"
|
||||
|
23
app/src/main/res/layout/fragment_dns_servers.xml
Normal file
23
app/src/main/res/layout/fragment_dns_servers.xml
Normal file
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/fragment_dns_servers"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab_add_server"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="bottom|end"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
android:src="@drawable/ic_device_hub_black_24dp"/>
|
||||
|
||||
<android.support.v7.widget.RecyclerView
|
||||
android:id="@+id/recyclerView_dns_servers"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scrollbars="horizontal" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"/>
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
28
app/src/main/res/layout/list_dns_servers.xml
Normal file
28
app/src/main/res/layout/list_dns_servers.xml
Normal file
@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
android:paddingLeft="@dimen/activity_horizontal_margin"
|
||||
android:paddingRight="@dimen/activity_horizontal_margin"
|
||||
android:paddingTop="4dp"
|
||||
android:paddingBottom="4dp">
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/textView_custom_dns_name"
|
||||
android:layout_marginTop="30dp"
|
||||
android:id="@+id/textView_custom_dns_address"
|
||||
android:textAppearance="?android:attr/textAppearanceSmall"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"/>
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:id="@+id/textView_custom_dns_name"
|
||||
android:textAppearance="?android:attr/textAppearanceLarge"
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentStart="true"/>
|
||||
</RelativeLayout>
|
@ -1,5 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<menu
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
>
|
||||
|
||||
<group android:checkableBehavior="single"
|
||||
android:id="@+id/nav_group_main">
|
||||
@ -11,6 +13,13 @@
|
||||
android:title="@string/action_dns_test"
|
||||
android:checked="false"
|
||||
android:icon="@drawable/ic_verified_user_black_24dp"/>
|
||||
</group>
|
||||
|
||||
<group android:checkableBehavior="single"
|
||||
android:id="@+id/nav_group_profiles">
|
||||
<item android:id="@+id/nav_dns_server"
|
||||
android:title="@string/action_dns_servers"
|
||||
android:icon="@drawable/ic_device_hub_black_24dp"/>
|
||||
<item android:id="@+id/nav_hosts"
|
||||
android:title="@string/action_hosts"
|
||||
android:checked="false"
|
||||
|
@ -56,4 +56,5 @@
|
||||
<string name="hosts_last_modified">最后修改:</string>
|
||||
<string name="hosts_size">大小:</string>
|
||||
<string name="settings_hosts_pan_domain_resolution">Hosts 域名泛解析</string>
|
||||
<string name="action_dns_servers">服务器</string>
|
||||
</resources>
|
@ -56,4 +56,5 @@
|
||||
<string name="hosts_last_modified">Last modified:</string>
|
||||
<string name="hosts_size">Size:</string>
|
||||
<string name="settings_hosts_pan_domain_resolution">Hosts pan domain name resolution</string>
|
||||
<string name="action_dns_servers">Servers</string>
|
||||
</resources>
|
||||
|
Loading…
Reference in New Issue
Block a user