NotSupported: check if the module exists

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2017-11-27 04:28:48 +01:00 committed by Samuel Holland
parent ad16d2cc7b
commit 6bc6aea2d0
5 changed files with 86 additions and 12 deletions

View File

@ -26,6 +26,11 @@
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".NotSupportedActivity"
android:label="@string/not_supported"
android:parentActivityName=".ConfigActivity" />
<activity <activity
android:name=".SettingsActivity" android:name=".SettingsActivity"
android:label="@string/settings" android:label="@string/settings"

View File

@ -0,0 +1,26 @@
package com.wireguard.android;
import android.app.Activity;
import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import com.wireguard.android.databinding.NotSupportedActivityBinding;
public class NotSupportedActivity extends Activity {
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final NotSupportedActivityBinding binding =
DataBindingUtil.setContentView(this, R.layout.not_supported_activity);
final String messageHtml = getString(R.string.not_supported_message);
final Spanned messageText;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
messageText = Html.fromHtml(messageHtml, Html.FROM_HTML_MODE_COMPACT);
else
messageText = Html.fromHtml(messageHtml);
binding.notSupportedMessage.setText(messageText);
}
}

View File

@ -17,7 +17,9 @@ import android.service.quicksettings.TileService;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import com.wireguard.android.NotSupportedActivity;
import com.wireguard.android.QuickTileService; import com.wireguard.android.QuickTileService;
import com.wireguard.android.R;
import com.wireguard.android.bindings.ObservableSortedMap; import com.wireguard.android.bindings.ObservableSortedMap;
import com.wireguard.android.bindings.ObservableTreeMap; import com.wireguard.android.bindings.ObservableTreeMap;
import com.wireguard.config.Config; import com.wireguard.config.Config;
@ -245,8 +247,11 @@ public class VpnService extends Service
@Override @Override
protected void onPostExecute(final Boolean result) { protected void onPostExecute(final Boolean result) {
config.setIsEnabled(!result); config.setIsEnabled(!result);
if (!result) if (!result) {
Toast.makeText(getApplicationContext(), getString(R.string.error_down),
Toast.LENGTH_SHORT).show();
return; return;
}
enabledConfigs.remove(config.getName()); enabledConfigs.remove(config.getName());
preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply(); preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply();
if (config.getName().equals(primaryName)) if (config.getName().equals(primaryName))
@ -254,7 +259,7 @@ public class VpnService extends Service
} }
} }
private class ConfigEnabler extends AsyncTask<Void, Void, Boolean> { private class ConfigEnabler extends AsyncTask<Void, Void, Integer> {
private final Config config; private final Config config;
private ConfigEnabler(final Config config) { private ConfigEnabler(final Config config) {
@ -262,17 +267,43 @@ public class VpnService extends Service
} }
@Override @Override
protected Boolean doInBackground(final Void... voids) { protected Integer doInBackground(final Void... voids) {
if (!new File("/sys/module/wireguard").exists())
return -0xfff0001;
if (!existsInUsualSuspects("wg") || !existsInUsualSuspects("wg-quick"))
return -0xfff0002;
Log.i(TAG, "Running wg-quick up for " + config.getName()); Log.i(TAG, "Running wg-quick up for " + config.getName());
final File configFile = new File(getFilesDir(), config.getName() + ".conf"); final File configFile = new File(getFilesDir(), config.getName() + ".conf");
return rootShell.run(null, "wg-quick up '" + configFile.getPath() + "'") == 0; return rootShell.run(null, "wg-quick up '" + configFile.getPath() + "'");
}
private boolean existsInUsualSuspects(final String file) {
return new File("/system/bin/" + file).exists() ||
new File("/system/xbin/" + file).exists() ||
new File("/system/sbin/" + file).exists() ||
new File("/bin/" + file).exists() ||
new File("/xbin/" + file).exists() ||
new File("/sbin/" + file).exists() ||
new File("/usr/bin/" + file).exists() ||
new File("/usr/xbin/" + file).exists() ||
new File("/usr/sbin/" + file).exists();
} }
@Override @Override
protected void onPostExecute(final Boolean result) { protected void onPostExecute(final Integer ret) {
config.setIsEnabled(result); config.setIsEnabled(ret == 0);
if (!result) if (ret != 0) {
if (ret == -0xfff0001) {
startActivity(new Intent(getApplicationContext(), NotSupportedActivity.class));
} else if (ret == -0xfff0002) {
Toast.makeText(getApplicationContext(), getString(R.string.error_missing),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), getString(R.string.error_up),
Toast.LENGTH_SHORT).show();
}
return; return;
}
enabledConfigs.add(config.getName()); enabledConfigs.add(config.getName());
preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply(); preferences.edit().putStringSet(KEY_ENABLED_CONFIGS, enabledConfigs).apply();
if (config.getName().equals(primaryName)) if (config.getName().equals(primaryName))
@ -378,10 +409,8 @@ public class VpnService extends Service
config.setName(configName); config.setName(configName);
configs.add(config); configs.add(config);
} catch (IllegalArgumentException | IOException e) { } catch (IllegalArgumentException | IOException e) {
try { if (!file.delete()) {
file.delete(); Log.e(TAG, "Could not delete configuration for config " + configName);
} catch (Exception e2) {
Log.w(TAG, "Could not remove " + fileName, e2);
} }
Log.w(TAG, "Failed to load config from " + fileName, e); Log.w(TAG, "Failed to load config from " + fileName, e);
publishProgress(fileName + ": " + e.getMessage()); publishProgress(fileName + ": " + e.getMessage());

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/not_supported_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp" />
</layout>

View File

@ -16,15 +16,21 @@
<string name="edit">Edit</string> <string name="edit">Edit</string>
<string name="enabled">Enabled</string> <string name="enabled">Enabled</string>
<string name="endpoint">Endpoint</string> <string name="endpoint">Endpoint</string>
<string name="error_down">Error bringing down WireGuard tunnel</string>
<string name="error_missing">Missing wg(8) and/or wg-quick(8) in PATH</string>
<string name="error_up">Error bringing up WireGuard tunnel</string>
<string name="generate">Generate</string> <string name="generate">Generate</string>
<string name="hint_automatic">(auto)</string> <string name="hint_automatic">(auto)</string>
<string name="hint_generated">(generated)</string> <string name="hint_generated">(generated)</string>
<string name="hint_optional">(optional)</string> <string name="hint_optional">(optional)</string>
<string name="hint_random">(random)</string> <string name="hint_random">(random)</string>
<string name="iface">Interface</string> <string name="iface">Interface</string>
<string name="import_config">Import</string>
<string name="listen_port">Listen port</string> <string name="listen_port">Listen port</string>
<string name="mtu">MTU</string> <string name="mtu">MTU</string>
<string name="name">Name</string> <string name="name">Name</string>
<string name="not_supported">WireGuard Not Supported</string>
<string name="not_supported_message"><![CDATA[<p>Your Android device does not currently support the WireGuard kernel module. Please talk to the manufacturer of your Android device or the author of your device\'s ROM about supporting the WireGuard kernel module.</p><br><p>Fortunately, the WireGuard development team is currently in the process of implementing support for WireGuard in a way that will work on all Android devices. This means that while you may not be able to use WireGuard today, in several weeks, things may very well be available for you.</p>]]></string>
<string name="peer">Peer</string> <string name="peer">Peer</string>
<string name="persistent_keepalive">Persistent keepalive</string> <string name="persistent_keepalive">Persistent keepalive</string>
<string name="placeholder_text">No configuration selected</string> <string name="placeholder_text">No configuration selected</string>
@ -41,5 +47,4 @@
<string name="settings">Settings</string> <string name="settings">Settings</string>
<string name="status">Status</string> <string name="status">Status</string>
<string name="toggle">Toggle</string> <string name="toggle">Toggle</string>
<string name="import_config">Import</string>
</resources> </resources>