From b9ff5188f4f9688f6c5d53abb758c0385eb0cbdf Mon Sep 17 00:00:00 2001 From: PeratX <1215714524@qq.com> Date: Sun, 4 Jun 2017 15:48:46 +0800 Subject: [PATCH] Improved DNS server structure --- app/src/main/assets/about_html/favicon.ico | Bin 0 -> 5694 bytes app/src/main/assets/about_html/index.html | 4 +- app/src/main/assets/about_html/index_zh.html | 4 +- .../java/org/itxtech/daedalus/Daedalus.java | 18 +++-- .../daedalus/activity/MainActivity.java | 6 +- .../fragment/DnsServerConfigFragment.java | 14 ++-- .../daedalus/fragment/DnsServersFragment.java | 22 +++--- .../daedalus/fragment/DnsTestFragment.java | 23 +++---- .../fragment/GlobalConfigFragment.java | 22 +++--- .../daedalus/provider/TcpDnsProvider.java | 4 +- .../daedalus/provider/UdpDnsProvider.java | 6 +- .../receiver/BootBroadcastReceiver.java | 6 +- .../daedalus/service/DaedalusVpnService.java | 6 +- .../itxtech/daedalus/util/Configurations.java | 13 ++-- .../util/server/AbstractDNSServer.java | 47 +++++++++++++ .../CustomDNSServer.java} | 27 ++------ .../{DnsServer.java => server/DNSServer.java} | 29 +++----- .../DNSServerHelper.java} | 63 ++++++++++-------- 18 files changed, 172 insertions(+), 142 deletions(-) create mode 100644 app/src/main/assets/about_html/favicon.ico create mode 100644 app/src/main/java/org/itxtech/daedalus/util/server/AbstractDNSServer.java rename app/src/main/java/org/itxtech/daedalus/util/{CustomDnsServer.java => server/CustomDNSServer.java} (60%) rename app/src/main/java/org/itxtech/daedalus/util/{DnsServer.java => server/DNSServer.java} (64%) rename app/src/main/java/org/itxtech/daedalus/util/{DnsServerHelper.java => server/DNSServerHelper.java} (63%) diff --git a/app/src/main/assets/about_html/favicon.ico b/app/src/main/assets/about_html/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..3a766b50298f411608267eab2273b3b8c6e6ae7c GIT binary patch literal 5694 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 literal 0 HcmV?d00001 diff --git a/app/src/main/assets/about_html/index.html b/app/src/main/assets/about_html/index.html index e9dd58f..7a9f7f5 100644 --- a/app/src/main/assets/about_html/index.html +++ b/app/src/main/assets/about_html/index.html @@ -53,6 +53,7 @@

+

@@ -69,8 +70,7 @@
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
+ 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.

diff --git a/app/src/main/assets/about_html/index_zh.html b/app/src/main/assets/about_html/index_zh.html index 1a02d85..14cdfd1 100644 --- a/app/src/main/assets/about_html/index_zh.html +++ b/app/src/main/assets/about_html/index_zh.html @@ -53,6 +53,7 @@

+

@@ -69,8 +70,7 @@
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
+ 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.

