diff options
5 files changed, 50 insertions, 54 deletions
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() { | |||
57 | } | 57 | } |
58 | 58 | ||
59 | fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { | 59 | fun confDirDialog(@Suppress("UNUSED_PARAMETER") v: View) { |
60 | val confDir = AppPaths.confDir().path | ||
61 | |||
62 | AlertDialog.Builder(this) | 60 | AlertDialog.Builder(this) |
63 | .setTitle(R.string.title_tinc_config_dir) | 61 | .setTitle(R.string.title_tinc_config_dir) |
64 | .setMessage(confDir) | 62 | .setMessage("Internal: " + AppPaths.confDir(AppPaths.Storage.INTERNAL) + "\n\n" + |
65 | .setNegativeButton(R.string.action_copy) { _, _ -> copyIntoClipboard(resources.getString(R.string.title_tinc_config_dir), confDir) } | 63 | "External: " + AppPaths.confDir(AppPaths.Storage.EXTERNAL)) |
66 | .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } | 64 | .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } |
67 | .show() | 65 | .show() |
68 | } | 66 | } |
69 | 67 | ||
70 | private fun startVpn(netName: String) { | 68 | private fun startVpn(netName: String) { |
71 | startService(Intent(this, TincVpnService::class.java) | 69 | startService(Intent(this, TincVpnService::class.java).putExtra(TincVpnService.INTENT_EXTRA_NET_CONF, |
72 | .putExtra(TincVpnService.INTENT_EXTRA_NET_NAME, netName)) | 70 | if (netName.startsWith("external/")) AppPaths.NetConf(AppPaths.Storage.EXTERNAL, netName.substringAfter("/")) |
71 | else AppPaths.NetConf(AppPaths.Storage.INTERNAL, netName))) | ||
73 | } | 72 | } |
74 | 73 | ||
75 | } | 74 | } |
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 | |||
8 | */ | 8 | */ |
9 | object Tinc { | 9 | object Tinc { |
10 | 10 | ||
11 | private fun newCommand(netName: String): Command = | 11 | private fun newCommand(netConf: AppPaths.NetConf): Command = |
12 | Command(AppPaths.tinc().absolutePath) | 12 | Command(AppPaths.tinc().absolutePath) |
13 | .withOption("config", AppPaths.confDir(netName).absolutePath) | 13 | .withOption("config", AppPaths.confDir(netConf).absolutePath) |
14 | .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) | 14 | .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath) |
15 | 15 | ||
16 | // independently runnable commands | 16 | // independently runnable commands |
17 | 17 | ||
18 | @Throws(IOException::class) | 18 | @Throws(IOException::class) |
19 | fun network(): List<String> = | 19 | fun fsck(netConf: AppPaths.NetConf, fix: Boolean): List<String> { |
20 | Executor.call(Command(AppPaths.tinc().absolutePath) | 20 | var cmd = newCommand(netConf).withArguments("fsck") |
21 | .withOption("config", AppPaths.confDir().absolutePath) | ||
22 | .withArguments("network")) | ||
23 | |||
24 | @Throws(IOException::class) | ||
25 | fun fsck(netName: String, fix: Boolean): List<String> { | ||
26 | var cmd = newCommand(netName).withArguments("fsck") | ||
27 | if (fix) cmd = cmd.withOption("force") | 21 | if (fix) cmd = cmd.withOption("force") |
28 | return Executor.call(cmd) | 22 | return Executor.call(cmd) |
29 | } | 23 | } |
@@ -31,18 +25,18 @@ object Tinc { | |||
31 | // commands requiring a running tinc daemon | 25 | // commands requiring a running tinc daemon |
32 | 26 | ||
33 | @Throws(IOException::class) | 27 | @Throws(IOException::class) |
34 | fun stop(netName: String) { | 28 | fun stop(netConf: AppPaths.NetConf) { |
35 | Executor.call(newCommand(netName).withArguments("stop")) | 29 | Executor.call(newCommand(netConf).withArguments("stop")) |
36 | } | 30 | } |
37 | 31 | ||
38 | @Throws(IOException::class) | 32 | @Throws(IOException::class) |
39 | fun dumpNodes(netName: String, reachable: Boolean): List<String> = | 33 | fun dumpNodes(netConf: AppPaths.NetConf, reachable: Boolean): List<String> = |
40 | Executor.call( | 34 | Executor.call( |
41 | if (reachable) newCommand(netName).withArguments("dump", "reachable", "nodes") | 35 | if (reachable) newCommand(netConf).withArguments("dump", "reachable", "nodes") |
42 | else newCommand(netName).withArguments("dump", "nodes")) | 36 | else newCommand(netConf).withArguments("dump", "nodes")) |
43 | 37 | ||
44 | @Throws(IOException::class) | 38 | @Throws(IOException::class) |
45 | fun info(netName: String, node: String): String = | 39 | fun info(netConf: AppPaths.NetConf, node: String): String = |
46 | Executor.call(newCommand(netName).withArguments("info", node)).joinToString("\n") | 40 | Executor.call(newCommand(netConf).withArguments("info", node)).joinToString("\n") |
47 | 41 | ||
48 | } | 42 | } |
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 | |||
9 | object Tincd { | 9 | object Tincd { |
10 | 10 | ||
11 | @Throws(IOException::class) | 11 | @Throws(IOException::class) |
12 | fun start(netName: String, fd: Int) { | 12 | fun start(netConf: AppPaths.NetConf, fd: Int) { |
13 | Executor.forkExec(Command(AppPaths.tincd().absolutePath) | 13 | Executor.forkExec(Command(AppPaths.tincd().absolutePath) |
14 | .withOption("no-detach") | 14 | .withOption("no-detach") |
15 | .withOption("config", AppPaths.confDir(netName).absolutePath) | 15 | .withOption("config", AppPaths.confDir(netConf).absolutePath) |
16 | .withOption("pidfile", AppPaths.pidFile(netName).absolutePath) | 16 | .withOption("pidfile", AppPaths.pidFile(netConf).absolutePath) |
17 | .withOption("logfile", AppPaths.logFile(netName).absolutePath) | 17 | .withOption("logfile", AppPaths.logFile(netConf).absolutePath) |
18 | .withOption("option", "DeviceType=fd") | 18 | .withOption("option", "DeviceType=fd") |
19 | .withOption("option", "Device=" + fd)) | 19 | .withOption("option", "Device=" + fd)) |
20 | } | 20 | } |
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 @@ | |||
1 | package org.pacien.tincapp.context | 1 | package org.pacien.tincapp.context |
2 | 2 | ||
3 | import android.content.Context | ||
4 | |||
5 | import java.io.File | 3 | import java.io.File |
4 | import java.io.Serializable | ||
6 | 5 | ||
7 | /** | 6 | /** |
8 | * @author pacien | 7 | * @author pacien |
@@ -11,9 +10,8 @@ import java.io.File | |||
11 | */ | 10 | */ |
12 | object AppPaths { | 11 | object AppPaths { |
13 | 12 | ||
14 | private val CONFDIR = "conf" | 13 | enum class Storage { INTERNAL, EXTERNAL } |
15 | private val LOGDIR = "log" | 14 | data class NetConf(val storage: Storage, val netName: String) : Serializable |
16 | private val PIDDIR = "pid" | ||
17 | 15 | ||
18 | private val TINCD_BIN = "libtincd.so" | 16 | private val TINCD_BIN = "libtincd.so" |
19 | private val TINC_BIN = "libtinc.so" | 17 | private val TINC_BIN = "libtinc.so" |
@@ -23,21 +21,26 @@ object AppPaths { | |||
23 | 21 | ||
24 | private val NET_CONF_FILE = "network.conf" | 22 | private val NET_CONF_FILE = "network.conf" |
25 | 23 | ||
26 | private fun createDirIfNotExists(basePath: File, newDir: String): File { | 24 | fun filesDir(storage: Storage): File = when (storage) { |
27 | val f = File(basePath, newDir) | 25 | Storage.INTERNAL -> App.getContext().filesDir |
28 | f.mkdirs() | 26 | Storage.EXTERNAL -> App.getContext().getExternalFilesDir(null) |
29 | return f | 27 | } |
28 | |||
29 | fun cacheDir(storage: Storage): File = when (storage) { | ||
30 | Storage.INTERNAL -> App.getContext().cacheDir | ||
31 | Storage.EXTERNAL -> App.getContext().externalCacheDir | ||
30 | } | 32 | } |
31 | 33 | ||
32 | fun confDir(): File = App.getContext().getDir(CONFDIR, Context.MODE_PRIVATE) | 34 | fun binDir() = File(App.getContext().applicationInfo.nativeLibraryDir) |
33 | fun confDir(netName: String): File = File(confDir(), netName) | 35 | |
34 | fun logDir(): File = createDirIfNotExists(App.getContext().cacheDir, LOGDIR) | 36 | fun confDir(storage: Storage) = filesDir(storage) |
35 | fun pidDir(): File = createDirIfNotExists(App.getContext().cacheDir, PIDDIR) | 37 | fun confDir(netConf: NetConf) = File(confDir(netConf.storage), netConf.netName) |
36 | fun logFile(netName: String): File = File(logDir(), String.format(LOGFILE_FORMAT, netName)) | 38 | |
37 | fun pidFile(netName: String): File = File(pidDir(), String.format(PIDFILE_FORMAT, netName)) | 39 | fun netConfFile(netConf: NetConf) = File(confDir(netConf), NET_CONF_FILE) |
38 | fun netConfFile(netName: String): File = File(confDir(netName), NET_CONF_FILE) | 40 | fun logFile(netConf: NetConf) = File(cacheDir(netConf.storage), String.format(LOGFILE_FORMAT, netConf.netName)) |
39 | fun binDir(): File = File(App.getContext().applicationInfo.nativeLibraryDir) | 41 | fun pidFile(netConf: NetConf) = File(cacheDir(Storage.INTERNAL), String.format(PIDFILE_FORMAT, netConf.netName)) |
40 | fun tincd(): File = File(binDir(), TINCD_BIN) | 42 | |
41 | fun tinc(): File = File(binDir(), TINC_BIN) | 43 | fun tincd() = File(binDir(), TINCD_BIN) |
44 | fun tinc() = File(binDir(), TINC_BIN) | ||
42 | 45 | ||
43 | } | 46 | } |
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 | |||
15 | */ | 15 | */ |
16 | class TincVpnService : VpnService() { | 16 | class TincVpnService : VpnService() { |
17 | 17 | ||
18 | private var netName: String = "" | 18 | private var netConf: AppPaths.NetConf? = null |
19 | 19 | ||
20 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { | 20 | override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int { |
21 | this.netName = intent.getStringExtra(INTENT_EXTRA_NET_NAME) | 21 | netConf = intent.getSerializableExtra(INTENT_EXTRA_NET_CONF)!! as AppPaths.NetConf |
22 | 22 | ||