DaedalusVpnService: listen network change and update upstream DNS

This commit is contained in:
PeratX 2019-10-23 20:13:11 +08:00
parent 000706a2ae
commit cb08d9ddeb
10 changed files with 123 additions and 87 deletions
app/src/main

View File

@ -16,10 +16,14 @@ import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonReader;
import org.itxtech.daedalus.activity.MainActivity; import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.server.AbstractDnsServer;
import org.itxtech.daedalus.server.DnsServer; import org.itxtech.daedalus.server.DnsServer;
import org.itxtech.daedalus.server.DnsServerHelper; import org.itxtech.daedalus.server.DnsServerHelper;
import org.itxtech.daedalus.service.DaedalusVpnService; 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.RuleResolver;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -206,19 +210,8 @@ public class Daedalus extends Application {
} }
public static void activateService(Context context) { public static void activateService(Context context) {
DaedalusVpnService.primaryServer = DnsServerHelper.getServerById(DnsServerHelper.getPrimary()); DaedalusVpnService.primaryServer = (AbstractDnsServer) DnsServerHelper.getServerById(DnsServerHelper.getPrimary()).clone();
DaedalusVpnService.secondaryServer = DnsServerHelper.getServerById(DnsServerHelper.getSecondary()); DaedalusVpnService.secondaryServer = (AbstractDnsServer) DnsServerHelper.getServerById(DnsServerHelper.getSecondary()).clone();
if (getPrefs().getBoolean("settings_use_system_dns", false)) {
String[] servers = DnsServersDetector.getServers(context);
if (servers != null) {
if (servers.length >= 2) {
DaedalusVpnService.primaryServer = new DnsServer(servers[0], 0);
DaedalusVpnService.secondaryServer = new DnsServer(servers[1], 0);
} else {
DaedalusVpnService.primaryServer = DaedalusVpnService.secondaryServer = new DnsServer(servers[0]);
}
}
}
if (getInstance().prefs.getBoolean("settings_foreground", false) if (getInstance().prefs.getBoolean("settings_foreground", false)
&& Build.VERSION.SDK_INT > Build.VERSION_CODES.O) { && Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
Logger.info("Starting foreground service"); Logger.info("Starting foreground service");

View File

@ -39,7 +39,6 @@ public class GlobalConfigFragment extends PreferenceFragmentCompat {
add("secondary_server"); add("secondary_server");
}}) { }}) {
ListPreference listPref = findPreference(k); ListPreference listPref = findPreference(k);
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()));
@ -110,13 +109,6 @@ public class GlobalConfigFragment extends PreferenceFragmentCompat {
updateOptions(advanced.isChecked(), "settings_advanced"); updateOptions(advanced.isChecked(), "settings_advanced");
updateOptions(appFilter.isChecked(), "settings_app_filter"); updateOptions(appFilter.isChecked(), "settings_app_filter");
findPreference("settings_use_system_dns").setOnPreferenceChangeListener((preference, newValue) -> {
boolean vis = !(boolean) newValue;
findPreference("primary_server").setVisible(vis);
findPreference("secondary_server").setVisible(vis);
return true;
});
} }
private void updateOptions(boolean checked, String pref) { private void updateOptions(boolean checked, String pref) {

View File

@ -1,5 +1,7 @@
package org.itxtech.daedalus.server; package org.itxtech.daedalus.server;
import androidx.annotation.NonNull;
/** /**
* Daedalus Project * Daedalus Project
* *
@ -11,7 +13,7 @@ package org.itxtech.daedalus.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 implements Cloneable {
public static final int DNS_SERVER_DEFAULT_PORT = 53; public static final int DNS_SERVER_DEFAULT_PORT = 53;
protected String address; protected String address;
@ -63,4 +65,14 @@ public class AbstractDnsServer {
public boolean isHttpsServer() { public boolean isHttpsServer() {
return address.contains("/"); return address.contains("/");
} }
@NonNull
@Override
public Object clone() {
try {
return super.clone();
} catch (Exception ignored) {
}
return new AbstractDnsServer("", 0);
}
} }

View File

@ -4,14 +4,18 @@ import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.VpnService; import android.net.VpnService;
import android.os.Build; import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.system.OsConstants; import android.system.OsConstants;
import android.util.Log; import android.util.Log;
import androidx.appcompat.app.AlertDialog;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import org.itxtech.daedalus.Daedalus; import org.itxtech.daedalus.Daedalus;
import org.itxtech.daedalus.R; import org.itxtech.daedalus.R;
@ -19,10 +23,12 @@ import org.itxtech.daedalus.activity.MainActivity;
import org.itxtech.daedalus.provider.Provider; import org.itxtech.daedalus.provider.Provider;
import org.itxtech.daedalus.provider.ProviderPicker; import org.itxtech.daedalus.provider.ProviderPicker;
import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver; import org.itxtech.daedalus.receiver.StatusBarBroadcastReceiver;
import org.itxtech.daedalus.server.AbstractDnsServer;
import org.itxtech.daedalus.server.DnsServer;
import org.itxtech.daedalus.server.DnsServerHelper;
import org.itxtech.daedalus.util.DnsServersDetector;
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.server.AbstractDnsServer;
import org.itxtech.daedalus.server.DnsServerHelper;
import java.net.Inet4Address; import java.net.Inet4Address;
import java.net.Inet6Address; import java.net.Inet6Address;
@ -54,20 +60,19 @@ public class DaedalusVpnService extends VpnService implements Runnable {
public static AbstractDnsServer primaryServer; public static AbstractDnsServer primaryServer;
public static AbstractDnsServer secondaryServer; public static AbstractDnsServer secondaryServer;
private static InetAddress aliasPrimary;
private static InetAddress aliasSecondary;
private NotificationCompat.Builder notification = null; private NotificationCompat.Builder notification = null;
private boolean running = false; private boolean running = false;
private long lastUpdate = 0; private long lastUpdate = 0;
private boolean statisticQuery; private boolean statisticQuery;
private Provider provider; private Provider provider;
private ParcelFileDescriptor descriptor; private ParcelFileDescriptor descriptor;
private Thread mThread = null; private Thread mThread = null;
public HashMap<String, AbstractDnsServer> dnsServers; public HashMap<String, AbstractDnsServer> dnsServers;
private static boolean activated = false; private static boolean activated = false;
private static BroadcastReceiver receiver;
public static boolean isActivated() { public static boolean isActivated() {
return activated; return activated;
@ -76,6 +81,44 @@ public class DaedalusVpnService extends VpnService implements Runnable {
@Override @Override
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
if (Daedalus.getPrefs().getBoolean("settings_use_system_dns", false)) {
registerReceiver(receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUpstreamServers(context);
}
}, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
public static void updateUpstreamServers(Context context) {
String[] servers = DnsServersDetector.getServers(context);
if (servers != null) {
if (servers.length >= 2 && (aliasPrimary == null || !aliasPrimary.getHostAddress().equals(servers[0])) &&
(aliasSecondary == null || !aliasSecondary.getHostAddress().equals(servers[0])) &&
(aliasPrimary == null || !aliasPrimary.getHostAddress().equals(servers[1])) &&
(aliasSecondary == null || !aliasSecondary.getHostAddress().equals(servers[1]))) {
primaryServer.setAddress(servers[0]);
primaryServer.setPort(DnsServer.DNS_SERVER_DEFAULT_PORT);
secondaryServer.setAddress(servers[1]);
secondaryServer.setPort(DnsServer.DNS_SERVER_DEFAULT_PORT);
} else if ((aliasPrimary == null || !aliasPrimary.getHostAddress().equals(servers[0])) &&
(aliasSecondary == null || !aliasSecondary.getHostAddress().equals(servers[0]))) {
primaryServer.setAddress(servers[0]);
primaryServer.setPort(DnsServer.DNS_SERVER_DEFAULT_PORT);
secondaryServer.setAddress(servers[0]);
secondaryServer.setPort(DnsServer.DNS_SERVER_DEFAULT_PORT);
} else {
StringBuilder buf = new StringBuilder();
for (String server : servers) {
buf.append(server).append(" ");
}
Logger.error("Invalid upstream DNS " + buf);
}
Logger.info("Upstream DNS updated: " + primaryServer.getAddress() + " " + secondaryServer.getAddress());
} else {
Logger.error("Cannot obtain upstream DNS server!");
}
} }
@Override @Override
@ -85,7 +128,6 @@ public class DaedalusVpnService extends VpnService implements Runnable {
case ACTION_ACTIVATE: case ACTION_ACTIVATE:
activated = true; activated = true;
if (Daedalus.getPrefs().getBoolean("settings_notification", true)) { if (Daedalus.getPrefs().getBoolean("settings_notification", true)) {
NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder; NotificationCompat.Builder builder;
@ -127,12 +169,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
} }
Daedalus.initRuleResolver(); Daedalus.initRuleResolver();
startThread();
if (this.mThread == null) {
this.mThread = new Thread(this, "DaedalusVpn");
this.running = true;
this.mThread.start();
}
Daedalus.updateShortcut(getApplicationContext()); Daedalus.updateShortcut(getApplicationContext());
if (MainActivity.getInstance() != null) { if (MainActivity.getInstance() != null) {
MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class) MainActivity.getInstance().startActivity(new Intent(getApplicationContext(), MainActivity.class)
@ -147,9 +184,20 @@ public class DaedalusVpnService extends VpnService implements Runnable {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
private void startThread() {
if (this.mThread == null) {
this.mThread = new Thread(this, "DaedalusVpn");
this.running = true;
this.mThread.start();
}
}
@Override @Override
public void onDestroy() { public void onDestroy() {
stopThread(); stopThread();
if (receiver != null) {
unregisterReceiver(receiver);
}
} }
private void stopThread() { private void stopThread() {
@ -203,7 +251,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
stopThread(); stopThread();
} }
private InetAddress addDnsServer(Builder builder, String format, byte[] ipv6Template, AbstractDnsServer 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.getAddress().contains("/")) {//https uri if (addr.getAddress().contains("/")) {//https uri
@ -241,7 +290,6 @@ public class DaedalusVpnService extends VpnService implements Runnable {
new Intent(this, MainActivity.class).putExtra(MainActivity.LAUNCH_FRAGMENT, MainActivity.FRAGMENT_SETTINGS), new Intent(this, MainActivity.class).putExtra(MainActivity.LAUNCH_FRAGMENT, MainActivity.FRAGMENT_SETTINGS),
PendingIntent.FLAG_ONE_SHOT)); PendingIntent.FLAG_ONE_SHOT));
//Set App Filter
if (Daedalus.getPrefs().getBoolean("settings_app_filter_switch", false)) { if (Daedalus.getPrefs().getBoolean("settings_app_filter_switch", false)) {
ArrayList<String> apps = Daedalus.configurations.getAppObjects(); ArrayList<String> apps = Daedalus.configurations.getAppObjects();
if (apps.size() > 0) { if (apps.size() > 0) {
@ -278,7 +326,6 @@ 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.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);
@ -288,12 +335,7 @@ public class DaedalusVpnService extends VpnService implements Runnable {
ipv6Template = null; ipv6Template = null;
} }
} else {
ipv6Template = null;
}
InetAddress aliasPrimary;
InetAddress aliasSecondary;
if (advanced) { if (advanced) {
dnsServers = new HashMap<>(); dnsServers = new HashMap<>();
aliasPrimary = addDnsServer(builder, format, ipv6Template, primaryServer); aliasPrimary = addDnsServer(builder, format, ipv6Template, primaryServer);
@ -303,8 +345,8 @@ public class DaedalusVpnService extends VpnService implements Runnable {
aliasSecondary = InetAddress.getByName(secondaryServer.getAddress()); aliasSecondary = InetAddress.getByName(secondaryServer.getAddress());
} }
Logger.info("Daedalus VPN service is listening on " + primaryServer + " as " + aliasPrimary.getHostAddress()); Logger.info("Daedalus VPN service is listening on " + primaryServer.getAddress() + " as " + aliasPrimary.getHostAddress());
Logger.info("Daedalus VPN service is listening on " + secondaryServer + " as " + aliasSecondary.getHostAddress()); Logger.info("Daedalus VPN service is listening on " + secondaryServer.getAddress() + " as " + aliasSecondary.getHostAddress());
builder.addDnsServer(aliasPrimary).addDnsServer(aliasSecondary); builder.addDnsServer(aliasPrimary).addDnsServer(aliasSecondary);
if (advanced) { if (advanced) {
@ -325,13 +367,17 @@ public class DaedalusVpnService extends VpnService implements Runnable {
Thread.sleep(1000); Thread.sleep(1000);
} }
} }
} catch ( } catch (InterruptedException ignored) {
InterruptedException ignored) { } catch (Exception e) {
} catch ( MainActivity.getInstance().runOnUiThread(() ->
Exception e) { new AlertDialog.Builder(MainActivity.getInstance())
.setTitle(R.string.error_occurred)
.setMessage(Logger.getExceptionMessage(e))
.setPositiveButton(android.R.string.ok, (d, id) -> {
})
.show());
Logger.logException(e); Logger.logException(e);
} finally { } finally {
Log.d(TAG, "quit");
stopThread(); stopThread();
} }
} }
@ -354,11 +400,11 @@ public class DaedalusVpnService extends VpnService implements Runnable {
} }
} }
public static class VpnNetworkException extends Exception { public static class VpnNetworkException extends Exception {
public VpnNetworkException(String s) { public VpnNetworkException(String s) {
super(s); super(s);
} }
public VpnNetworkException(String s, Throwable t) { public VpnNetworkException(String s, Throwable t) {
super(s, t); super(s, t);
} }

View File

@ -1,20 +0,0 @@
<resources>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
</style>
<style name="AppTheme.NoActionBar.TransparentStatusBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppTheme.Dark.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
</style>
<style name="AppTheme.Dark.NoActionBar.TransparentStatusBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources>

View File

@ -104,4 +104,6 @@
<string name="test_test_domain">google.com</string> <string name="test_test_domain">google.com</string>
<string name="nav_version">版本:</string> <string name="nav_version">版本:</string>
<string name="error_occurred">启动时出现了一个错误</string>
</resources> </resources>

View File

@ -101,4 +101,6 @@
<string name="test_test_domain">google.com</string> <string name="test_test_domain">google.com</string>
<string name="nav_version">版本:</string> <string name="nav_version">版本:</string>
<string name="error_occurred">啟動時出現了一個錯誤</string>
</resources> </resources>

View File

@ -112,4 +112,6 @@
<string name="nav_version">Version:</string> <string name="nav_version">Version:</string>
<string name="nav_git_commit">Git commit:</string> <string name="nav_git_commit">Git commit:</string>
<string name="nav_github">GitHub</string> <string name="nav_github">GitHub</string>
<string name="error_occurred">An error occurred at startup</string>
</resources> </resources>

View File

@ -1,8 +1,5 @@
<resources> <resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
@ -12,15 +9,25 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorPrimaryLight</item> <item name="colorAccent">@color/colorPrimaryLight</item>
</style> </style>
<style name="AppTheme.NoActionBar"> <style name="AppTheme.NoActionBar" parent="AppTheme">
<item name="windowActionBar">false</item> <item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
</style> </style>
<style name="AppTheme.Dark.NoActionBar"> <style name="AppTheme.Dark.NoActionBar" parent="AppTheme.Dark">
<item name="windowActionBar">false</item> <item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item> <item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item>
</style> </style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>
<style name="AppTheme.NoActionBar.TransparentStatusBar" parent="AppTheme.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
<style name="AppTheme.Dark.NoActionBar.TransparentStatusBar" parent="AppTheme.Dark.NoActionBar">
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
</resources> </resources>

View File

@ -6,10 +6,6 @@
android:key="settingsServer" android:key="settingsServer"
android:title="@string/settings_server"> android:title="@string/settings_server">
<SwitchPreference
android:key="settings_use_system_dns"
android:title="@string/settings_use_system_dns"
android:defaultValue="false"/>
<ListPreference <ListPreference
android:key="primary_server" android:key="primary_server"
android:title="@string/primary_server" android:title="@string/primary_server"
@ -96,6 +92,10 @@
android:title="@string/settings_dont_build_cache" android:title="@string/settings_dont_build_cache"
android:defaultValue="false" android:defaultValue="false"
android:enabled="false"/> android:enabled="false"/>
<SwitchPreference
android:key="settings_use_system_dns"
android:title="@string/settings_use_system_dns"
android:defaultValue="false"/>
<ListPreference <ListPreference
android:key="settings_dns_query_method" android:key="settings_dns_query_method"
android:title="@string/settings_dns_query_method" android:title="@string/settings_dns_query_method"