From d221543bb9d6bc9b7344cc96f010a501ff35bb7f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 1 Jul 2017 23:18:40 +0200 Subject: Support config located on external storage with the "external/" prefix --- .../org/pacien/tincapp/activities/StartActivity.kt | 11 +++--- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 30 +++++++--------- .../main/java/org/pacien/tincapp/commands/Tincd.kt | 8 ++--- .../java/org/pacien/tincapp/context/AppPaths.kt | 41 ++++++++++++---------- .../org/pacien/tincapp/service/TincVpnService.kt | 14 ++++---- 5 files changed, 50 insertions(+), 54 deletions(-) (limited to 'app/src/main/java/org') diff --git a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt index 3807ddc..910b36f 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -57,19 +57,18 @@ class StartActivity : BaseActivity() { } fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val confDir = AppPaths.confDir().path - AlertDialog.Builder(this) .setTitle(R.string.title_tinc_config_dir) - .setMessage(confDir) - .setNegativeButton(R.string.action_copy) { _, _ -> copyIntoClipboard(resources.getString(R.string.title_tinc_config_dir), confDir) } + .setMessage("Internal: " + AppPaths.confDir(AppPaths.Storage.INTERNAL) + "\n\n" + + "External: " + AppPaths.confDir(AppPaths.Storage.EXTERNAL)) .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } .show() } private fun startVpn(netName: String) { - startService(Intent(this, TincVpnService::class.java) - .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) + startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_CONF, + if (netName.startsWith("external/")) AppPaths.NetConf(AppPaths.Storage.EXTERNAL, netName.substringAfter("/")) + else AppPaths.NetConf(AppPaths.Storage.INTERNAL, netName))) } } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt index 91a2678..5116d63 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tinc.kt @@ -8,22 +8,16 @@ import java.io.IOException */ object Tinc { - private fun newCommand(netName: String): Command = + private fun newCommand(netConf: AppPaths.NetConf): Command = Command(AppPaths.tinc().absolutePath) - .withOption("config", AppPaths.confDir(netName).absolutePath) - .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) + .withOption("config", AppPaths.confDir(netConf).absolutePath) + .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath) // independently runnable commands @Throws(IOException::class) - fun network(): List = - Executor.call(Command(AppPaths.tinc().absolutePath) - .withOption("config", AppPaths.confDir().absolutePath) - .withArguments("network")) - - @Throws(IOException::class) - fun fsck(netName: String, fix: Boolean): List { - var cmd = newCommand(netName).withArguments("fsck") + fun fsck(netConf: AppPaths.NetConf, fix: Boolean): List { + var cmd = newCommand(netConf).withArguments("fsck") if (fix) cmd = cmd.withOption("force") return Executor.call(cmd) } @@ -31,18 +25,18 @@ object Tinc { // commands requiring a running tinc daemon @Throws(IOException::class) - fun stop(netName: String) { - Executor.call(newCommand(netName).withArguments("stop")) + fun stop(netConf: AppPaths.NetConf) { + Executor.call(newCommand(netConf).withArguments("stop")) } @Throws(IOException::class) - fun dumpNodes(netName: String, reachable: Boolean): List = + fun dumpNodes(netConf: AppPaths.NetConf, reachable: Boolean): List = Executor.call( - if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes") - else newCommand(netName).withArguments("dump", "nodes")) + if (reachable) newCommand(netConf).withArguments("dump", "reachable", "nodes") + else newCommand(netConf).withArguments("dump", "nodes")) @Throws(IOException::class) - fun info(netName: String, node: String): String = - Executor.call(newCommand(netName).withArguments("info", node)).joinToString("\n") + fun info(netConf: AppPaths.NetConf, node: String): String = + Executor.call(newCommand(netConf).withArguments("info", node)).joinToString("\n") } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt index 19ebfbd..c54b328 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Tincd.kt @@ -9,12 +9,12 @@ import java.io.IOException object Tincd { @Throws(IOException::class) - fun start(netName: String, fd: Int) { + fun start(netConf: AppPaths.NetConf, fd: Int) { Executor.forkExec(Command(AppPaths.tincd().absolutePath) .withOption("no-detach") - .withOption("config", AppPaths.confDir(netName).absolutePath) - .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) - .withOption("logfile", AppPaths.logFile(netName).absolutePath) + .withOption("config", AppPaths.confDir(netConf).absolutePath) + .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath) + .withOption("logfile", AppPaths.logFile(netConf).absolutePath) .withOption("option", "DeviceType=fd") .withOption("option", "Device=" + fd)) } diff --git a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt index 06bb318..58c6de2 100644 --- a/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt +++ b/app/src/main/java/org/pacien/tincapp/context/AppPaths.kt @@ -1,8 +1,7 @@ package org.pacien.tincapp.context -import android.content.Context - import java.io.File +import java.io.Serializable /** * @author pacien @@ -11,9 +10,8 @@ import java.io.File */ object AppPaths { - private val CONFDIR = "conf" - private val LOGDIR = "log" - private val PIDDIR = "pid" + enum class Storage { INTERNAL, EXTERNAL } + data class NetConf(val storage: Storage, val netName: String) : Serializable private val TINCD_BIN = "libtincd.so" private val TINC_BIN = "libtinc.so" @@ -23,21 +21,26 @@ object AppPaths { private val NET_CONF_FILE = "network.conf" - private fun createDirIfNotExists(basePath: File, newDir: String): File { - val f = File(basePath, newDir) - f.mkdirs() - return f + fun filesDir(storage: Storage): File = when (storage) { + Storage.INTERNAL -> App.getContext().filesDir + Storage.EXTERNAL -> App.getContext().getExternalFilesDir(null) + } + + fun cacheDir(storage: Storage): File = when (storage) { + Storage.INTERNAL -> App.getContext().cacheDir + Storage.EXTERNAL -> App.getContext().externalCacheDir } - fun confDir(): File = App.getContext().getDir(CONFDIR, Context.MODE_PRIVATE) - fun confDir(netName: String): File = File(confDir(), netName) - fun logDir(): File = createDirIfNotExists(App.getContext().cacheDir, LOGDIR) - fun pidDir(): File = createDirIfNotExists(App.getContext().cacheDir, PIDDIR) - fun logFile(netName: String): File = File(logDir(), String.format(LOGFILE_FORMAT, netName)) - fun pidFile(netName: String): File = File(pidDir(), String.format(PIDFILE_FORMAT, netName)) - fun netConfFile(netName: String): File = File(confDir(netName), NET_CONF_FILE) - fun binDir(): File = File(App.getContext().applicationInfo.nativeLibraryDir) - fun tincd(): File = File(binDir(), TINCD_BIN) - fun tinc(): File = File(binDir(), TINC_BIN) + fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir) + + fun confDir(storage: Storage) = filesDir(storage) + fun confDir(netConf: NetConf) = File(confDir(netConf.storage), netConf.netName) + + fun netConfFile(netConf: NetConf) = File(confDir(netConf), NET_CONF_FILE) + fun logFile(netConf: NetConf) = File(cacheDir(netConf.storage), String.format(LOGFILE_FORMAT, netConf.netName)) + fun pidFile(netConf: NetConf) = File(cacheDir(Storage.INTERNAL), String.format(PIDFILE_FORMAT, netConf.netName)) + + fun tincd() = File(binDir(), TINCD_BIN) + fun tinc() = File(binDir(), TINC_BIN) } diff --git a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt index cb520bb..366e208 100644 --- a/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt +++ b/app/src/main/java/org/pacien/tincapp/service/TincVpnService.kt @@ -15,17 +15,17 @@ import java.io.IOException */ class TincVpnService : VpnService() { - private var netName: String = "" + private var netConf: AppPaths.NetConf? = null override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { - this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) + netConf = intent.getSerializableExtra(INTENT_EXTRA_NET_CONF)!! as AppPaths.NetConf - val net = Builder().setSession(this.netName) - net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(this.netName))) + val net = Builder().setSession(netConf!!.netName) + net.apply(VpnInterfaceConfiguration(AppPaths.netConfFile(netConf!!))) applyIgnoringException(net::addDisallowedApplication, BuildConfig.APPLICATION_ID) try { - Tincd.start(this.netName, net.establish().detachFd()) + Tincd.start(netConf!!, net.establish().detachFd()) } catch (e: IOException) { e.printStackTrace() } @@ -35,7 +35,7 @@ class TincVpnService : VpnService() { override fun onDestroy() { try { - Tinc.stop(this.netName) + Tinc.stop(netConf!!) } catch (e: IOException) { e.printStackTrace() } @@ -43,7 +43,7 @@ class TincVpnService : VpnService() { } companion object { - val INTENT_EXTRA_NET_NAME = "netName" + val INTENT_EXTRA_NET_CONF = "netConf" } } -- cgit v1.2.3