diff --git a/app/src/main/java/org/itxtech/daedalus/Daedalus.java b/app/src/main/java/org/itxtech/daedalus/Daedalus.java index 05ae069..86da3a8 100644 --- a/app/src/main/java/org/itxtech/daedalus/Daedalus.java +++ b/app/src/main/java/org/itxtech/daedalus/Daedalus.java @@ -19,7 +19,11 @@ 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.*; +import org.itxtech.daedalus.util.Configurations; +import org.itxtech.daedalus.util.Logger; +import org.itxtech.daedalus.util.Rule; +import org.itxtech.daedalus.util.RulesResolver; +import org.itxtech.daedalus.util.server.DNSServer; import java.io.File; import java.util.ArrayList; @@ -49,12 +53,12 @@ public class Daedalus extends Application { private static final String SHORTCUT_ID_ACTIVATE = "shortcut_activate"; - public static final List DNS_SERVERS = new ArrayList() {{ - 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)); - add(new DnsServer("119.29.105.234", R.string.server_cutedns_south_china)); + public static final List DNS_SERVERS = new ArrayList() {{ + 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)); + add(new DNSServer("119.29.105.234", R.string.server_cutedns_south_china)); }}; public static final List RULES = new ArrayList() {{ diff --git a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java index ecff578..008158f 100644 --- a/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java +++ b/app/src/main/java/org/itxtech/daedalus/activity/MainActivity.java @@ -26,7 +26,7 @@ import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; import org.itxtech.daedalus.fragment.*; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServerHelper; +import org.itxtech.daedalus.util.server.DNSServerHelper; /** * Daedalus Project @@ -175,8 +175,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public void onActivityResult(int request, int result, Intent data) { if (result == Activity.RESULT_OK) { - DaedalusVpnService.primaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getPrimary()); - DaedalusVpnService.secondaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getSecondary()); + DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); + DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary()); Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE)); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java index 2f9a0f8..0c1c888 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServerConfigFragment.java @@ -13,8 +13,8 @@ import android.view.ViewGroup; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; import org.itxtech.daedalus.activity.ConfigActivity; -import org.itxtech.daedalus.util.CustomDnsServer; -import org.itxtech.daedalus.util.DnsServer; +import org.itxtech.daedalus.util.server.CustomDNSServer; +import org.itxtech.daedalus.util.server.DNSServer; /** * Daedalus Project @@ -70,7 +70,7 @@ public class DnsServerConfigFragment extends ConfigFragment { index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE); if (index != ConfigActivity.ID_NONE) { - CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index); + CustomDNSServer server = Daedalus.configurations.getCustomDNSServers().get(index); serverName.setText(server.getName()); serverName.setSummary(server.getName()); serverAddress.setText(server.getAddress()); @@ -80,7 +80,7 @@ public class DnsServerConfigFragment extends ConfigFragment { } else { serverName.setText(""); serverAddress.setText(""); - String port = String.valueOf(DnsServer.DNS_SERVER_DEFAULT_PORT); + String port = String.valueOf(DNSServer.DNS_SERVER_DEFAULT_PORT); serverPort.setText(port); serverPort.setSummary(port); } @@ -104,9 +104,9 @@ public class DnsServerConfigFragment extends ConfigFragment { } if (index == ConfigActivity.ID_NONE) { - Daedalus.configurations.getCustomDnsServers().add(new CustomDnsServer(serverName, serverAddress, Integer.parseInt(serverPort))); + Daedalus.configurations.getCustomDNSServers().add(new CustomDNSServer(serverName, serverAddress, Integer.parseInt(serverPort))); } else { - CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(index); + CustomDNSServer server = Daedalus.configurations.getCustomDNSServers().get(index); server.setName(serverName); server.setAddress(serverAddress); server.setPort(Integer.parseInt(serverPort)); @@ -120,7 +120,7 @@ public class DnsServerConfigFragment extends ConfigFragment { .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - Daedalus.configurations.getCustomDnsServers().remove(index); + Daedalus.configurations.getCustomDNSServers().remove(index); getActivity().finish(); } }) diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java index f2af34d..d9c6f09 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsServersFragment.java @@ -14,8 +14,8 @@ import android.widget.TextView; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; import org.itxtech.daedalus.activity.ConfigActivity; -import org.itxtech.daedalus.util.CustomDnsServer; -import org.itxtech.daedalus.util.DnsServerHelper; +import org.itxtech.daedalus.util.server.CustomDNSServer; +import org.itxtech.daedalus.util.server.DNSServerHelper; /** * Daedalus Project @@ -30,7 +30,7 @@ import org.itxtech.daedalus.util.DnsServerHelper; */ public class DnsServersFragment extends ToolbarFragment { private DnsServersFragment.DnsServerAdapter adapter; - private CustomDnsServer server = null; + private CustomDNSServer server = null; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -45,8 +45,8 @@ public class DnsServersFragment extends ToolbarFragment { public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) { if (viewHolder instanceof ViewHolder) { int index = ((ViewHolder) viewHolder).getIndex(); - if (index < Daedalus.configurations.getCustomDnsServers().size() && - DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDnsServers().get(index))) { + if (index < Daedalus.configurations.getCustomDNSServers().size() && + DNSServerHelper.isInUsing(Daedalus.configurations.getCustomDNSServers().get(index))) { return 0; } } @@ -61,8 +61,8 @@ public class DnsServersFragment extends ToolbarFragment { @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { int position = viewHolder.getAdapterPosition(); - server = Daedalus.configurations.getCustomDnsServers().get(position); - Daedalus.configurations.getCustomDnsServers().remove(position); + server = Daedalus.configurations.getCustomDNSServers().get(position); + Daedalus.configurations.getCustomDNSServers().remove(position); Snackbar.make(view, R.string.action_removed, Snackbar.LENGTH_LONG) .setAction(R.string.action_undo, new SnackbarClickListener(position)).show(); adapter.notifyItemRemoved(position); @@ -98,7 +98,7 @@ public class DnsServersFragment extends ToolbarFragment { @Override public void onClick(View v) { - Daedalus.configurations.getCustomDnsServers().add(position, server); + Daedalus.configurations.getCustomDNSServers().add(position, server); adapter.notifyItemInserted(position); } } @@ -122,7 +122,7 @@ public class DnsServersFragment extends ToolbarFragment { private class DnsServerAdapter extends RecyclerView.Adapter { @Override public void onBindViewHolder(ViewHolder holder, int position) { - CustomDnsServer server = Daedalus.configurations.getCustomDnsServers().get(position); + CustomDNSServer server = Daedalus.configurations.getCustomDNSServers().get(position); holder.setIndex(position); holder.textViewName.setText(server.getName()); holder.textViewAddress.setText(server.getAddress() + ":" + server.getPort()); @@ -130,7 +130,7 @@ public class DnsServersFragment extends ToolbarFragment { @Override public int getItemCount() { - return Daedalus.configurations.getCustomDnsServers().size(); + return Daedalus.configurations.getCustomDNSServers().size(); } @Override @@ -162,7 +162,7 @@ public class DnsServersFragment extends ToolbarFragment { @Override public void onClick(View v) { - if (!DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDnsServers().get(index))) { + if (!DNSServerHelper.isInUsing(Daedalus.configurations.getCustomDNSServers().get(index))) { Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class) .putExtra(ConfigActivity.LAUNCH_ACTION_ID, index) .putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER) diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java index 602b6fd..39057f8 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/DnsTestFragment.java @@ -16,8 +16,9 @@ import de.measite.minidns.Record; import de.measite.minidns.source.NetworkDataSource; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.util.DnsServerHelper; import org.itxtech.daedalus.util.Logger; +import org.itxtech.daedalus.util.server.AbstractDNSServer; +import org.itxtech.daedalus.util.server.DNSServerHelper; import java.io.IOException; import java.net.InetAddress; @@ -46,17 +47,13 @@ public class DnsTestFragment extends ToolbarFragment { this.type = type; } - private String getName() { - return name; - } - private Record.TYPE getType() { return type; } @Override public String toString() { - return getName(); + return name; } } @@ -71,9 +68,9 @@ public class DnsTestFragment extends ToolbarFragment { final TextView textViewTestInfo = (TextView) view.findViewById(R.id.textView_test_info); final Spinner spinnerServerChoice = (Spinner) view.findViewById(R.id.spinner_server_choice); - ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DnsServerHelper.getNames(Daedalus.getInstance())); + ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DNSServerHelper.getAllServers()); spinnerServerChoice.setAdapter(spinnerArrayAdapter); - spinnerServerChoice.setSelection(DnsServerHelper.getPosition(DnsServerHelper.getPrimary())); + spinnerServerChoice.setSelection(DNSServerHelper.getPosition(DNSServerHelper.getPrimary())); ArrayList types = new ArrayList() {{ add(new Type("A", Record.TYPE.A)); @@ -101,21 +98,21 @@ public class DnsTestFragment extends ToolbarFragment { ArrayAdapter typeAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, types); spinnerType.setAdapter(typeAdapter); - final AutoCompleteTextView textViewTestUrl = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView_test_url); + final AutoCompleteTextView textViewTestDomain = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTextView_test_url); ArrayAdapter autoCompleteArrayAdapter = new ArrayAdapter<>(Daedalus.getInstance(), android.R.layout.simple_list_item_1, Daedalus.DEFAULT_TEST_DOMAINS); - textViewTestUrl.setAdapter(autoCompleteArrayAdapter); + textViewTestDomain.setAdapter(autoCompleteArrayAdapter); mRunnable = new Runnable() { @Override public void run() { try { - String testDomain = textViewTestUrl.getText().toString(); + String testDomain = textViewTestDomain.getText().toString(); if (testDomain.equals("")) { testDomain = Daedalus.DEFAULT_TEST_DOMAINS[0]; } StringBuilder testText = new StringBuilder(); ArrayList dnsServers = new ArrayList() {{ - add(DnsServerHelper.getAddressByDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString())); + add(((AbstractDNSServer) spinnerServerChoice.getSelectedItem()).getAddress()); String servers = Daedalus.getPrefs().getString("dns_test_servers", ""); if (!servers.equals("")) { addAll(Arrays.asList(servers.split(","))); @@ -134,7 +131,7 @@ public class DnsTestFragment extends ToolbarFragment { private StringBuilder testServer(DNSQuery dnsQuery, Record.TYPE type, String server, String domain, StringBuilder testText) { - Logger.debug("Testing DNS " + server); + Logger.debug("Testing DNS server" + server); testText.append(getString(R.string.test_domain)).append(" ").append(domain).append("\n").append(getString(R.string.test_dns_server)).append(" ").append(server); mHandler.obtainMessage(DnsTestHandler.MSG_DISPLAY_STATUS, testText.toString()).sendToTarget(); diff --git a/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java b/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java index b574817..e6ed943 100644 --- a/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java +++ b/app/src/main/java/org/itxtech/daedalus/fragment/GlobalConfigFragment.java @@ -9,7 +9,7 @@ import android.support.design.widget.Snackbar; import android.view.View; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.R; -import org.itxtech.daedalus.util.DnsServerHelper; +import org.itxtech.daedalus.util.server.DNSServerHelper; /** * Daedalus Project @@ -29,20 +29,20 @@ public class GlobalConfigFragment extends PreferenceFragment { super.onCreate(savedInstanceState); Daedalus.getPrefs().edit() - .putString("primary_server", DnsServerHelper.getPrimary()) - .putString("secondary_server", DnsServerHelper.getSecondary()) + .putString("primary_server", DNSServerHelper.getPrimary()) + .putString("secondary_server", DNSServerHelper.getSecondary()) .apply(); addPreferencesFromResource(R.xml.perf_settings); ListPreference primaryServer = (ListPreference) findPreference("primary_server"); - primaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); - primaryServer.setEntryValues(DnsServerHelper.getIds()); - primaryServer.setSummary(DnsServerHelper.getDescription(primaryServer.getValue(), Daedalus.getInstance())); + primaryServer.setEntries(DNSServerHelper.getNames(Daedalus.getInstance())); + primaryServer.setEntryValues(DNSServerHelper.getIds()); + primaryServer.setSummary(DNSServerHelper.getDescription(primaryServer.getValue(), Daedalus.getInstance())); primaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); + preference.setSummary(DNSServerHelper.getDescription((String) newValue, Daedalus.getInstance())); Snackbar.make(getView(), R.string.notice_need_restart, Snackbar.LENGTH_LONG) .setAction("Action", null).show(); return true; @@ -50,13 +50,13 @@ public class GlobalConfigFragment extends PreferenceFragment { }); ListPreference secondaryServer = (ListPreference) findPreference("secondary_server"); - secondaryServer.setEntries(DnsServerHelper.getNames(Daedalus.getInstance())); - secondaryServer.setEntryValues(DnsServerHelper.getIds()); - secondaryServer.setSummary(DnsServerHelper.getDescription(secondaryServer.getValue(), Daedalus.getInstance())); + secondaryServer.setEntries(DNSServerHelper.getNames(Daedalus.getInstance())); + secondaryServer.setEntryValues(DNSServerHelper.getIds()); + secondaryServer.setSummary(DNSServerHelper.getDescription(secondaryServer.getValue(), Daedalus.getInstance())); secondaryServer.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance())); + preference.setSummary(DNSServerHelper.getDescription((String) newValue, Daedalus.getInstance())); Snackbar.make(getView(), R.string.notice_need_restart, Snackbar.LENGTH_LONG) .setAction("Action", null).show(); return true; diff --git a/app/src/main/java/org/itxtech/daedalus/provider/TcpDnsProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/TcpDnsProvider.java index a9f789b..a359b27 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/TcpDnsProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/TcpDnsProvider.java @@ -9,8 +9,8 @@ import android.system.OsConstants; import android.system.StructPollfd; import android.util.Log; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServerHelper; import org.itxtech.daedalus.util.Logger; +import org.itxtech.daedalus.util.server.DNSServerHelper; import org.pcap4j.packet.IpPacket; import java.io.*; @@ -136,7 +136,7 @@ public class TcpDnsProvider extends UdpDnsProvider { service.protect(dnsSocket); - SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DnsServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort())); + SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DNSServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort())); dnsSocket.connect(address, 5000); dnsSocket.setSoTimeout(5000); Logger.info("TcpDnsProvider: Sending DNS query request"); diff --git a/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java b/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java index b13d489..7c6f86d 100644 --- a/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java +++ b/app/src/main/java/org/itxtech/daedalus/provider/UdpDnsProvider.java @@ -13,9 +13,9 @@ import de.measite.minidns.Record; import de.measite.minidns.record.A; import de.measite.minidns.util.InetAddressUtil; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServerHelper; import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.RulesResolver; +import org.itxtech.daedalus.util.server.DNSServerHelper; import org.pcap4j.packet.*; import org.pcap4j.packet.factory.PacketFactoryPropertiesLoader; import org.pcap4j.util.PropertiesLoader; @@ -325,7 +325,7 @@ public class UdpDnsProvider extends DnsProvider { // the gateway to reduce the RTT. For further details, please see // https://bugzilla.mozilla.org/show_bug.cgi?id=888268 DatagramPacket outPacket = new DatagramPacket(new byte[0], 0, 0, destAddr, - DnsServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); + DNSServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); forwardPacket(outPacket, null); return; } @@ -363,7 +363,7 @@ public class UdpDnsProvider extends DnsProvider { } else { Logger.info("DnsProvider: Resolving " + dnsQueryName + " Sending to " + destAddr); DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr, - DnsServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); + DNSServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); forwardPacket(outPacket, parsedPacket); } } catch (Exception e) { diff --git a/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java b/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java index b18d821..9f97064 100644 --- a/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java +++ b/app/src/main/java/org/itxtech/daedalus/receiver/BootBroadcastReceiver.java @@ -6,8 +6,8 @@ import android.content.Intent; import android.net.VpnService; import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.service.DaedalusVpnService; -import org.itxtech.daedalus.util.DnsServerHelper; import org.itxtech.daedalus.util.Logger; +import org.itxtech.daedalus.util.server.DNSServerHelper; /** * Daedalus Project @@ -30,8 +30,8 @@ public class BootBroadcastReceiver extends BroadcastReceiver { context.startActivity(vIntent); } - DaedalusVpnService.primaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getPrimary()); - DaedalusVpnService.secondaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getSecondary()); + DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); + DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary()); context.startService((new Intent(context, DaedalusVpnService.class)).setAction(DaedalusVpnService.ACTION_ACTIVATE)); diff --git a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java index 6447b6b..f6032f6 100644 --- a/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java +++ b/app/src/main/java/org/itxtech/daedalus/service/DaedalusVpnService.java @@ -18,9 +18,9 @@ import org.itxtech.daedalus.provider.DnsProvider; import org.itxtech.daedalus.provider.TcpDnsProvider; import org.itxtech.daedalus.provider.UdpDnsProvider; import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver; -import org.itxtech.daedalus.util.DnsServerHelper; import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.RulesResolver; +import org.itxtech.daedalus.util.server.DNSServerHelper; import java.net.InetAddress; import java.util.HashMap; @@ -101,7 +101,7 @@ public class DaedalusVpnService extends VpnService implements Runnable { } Daedalus.initHostsResolver(); - DnsServerHelper.buildPortCache(); + DNSServerHelper.buildPortCache(); if (this.mThread == null) { this.mThread = new Thread(this, "DaedalusVpn"); @@ -157,7 +157,7 @@ public class DaedalusVpnService extends VpnService implements Runnable { if (shouldRefresh) { RulesResolver.clear(); - DnsServerHelper.clearPortCache(); + DNSServerHelper.clearPortCache(); Logger.info("Daedalus VPN service has stopped"); } diff --git a/app/src/main/java/org/itxtech/daedalus/util/Configurations.java b/app/src/main/java/org/itxtech/daedalus/util/Configurations.java index a76706f..baed8b9 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/Configurations.java +++ b/app/src/main/java/org/itxtech/daedalus/util/Configurations.java @@ -3,6 +3,7 @@ package org.itxtech.daedalus.util; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; import org.itxtech.daedalus.Daedalus; +import org.itxtech.daedalus.util.server.CustomDNSServer; import java.io.File; import java.io.FileReader; @@ -26,7 +27,7 @@ public class Configurations { private static File file; - private ArrayList customDnsServers; + private ArrayList customDNSServers; private ArrayList hostsRules; private ArrayList dnsmasqRules; @@ -36,7 +37,7 @@ public class Configurations { private long activateCounter; - int getNextDnsId() { + public int getNextDnsId() { if (totalDnsId < CUSTOM_ID_START) { totalDnsId = CUSTOM_ID_START; } @@ -58,11 +59,11 @@ public class Configurations { this.activateCounter = activateCounter; } - public ArrayList getCustomDnsServers() { - if (customDnsServers == null) { - customDnsServers = new ArrayList<>(); + public ArrayList getCustomDNSServers() { + if (customDNSServers == null) { + customDNSServers = new ArrayList<>(); } - return customDnsServers; + return customDNSServers; } public ArrayList getHostsRules() { diff --git a/app/src/main/java/org/itxtech/daedalus/util/server/AbstractDNSServer.java b/app/src/main/java/org/itxtech/daedalus/util/server/AbstractDNSServer.java new file mode 100644 index 0000000..209011e --- /dev/null +++ b/app/src/main/java/org/itxtech/daedalus/util/server/AbstractDNSServer.java @@ -0,0 +1,47 @@ +package org.itxtech.daedalus.util.server; + +/** + * Daedalus Project + * + * @author iTX Technologies + * @link https://itxtech.org + *

+ * 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. + */ +abstract public class AbstractDNSServer { + public static final int DNS_SERVER_DEFAULT_PORT = 53; + + protected String address; + protected int port; + + public AbstractDNSServer(String address, int port) { + this.address = address; + this.port = port; + } + + public void setAddress(String address) { + this.address = address; + } + + public void setPort(int port) { + this.port = port; + } + + public String getAddress() { + return address; + } + + public int getPort() { + return port; + } + + public abstract String getName(); + + @Override + public String toString() { + return getName(); + } +} diff --git a/app/src/main/java/org/itxtech/daedalus/util/CustomDnsServer.java b/app/src/main/java/org/itxtech/daedalus/util/server/CustomDNSServer.java similarity index 60% rename from app/src/main/java/org/itxtech/daedalus/util/CustomDnsServer.java rename to app/src/main/java/org/itxtech/daedalus/util/server/CustomDNSServer.java index d7528de..56056ad 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/CustomDnsServer.java +++ b/app/src/main/java/org/itxtech/daedalus/util/server/CustomDNSServer.java @@ -1,4 +1,4 @@ -package org.itxtech.daedalus.util; +package org.itxtech.daedalus.util.server; import org.itxtech.daedalus.Daedalus; @@ -13,16 +13,13 @@ import org.itxtech.daedalus.Daedalus; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class CustomDnsServer { +public class CustomDNSServer extends AbstractDNSServer { private String name; - private String address; - private int port; private String id; - public CustomDnsServer(String name, String address, int port) { + public CustomDNSServer(String name, String address, int port) { + super(address, port); this.name = name; - this.address = address; - this.port = port; this.id = String.valueOf(Daedalus.configurations.getNextDnsId()); } @@ -38,23 +35,7 @@ public class CustomDnsServer { 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; - } } diff --git a/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java b/app/src/main/java/org/itxtech/daedalus/util/server/DNSServer.java similarity index 64% rename from app/src/main/java/org/itxtech/daedalus/util/DnsServer.java rename to app/src/main/java/org/itxtech/daedalus/util/server/DNSServer.java index de852c0..e6e8be9 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/DnsServer.java +++ b/app/src/main/java/org/itxtech/daedalus/util/server/DNSServer.java @@ -1,6 +1,7 @@ -package org.itxtech.daedalus.util; +package org.itxtech.daedalus.util.server; import android.content.Context; +import org.itxtech.daedalus.Daedalus; /** * Daedalus Project @@ -13,41 +14,33 @@ import android.content.Context; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class DnsServer { - public static int DNS_SERVER_DEFAULT_PORT = 53; - +public class DNSServer extends AbstractDNSServer { private static int totalId = 0; private String id; - private String address; - private int port; private int description = 0; - public DnsServer(String address, int description, int port) { + public DNSServer(String address, int description, int port) { + super(address, port); this.id = String.valueOf(totalId++); - this.address = address; this.description = description; - this.port = port; } - public DnsServer(String address, int description) { + public DNSServer(String address, int description) { this(address, description, DNS_SERVER_DEFAULT_PORT); } - public int getPort() { - return port; - } - public String getId() { return id; } - public String getAddress() { - return address; - } - public String getStringDescription(Context context) { return context.getResources().getString(description); } + + @Override + public String getName() { + return getStringDescription(Daedalus.getInstance()); + } } diff --git a/app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java b/app/src/main/java/org/itxtech/daedalus/util/server/DNSServerHelper.java similarity index 63% rename from app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java rename to app/src/main/java/org/itxtech/daedalus/util/server/DNSServerHelper.java index 2df4f8b..f8bf832 100644 --- a/app/src/main/java/org/itxtech/daedalus/util/DnsServerHelper.java +++ b/app/src/main/java/org/itxtech/daedalus/util/server/DNSServerHelper.java @@ -1,4 +1,4 @@ -package org.itxtech.daedalus.util; +package org.itxtech.daedalus.util.server; import android.content.Context; import org.itxtech.daedalus.Daedalus; @@ -18,7 +18,7 @@ import java.util.HashMap; * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. */ -public class DnsServerHelper { +public class DNSServerHelper { private static HashMap portCache = null; public static void clearPortCache() { @@ -27,11 +27,11 @@ public class DnsServerHelper { public static void buildPortCache() { portCache = new HashMap<>(); - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { portCache.put(server.getAddress(), server.getPort()); } - for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) { + for (CustomDNSServer server : Daedalus.configurations.getCustomDNSServers()) { portCache.put(server.getAddress(), server.getPort()); } @@ -53,8 +53,8 @@ public class DnsServerHelper { return intId; } - for (int i = 0; i < Daedalus.configurations.getCustomDnsServers().size(); i++) { - if (Daedalus.configurations.getCustomDnsServers().get(i).getId().equals(id)) { + for (int i = 0; i < Daedalus.configurations.getCustomDNSServers().size(); i++) { + if (Daedalus.configurations.getCustomDNSServers().get(i).getId().equals(id)) { return i + Daedalus.DNS_SERVERS.size(); } } @@ -62,18 +62,18 @@ public class DnsServerHelper { } public static String getPrimary() { - return String.valueOf(DnsServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("primary_server", "0")))); + return String.valueOf(DNSServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("primary_server", "0")))); } public static String getSecondary() { - return String.valueOf(DnsServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("secondary_server", "1")))); + return String.valueOf(DNSServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("secondary_server", "1")))); } private static int checkServerId(int id) { if (id < (Daedalus.DNS_SERVERS.size() - 1)) { return id; } - for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) { + for (CustomDNSServer server : Daedalus.configurations.getCustomDNSServers()) { if (server.getId().equals(String.valueOf(id))) { return id; } @@ -82,28 +82,28 @@ public class DnsServerHelper { } public static String getAddressById(String id) { - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { if (server.getId().equals(id)) { return server.getAddress(); } } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getId().equals(id)) { - return customDnsServer.getAddress(); + for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { + if (customDNSServer.getId().equals(id)) { + return customDNSServer.getAddress(); } } return Daedalus.DNS_SERVERS.get(0).getAddress(); } public static String getAddressByDescription(Context context, String description) { - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { if (server.getStringDescription(context).equals(description)) { return server.getAddress(); } } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getName().equals(description)) { - return customDnsServer.getAddress(); + for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { + if (customDNSServer.getName().equals(description)) { + return customDNSServer.getAddress(); } } return Daedalus.DNS_SERVERS.get(0).getAddress(); @@ -111,11 +111,11 @@ public class DnsServerHelper { public static String[] getIds() { ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { servers.add(server.getId()); } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - servers.add(customDnsServer.getId()); + for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { + servers.add(customDNSServer.getId()); } String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; return servers.toArray(stringServers); @@ -123,31 +123,38 @@ public class DnsServerHelper { public static String[] getNames(Context context) { ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { servers.add(server.getStringDescription(context)); } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - servers.add(customDnsServer.getName()); + for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { + servers.add(customDNSServer.getName()); } String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; return servers.toArray(stringServers); } + public static ArrayList getAllServers() { + ArrayList servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); + servers.addAll(Daedalus.DNS_SERVERS); + servers.addAll(Daedalus.configurations.getCustomDNSServers()); + return servers; + } + public static String getDescription(String id, Context context) { - for (DnsServer server : Daedalus.DNS_SERVERS) { + for (DNSServer server : Daedalus.DNS_SERVERS) { if (server.getId().equals(id)) { return server.getStringDescription(context); } } - for (CustomDnsServer customDnsServer : Daedalus.configurations.getCustomDnsServers()) { - if (customDnsServer.getId().equals(id)) { - return customDnsServer.getName(); + for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { + if (customDNSServer.getId().equals(id)) { + return customDNSServer.getName(); } } return Daedalus.DNS_SERVERS.get(0).getStringDescription(context); } - public static boolean isInUsing(CustomDnsServer server) { + public static boolean isInUsing(CustomDNSServer server) { return Daedalus.getInstance().isServiceActivated() && (server.getId().equals(getPrimary()) || server.getId().equals(getSecondary())); } }