Added support for custom dns server port

This commit is contained in:
PeratX 2017-05-01 15:58:30 +08:00
parent d2ebad1f5a
commit ef2729d781
7 changed files with 59 additions and 28 deletions

View File

@ -52,9 +52,9 @@ public class DnsTestFragment extends Fragment {
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.getDnsServerNames(Daedalus.getInstance()));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, DnsServerHelper.getNames(Daedalus.getInstance()));
spinnerServerChoice.setAdapter(spinnerArrayAdapter);
spinnerServerChoice.setSelection(Integer.parseInt(DnsServerHelper.getPrimaryDnsServer()));
spinnerServerChoice.setSelection(Integer.parseInt(DnsServerHelper.getPrimary()));
final AutoCompleteTextView textViewTestUrl = (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);
@ -70,7 +70,7 @@ public class DnsTestFragment extends Fragment {
}
StringBuilder testText = new StringBuilder();
ArrayList<String> dnsServers = new ArrayList<String>() {{
add(DnsServerHelper.getDnsServerAddressByStringDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString()));
add(DnsServerHelper.getAddressByDescription(Daedalus.getInstance(), spinnerServerChoice.getSelectedItem().toString()));
String servers = Daedalus.getPrefs().getString("dns_test_servers", "");
if (!servers.equals("")) {
for (String server : servers.split(",")) {

View File

@ -86,8 +86,8 @@ public class MainFragment extends Fragment {
public void onActivityResult(int request, int result, Intent data) {
if (result == Activity.RESULT_OK) {
DaedalusVpnService.primaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getPrimaryDnsServer());
DaedalusVpnService.secondaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getSecondaryDnsServer());
DaedalusVpnService.primaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getSecondary());
Daedalus.getInstance().startService(Daedalus.getInstance().getServiceIntent().setAction(DaedalusVpnService.ACTION_ACTIVATE));

View File

@ -30,20 +30,20 @@ public class SettingsFragment extends PreferenceFragment {
super.onCreate(savedInstanceState);
Daedalus.getPrefs().edit()
.putString("primary_server", DnsServerHelper.getPrimaryDnsServer())
.putString("secondary_server", DnsServerHelper.getSecondaryDnsServer())
.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.getDnsServerNames(Daedalus.getInstance()));
primaryServer.setEntryValues(DnsServerHelper.getDnsServerIds());
primaryServer.setSummary(DnsServerHelper.getDnsServerDescription(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.getDnsServerDescription((String) newValue, Daedalus.getInstance()));
preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance()));
Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return true;
@ -51,13 +51,13 @@ public class SettingsFragment extends PreferenceFragment {
});
ListPreference secondaryServer = (ListPreference) findPreference("secondary_server");
secondaryServer.setEntries(DnsServerHelper.getDnsServerNames(Daedalus.getInstance()));
secondaryServer.setEntryValues(DnsServerHelper.getDnsServerIds());
secondaryServer.setSummary(DnsServerHelper.getDnsServerDescription(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.getDnsServerDescription((String) newValue, Daedalus.getInstance()));
preference.setSummary(DnsServerHelper.getDescription((String) newValue, Daedalus.getInstance()));
Snackbar.make(view, R.string.notice_need_restart, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
return true;

View File

@ -28,8 +28,8 @@ public class BootBroadcastReceiver extends BroadcastReceiver {
context.startActivity(vIntent);
}
DaedalusVpnService.primaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getPrimaryDnsServer());
DaedalusVpnService.secondaryServer = DnsServerHelper.getDnsServerAddressById(DnsServerHelper.getSecondaryDnsServer());
DaedalusVpnService.primaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getPrimary());
DaedalusVpnService.secondaryServer = DnsServerHelper.getAddressById(DnsServerHelper.getSecondary());
context.startService((new Intent(context, DaedalusVpnService.class)).setAction(DaedalusVpnService.ACTION_ACTIVATE));

View File

@ -23,6 +23,7 @@ import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R;
import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver;
import org.itxtech.daedalus.util.DnsServerHelper;
import org.itxtech.daedalus.util.HostsResolver;
import org.pcap4j.packet.*;
import org.pcap4j.packet.factory.PacketFactoryPropertiesLoader;
@ -492,7 +493,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
// Let's be nice to Firefox. Firefox uses an empty UDP packet to
// 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, parsedUdp.getHeader().getDstPort().valueAsInt());
DatagramPacket outPacket = new DatagramPacket(new byte[0], 0, 0, destAddr,
DnsServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt()));
forwardPacket(outPacket, null);
return;
}
@ -526,7 +528,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
handleDnsResponse(parsedPacket, builder.build().toArray());
} else {
Log.i(TAG, "handleDnsRequest: DNS Name " + dnsQueryName + " , sending to " + destAddr);
DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr, parsedUdp.getHeader().getDstPort().valueAsInt());
DatagramPacket outPacket = new DatagramPacket(dnsRawData, 0, dnsRawData.length, destAddr,
DnsServerHelper.getPortOrDefault(destAddr, parsedUdp.getHeader().getDstPort().valueAsInt()));
forwardPacket(outPacket, parsedPacket);
}
} catch (Exception e) {

View File

@ -17,12 +17,22 @@ public class DnsServer {
private String id;
private String address;
private int port;
private int description = 0;
public DnsServer(String address, int description) {
public DnsServer(String address, int description, int port) {
this.id = String.valueOf(totalId++);
this.address = address;
this.description = description;
this.port = port;
}
public DnsServer(String address, int description) {
this(address, description, 53);
}
public int getPort() {
return port;
}
public String getId() {

View File

@ -3,6 +3,7 @@ package org.itxtech.daedalus.util;
import android.content.Context;
import org.itxtech.daedalus.Daedalus;
import java.net.InetAddress;
import java.util.ArrayList;
/**
@ -16,15 +17,32 @@ import java.util.ArrayList;
* the Free Software Foundation, version 3.
*/
public class DnsServerHelper {
public static String getPrimaryDnsServer() {
public static int getPortOrDefault(InetAddress address, int defaultPort) {
String hostAddress = address.getHostAddress();
for (DnsServer server : Daedalus.DNS_SERVERS) {
if (server.getAddress().equals(hostAddress)) {
return server.getPort();
}
}
for (CustomDnsServer server : Daedalus.configurations.getCustomDnsServers()) {
if (server.getAddress().equals(hostAddress)) {
return server.getPort();
}
}
return defaultPort;
}
public static String getPrimary() {
return String.valueOf(DnsServerHelper.checkServerId(Integer.parseInt(Daedalus.getPrefs().getString("primary_server", "0"))));
}
public static String getSecondaryDnsServer() {
public static String getSecondary() {
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;
@ -37,7 +55,7 @@ public class DnsServerHelper {
return 0;
}
public static String getDnsServerAddressById(String id) {
public static String getAddressById(String id) {
for (DnsServer server : Daedalus.DNS_SERVERS) {
if (server.getId().equals(id)) {
return server.getAddress();
@ -51,7 +69,7 @@ public class DnsServerHelper {
return Daedalus.DNS_SERVERS.get(0).getAddress();
}
public static String getDnsServerAddressByStringDescription(Context context, String description) {
public static String getAddressByDescription(Context context, String description) {
for (DnsServer server : Daedalus.DNS_SERVERS) {
if (server.getStringDescription(context).equals(description)) {
return server.getAddress();
@ -65,7 +83,7 @@ public class DnsServerHelper {
return Daedalus.DNS_SERVERS.get(0).getAddress();
}
public static String[] getDnsServerIds() {
public static String[] getIds() {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size());
for (DnsServer server : Daedalus.DNS_SERVERS) {
servers.add(server.getId());
@ -77,7 +95,7 @@ public class DnsServerHelper {
return servers.toArray(stringServers);
}
public static String[] getDnsServerNames(Context context) {
public static String[] getNames(Context context) {
ArrayList<String> servers = new ArrayList<>(Daedalus.DNS_SERVERS.size());
for (DnsServer server : Daedalus.DNS_SERVERS) {
servers.add(server.getStringDescription(context));
@ -89,7 +107,7 @@ public class DnsServerHelper {
return servers.toArray(stringServers);
}
public static String getDnsServerDescription(String id, Context context) {
public static String getDescription(String id, Context context) {
for (DnsServer server : Daedalus.DNS_SERVERS) {
if (server.getId().equals(id)) {
return server.getStringDescription(context);