ConfigImporter: give updates when something goes wrong

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2017-11-27 01:28:43 +01:00 committed by Samuel Holland
parent d43c87a869
commit b0bb46382c

View File

@ -15,6 +15,7 @@ import android.preference.PreferenceManager;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.service.quicksettings.TileService; import android.service.quicksettings.TileService;
import android.util.Log; import android.util.Log;
import android.widget.Toast;
import com.wireguard.android.QuickTileService; import com.wireguard.android.QuickTileService;
import com.wireguard.android.bindings.ObservableSortedMap; import com.wireguard.android.bindings.ObservableSortedMap;
@ -279,7 +280,7 @@ public class VpnService extends Service
} }
} }
private class ConfigImporter extends AsyncTask<Uri, Void, List<File>> { private class ConfigImporter extends AsyncTask<Uri, String, List<File>> {
@Override @Override
protected List<File> doInBackground(final Uri... uris) { protected List<File> doInBackground(final Uri... uris) {
final ContentResolver contentResolver = getContentResolver(); final ContentResolver contentResolver = getContentResolver();
@ -310,12 +311,14 @@ public class VpnService extends Service
name = name + ".conf"; name = name + ".conf";
if (!Config.isNameValid(name.substring(0, name.length() - 5))) { if (!Config.isNameValid(name.substring(0, name.length() - 5))) {
Log.v(getClass().getSimpleName(), "Detected name is not valid: " + name); Log.v(getClass().getSimpleName(), "Detected name is not valid: " + name);
name = "ImportedConfig.conf"; publishProgress(name + ": Invalid config filename");
continue;
} }
Log.d(getClass().getSimpleName(), "Mapped URI " + uri + " to file name " + name); Log.d(getClass().getSimpleName(), "Mapped URI " + uri + " to file name " + name);
final File output = new File(getFilesDir(), name); final File output = new File(getFilesDir(), name);
if (output.exists()) { if (output.exists()) {
Log.w(getClass().getSimpleName(), "Config file for " + uri + " already exists"); Log.w(getClass().getSimpleName(), "Config file " + name + " already exists");
publishProgress(name + " already exists");
continue; continue;
} }
try (final InputStream in = contentResolver.openInputStream(uri); try (final InputStream in = contentResolver.openInputStream(uri);
@ -330,18 +333,24 @@ public class VpnService extends Service
files.add(output); files.add(output);
} catch (final IOException e) { } catch (final IOException e) {
Log.w(getClass().getSimpleName(), "Failed to import config from " + uri, e); Log.w(getClass().getSimpleName(), "Failed to import config from " + uri, e);
publishProgress(name + ": " + e.getMessage());
} }
} }
return files; return files;
} }
@Override
protected void onProgressUpdate(final String... errors) {
Toast.makeText(getApplicationContext(), errors[0], Toast.LENGTH_SHORT).show();
}
@Override @Override
protected void onPostExecute(final List<File> files) { protected void onPostExecute(final List<File> files) {
new ConfigLoader().execute(files.toArray(new File[files.size()])); new ConfigLoader().execute(files.toArray(new File[files.size()]));
} }
} }
private class ConfigLoader extends AsyncTask<File, Void, List<Config>> { private class ConfigLoader extends AsyncTask<File, String, List<Config>> {
@Override @Override
protected List<Config> doInBackground(final File... files) { protected List<Config> doInBackground(final File... files) {
final List<Config> configs = new LinkedList<>(); final List<Config> configs = new LinkedList<>();
@ -375,11 +384,17 @@ public class VpnService extends Service
Log.w(TAG, "Could not remove " + fileName, 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());
} }
} }
return configs; return configs;
} }
@Override
protected void onProgressUpdate(final String... errors) {
Toast.makeText(getApplicationContext(), errors[0], Toast.LENGTH_SHORT).show();
}
@Override @Override
protected void onPostExecute(final List<Config> configs) { protected void onPostExecute(final List<Config> configs) {
if (configs == null) if (configs == null)
@ -453,7 +468,7 @@ public class VpnService extends Service
if (isAddOrRename() && configurations.containsKey(newName)) if (isAddOrRename() && configurations.containsKey(newName))
throw new IllegalStateException("Configuration " + newName + " already exists"); throw new IllegalStateException("Configuration " + newName + " already exists");
if (newConfig.getInterface().getPublicKey() == null) if (newConfig.getInterface().getPublicKey() == null)
throw new IllegalArgumentException("This configuration must have a valid keypair"); throw new IllegalArgumentException("This configuration needs a valid private key");
for (final Peer peer : newConfig.getPeers()) for (final Peer peer : newConfig.getPeers())
if (peer.getPublicKey() == null || peer.getPublicKey().isEmpty()) if (peer.getPublicKey() == null || peer.getPublicKey().isEmpty())
throw new IllegalArgumentException("Each peer must have a valid public key"); throw new IllegalArgumentException("Each peer must have a valid public key");