diff options
Diffstat (limited to 'app/src/main/kotlin/org/pacien/tincapp/activities/ConfigureActivity.kt')
-rw-r--r-- | app/src/main/kotlin/org/pacien/tincapp/activities/ConfigureActivity.kt | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/app/src/main/kotlin/org/pacien/tincapp/activities/ConfigureActivity.kt b/app/src/main/kotlin/org/pacien/tincapp/activities/ConfigureActivity.kt new file mode 100644 index 0000000..2aab304 --- /dev/null +++ b/app/src/main/kotlin/org/pacien/tincapp/activities/ConfigureActivity.kt | |||
@@ -0,0 +1,145 @@ | |||
1 | /* | ||
2 | * Tinc App, an Android binding and user interface for the tinc mesh VPN daemon | ||
3 | * Copyright (C) 2017-2018 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * This program is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU General Public License as published by | ||
7 | * the Free Software Foundation, either version 3 of the License, or | ||
8 | * (at your option) any later version. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | package org.pacien.tincapp.activities | ||
20 | |||
21 | import android.content.Intent | ||
22 | import android.os.Bundle | ||
23 | import android.support.annotation.StringRes | ||
24 | import android.support.v7.app.AlertDialog | ||
25 | import android.view.View | ||
26 | import com.google.zxing.integration.android.IntentIntegrator | ||
27 | import com.google.zxing.integration.android.IntentResult | ||
28 | import java8.util.concurrent.CompletableFuture | ||
29 | import kotlinx.android.synthetic.main.base.* | ||
30 | import kotlinx.android.synthetic.main.dialog_encrypt_decrypt_keys.view.* | ||
31 | import kotlinx.android.synthetic.main.dialog_network_generate.view.* | ||
32 | import kotlinx.android.synthetic.main.dialog_network_join.view.* | ||
33 | import kotlinx.android.synthetic.main.page_configure.* | ||
34 | import org.pacien.tincapp.R | ||
35 | import org.pacien.tincapp.commands.Tinc | ||
36 | import org.pacien.tincapp.commands.TincApp | ||
37 | import org.pacien.tincapp.context.AppPaths | ||
38 | import org.pacien.tincapp.extensions.Java.exceptionallyAccept | ||
39 | import java.util.regex.Pattern | ||
40 | |||
41 | /** | ||
42 | * @author pacien | ||
43 | */ | ||
44 | class ConfigureActivity : BaseActivity() { | ||
45 | companion object { | ||
46 | private val NETWORK_NAME_PATTERN = Pattern.compile("^[^\\x00/]*$") | ||
47 | } | ||
48 | |||
49 | private var joinDialog: View? = null | ||
50 | |||
51 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
52 | super.onActivityResult(requestCode, resultCode, data) | ||
53 | |||
54 | IntentIntegrator.parseActivityResult(requestCode, resultCode, data) | ||
55 | ?.let(IntentResult::getContents) | ||
56 | ?.let(String::trim) | ||
57 | ?.let { joinDialog?.invitation_url?.setText(it) } | ||
58 | } | ||
59 | |||
60 | override fun onCreate(savedInstanceState: Bundle?) { | ||
61 | super.onCreate(savedInstanceState) | ||
62 | supportActionBar!!.setDisplayHomeAsUpEnabled(true) | ||
63 | layoutInflater.inflate(R.layout.page_configure, main_content) | ||
64 | writeContent() | ||
65 | } | ||
66 | |||
67 | fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { | ||
68 | IntentIntegrator(this).initiateScan() | ||
69 | } | ||
70 | |||
71 | fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
72 | val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) | ||
73 | AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) | ||
74 | .setPositiveButton(R.string.action_create) { _, _ -> | ||
75 | generateConf( | ||
76 | genDialog.new_net_name.text.toString(), | ||
77 | genDialog.new_node_name.text.toString(), | ||
78 | genDialog.new_passphrase.text.toString()) | ||
79 | }.setNegativeButton(R.string.action_cancel) { _, _ -> Unit }.show() | ||
80 | } | ||
81 | |||
82 | fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
83 | joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) | ||
84 | AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) | ||
85 | .setPositiveButton(R.string.action_join) { _, _ -> | ||
86 | joinNetwork( | ||
87 | joinDialog!!.net_name.text.toString(), | ||
88 | joinDialog!!.invitation_url.text.toString(), | ||
89 | joinDialog!!.join_passphrase.text.toString()) | ||
90 | }.setNegativeButton(R.string.action_cancel) { _, _ -> Unit }.show() | ||
91 | } | ||
92 | |||
93 | fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { | ||
94 | val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) | ||
95 | AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) | ||
96 | .setPositiveButton(R.string.action_apply) { _, _ -> | ||
97 | encryptDecryptPrivateKeys( | ||
98 | encryptDecryptDialog!!.enc_dec_net_name.text.toString(), | ||
99 | encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), | ||
100 | encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) | ||
101 | }.setNegativeButton(R.string.action_cancel) { _, _ -> Unit }.show() | ||
102 | } | ||
103 | |||
104 | private fun writeContent() { | ||
105 | text_configuration_directory.text = AppPaths.confDir().absolutePath | ||
106 | text_log_directory.text = AppPaths.cacheDir().absolutePath | ||
107 | text_tinc_binary.text = AppPaths.tinc().absolutePath | ||
108 | } | ||
109 | |||
110 | private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( | ||
111 | R.string.message_generating_configuration, | ||
112 | validateNetName(netName) | ||
113 | .thenCompose { Tinc.init(netName, nodeName) } | ||
114 | .thenCompose { TincApp.removeScripts(netName) } | ||
115 | .thenCompose { TincApp.generateIfaceCfgTemplate(netName) } | ||
116 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | ||
117 | |||
118 | private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( | ||
119 | R.string.message_joining_network, | ||
120 | validateNetName(netName) | ||
121 | .thenCompose { Tinc.join(netName, url) } | ||
122 | .thenCompose { TincApp.removeScripts(netName) } | ||
123 | .thenCompose { TincApp.generateIfaceCfg(netName) } | ||
124 | .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) | ||
125 | |||
126 | private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( | ||
127 | R.string.message_encrypting_decrypting_private_keys, | ||
128 | validateNetName(netName) | ||
129 | .thenCompose { TincApp.setPassphrase(netName, currentPassphrase, newPassphrase) }) | ||
130 | |||
131 | private fun execAction(@StringRes label: Int, action: CompletableFuture<Unit>) { | ||
132 | showProgressDialog(label).let { progressDialog -> | ||
133 | action | ||
134 | .whenComplete { _, _ -> progressDialog.dismiss() } | ||
135 | .thenAccept { notify(R.string.message_network_configuration_written) } | ||
136 | .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } | ||
137 | } | ||
138 | } | ||
139 | |||
140 | private fun validateNetName(netName: String): CompletableFuture<Unit> = | ||
141 | if (NETWORK_NAME_PATTERN.matcher(netName).matches()) | ||
142 | CompletableFuture.completedFuture(Unit) | ||
143 | else | ||
144 | CompletableFuture.failedFuture(IllegalArgumentException(resources.getString(R.string.message_invalid_network_name))) | ||
145 | } | ||