Provider: use DnsServer directly, get rid of magics

This commit is contained in:
PeratX 2019-10-23 14:33:42 +08:00
parent c95b87b5a8
commit a2d92d49aa
24 changed files with 157 additions and 171 deletions

View File

@ -10,7 +10,6 @@ import android.graphics.drawable.Icon;
import android.net.Uri; import android.net.Uri;
import android.net.VpnService; import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.util.Log;
import androidx.preference.PreferenceManager; import androidx.preference.PreferenceManager;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
@ -19,8 +18,8 @@ import com.google.gson.stream.JsonReader;
import org.itxtech.daedalus.activity.MainActivity; import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.*; import org.itxtech.daedalus.util.*;
import org.itxtech.daedalus.util.server.DNSServer; import org.itxtech.daedalus.server.DnsServer;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -42,13 +41,13 @@ public class Daedalus extends Application {
private static final String SHORTCUT_ID_ACTIVATE = "shortcut_activate"; private static final String SHORTCUT_ID_ACTIVATE = "shortcut_activate";
public static final List<DNSServer> DNS_SERVERS = new ArrayList<DNSServer>() {{ public static final List<DnsServer> DNS_SERVERS = new ArrayList<DnsServer>() {{
add(new DNSServer("101.101.101.101", R.string.server_twnic_primary)); add(new DnsServer("101.101.101.101", R.string.server_twnic_primary));
add(new DNSServer("101.102.103.104", R.string.server_twnic_secondary)); add(new DnsServer("101.102.103.104", R.string.server_twnic_secondary));
add(new DNSServer("dns.rubyfish.cn/dns-query", R.string.server_rubyfish)); add(new DnsServer("rubyfish.cn/dns-query", R.string.server_rubyfish));
add(new DNSServer("cloudflare-dns.com/dns-query", R.string.server_cloudflare)); add(new DnsServer("cloudflare-dns.com/dns-query", R.string.server_cloudflare));
add(new DNSServer("dns.google/dns-query", R.string.server_google_ietf)); add(new DnsServer("dns.google/dns-query", R.string.server_google_ietf));
add(new DNSServer("dns.google/resolve", R.string.server_google_json)); add(new DnsServer("dns.google/resolve", R.string.server_google_json));
}}; }};
public static final List<Rule> RULES = new ArrayList<Rule>() {{ public static final List<Rule> RULES = new ArrayList<Rule>() {{
@ -211,16 +210,16 @@ public class Daedalus extends Application {
} }
public static void activateService(Context context) { public static void activateService(Context context) {
DaedalusVpnService.primaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getPrimary()); DaedalusVpnService.primaryServer = DnsServerHelper.getServerById(DnsServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DNSServerHelper.getAddressById(DNSServerHelper.getSecondary()); DaedalusVpnService.secondaryServer = DnsServerHelper.getServerById(DnsServerHelper.getSecondary());
if (getPrefs().getBoolean("settings_use_system_dns", false)) { if (getPrefs().getBoolean("settings_use_system_dns", false)) {
String[] servers = DnsServersDetector.getServers(context); String[] servers = DnsServersDetector.getServers(context);
if (servers != null) { if (servers != null) {
if (servers.length >= 2) { if (servers.length >= 2) {
DaedalusVpnService.primaryServer = servers[0]; DaedalusVpnService.primaryServer = new DnsServer(servers[0], 0);
DaedalusVpnService.secondaryServer = servers[1]; DaedalusVpnService.secondaryServer = new DnsServer(servers[1], 0);
} else { } else {
DaedalusVpnService.primaryServer = DaedalusVpnService.secondaryServer = servers[0]; DaedalusVpnService.primaryServer = DaedalusVpnService.secondaryServer = new DnsServer(servers[0]);
} }
} }
} }

View File

@ -12,7 +12,7 @@ import androidx.fragment.app.FragmentTransaction;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.ConfigFragment; import org.itxtech.daedalus.fragment.ConfigFragment;
import org.itxtech.daedalus.fragment.DNSServerConfigFragment; import org.itxtech.daedalus.fragment.DnsServerConfigFragment;
import org.itxtech.daedalus.fragment.RuleConfigFragment; import org.itxtech.daedalus.fragment.RuleConfigFragment;
/** /**
@ -45,13 +45,13 @@ public class ConfigActivity extends AppCompatActivity {
ConfigFragment fragment; ConfigFragment fragment;
switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) { switch (getIntent().getIntExtra(LAUNCH_ACTION_FRAGMENT, LAUNCH_FRAGMENT_DNS_SERVER)) {
case LAUNCH_FRAGMENT_DNS_SERVER: case LAUNCH_FRAGMENT_DNS_SERVER:
fragment = new DNSServerConfigFragment(); fragment = new DnsServerConfigFragment();
break; break;
case LAUNCH_FRAGMENT_RULE: case LAUNCH_FRAGMENT_RULE:
fragment = new RuleConfigFragment(); fragment = new RuleConfigFragment();
break; break;
default://should never reach this default://should never reach this
fragment = new DNSServerConfigFragment(); fragment = new DnsServerConfigFragment();
break; break;
} }

View File

@ -2,7 +2,6 @@ package org.itxtech.daedalus.activity;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.VpnService; import android.net.VpnService;
import android.os.Bundle; import android.os.Bundle;
@ -25,7 +24,6 @@ import org.itxtech.daedalus.R;
import org.itxtech.daedalus.fragment.*; import org.itxtech.daedalus.fragment.*;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper;
/** /**
* Daedalus Project * Daedalus Project
@ -215,10 +213,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
switchFragment(AboutFragment.class); switchFragment(AboutFragment.class);
break; break;
case FRAGMENT_DNS_SERVERS: case FRAGMENT_DNS_SERVERS:
switchFragment(DNSServersFragment.class); switchFragment(DnsServersFragment.class);
break; break;
case FRAGMENT_DNS_TEST: case FRAGMENT_DNS_TEST:
switchFragment(DNSTestFragment.class); switchFragment(DnsTestFragment.class);
break; break;
case FRAGMENT_HOME: case FRAGMENT_HOME:
switchFragment(HomeFragment.class); switchFragment(HomeFragment.class);
@ -247,10 +245,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
switchFragment(AboutFragment.class); switchFragment(AboutFragment.class);
break; break;
case R.id.nav_dns_server: case R.id.nav_dns_server:
switchFragment(DNSServersFragment.class); switchFragment(DnsServersFragment.class);
break; break;
case R.id.nav_dns_test: case R.id.nav_dns_test:
switchFragment(DNSTestFragment.class); switchFragment(DnsTestFragment.class);
break; break;
case R.id.nav_github: case R.id.nav_github:
Daedalus.openUri("https://github.com/iTXTech/Daedalus"); Daedalus.openUri("https://github.com/iTXTech/Daedalus");

View File

@ -13,6 +13,7 @@ import android.webkit.WebViewClient;
import org.itxtech.daedalus.BuildConfig; import org.itxtech.daedalus.BuildConfig;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.Logger;
import java.util.Locale; import java.util.Locale;
@ -64,7 +65,7 @@ public class AboutFragment extends ToolbarFragment {
mWebView.loadUrl("javascript:changeColor('"+(Daedalus.isDarkTheme() ? "#FFFFFF" : "#000000")+"')"); mWebView.loadUrl("javascript:changeColor('"+(Daedalus.isDarkTheme() ? "#FFFFFF" : "#000000")+"')");
mWebView.loadUrl("javascript:changeVersionInfo('" + Daedalus.getInstance().getPackageManager().getPackageInfo(Daedalus.getInstance().getPackageName(), 0).versionName + "', '" + BuildConfig.BUILD_TIME + "', '" + BuildConfig.GIT_COMMIT + "')"); mWebView.loadUrl("javascript:changeVersionInfo('" + Daedalus.getInstance().getPackageManager().getPackageInfo(Daedalus.getInstance().getPackageName(), 0).versionName + "', '" + BuildConfig.BUILD_TIME + "', '" + BuildConfig.GIT_COMMIT + "')");
} catch (Exception e) { } catch (Exception e) {
Log.e("DAboutActivity", e.toString()); Logger.logException(e);
} }
} }
}); });
@ -82,8 +83,6 @@ public class AboutFragment extends ToolbarFragment {
super.onDestroyView(); super.onDestroyView();
if (mWebView != null) { if (mWebView != null) {
Log.d("DAboutActivity", "onDestroy");
mWebView.removeAllViews(); mWebView.removeAllViews();
mWebView.setWebViewClient(null); mWebView.setWebViewClient(null);
((ViewGroup) mWebView.getParent()).removeView(mWebView); ((ViewGroup) mWebView.getParent()).removeView(mWebView);

View File

@ -11,8 +11,8 @@ import com.google.android.material.snackbar.Snackbar;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity; import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.util.server.CustomDNSServer; import org.itxtech.daedalus.server.CustomDnsServer;
import org.itxtech.daedalus.util.server.DNSServer; import org.itxtech.daedalus.server.DnsServer;
/** /**
* Daedalus Project * Daedalus Project
@ -25,7 +25,7 @@ import org.itxtech.daedalus.util.server.DNSServer;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class DNSServerConfigFragment extends ConfigFragment { public class DnsServerConfigFragment extends ConfigFragment {
private int index; private int index;
@Override @Override
@ -58,7 +58,7 @@ public class DNSServerConfigFragment extends ConfigFragment {
index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE); index = intent.getIntExtra(ConfigActivity.LAUNCH_ACTION_ID, ConfigActivity.ID_NONE);
if (index != 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.setText(server.getName());
serverName.setSummary(server.getName()); serverName.setSummary(server.getName());
serverAddress.setText(server.getAddress()); serverAddress.setText(server.getAddress());
@ -68,7 +68,7 @@ public class DNSServerConfigFragment extends ConfigFragment {
} else { } else {
serverName.setText(""); serverName.setText("");
serverAddress.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.setText(port);
serverPort.setSummary(port); serverPort.setSummary(port);
} }
@ -92,9 +92,9 @@ public class DNSServerConfigFragment extends ConfigFragment {
} }
if (index == ConfigActivity.ID_NONE) { 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 { } else {
CustomDNSServer server = Daedalus.configurations.getCustomDNSServers().get(index); CustomDnsServer server = Daedalus.configurations.getCustomDNSServers().get(index);
server.setName(serverName); server.setName(serverName);
server.setAddress(serverAddress); server.setAddress(serverAddress);
server.setPort(Integer.parseInt(serverPort)); server.setPort(Integer.parseInt(serverPort));

View File

@ -15,8 +15,8 @@ import com.google.android.material.snackbar.Snackbar;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.ConfigActivity; import org.itxtech.daedalus.activity.ConfigActivity;
import org.itxtech.daedalus.util.server.CustomDNSServer; import org.itxtech.daedalus.server.CustomDnsServer;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
/** /**
* Daedalus Project * Daedalus Project
@ -29,9 +29,9 @@ import org.itxtech.daedalus.util.server.DNSServerHelper;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class DNSServersFragment extends ToolbarFragment { public class DnsServersFragment extends ToolbarFragment {
private DNSServerAdapter adapter; private DNSServerAdapter adapter;
private CustomDNSServer server = null; private CustomDnsServer server = null;
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -47,7 +47,7 @@ public class DNSServersFragment extends ToolbarFragment {
if (viewHolder instanceof ViewHolder) { if (viewHolder instanceof ViewHolder) {
int index = ((ViewHolder) viewHolder).getIndex(); int index = ((ViewHolder) viewHolder).getIndex();
if (index < Daedalus.configurations.getCustomDNSServers().size() && if (index < Daedalus.configurations.getCustomDNSServers().size() &&
DNSServerHelper.isInUsing(Daedalus.configurations.getCustomDNSServers().get(index))) { DnsServerHelper.isInUsing(Daedalus.configurations.getCustomDNSServers().get(index))) {
return 0; return 0;
} }
} }
@ -118,7 +118,7 @@ public class DNSServersFragment extends ToolbarFragment {
private class DNSServerAdapter extends RecyclerView.Adapter<ViewHolder> { private class DNSServerAdapter extends RecyclerView.Adapter<ViewHolder> {
@Override @Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) { public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CustomDNSServer server = Daedalus.configurations.getCustomDNSServers().get(position); CustomDnsServer server = Daedalus.configurations.getCustomDNSServers().get(position);
holder.setIndex(position); holder.setIndex(position);
holder.textViewName.setText(server.getName()); holder.textViewName.setText(server.getName());
holder.textViewAddress.setText(server.getRealName()); holder.textViewAddress.setText(server.getRealName());
@ -159,7 +159,7 @@ public class DNSServersFragment extends ToolbarFragment {
@Override @Override
public void onClick(View v) { 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) Daedalus.getInstance().startActivity(new Intent(Daedalus.getInstance(), ConfigActivity.class)
.putExtra(ConfigActivity.LAUNCH_ACTION_ID, index) .putExtra(ConfigActivity.LAUNCH_ACTION_ID, index)
.putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER) .putExtra(ConfigActivity.LAUNCH_ACTION_FRAGMENT, ConfigActivity.LAUNCH_FRAGMENT_DNS_SERVER)

View File

@ -12,8 +12,8 @@ import android.widget.*;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.AbstractDNSServer; import org.itxtech.daedalus.server.AbstractDnsServer;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
import org.minidns.dnsmessage.DnsMessage; import org.minidns.dnsmessage.DnsMessage;
import org.minidns.dnsmessage.Question; import org.minidns.dnsmessage.Question;
import org.minidns.record.Record; import org.minidns.record.Record;
@ -36,7 +36,7 @@ import java.util.Random;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class DNSTestFragment extends ToolbarFragment { public class DnsTestFragment extends ToolbarFragment {
private class Type { private class Type {
private Record.TYPE type; private Record.TYPE type;
private String name; private String name;
@ -67,9 +67,9 @@ public class DNSTestFragment extends ToolbarFragment {
final TextView textViewTestInfo = view.findViewById(R.id.textView_test_info); final TextView textViewTestInfo = view.findViewById(R.id.textView_test_info);
final Spinner spinnerServerChoice = view.findViewById(R.id.spinner_server_choice); final Spinner spinnerServerChoice = view.findViewById(R.id.spinner_server_choice);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DNSServerHelper.getAllServers()); ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DnsServerHelper.getAllServers());
spinnerServerChoice.setAdapter(spinnerArrayAdapter); spinnerServerChoice.setAdapter(spinnerArrayAdapter);
spinnerServerChoice.setSelection(DNSServerHelper.getPosition(DNSServerHelper.getPrimary())); spinnerServerChoice.setSelection(DnsServerHelper.getPosition(DnsServerHelper.getPrimary()));
ArrayList<Type> types = new ArrayList<Type>() {{ ArrayList<Type> types = new ArrayList<Type>() {{
add(new Type("A", Record.TYPE.A)); add(new Type("A", Record.TYPE.A));
@ -110,38 +110,38 @@ public class DNSTestFragment extends ToolbarFragment {
testDomain = Daedalus.DEFAULT_TEST_DOMAINS[0]; testDomain = Daedalus.DEFAULT_TEST_DOMAINS[0];
} }
StringBuilder testText = new StringBuilder(); StringBuilder testText = new StringBuilder();
ArrayList<AbstractDNSServer> dnsServers = new ArrayList<AbstractDNSServer>() {{ ArrayList<AbstractDnsServer> dnsServers = new ArrayList<AbstractDnsServer>() {{
add(((AbstractDNSServer) spinnerServerChoice.getSelectedItem())); add(((AbstractDnsServer) spinnerServerChoice.getSelectedItem()));
String servers = Daedalus.getPrefs().getString("dns_test_servers", ""); String servers = Daedalus.getPrefs().getString("dns_test_servers", "");
if (!servers.equals("")) { if (!servers.equals("")) {
for (String server : servers.split(",")) { for (String server : servers.split(",")) {
if (server.contains(".") && server.contains(":")) {//IPv4 if (server.contains(".") && server.contains(":")) {//IPv4
String[] pieces = servers.split(":"); String[] pieces = servers.split(":");
int port = AbstractDNSServer.DNS_SERVER_DEFAULT_PORT; int port = AbstractDnsServer.DNS_SERVER_DEFAULT_PORT;
try { try {
port = Integer.parseInt(pieces[1]); port = Integer.parseInt(pieces[1]);
} catch (Exception e) { } catch (Exception e) {
Logger.logException(e); Logger.logException(e);
} }
add(new AbstractDNSServer(pieces[0], port)); add(new AbstractDnsServer(pieces[0], port));
} else if (!server.contains(".") && server.contains("|")) {//IPv6 } else if (!server.contains(".") && server.contains("|")) {//IPv6
String[] pieces = servers.split("\\|"); String[] pieces = servers.split("\\|");
int port = AbstractDNSServer.DNS_SERVER_DEFAULT_PORT; int port = AbstractDnsServer.DNS_SERVER_DEFAULT_PORT;
try { try {
port = Integer.parseInt(pieces[1]); port = Integer.parseInt(pieces[1]);
} catch (Exception e) { } catch (Exception e) {
Logger.logException(e); Logger.logException(e);
} }
add(new AbstractDNSServer(pieces[0], port)); add(new AbstractDnsServer(pieces[0], port));
} else { } else {
add(new AbstractDNSServer(server, AbstractDNSServer.DNS_SERVER_DEFAULT_PORT)); add(new AbstractDnsServer(server, AbstractDnsServer.DNS_SERVER_DEFAULT_PORT));
} }
} }
} }
}}; }};
DnsQuery dnsQuery = new DnsQuery(); DnsQuery dnsQuery = new DnsQuery();
Record.TYPE type = ((Type) spinnerType.getSelectedItem()).getType(); Record.TYPE type = ((Type) spinnerType.getSelectedItem()).getType();
for (AbstractDNSServer dnsServer : dnsServers) { for (AbstractDnsServer dnsServer : dnsServers) {
testText = testServer(dnsQuery, type, dnsServer, testDomain, testText); testText = testServer(dnsQuery, type, dnsServer, testDomain, testText);
} }
mHandler.obtainMessage(DnsTestHandler.MSG_TEST_DONE).sendToTarget(); mHandler.obtainMessage(DnsTestHandler.MSG_TEST_DONE).sendToTarget();
@ -152,7 +152,7 @@ public class DNSTestFragment extends ToolbarFragment {
} }
private StringBuilder testServer(DnsQuery dnsQuery, Record.TYPE type, AbstractDNSServer server, String domain, StringBuilder testText) { private StringBuilder testServer(DnsQuery dnsQuery, Record.TYPE type, AbstractDnsServer server, String domain, StringBuilder testText) {
Logger.debug("Testing DNS server " + server.getRealName()); Logger.debug("Testing DNS server " + server.getRealName());
testText.append(getString(R.string.test_domain)).append(" ").append(domain).append("\n") testText.append(getString(R.string.test_domain)).append(" ").append(domain).append("\n")
.append(getString(R.string.test_dns_server)).append(" ").append(server.getRealName()); .append(getString(R.string.test_dns_server)).append(" ").append(server.getRealName());

View File

@ -7,7 +7,7 @@ import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.AppFilterActivity; import org.itxtech.daedalus.activity.AppFilterActivity;
import org.itxtech.daedalus.activity.MainActivity; import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
import java.util.ArrayList; import java.util.ArrayList;
@ -27,8 +27,8 @@ public class GlobalConfigFragment extends PreferenceFragmentCompat {
@Override @Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
Daedalus.getPrefs().edit() Daedalus.getPrefs().edit()
.putString("primary_server", DNSServerHelper.getPrimary()) .putString("primary_server", DnsServerHelper.getPrimary())
.putString("secondary_server", DNSServerHelper.getSecondary()) .putString("secondary_server", DnsServerHelper.getSecondary())
.apply(); .apply();
addPreferencesFromResource(R.xml.perf_settings); addPreferencesFromResource(R.xml.perf_settings);
@ -40,11 +40,11 @@ public class GlobalConfigFragment extends PreferenceFragmentCompat {
}}) { }}) {
ListPreference listPref = findPreference(k); ListPreference listPref = findPreference(k);
listPref.setVisible(visible); listPref.setVisible(visible);
listPref.setEntries(DNSServerHelper.getNames(Daedalus.getInstance())); listPref.setEntries(DnsServerHelper.getNames(Daedalus.getInstance()));
listPref.setEntryValues(DNSServerHelper.getIds()); listPref.setEntryValues(DnsServerHelper.getIds());
listPref.setSummary(DNSServerHelper.getDescription(listPref.getValue(), Daedalus.getInstance())); listPref.setSummary(DnsServerHelper.getDescription(listPref.getValue(), Daedalus.getInstance()));
listPref.setOnPreferenceChangeListener((preference, newValue) -> { listPref.setOnPreferenceChangeListener((preference, newValue) -> {
preference.setSummary(DNSServerHelper.getDescription((String) newValue, Daedalus.getInstance())); preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance()));
return true; return true;
}); });
} }

View File

@ -50,7 +50,6 @@ public class HomeFragment extends ToolbarFragment {
} }
private void updateUserInterface() { private void updateUserInterface() {
Log.d("DMainFragment", "updateInterface");
Button but = getView().findViewById(R.id.button_activate); Button but = getView().findViewById(R.id.button_activate);
if (DaedalusVpnService.isActivated()) { if (DaedalusVpnService.isActivated()) {
but.setText(R.string.button_text_deactivate); but.setText(R.string.button_text_deactivate);

View File

@ -10,7 +10,7 @@ import okhttp3.OkHttpClient;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
import org.minidns.dnsmessage.DnsMessage; import org.minidns.dnsmessage.DnsMessage;
import org.pcap4j.packet.IpPacket; import org.pcap4j.packet.IpPacket;
import org.pcap4j.packet.IpSelector; import org.pcap4j.packet.IpSelector;
@ -58,8 +58,8 @@ abstract public class HttpsProvider extends Provider {
.header("Accept", accept) .header("Accept", accept)
.build())) .build()))
.dns(hostname -> { .dns(hostname -> {
if (DNSServerHelper.domainCache.containsKey(hostname)) { if (DnsServerHelper.domainCache.containsKey(hostname)) {
return DNSServerHelper.domainCache.get(hostname); return DnsServerHelper.domainCache.get(hostname);
} }
return Arrays.asList(InetAddress.getAllByName(hostname)); return Arrays.asList(InetAddress.getAllByName(hostname));
}) })
@ -138,7 +138,7 @@ abstract public class HttpsProvider extends Provider {
return; return;
String uri; String uri;
try { try {
uri = service.dnsServers.get(destAddr.getHostAddress());//https uri uri = service.dnsServers.get(destAddr.getHostAddress()).getAddress();//https uri
} catch (Exception e) { } catch (Exception e) {
Logger.logException(e); Logger.logException(e);
return; return;
@ -161,7 +161,7 @@ abstract public class HttpsProvider extends Provider {
return; return;
} }
if (dnsMsg.getQuestion() == null) { if (dnsMsg.getQuestion() == null) {
Log.i(TAG, "handleDnsRequest: Discarding DNS packet with no query " + dnsMsg); Logger.debug("handleDnsRequest: Discarding DNS packet with no query " + dnsMsg);
return; return;
} }

View File

@ -40,6 +40,6 @@ public class ProviderPicker {
} }
public static int getDnsQueryMethod() { public static int getDnsQueryMethod() {
return Integer.valueOf(Daedalus.getPrefs().getString("settings_dns_query_method", "0")); return Integer.parseInt(Daedalus.getPrefs().getString("settings_dns_query_method", "0"));
} }
} }

View File

@ -9,7 +9,7 @@ import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.AbstractDnsServer;
import org.pcap4j.packet.IpPacket; import org.pcap4j.packet.IpPacket;
import java.io.*; import java.io.*;
@ -123,7 +123,7 @@ public class TcpProvider extends UdpProvider {
} }
@Override @Override
protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket) throws DaedalusVpnService.VpnNetworkException { protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket, AbstractDnsServer dnsServer) throws DaedalusVpnService.VpnNetworkException {
Socket dnsSocket; Socket dnsSocket;
try { try {
// Packets to be sent to the real DNS server will need to be protected from the VPN // Packets to be sent to the real DNS server will need to be protected from the VPN
@ -131,7 +131,7 @@ public class TcpProvider extends UdpProvider {
service.protect(dnsSocket); service.protect(dnsSocket);
SocketAddress address = new InetSocketAddress(outPacket.getAddress(), DNSServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort())); SocketAddress address = new InetSocketAddress(outPacket.getAddress(), dnsServer.getPort());
dnsSocket.connect(address, 5000); dnsSocket.connect(address, 5000);
dnsSocket.setSoTimeout(5000); dnsSocket.setSoTimeout(5000);
Logger.info("TcpProvider: Sending DNS query request"); Logger.info("TcpProvider: Sending DNS query request");

View File

@ -3,7 +3,7 @@ package org.itxtech.daedalus.provider;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.AbstractDnsServer;
import org.pcap4j.packet.IpPacket; import org.pcap4j.packet.IpPacket;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
@ -28,13 +28,12 @@ public class TlsProvider extends TcpProvider{
} }
@Override @Override
protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket) { protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket, AbstractDnsServer dnsServer) {
Socket dnsSocket; Socket dnsSocket;
try { try {
SSLContext context = SSLContext.getInstance("TLSv1.2"); SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null, null, null); context.init(null, null, null);
dnsSocket = context.getSocketFactory().createSocket(outPacket.getAddress(), dnsSocket = context.getSocketFactory().createSocket(outPacket.getAddress(), dnsServer.getPort());
DNSServerHelper.getPortOrDefault(outPacket.getAddress(), outPacket.getPort()));
//Create TLS v1.2 socket //Create TLS v1.2 socket
//TODO: SNI //TODO: SNI

View File

@ -9,7 +9,7 @@ import androidx.annotation.NonNull;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.service.DaedalusVpnService; import org.itxtech.daedalus.service.DaedalusVpnService;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.AbstractDnsServer;
import org.minidns.dnsmessage.DnsMessage; import org.minidns.dnsmessage.DnsMessage;
import org.pcap4j.packet.IpPacket; import org.pcap4j.packet.IpPacket;
import org.pcap4j.packet.IpSelector; import org.pcap4j.packet.IpSelector;
@ -118,7 +118,7 @@ public class UdpProvider extends Provider {
} }
} }
protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket) throws DaedalusVpnService.VpnNetworkException { protected void forwardPacket(DatagramPacket outPacket, IpPacket parsedPacket, AbstractDnsServer dnsServer) throws DaedalusVpnService.VpnNetworkException {
DatagramSocket dnsSocket; DatagramSocket dnsSocket;
try { try {
// Packets to be sent to the real DNS server will need to be protected from the VPN // Packets to be sent to the real DNS server will need to be protected from the VPN
@ -158,7 +158,6 @@ public class UdpProvider extends Provider {
*/ */
@Override @Override
protected void handleDnsRequest(byte[] packetData) throws DaedalusVpnService.VpnNetworkException { protected void handleDnsRequest(byte[] packetData) throws DaedalusVpnService.VpnNetworkException {
IpPacket parsedPacket; IpPacket parsedPacket;
try { try {
parsedPacket = (IpPacket) IpSelector.newPacket(packetData, 0, packetData.length); parsedPacket = (IpPacket) IpSelector.newPacket(packetData, 0, packetData.length);
@ -169,17 +168,20 @@ public class UdpProvider extends Provider {
if (!(parsedPacket.getPayload() instanceof UdpPacket)) { if (!(parsedPacket.getPayload() instanceof UdpPacket)) {
try { try {
Log.i(TAG, "handleDnsRequest: Discarding unknown packet type " + parsedPacket.getPayload()); Logger.debug("handleDnsRequest: Discarding unknown packet type " + parsedPacket.getPayload());
} catch (Exception ignored) { } catch (Exception ignored) {
} }
return; return;
} }
InetAddress destAddr = parsedPacket.getHeader().getDstAddr(); InetAddress destAddr = parsedPacket.getHeader().getDstAddr();
if (destAddr == null) if (destAddr == null) {
return; return;
}
AbstractDnsServer dnsServer;
try { try {
destAddr = InetAddress.getByName(service.dnsServers.get(destAddr.getHostAddress())); dnsServer = service.dnsServers.get(destAddr.getHostAddress());
destAddr = InetAddress.getByName(dnsServer.getHostAddress());
} catch (Exception e) { } catch (Exception e) {
Logger.logException(e); Logger.logException(e);
Logger.error("handleDnsRequest: DNS server alias query failed for " + destAddr.getHostAddress()); Logger.error("handleDnsRequest: DNS server alias query failed for " + destAddr.getHostAddress());
@ -194,9 +196,8 @@ public class UdpProvider extends Provider {
// Let's be nice to Firefox. Firefox uses an empty UDP packet to // Let's be nice to Firefox. Firefox uses an empty UDP packet to
// the gateway to reduce the RTT. For further details, please see // the gateway to reduce the RTT. For further details, please see
// https://bugzilla.mozilla.org/show_bug.cgi?id=888268 // https://bugzilla.mozilla.org/show_bug.cgi?id=888268
DatagramPacket outPacket = new DatagramPacket(new byte[0], 0, 0, destAddr, DatagramPacket outPacket = new DatagramPacket(new byte[0], 0, 0, destAddr, dnsServer.getPort());
DNSServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); forwardPacket(outPacket, null, dnsServer);
forwardPacket(outPacket, null);
return; return;
} }
@ -212,14 +213,13 @@ public class UdpProvider extends Provider {
return; return;
} }
if (dnsMsg.getQuestion() == null) { if (dnsMsg.getQuestion() == null) {
Log.i(TAG, "handleDnsRequest: Discarding DNS packet with no query " + dnsMsg); Logger.debug("handleDnsRequest: Discarding DNS packet with no query " + dnsMsg);
return; return;
} }
if (!resolve(parsedPacket, dnsMsg)) { if (!resolve(parsedPacket, dnsMsg)) {
DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr, DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr, dnsServer.getPort());
DNSServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt())); forwardPacket(outPacket, parsedPacket, dnsServer);
forwardPacket(outPacket, parsedPacket);
} }
} }

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.util.server; package org.itxtech.daedalus.server;
/** /**
* Daedalus Project * Daedalus Project
@ -11,17 +11,26 @@ package org.itxtech.daedalus.util.server;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class AbstractDNSServer { public class AbstractDnsServer {
public static final int DNS_SERVER_DEFAULT_PORT = 53; public static final int DNS_SERVER_DEFAULT_PORT = 53;
protected String address; protected String address;
protected int port; protected int port;
protected String hostAddress;
public AbstractDNSServer(String address, int port) { public AbstractDnsServer(String address, int port) {
this.address = address; this.address = address;
this.port = port; this.port = port;
} }
public void setHostAddress(String hostAddress) {
this.hostAddress = hostAddress;
}
public String getHostAddress() {
return hostAddress;
}
public void setAddress(String address) { public void setAddress(String address) {
this.address = address; this.address = address;
} }

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.util.server; package org.itxtech.daedalus.server;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
@ -13,11 +13,11 @@ import org.itxtech.daedalus.Daedalus;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class CustomDNSServer extends AbstractDNSServer { public class CustomDnsServer extends AbstractDnsServer {
private String name; private String name;
private String id; private String id;
public CustomDNSServer(String name, String address, int port) { public CustomDnsServer(String name, String address, int port) {
super(address, port); super(address, port);
this.name = name; this.name = name;
this.id = String.valueOf(Daedalus.configurations.getNextDnsId()); this.id = String.valueOf(Daedalus.configurations.getNextDnsId());

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.util.server; package org.itxtech.daedalus.server;
import android.content.Context; import android.content.Context;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
@ -14,23 +14,27 @@ import org.itxtech.daedalus.Daedalus;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class DNSServer extends AbstractDNSServer { public class DnsServer extends AbstractDnsServer {
private static int totalId = 0; private static int totalId = 0;
private String id; private String id;
private int description; private int description;
public DNSServer(String address, int description, int port) { public DnsServer(String address, int description, int port) {
super(address, port); super(address, port);
this.id = String.valueOf(totalId++); this.id = String.valueOf(totalId++);
this.description = description; this.description = description;
} }
public DNSServer(String address, int description) { public DnsServer(String address, int description) {
this(address, description, DNS_SERVER_DEFAULT_PORT); this(address, description, DNS_SERVER_DEFAULT_PORT);
} }
public DnsServer(String address) {
this(address, 0);
}
public String getId() { public String getId() {
return id; return id;
} }

View File

@ -1,4 +1,4 @@
package org.itxtech.daedalus.util.server; package org.itxtech.daedalus.server;
import android.content.Context; import android.content.Context;
import android.net.Uri; import android.net.Uri;
@ -25,31 +25,19 @@ import java.util.List;
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
*/ */
public class DNSServerHelper { public class DnsServerHelper {
private static HashMap<String, Integer> portCache = new HashMap<>();
public static HashMap<String, List<InetAddress>> domainCache = new HashMap<>(); public static HashMap<String, List<InetAddress>> domainCache = new HashMap<>();
public static void clearCache() { public static void clearCache() {
portCache = new HashMap<>();
domainCache = new HashMap<>(); domainCache = new HashMap<>();
} }
public static void buildCache() { public static void buildCache() {
domainCache = new HashMap<>(); domainCache = new HashMap<>();
portCache = new HashMap<>();
if (ProviderPicker.getDnsQueryMethod() >= ProviderPicker.DNS_QUERY_METHOD_HTTPS_IETF && if (ProviderPicker.getDnsQueryMethod() >= ProviderPicker.DNS_QUERY_METHOD_HTTPS_IETF &&
!Daedalus.getPrefs().getBoolean("settings_dont_build_doh_cache", false)) { !Daedalus.getPrefs().getBoolean("settings_dont_build_cache", false)) {
buildDomainCache(getAddressById(getPrimary())); buildDomainCache(getServerById(getPrimary()).getAddress());
buildDomainCache(getAddressById(getSecondary())); buildDomainCache(getServerById(getSecondary()).getAddress());
}
for (DNSServer server : Daedalus.DNS_SERVERS) {
portCache.put(server.getAddress(), server.getPort());
}
for (CustomDNSServer server : Daedalus.configurations.getCustomDNSServers()) {
portCache.put(server.getAddress(), server.getPort());
} }
} }
@ -63,16 +51,6 @@ public class DNSServerHelper {
} }
} }
public static int getPortOrDefault(InetAddress address, int defaultPort) {
String hostAddress = address.getHostAddress();
if (portCache.containsKey(hostAddress)) {
return portCache.get(hostAddress);
}
return defaultPort;
}
public static int getPosition(String id) { public static int getPosition(String id) {
int intId = Integer.parseInt(id); int intId = Integer.parseInt(id);
if (intId < Daedalus.DNS_SERVERS.size()) { if (intId < Daedalus.DNS_SERVERS.size()) {
@ -88,18 +66,18 @@ public class DNSServerHelper {
} }
public static String getPrimary() { 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() { 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) { private static int checkServerId(int id) {
if (id < Daedalus.DNS_SERVERS.size()) { if (id < Daedalus.DNS_SERVERS.size()) {
return id; return id;
} }
for (CustomDNSServer server : Daedalus.configurations.getCustomDNSServers()) { for (CustomDnsServer server : Daedalus.configurations.getCustomDNSServers()) {
if (server.getId().equals(String.valueOf(id))) { if (server.getId().equals(String.valueOf(id))) {
return id; return id;
} }
@ -107,26 +85,26 @@ public class DNSServerHelper {
return 0; return 0;
} }
public static String getAddressById(String id) { public static AbstractDnsServer getServerById(String id) {
for (DNSServer server : Daedalus.DNS_SERVERS) { for (DnsServer server : Daedalus.DNS_SERVERS) {
if (server.getId().equals(id)) { if (server.getId().equals(id)) {
return server.getAddress(); return server;
} }
} }
for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { for (CustomDnsServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) {
if (customDNSServer.getId().equals(id)) { if (customDNSServer.getId().equals(id)) {
return customDNSServer.getAddress(); return customDNSServer;
} }
} }
return Daedalus.DNS_SERVERS.get(0).getAddress(); return Daedalus.DNS_SERVERS.get(0);
} }
public static String[] getIds() { public static String[] getIds() {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size());
for (DNSServer server : Daedalus.DNS_SERVERS) { for (DnsServer server : Daedalus.DNS_SERVERS) {
servers.add(server.getId()); servers.add(server.getId());
} }
for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { for (CustomDnsServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) {
servers.add(customDNSServer.getId()); servers.add(customDNSServer.getId());
} }
String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; String[] stringServers = new String[Daedalus.DNS_SERVERS.size()];
@ -135,30 +113,30 @@ public class DNSServerHelper {
public static String[] getNames(Context context) { public static String[] getNames(Context context) {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size());
for (DNSServer server : Daedalus.DNS_SERVERS) { for (DnsServer server : Daedalus.DNS_SERVERS) {
servers.add(server.getStringDescription(context)); servers.add(server.getStringDescription(context));
} }
for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { for (CustomDnsServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) {
servers.add(customDNSServer.getName()); servers.add(customDNSServer.getName());
} }
String[] stringServers = new String[Daedalus.DNS_SERVERS.size()]; String[] stringServers = new String[Daedalus.DNS_SERVERS.size()];
return servers.toArray(stringServers); return servers.toArray(stringServers);
} }
public static ArrayList<AbstractDNSServer> getAllServers() { public static ArrayList<AbstractDnsServer> getAllServers() {
ArrayList<AbstractDNSServer> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size()); ArrayList<AbstractDnsServer> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size());
servers.addAll(Daedalus.DNS_SERVERS); servers.addAll(Daedalus.DNS_SERVERS);
servers.addAll(Daedalus.configurations.getCustomDNSServers()); servers.addAll(Daedalus.configurations.getCustomDNSServers());
return servers; return servers;
} }
public static String getDescription(String id, Context context) { 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)) { if (server.getId().equals(id)) {
return server.getStringDescription(context); return server.getStringDescription(context);
} }
} }
for (CustomDNSServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) { for (CustomDnsServer customDNSServer : Daedalus.configurations.getCustomDNSServers()) {
if (customDNSServer.getId().equals(id)) { if (customDNSServer.getId().equals(id)) {
return customDNSServer.getName(); return customDNSServer.getName();
} }
@ -166,7 +144,7 @@ public class DNSServerHelper {
return Daedalus.DNS_SERVERS.get(0).getStringDescription(context); return Daedalus.DNS_SERVERS.get(0).getStringDescription(context);
} }
public static boolean isInUsing(CustomDNSServer server) { public static boolean isInUsing(CustomDnsServer server) {
return DaedalusVpnService.isActivated() && (server.getId().equals(getPrimary()) || server.getId().equals(getSecondary())); return DaedalusVpnService.isActivated() && (server.getId().equals(getPrimary()) || server.getId().equals(getSecondary()));
} }
} }

View File

@ -21,7 +21,8 @@ import org.itxtech.daedalus.provider.ProviderPicker;
import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver; import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver;
import org.itxtech.daedalus.util.Logger; import org.itxtech.daedalus.util.Logger;
import org.itxtech.daedalus.util.RuleResolver; import org.itxtech.daedalus.util.RuleResolver;
import org.itxtech.daedalus.util.server.DNSServerHelper; import org.itxtech.daedalus.server.AbstractDnsServer;
import org.itxtech.daedalus.server.DnsServerHelper;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
@ -51,8 +52,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
private static final String CHANNEL_ID = "daedalus_channel_1"; private static final String CHANNEL_ID = "daedalus_channel_1";
private static final String CHANNEL_NAME = "daedalus_channel"; private static final String CHANNEL_NAME = "daedalus_channel";
public static String primaryServer; public static AbstractDnsServer primaryServer;
public static String secondaryServer; public static AbstractDnsServer secondaryServer;
private NotificationCompat.Builder notification = null; private NotificationCompat.Builder notification = null;
@ -64,7 +65,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
private Thread mThread = null; private Thread mThread = null;
public HashMap<String, String> dnsServers; public HashMap<String, AbstractDnsServer> dnsServers;
private static boolean activated = false; private static boolean activated = false;
@ -185,7 +186,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
if (shouldRefresh) { if (shouldRefresh) {
RuleResolver.clear(); RuleResolver.clear();
DNSServerHelper.clearCache(); DnsServerHelper.clearCache();
Logger.info("Daedalus VPN service has stopped"); Logger.info("Daedalus VPN service has stopped");
} }
@ -202,27 +203,29 @@ public class DaedalusVpnService extends VpnService implements Runnable {
stopThread(); stopThread();
} }
private InetAddress addDnsServer(Builder builder, String format, byte[] ipv6Template, String addr) throws UnknownHostException { private InetAddress addDnsServer(Builder builder, String format, byte[] ipv6Template, AbstractDnsServer addr) throws UnknownHostException {
int size = dnsServers.size(); int size = dnsServers.size();
size++; size++;
if (addr.contains("/")) {//https uri if (addr.getAddress().contains("/")) {//https uri
String alias = String.format(format, size + 1); String alias = String.format(format, size + 1);
dnsServers.put(alias, addr); dnsServers.put(alias, addr);
builder.addRoute(alias, 32); builder.addRoute(alias, 32);
return InetAddress.getByName(alias); return InetAddress.getByName(alias);
} }
InetAddress address = InetAddress.getByName(addr); InetAddress address = InetAddress.getByName(addr.getAddress());
if (address instanceof Inet6Address && ipv6Template == null) { if (address instanceof Inet6Address && ipv6Template == null) {
Log.i(TAG, "addDnsServer: Ignoring DNS server " + address); Log.i(TAG, "addDnsServer: Ignoring DNS server " + address);
} else if (address instanceof Inet4Address) { } else if (address instanceof Inet4Address) {
String alias = String.format(format, size + 1); String alias = String.format(format, size + 1);
dnsServers.put(alias, address.getHostAddress()); addr.setHostAddress(address.getHostAddress());
dnsServers.put(alias, addr);
builder.addRoute(alias, 32); builder.addRoute(alias, 32);
return InetAddress.getByName(alias); return InetAddress.getByName(alias);
} else if (address instanceof Inet6Address) { } else if (address instanceof Inet6Address) {
ipv6Template[ipv6Template.length - 1] = (byte) (size + 1); ipv6Template[ipv6Template.length - 1] = (byte) (size + 1);
InetAddress i6addr = Inet6Address.getByAddress(ipv6Template); InetAddress i6addr = Inet6Address.getByAddress(ipv6Template);
dnsServers.put(i6addr.getHostAddress(), address.getHostAddress()); addr.setHostAddress(address.getHostAddress());
dnsServers.put(i6addr.getHostAddress(), addr);
return i6addr; return i6addr;
} }
return null; return null;
@ -231,7 +234,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
@Override @Override
public void run() { public void run() {
try { try {
DNSServerHelper.buildCache(); DnsServerHelper.buildCache();
Builder builder = new Builder() Builder builder = new Builder()
.setSession("Daedalus") .setSession("Daedalus")
.setConfigureIntent(PendingIntent.getActivity(this, 0, .setConfigureIntent(PendingIntent.getActivity(this, 0,
@ -275,7 +278,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
statisticQuery = Daedalus.getPrefs().getBoolean("settings_count_query_times", false); statisticQuery = Daedalus.getPrefs().getBoolean("settings_count_query_times", false);
byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; byte[] ipv6Template = new byte[]{32, 1, 13, (byte) (184 & 0xFF), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
if (primaryServer.contains(":") || secondaryServer.contains(":")) {//IPv6 if (primaryServer.getAddress().contains(":") || secondaryServer.getAddress().contains(":")) {//IPv6
try { try {
InetAddress addr = Inet6Address.getByAddress(ipv6Template); InetAddress addr = Inet6Address.getByAddress(ipv6Template);
Log.d(TAG, "configure: Adding IPv6 address" + addr); Log.d(TAG, "configure: Adding IPv6 address" + addr);
@ -296,15 +299,13 @@ public class DaedalusVpnService extends VpnService implements Runnable {
aliasPrimary = addDnsServer(builder, format, ipv6Template, primaryServer); aliasPrimary = addDnsServer(builder, format, ipv6Template, primaryServer);
aliasSecondary = addDnsServer(builder, format, ipv6Template, secondaryServer); aliasSecondary = addDnsServer(builder, format, ipv6Template, secondaryServer);
} else { } else {
aliasPrimary = InetAddress.getByName(primaryServer); aliasPrimary = InetAddress.getByName(primaryServer.getAddress());
aliasSecondary = InetAddress.getByName(secondaryServer); aliasSecondary = InetAddress.getByName(secondaryServer.getAddress());
} }
InetAddress primaryDNSServer = aliasPrimary; Logger.info("Daedalus VPN service is listening on " + primaryServer + " as " + aliasPrimary.getHostAddress());
InetAddress secondaryDNSServer = aliasSecondary; Logger.info("Daedalus VPN service is listening on " + secondaryServer + " as " + aliasSecondary.getHostAddress());
Logger.info("Daedalus VPN service is listening on " + primaryServer + " as " + primaryDNSServer.getHostAddress()); builder.addDnsServer(aliasPrimary).addDnsServer(aliasSecondary);
Logger.info("Daedalus VPN service is listening on " + secondaryServer + " as " + secondaryDNSServer.getHostAddress());
builder.addDnsServer(primaryDNSServer).addDnsServer(secondaryDNSServer);
if (advanced) { if (advanced) {
builder.setBlocking(true); builder.setBlocking(true);

View File

@ -3,7 +3,7 @@ package org.itxtech.daedalus.util;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.util.server.CustomDNSServer; import org.itxtech.daedalus.server.CustomDnsServer;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -27,7 +27,7 @@ public class Configurations {
private static File file; private static File file;
private ArrayList<CustomDNSServer> customDNSServers; private ArrayList<CustomDnsServer> customDNSServers;
private ArrayList<String> appObjects; private ArrayList<String> appObjects;
private ArrayList<Rule> hostsRules; private ArrayList<Rule> hostsRules;
@ -60,7 +60,7 @@ public class Configurations {
this.activateCounter = activateCounter; this.activateCounter = activateCounter;
} }
public ArrayList<CustomDNSServer> getCustomDNSServers() { public ArrayList<CustomDnsServer> getCustomDNSServers() {
if (customDNSServers == null) { if (customDNSServers == null) {
customDNSServers = new ArrayList<>(); customDNSServers = new ArrayList<>();
} }

View File

@ -62,7 +62,7 @@
<string name="settings_manual_summary">访问 GitHub wiki 页面。</string> <string name="settings_manual_summary">访问 GitHub wiki 页面。</string>
<string name="settings_log_size">日志大小限制</string> <string name="settings_log_size">日志大小限制</string>
<string name="settings_debug_output">调试输出</string> <string name="settings_debug_output">调试输出</string>
<string name="settings_dont_build_doh_cache">不缓存 DoH DNS 的 IP地址</string> <string name="settings_dont_build_cache">不缓存 DNS 的 IP地址</string>
<string name="settings_dark_theme">使用暗主题</string> <string name="settings_dark_theme">使用暗主题</string>
<string name="settings_dns_query_method">DNS 查询方式</string> <string name="settings_dns_query_method">DNS 查询方式</string>
<string name="settings_foreground">运行前台服务</string> <string name="settings_foreground">运行前台服务</string>

View File

@ -62,7 +62,7 @@
<string name="settings_manual_summary">造訪 GitHub wiki 頁面。</string> <string name="settings_manual_summary">造訪 GitHub wiki 頁面。</string>
<string name="settings_log_size">紀錄檔大小限制</string> <string name="settings_log_size">紀錄檔大小限制</string>
<string name="settings_debug_output">调试输出 TODO</string> <string name="settings_debug_output">调试输出 TODO</string>
<string name="settings_dont_build_doh_cache">不緩存 DoH DNS 的 IP地址</string> <string name="settings_dont_build_cache">不緩存 DNS 的 IP地址</string>
<string name="settings_dark_theme">使用暗主题</string> <string name="settings_dark_theme">使用暗主题</string>
<string name="settings_rule_name">規則名稱</string> <string name="settings_rule_name">規則名稱</string>

View File

@ -60,7 +60,7 @@
<string name="settings_log_size">Log Size Limit</string> <string name="settings_log_size">Log Size Limit</string>
<string name="settings_manual">Manual</string> <string name="settings_manual">Manual</string>
<string name="settings_manual_summary">Visit GitHub wiki page.</string> <string name="settings_manual_summary">Visit GitHub wiki page.</string>
<string name="settings_dont_build_doh_cache">Don\'t build DNS Cache for DoH DNS</string> <string name="settings_dont_build_cache">Don\'t cache ip for DNS servers</string>
<string name="settings_debug_output">Debug Output</string> <string name="settings_debug_output">Debug Output</string>
<string name="settings_dark_theme">Use Dark Theme</string> <string name="settings_dark_theme">Use Dark Theme</string>
<string name="settings_dns_query_method">DNS Query Method</string> <string name="settings_dns_query_method">DNS Query Method</string>

View File

@ -92,8 +92,8 @@
android:defaultValue="false" android:defaultValue="false"
android:enabled="false"/> android:enabled="false"/>
<SwitchPreference <SwitchPreference
android:key="settings_dont_build_doh_cache" android:key="settings_dont_build_cache"
android:title="@string/settings_dont_build_doh_cache" android:title="@string/settings_dont_build_cache"
android:defaultValue="false" android:defaultValue="false"
android:enabled="false"/> android:enabled="false"/>
<ListPreference <ListPreference