From 87ec3620d2259064831356c2f4000ae591756fd2 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 10 Feb 2018 14:16:15 +0100 Subject: Reformat code --- app/build.gradle | 94 ++++----- app/src/main/AndroidManifest.xml | 4 +- app/src/main/c/exec.c | 36 ++-- .../org/pacien/tincapp/activities/BaseActivity.kt | 52 ++--- .../pacien/tincapp/activities/ConfigureActivity.kt | 190 +++++++++--------- .../pacien/tincapp/activities/LaunchActivity.kt | 118 ++++++------ .../org/pacien/tincapp/activities/StartActivity.kt | 100 +++++----- .../pacien/tincapp/activities/StatusActivity.kt | 212 ++++++++++----------- .../java/org/pacien/tincapp/commands/Command.kt | 30 +-- .../java/org/pacien/tincapp/commands/Executor.kt | 48 ++--- .../main/java/org/pacien/tincapp/commands/Tinc.kt | 70 +++---- .../java/org/pacien/tincapp/commands/TincApp.kt | 62 +++--- .../main/java/org/pacien/tincapp/commands/Tincd.kt | 22 +-- .../main/java/org/pacien/tincapp/context/App.kt | 36 ++-- .../java/org/pacien/tincapp/context/AppInfo.kt | 30 +-- .../java/org/pacien/tincapp/context/AppPaths.kt | 54 +++--- .../java/org/pacien/tincapp/data/CidrAddress.kt | 18 +- .../org/pacien/tincapp/data/TincConfiguration.kt | 16 +- .../tincapp/data/VpnInterfaceConfiguration.kt | 82 ++++---- .../java/org/pacien/tincapp/extensions/Android.kt | 20 +- .../tincapp/extensions/ApacheConfiguration.kt | 8 +- .../java/org/pacien/tincapp/extensions/Java.kt | 12 +- .../pacien/tincapp/extensions/VpnServiceBuilder.kt | 74 +++---- .../org/pacien/tincapp/service/TincVpnService.kt | 210 ++++++++++---------- .../main/java/org/pacien/tincapp/utils/PemUtils.kt | 82 ++++---- .../main/res/drawable/ic_build_primary_24dp.xml | 14 +- app/src/main/res/drawable/ic_help_primary_24dp.xml | 14 +- .../res/drawable/ic_photo_camera_primary_24dp.xml | 8 +- app/src/main/res/drawable/ic_stop_primary_24dp.xml | 8 +- app/src/main/res/icon.svg | 56 +++++- app/src/main/res/layout/base.xml | 14 +- app/src/main/res/layout/dialog_decrypt_keys.xml | 14 +- .../res/layout/dialog_encrypt_decrypt_keys.xml | 14 +- .../main/res/layout/dialog_network_generate.xml | 14 +- app/src/main/res/layout/dialog_network_join.xml | 14 +- app/src/main/res/layout/dialog_node_details.xml | 12 +- app/src/main/res/menu/menu_base.xml | 6 +- app/src/main/res/menu/menu_start.xml | 6 +- app/src/main/res/menu/menu_status.xml | 6 +- app/src/main/res/values-v21/styles.xml | 1 + build.gradle | 28 +-- gradle.properties | 4 - 42 files changed, 982 insertions(+), 931 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1e96fdc..4b11a3e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -3,66 +3,66 @@ apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { - compileSdkVersion 25 - buildToolsVersion '26.0.2' + compileSdkVersion 25 + buildToolsVersion '26.0.2' - defaultConfig { - applicationId "org.pacien.tincapp" - minSdkVersion 21 - targetSdkVersion 21 - multiDexEnabled true - versionCode 7 - versionName "0.7" - ndk { - abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' - } - } + defaultConfig { + applicationId "org.pacien.tincapp" + minSdkVersion 21 + targetSdkVersion 21 + multiDexEnabled true + versionCode 7 + versionName "0.7" + ndk { + abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' + } + } - buildTypes { - release { - minifyEnabled true - proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' - } - } + buildTypes { + release { + minifyEnabled true + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } - externalNativeBuild { - cmake { - path "CMakeLists.txt" - } - } + externalNativeBuild { + cmake { + path "CMakeLists.txt" + } + } - lintOptions { - disable 'MissingTranslation' - } + lintOptions { + disable 'MissingTranslation' + } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) + compile fileTree(dir: 'libs', include: ['*.jar']) - compile 'com.android.support:appcompat-v7:25.4.0' - compile 'com.android.support:design:25.4.0' - compile 'com.android.support:support-v4:25.4.0' - compile 'com.android.support:recyclerview-v7:25.4.0' - compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'com.android.support:appcompat-v7:25.4.0' + compile 'com.android.support:design:25.4.0' + compile 'com.android.support:support-v4:25.4.0' + compile 'com.android.support:recyclerview-v7:25.4.0' + compile 'com.android.support.constraint:constraint-layout:1.0.2' - compile('org.apache.commons:commons-configuration2:2.1.1') { - exclude group: 'commons-logging', module: 'commons-logging' - } - compile('commons-beanutils:commons-beanutils:1.9.3') { - exclude group: 'commons-logging', module: 'commons-logging' - } + compile('org.apache.commons:commons-configuration2:2.1.1') { + exclude group: 'commons-logging', module: 'commons-logging' + } + compile('commons-beanutils:commons-beanutils:1.9.3') { + exclude group: 'commons-logging', module: 'commons-logging' + } - compile 'org.bouncycastle:bcpkix-jdk15on:1.57' + compile 'org.bouncycastle:bcpkix-jdk15on:1.57' - compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.5.5' + compile 'net.sourceforge.streamsupport:streamsupport-cfuture:1.5.5' - compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" + compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } repositories { - mavenCentral() - jcenter() - maven { - url "https://maven.google.com" - } + mavenCentral() + jcenter() + maven { + url "https://maven.google.com" + } } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3ccc9e6..f03a640 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ + xmlns:tools="http://schemas.android.com/tools" + package="org.pacien.tincapp"> diff --git a/app/src/main/c/exec.c b/app/src/main/c/exec.c index fdaec0f..a9871a5 100644 --- a/app/src/main/c/exec.c +++ b/app/src/main/c/exec.c @@ -3,32 +3,32 @@ #include static inline const char **to_string_array(JNIEnv *env, jobjectArray ja) { - const int len = (*env)->GetArrayLength(env, ja); - const char **ca = calloc((size_t) len + 1, sizeof(char *)); + const int len = (*env)->GetArrayLength(env, ja); + const char **ca = calloc((size_t) len + 1, sizeof(char *)); - for (int i = 0; i < len; ++i) { - jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, ja, i); - ca[i] = (*env)->GetStringUTFChars(env, jstr, NULL); - } + for (int i = 0; i < len; ++i) { + jstring jstr = (jstring) (*env)->GetObjectArrayElement(env, ja, i); + ca[i] = (*env)->GetStringUTFChars(env, jstr, NULL); + } - ca[len] = NULL; - return ca; + ca[len] = NULL; + return ca; } static inline void exec(const char **argcv) { - execv(argcv[0], (char *const *) argcv); - exit(1); + execv(argcv[0], (char *const *) argcv); + exit(1); } JNIEXPORT jint JNICALL Java_org_pacien_tincapp_commands_Executor_forkExec(JNIEnv *env, jclass class, jobjectArray argcv) { - pid_t pid = fork(); - switch (pid) { - case 0: - exec(to_string_array(env, argcv)); - return 0; + pid_t pid = fork(); + switch (pid) { + case 0: + exec(to_string_array(env, argcv)); + return 0; - default: - return pid; - } + default: + return pid; + } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt index 4904a66..c092111 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseActivity.kt @@ -19,34 +19,34 @@ import org.pacien.tincapp.context.AppInfo */ abstract class BaseActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.base) - setSupportActionBar(toolbar) - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.base) + setSupportActionBar(toolbar) + } - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_base, m) - return true - } + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_base, m) + return true + } - fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) { - AlertDialog.Builder(this) - .setTitle(BuildConfig.APPLICATION_ID) - .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" + - resources.getString(R.string.app_copyright) + " " + - resources.getString(R.string.app_license) + "\n\n" + - AppInfo.all()) - .setNeutralButton(R.string.action_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.app_website_url)) } - .setPositiveButton(R.string.action_close, App.dismissAction) - .show() - } + fun aboutDialog(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + AlertDialog.Builder(this) + .setTitle(BuildConfig.APPLICATION_ID) + .setMessage(resources.getString(R.string.app_short_desc) + "\n\n" + + resources.getString(R.string.app_copyright) + " " + + resources.getString(R.string.app_license) + "\n\n" + + AppInfo.all()) + .setNeutralButton(R.string.action_open_project_website) { _, _ -> App.openURL(resources.getString(R.string.app_website_url)) } + .setPositiveButton(R.string.action_close, App.dismissAction) + .show() + } - protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() - protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() - protected fun showProgressDialog(@StringRes msg: Int): ProgressDialog = ProgressDialog.show(this, null, getString(msg), true, false) - protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this) - .setTitle(R.string.title_error).setMessage(msg) - .setPositiveButton(R.string.action_close, App.dismissAction).show() + protected fun notify(@StringRes msg: Int) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() + protected fun notify(msg: String) = Snackbar.make(activity_base, msg, Snackbar.LENGTH_LONG).show() + protected fun showProgressDialog(@StringRes msg: Int): ProgressDialog = ProgressDialog.show(this, null, getString(msg), true, false) + protected fun showErrorDialog(msg: String): AlertDialog = AlertDialog.Builder(this) + .setTitle(R.string.title_error).setMessage(msg) + .setPositiveButton(R.string.action_close, App.dismissAction).show() } 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 6c29a53..ec17c11 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/ConfigureActivity.kt @@ -26,102 +26,102 @@ import org.pacien.tincapp.extensions.Java.exceptionallyAccept */ class ConfigureActivity : BaseActivity() { - companion object { - val REQUEST_SCAN = 0 - val SCAN_PROVIDER = "com.google.zxing.client.android" + companion object { + val REQUEST_SCAN = 0 + val SCAN_PROVIDER = "com.google.zxing.client.android" + } + + private var joinDialog: View? = null + + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) + joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) + } + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + supportActionBar!!.setDisplayHomeAsUpEnabled(true) + layoutInflater.inflate(R.layout.page_configure, main_content) + writeContent() + } + + fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { + try { + startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) + } catch (e: ActivityNotFoundException) { + AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) + .setMessage(R.string.message_no_qr_code_scanner) + .setPositiveButton(R.string.action_install) { _, _ -> + startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() } - - private var joinDialog: View? = null - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - super.onActivityResult(requestCode, resultCode, data) - - if (requestCode == REQUEST_SCAN && resultCode == Activity.RESULT_OK) - joinDialog?.invitation_url?.setText(data!!.getStringExtra("SCAN_RESULT").trim()) - } - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - supportActionBar!!.setDisplayHomeAsUpEnabled(true) - layoutInflater.inflate(R.layout.page_configure, main_content) - writeContent() - } - - fun scanCode(@Suppress("UNUSED_PARAMETER") v: View) { - try { - startActivityForResult(Intent("$SCAN_PROVIDER.SCAN"), REQUEST_SCAN) - } catch (e: ActivityNotFoundException) { - AlertDialog.Builder(this).setTitle(R.string.action_scan_qr_code) - .setMessage(R.string.message_no_qr_code_scanner) - .setPositiveButton(R.string.action_install) { _, _ -> - startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$SCAN_PROVIDER"))) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - } - - fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) - .setPositiveButton(R.string.action_create) { _, _ -> - generateConf( - genDialog.new_net_name.text.toString(), - genDialog.new_node_name.text.toString(), - genDialog.new_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { - joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) - .setPositiveButton(R.string.action_join) { _, _ -> - joinNetwork( - joinDialog!!.net_name.text.toString(), - joinDialog!!.invitation_url.text.toString(), - joinDialog!!.join_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { - val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) - AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) - .setPositiveButton(R.string.action_apply) { _, _ -> - encryptDecryptPrivateKeys( - encryptDecryptDialog!!.enc_dec_net_name.text.toString(), - encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), - encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) - }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() - } - - private fun writeContent() { - text_configuration_directory.text = AppPaths.confDir().absolutePath - text_log_directory.text = AppPaths.cacheDir().absolutePath - text_tinc_binary.text = AppPaths.tinc().absolutePath - } - - private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( - R.string.message_generating_configuration, - Tinc.init(netName, nodeName) - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) - - private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( - R.string.message_joining_network, - Tinc.join(netName, url) - .thenCompose { TincApp.removeScripts(netName) } - .thenCompose { TincApp.generateIfaceCfg(netName) } - .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) - - private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( - R.string.message_encrypting_decrypting_private_keys, - TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) - - private fun execAction(@StringRes label: Int, action: CompletableFuture) { - showProgressDialog(label).let { progressDialog -> - action - .whenComplete { _, _ -> progressDialog.dismiss() } - .thenAccept { notify(R.string.message_network_configuration_written) } - .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } - } + } + + fun openGenerateConfDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val genDialog = layoutInflater.inflate(R.layout.dialog_network_generate, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_new_network).setView(genDialog) + .setPositiveButton(R.string.action_create) { _, _ -> + generateConf( + genDialog.new_net_name.text.toString(), + genDialog.new_node_name.text.toString(), + genDialog.new_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + fun openJoinNetworkDialog(@Suppress("UNUSED_PARAMETER") v: View) { + joinDialog = layoutInflater.inflate(R.layout.dialog_network_join, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_join_network).setView(joinDialog) + .setPositiveButton(R.string.action_join) { _, _ -> + joinNetwork( + joinDialog!!.net_name.text.toString(), + joinDialog!!.invitation_url.text.toString(), + joinDialog!!.join_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + fun openEncryptDecryptPrivateKeyDialog(@Suppress("UNUSED_PARAMETER") v: View) { + val encryptDecryptDialog = layoutInflater.inflate(R.layout.dialog_encrypt_decrypt_keys, main_content, false) + AlertDialog.Builder(this).setTitle(R.string.title_private_keys_encryption).setView(encryptDecryptDialog) + .setPositiveButton(R.string.action_apply) { _, _ -> + encryptDecryptPrivateKeys( + encryptDecryptDialog!!.enc_dec_net_name.text.toString(), + encryptDecryptDialog.enc_dec_current_passphrase.text.toString(), + encryptDecryptDialog.enc_dec_new_passphrase.text.toString()) + }.setNegativeButton(R.string.action_cancel, App.dismissAction).show() + } + + private fun writeContent() { + text_configuration_directory.text = AppPaths.confDir().absolutePath + text_log_directory.text = AppPaths.cacheDir().absolutePath + text_tinc_binary.text = AppPaths.tinc().absolutePath + } + + private fun generateConf(netName: String, nodeName: String, passphrase: String? = null) = execAction( + R.string.message_generating_configuration, + Tinc.init(netName, nodeName) + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) + + private fun joinNetwork(netName: String, url: String, passphrase: String? = null) = execAction( + R.string.message_joining_network, + Tinc.join(netName, url) + .thenCompose { TincApp.removeScripts(netName) } + .thenCompose { TincApp.generateIfaceCfg(netName) } + .thenCompose { TincApp.setPassphrase(netName, newPassphrase = passphrase) }) + + private fun encryptDecryptPrivateKeys(netName: String, currentPassphrase: String, newPassphrase: String) = execAction( + R.string.message_encrypting_decrypting_private_keys, + TincApp.setPassphrase(netName, currentPassphrase, newPassphrase)) + + private fun execAction(@StringRes label: Int, action: CompletableFuture) { + showProgressDialog(label).let { progressDialog -> + action + .whenComplete { _, _ -> progressDialog.dismiss() } + .thenAccept { notify(R.string.message_network_configuration_written) } + .exceptionallyAccept { runOnUiThread { showErrorDialog(it.cause!!.localizedMessage) } } } + } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt index 74a059b..0179040 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/LaunchActivity.kt @@ -24,75 +24,75 @@ import java.io.FileNotFoundException */ class LaunchActivity : AppCompatActivity() { - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - when (intent.action) { - ACTION_CONNECT -> requestPerm() - ACTION_DISCONNECT -> disconnect() - } + when (intent.action) { + ACTION_CONNECT -> requestPerm() + ACTION_DISCONNECT -> disconnect() } - - override fun onActivityResult(request: Int, result: Int, data: Intent?) { - if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() + } + + override fun onActivityResult(request: Int, result: Int, data: Intent?) { + if (request == PERMISSION_REQUEST_CODE && result == Activity.RESULT_OK) askPassphrase() + } + + private fun requestPerm() = VpnService.prepare(this).let { + if (it != null) + startActivityForResult(it, PERMISSION_REQUEST_CODE) + else + onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) + } + + @SuppressLint("InflateParams") + private fun askPassphrase() { + val netName = intent.data.schemeSpecificPart + + if (needPassphrase(netName) && intent.data.fragment == null) { + val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) + AlertDialog.Builder(this) + .setTitle(R.string.title_unlock_private_keys).setView(dialog) + .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } + .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) + .show() + } else { + connect(netName, intent.data.fragment) } + } - private fun requestPerm() = VpnService.prepare(this).let { - if (it != null) - startActivityForResult(it, PERMISSION_REQUEST_CODE) - else - onActivityResult(PERMISSION_REQUEST_CODE, Activity.RESULT_OK, null) - } + private fun needPassphrase(netName: String) = try { + TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } + } catch (e: FileNotFoundException) { + false + } - @SuppressLint("InflateParams") - private fun askPassphrase() { - val netName = intent.data.schemeSpecificPart - - if (needPassphrase(netName) && intent.data.fragment == null) { - val dialog = layoutInflater.inflate(R.layout.dialog_decrypt_keys, null, false) - AlertDialog.Builder(this) - .setTitle(R.string.title_unlock_private_keys).setView(dialog) - .setPositiveButton(R.string.action_unlock) { _, _ -> connect(netName, dialog.passphrase.text.toString()) } - .setNegativeButton(R.string.action_cancel, { _, _ -> finish() }) - .show() - } else { - connect(netName, intent.data.fragment) - } - } + private fun connect(netName: String, passphrase: String? = null) { + TincVpnService.startVpn(netName, passphrase) + finish() + } - private fun needPassphrase(netName: String) = try { - TincApp.listPrivateKeys(netName).filter { it.exists() }.any { PemUtils.isEncrypted(PemUtils.read(it)) } - } catch (e: FileNotFoundException) { - false - } + private fun disconnect() { + TincVpnService.stopVpn() + finish() + } - private fun connect(netName: String, passphrase: String? = null) { - TincVpnService.startVpn(netName, passphrase) - finish() - } + companion object { - private fun disconnect() { - TincVpnService.stopVpn() - finish() - } + private val PERMISSION_REQUEST_CODE = 0 - companion object { - - private val PERMISSION_REQUEST_CODE = 0 - - fun connect(netName: String, passphrase: String? = null) { - App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setAction(ACTION_CONNECT) - .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) - } - - fun disconnect() { - App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - .setAction(ACTION_DISCONNECT)) - } + fun connect(netName: String, passphrase: String? = null) { + App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setAction(ACTION_CONNECT) + .setData(Uri.Builder().scheme(TINC_SCHEME).opaquePart(netName).fragment(passphrase).build())) + } + fun disconnect() { + App.getContext().startActivity(Intent(App.getContext(), LaunchActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .setAction(ACTION_DISCONNECT)) } + } + } 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 44d6e95..719bbc1 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StartActivity.kt @@ -22,67 +22,67 @@ import org.pacien.tincapp.service.TincVpnService */ class StartActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { - private var networkListAdapter: ArrayAdapter? = null + private var networkListAdapter: ArrayAdapter? = null - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) - layoutInflater.inflate(R.layout.fragment_list_view, main_content) - list_wrapper.setOnRefreshListener(this) - list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) - list.addFooterView(View(this), null, false) - list.adapter = networkListAdapter - list.onItemClickListener = this - } + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + networkListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) + layoutInflater.inflate(R.layout.fragment_list_view, main_content) + list_wrapper.setOnRefreshListener(this) + list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_list_header, list, false), null, false) + list.addFooterView(View(this), null, false) + list.adapter = networkListAdapter + list.onItemClickListener = this + } - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_start, m) - return super.onCreateOptionsMenu(m) - } + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_start, m) + return super.onCreateOptionsMenu(m) + } - override fun onDestroy() { - networkListAdapter = null - super.onDestroy() - } + override fun onDestroy() { + networkListAdapter = null + super.onDestroy() + } - override fun onStart() { - super.onStart() - onRefresh() - } + override fun onStart() { + super.onStart() + onRefresh() + } - override fun onResume() { - super.onResume() - if (TincVpnService.isConnected()) openStatusActivity() - } + override fun onResume() { + super.onResume() + if (TincVpnService.isConnected()) openStatusActivity() + } - override fun onRefresh() { - val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList() - runOnUiThread { - networkListAdapter?.setElements(networks) - setPlaceholderVisibility() - list_wrapper.isRefreshing = false - } + override fun onRefresh() { + val networks = AppPaths.confDir()?.list()?.toList() ?: emptyList() + runOnUiThread { + networkListAdapter?.setElements(networks) + setPlaceholderVisibility() + list_wrapper.isRefreshing = false } + } - override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = - LaunchActivity.connect((view as TextView).text.toString()) + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) = + LaunchActivity.connect((view as TextView).text.toString()) - fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = - startActivity(Intent(this, ConfigureActivity::class.java)) + fun openConfigureActivity(@Suppress("UNUSED_PARAMETER") i: MenuItem) = + startActivity(Intent(this, ConfigureActivity::class.java)) - fun openStatusActivity() = - startActivity(Intent(this, StatusActivity::class.java) - .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) + fun openStatusActivity() = + startActivity(Intent(this, StatusActivity::class.java) + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)) - private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { - network_list_placeholder.text = getListPlaceholderText() - network_list_placeholder.visibility = View.VISIBLE - } else { - network_list_placeholder.visibility = View.GONE - } + private fun setPlaceholderVisibility() = if (networkListAdapter?.isEmpty != false) { + network_list_placeholder.text = getListPlaceholderText() + network_list_placeholder.visibility = View.VISIBLE + } else { + network_list_placeholder.visibility = View.GONE + } - private fun getListPlaceholderText() = - if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable) - else getText(R.string.message_no_network_configuration_found) + private fun getListPlaceholderText() = + if (!AppPaths.storageAvailable()) getText(R.string.message_storage_unavailable) + else getText(R.string.message_no_network_configuration_found) } diff --git a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt index 356ff6e..4b5384c 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/StatusActivity.kt @@ -29,115 +29,115 @@ import kotlin.concurrent.timerTask */ class StatusActivity : BaseActivity(), AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener { - private var nodeListAdapter: ArrayAdapter? = null - private var refreshTimer: Timer? = null - private var updateView: Boolean = false - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - - nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) - refreshTimer = Timer(true) - - layoutInflater.inflate(R.layout.fragment_list_view, main_content) - list_wrapper.setOnRefreshListener(this) - list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_status_header, list, false), null, false) - list.addFooterView(View(this), null, false) - list.onItemClickListener = this - list.adapter = nodeListAdapter - } - - override fun onCreateOptionsMenu(m: Menu): Boolean { - menuInflater.inflate(R.menu.menu_status, m) - return super.onCreateOptionsMenu(m) - } - - override fun onDestroy() { - super.onDestroy() - refreshTimer?.cancel() - nodeListAdapter = null - refreshTimer = null - } - - override fun onStart() { - super.onStart() - writeNetworkInfo(TincVpnService.getCurrentInterfaceCfg() ?: VpnInterfaceConfiguration()) - updateView = true - onRefresh() - updateNodeList() - } - - override fun onStop() { - super.onStop() - updateView = false - } - - override fun onResume() { - super.onResume() + private var nodeListAdapter: ArrayAdapter? = null + private var refreshTimer: Timer? = null + private var updateView: Boolean = false + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + nodeListAdapter = ArrayAdapter(this, R.layout.fragment_list_item) + refreshTimer = Timer(true) + + layoutInflater.inflate(R.layout.fragment_list_view, main_content) + list_wrapper.setOnRefreshListener(this) + list.addHeaderView(layoutInflater.inflate(R.layout.fragment_network_status_header, list, false), null, false) + list.addFooterView(View(this), null, false) + list.onItemClickListener = this + list.adapter = nodeListAdapter + } + + override fun onCreateOptionsMenu(m: Menu): Boolean { + menuInflater.inflate(R.menu.menu_status, m) + return super.onCreateOptionsMenu(m) + } + + override fun onDestroy() { + super.onDestroy() + refreshTimer?.cancel() + nodeListAdapter = null + refreshTimer = null + } + + override fun onStart() { + super.onStart() + writeNetworkInfo(TincVpnService.getCurrentInterfaceCfg() ?: VpnInterfaceConfiguration()) + updateView = true + onRefresh() + updateNodeList() + } + + override fun onStop() { + super.onStop() + updateView = false + } + + override fun onResume() { + super.onResume() + if (!TincVpnService.isConnected()) openStartActivity() + } + + override fun onRefresh() { + getNodeNames().thenAccept { + runOnUiThread { + nodeListAdapter?.setElements(it) + node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty != false) View.VISIBLE else View.GONE + list_wrapper.isRefreshing = false if (!TincVpnService.isConnected()) openStartActivity() + } } - - override fun onRefresh() { - getNodeNames().thenAccept { - runOnUiThread { - nodeListAdapter?.setElements(it) - node_list_placeholder.visibility = if (nodeListAdapter?.isEmpty != false) View.VISIBLE else View.GONE - list_wrapper.isRefreshing = false - if (!TincVpnService.isConnected()) openStartActivity() - } - } - } - - override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { - val nodeName = (view as TextView).text.toString() - val dialogTextView = layoutInflater.inflate(R.layout.dialog_node_details, main_content, false) - Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName).thenAccept { - runOnUiThread { - dialogTextView.dialog_node_details.text = it - AlertDialog.Builder(this) - .setTitle(R.string.title_node_info) - .setView(dialogTextView) - .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } - .show() - } - } - } - - fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { - text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) - text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) - text_network_routes.setText(cfg.routes.map { it.toSlashSeparated() }) - text_network_dns_servers.setText(cfg.dnsServers) - text_network_search_domains.setText(cfg.searchDomains) - text_network_allow_bypass.text = getString(if (cfg.allowBypass) R.string.value_yes else R.string.value_no) - block_network_allowed_applications.visibility = if (cfg.allowedApplications.isNotEmpty()) View.VISIBLE else View.GONE - text_network_allowed_applications.setText(cfg.allowedApplications) - block_network_disallowed_applications.visibility = if (cfg.disallowedApplications.isNotEmpty()) View.VISIBLE else View.GONE - text_network_disallowed_applications.setText(cfg.disallowedApplications) - } - - fun updateNodeList() { - refreshTimer?.schedule(timerTask { - onRefresh() - if (updateView) updateNodeList() - }, REFRESH_RATE) + } + + override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { + val nodeName = (view as TextView).text.toString() + val dialogTextView = layoutInflater.inflate(R.layout.dialog_node_details, main_content, false) + Tinc.info(TincVpnService.getCurrentNetName()!!, nodeName).thenAccept { + runOnUiThread { + dialogTextView.dialog_node_details.text = it + AlertDialog.Builder(this) + .setTitle(R.string.title_node_info) + .setView(dialogTextView) + .setPositiveButton(R.string.action_close) { _, _ -> /* nop */ } + .show() + } } - - fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) { - TincVpnService.stopVpn() - openStartActivity() - finish() - } - - fun openStartActivity() = startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) - - companion object { - private val REFRESH_RATE = 5000L - - fun getNodeNames(): CompletableFuture> = when (TincVpnService.isConnected()) { - true -> Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).thenApply> { it.map { it.substringBefore(' ') } } - false -> CompletableFuture.supplyAsync> { emptyList() } - } + } + + fun writeNetworkInfo(cfg: VpnInterfaceConfiguration) { + text_network_name.text = TincVpnService.getCurrentNetName() ?: getString(R.string.value_none) + text_network_ip_addresses.setText(cfg.addresses.map { it.toSlashSeparated() }) + text_network_routes.setText(cfg.routes.map { it.toSlashSeparated() }) + text_network_dns_servers.setText(cfg.dnsServers) + text_network_search_domains.setText(cfg.searchDomains) + text_network_allow_bypass.text = getString(if (cfg.allowBypass) R.string.value_yes else R.string.value_no) + block_network_allowed_applications.visibility = if (cfg.allowedApplications.isNotEmpty()) View.VISIBLE else View.GONE + text_network_allowed_applications.setText(cfg.allowedApplications) + block_network_disallowed_applications.visibility = if (cfg.disallowedApplications.isNotEmpty()) View.VISIBLE else View.GONE + text_network_disallowed_applications.setText(cfg.disallowedApplications) + } + + fun updateNodeList() { + refreshTimer?.schedule(timerTask { + onRefresh() + if (updateView) updateNodeList() + }, REFRESH_RATE) + } + + fun stopVpn(@Suppress("UNUSED_PARAMETER") i: MenuItem) { + TincVpnService.stopVpn() + openStartActivity() + finish() + } + + fun openStartActivity() = startActivity(Intent(this, StartActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) + + companion object { + private val REFRESH_RATE = 5000L + + fun getNodeNames(): CompletableFuture> = when (TincVpnService.isConnected()) { + true -> Tinc.dumpNodes(TincVpnService.getCurrentNetName()!!).thenApply> { it.map { it.substringBefore(' ') } } + false -> CompletableFuture.supplyAsync> { emptyList() } } + } } diff --git a/app/src/main/java/org/pacien/tincapp/commands/Command.kt b/app/src/main/java/org/pacien/tincapp/commands/Command.kt index 6eab66b..cb95619 100644 --- a/app/src/main/java/org/pacien/tincapp/commands/Command.kt +++ b/app/src/main/java/org/pacien/tincapp/commands/Command.kt @@ -7,25 +7,25 @@ import java.util.* */ internal class Command(private val cmd: String) { - private data class Option(val key: String, val value: String?) { - fun toCommandLineOption(): String = if (value != null) "--$key=$value" else "--$key" - } + private data class Option(val key: String, val value: String?) { + fun toCommandLineOption(): String = if (value != null) "--$key=$value" else "--$key" + } - private val opts: MutableList