diff options
author | Samuel Holland <samuel@sholland.org> | 2017-11-08 00:49:44 -0600 |
---|---|---|
committer | Samuel Holland <samuel@sholland.org> | 2017-11-08 00:49:44 -0600 |
commit | d82639afb2e2418828a64535d94585cee67a61a3 (patch) | |
tree | 35add9f936cbcb2f4d4ff2623f2da34f7378edf0 /app/src/main/java/com | |
parent | 51ae22f211e0a17c817c9a45941c1e50bbd8d245 (diff) |
VpnService: Add helper for importing configs
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r-- | app/src/main/java/com/wireguard/android/VpnService.java | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/app/src/main/java/com/wireguard/android/VpnService.java b/app/src/main/java/com/wireguard/android/VpnService.java index 265c6e57..0a828bc0 100644 --- a/app/src/main/java/com/wireguard/android/VpnService.java +++ b/app/src/main/java/com/wireguard/android/VpnService.java @@ -2,8 +2,10 @@ package com.wireguard.android; import android.app.Service; import android.content.ComponentName; +import android.content.ContentResolver; import android.content.Intent; import android.content.SharedPreferences; +import android.net.Uri; import android.os.AsyncTask; import android.os.Binder; import android.os.Build; @@ -21,7 +23,10 @@ import java.io.File; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.charset.StandardCharsets; +import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.LinkedList; @@ -112,6 +117,10 @@ public class VpnService extends Service return configurations; } + public void importFrom(final Uri... uris) { + new ConfigImporter().execute(uris); + } + @Override public IBinder onBind(final Intent intent) { instance = this; @@ -268,6 +277,47 @@ public class VpnService extends Service } } + private class ConfigImporter extends AsyncTask<Uri, Void, List<File>> { + @Override + protected List<File> doInBackground(final Uri... uris) { + final ContentResolver contentResolver = getContentResolver(); + final List<File> files = new ArrayList<>(uris.length); + for (final Uri uri : uris) { + if (isCancelled()) + return null; + String name = Uri.decode(uri.getLastPathSegment()); + if (name.indexOf('/') >= 0) + name = name.substring(name.lastIndexOf('/')); + if (!name.endsWith(".conf")) + name = name + ".conf"; + final File output = new File(getFilesDir(), name); + if (output.exists()) { + Log.w(getClass().getSimpleName(), "Config file for " + uri + " already exists"); + continue; + } + try (final InputStream in = contentResolver.openInputStream(uri); + final OutputStream out = new FileOutputStream(output, false)) { + if (in == null) + throw new IOException("Failed to open input"); + // FIXME: This is a rather arbitrary size. + final byte[] buffer = new byte[4096]; + int bytes; + while ((bytes = in.read(buffer)) != -1) + out.write(buffer, 0, bytes); + files.add(output); + } catch (final IOException e) { + Log.w(getClass().getSimpleName(), "Failed to import config from " + uri, e); + } + } + return files; + } + + @Override + protected void onPostExecute(final List<File> files) { + new ConfigLoader().execute(files.toArray(new File[files.size()])); + } + } + private class ConfigLoader extends AsyncTask<File, Void, List<Config>> { @Override protected List<Config> doInBackground(final File... files) { |