diff options
author | pacien | 2018-03-22 23:45:11 +0100 |
---|---|---|
committer | pacien | 2018-03-22 23:45:11 +0100 |
commit | 18b8c8fe1b370c47978e7ae0342139d556969f00 (patch) | |
tree | 43f08e340726e46e296ac56b464174528ac7c6fc /app/src/main | |
parent | 2e0937a0fb6db92553dcddecb66a83af656b22c1 (diff) | |
download | tincapp-18b8c8fe1b370c47978e7ae0342139d556969f00.tar.gz |
Validate network names
Signed-off-by: pacien <pacien.trangirard@pacien.net>
Diffstat (limited to 'app/src/main')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt | 21 | ||||
-rw-r--r-- | app/src/main/res/values/strings.xml | 1 |
2 files changed, 17 insertions, 5 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt index 64ed61d..e311415 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt | |||
@@ -19,14 +19,16 @@ import org.pacien.tincapp.commands.Tinc | |||
19 | import org.pacien.tincapp.commands.TincApp | 19 | import org.pacien.tincapp.commands.TincApp |
20 | import org.pacien.tincapp.context.AppPaths | 20 | import org.pacien.tincapp.context.AppPaths |
21 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept | 21 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept |
22 | import java.util.regex.Pattern | ||
22 | 23 | ||
23 | /** | 24 | /** |
24 | * @author pacien | 25 | * @author pacien |
25 | */ | 26 | */ |
26 | class ConfigureActivity : BaseActivity() { | 27 | class ConfigureActivity : BaseActivity() { |
27 | companion object { | 28 | companion object { |
28 | val REQUEST_SCAN = 0 | 29 | private const val REQUEST_SCAN = 0 |
29 | val SCAN_PROVIDER = "com.google.zxing.client.android" | 30 | private const val SCAN_PROVIDER = "com.google.zxing.client.android" |
31 | private val NETWORK_NAME_PATTERN = Pattern.compile("^[^\\x00/]*$") | ||
30 | } | 32 | } |
31 | 33 | ||
32 | private var joinDialog: View? = null | 34 | private var joinDialog: View? = null |
@@ -98,21 +100,24 @@ class ConfigureActivity : BaseActivity() { | |||
98 | 100 | ||
99 | private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( | 101 | private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( |
100 | R.string.message_generating_configuration, | 102 | R.string.message_generating_configuration, |
101 | Tinc.init(netName, nodeName) | 103 | validateNetName(netName) |
104 | .thenCompose { Tinc.init(netName, nodeName) } | ||
102 | .thenCompose { TincApp.removeScripts(netName) } | 105 | .thenCompose { TincApp.removeScripts(netName) } |
103 | .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } | 106 | .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } |
104 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | 107 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) |
105 | 108 | ||
106 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( | 109 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( |
107 | R.string.message_joining_network, | 110 | R.string.message_joining_network, |
108 | Tinc.join(netName, url) | 111 | validateNetName(netName) |
112 | .thenCompose { Tinc.join(netName, url) } | ||
109 | .thenCompose { TincApp.removeScripts(netName) } | 113 | .thenCompose { TincApp.removeScripts(netName) } |
110 | .thenCompose { TincApp.generateIfaceCfg(netName) } | 114 | .thenCompose { TincApp.generateIfaceCfg(netName) } |
111 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | 115 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) |
112 | 116 | ||
113 | private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( | 117 | private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( |
114 | R.string.message_encrypting_decrypting_private_keys, | 118 | R.string.message_encrypting_decrypting_private_keys, |
115 | TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) | 119 | validateNetName(netName) |
120 | .thenCompose { TincApp.setPassphrase(netName, currentPassphrase, newPassphrase) }) | ||
116 | 121 | ||
117 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) { | 122 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) { |
118 | showProgressDialog(label).let { progressDialog -> | 123 | showProgressDialog(label).let { progressDialog -> |
@@ -122,4 +127,10 @@ class ConfigureActivity : BaseActivity() { | |||
122 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } | 127 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } |
123 | } | 128 | } |
124 | } | 129 | } |
130 | |||
131 | private fun validateNetName(netName: String): CompletableFuture<Unit> = | ||
132 | if (NETWORK_NAME_PATTERN.matcher(netName).matches()) | ||
133 | CompletableFuture.completedFuture(Unit) | ||
134 | else | ||
135 | CompletableFuture.failedFuture(IllegalArgumentException(resources.getString(R.string.message_invalid_network_name))) | ||
125 | } | 136 | } |
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 25bee44..da3fa10 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml | |||
@@ -88,6 +88,7 @@ | |||
88 | <string name="message_log_paused">Logging paused.</string> | 88 | <string name="message_log_paused">Logging paused.</string> |
89 | <string name="message_could_not_configure_iface">Could not apply network interface configuration:\n\n%1$s</string> | 89 | <string name="message_could_not_configure_iface">Could not apply network interface configuration:\n\n%1$s</string> |
90 | <string name="message_could_not_bind_iface">Could not bind network interface. Is another VPN running?</string> | 90 | <string name="message_could_not_bind_iface">Could not bind network interface. Is another VPN running?</string> |
91 | <string name="message_invalid_network_name">Invalid network name.</string> | ||
91 | 92 | ||
92 | <string name="value_none">none</string> | 93 | <string name="value_none">none</string> |
93 | <string name="value_yes">yes</string> | 94 | <string name="value_yes">yes</string> |