diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2020-03-16 12:45:47 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2020-03-19 09:24:47 +0530 |
commit | 94c864503ea3a53f34cc48071b2522fc7250c012 (patch) | |
tree | 978df58de54e19eb46d0360ecf4ef8cd0281852e /ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt | |
parent | strings: Update Japanese Translation (diff) | |
download | wireguard-android-94c864503ea3a53f34cc48071b2522fc7250c012.tar.xz wireguard-android-94c864503ea3a53f34cc48071b2522fc7250c012.zip |
ui: Convert configStore package to Kotlin
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt')
-rw-r--r-- | ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt new file mode 100644 index 00000000..c8d47b40 --- /dev/null +++ b/ui/src/main/java/com/wireguard/android/configStore/FileConfigStore.kt @@ -0,0 +1,79 @@ +/* + * Copyright © 2017-2019 WireGuard LLC. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ +package com.wireguard.android.configStore + +import android.content.Context +import android.util.Log +import com.wireguard.android.R +import com.wireguard.config.BadConfigException +import com.wireguard.config.Config +import java.io.File +import java.io.FileInputStream +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException +import java.nio.charset.StandardCharsets + +/** + * Configuration store that uses a `wg-quick`-style file for each configured tunnel. + */ +class FileConfigStore(private val context: Context) : ConfigStore { + @Throws(IOException::class) + override fun create(name: String, config: Config): Config { + Log.d(TAG, "Creating configuration for tunnel $name") + val file = fileFor(name) + if (!file.createNewFile()) throw IOException(context.getString(R.string.config_file_exists_error, file.name)) + FileOutputStream(file, false).use { stream -> stream.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) } + return config + } + + @Throws(IOException::class) + override fun delete(name: String) { + Log.d(TAG, "Deleting configuration for tunnel $name") + val file = fileFor(name) + if (!file.delete()) throw IOException(context.getString(R.string.config_delete_error, file.name)) + } + + override fun enumerate(): Set<String> { + return context.fileList() + .filter { it.endsWith(".conf") } + .map { it.substring(0, it.length - ".conf".length) } + .toSet() + } + + private fun fileFor(name: String): File { + return File(context.filesDir, "$name.conf") + } + + @Throws(BadConfigException::class, IOException::class) + override fun load(name: String): Config { + FileInputStream(fileFor(name)).use { stream -> return Config.parse(stream) } + } + + @Throws(IOException::class) + override fun rename(name: String, replacement: String) { + Log.d(TAG, "Renaming configuration for tunnel $name to $replacement") + val file = fileFor(name) + val replacementFile = fileFor(replacement) + if (!replacementFile.createNewFile()) throw IOException(context.getString(R.string.config_exists_error, replacement)) + if (!file.renameTo(replacementFile)) { + if (!replacementFile.delete()) Log.w(TAG, "Couldn't delete marker file for new name $replacement") + throw IOException(context.getString(R.string.config_rename_error, file.name)) + } + } + + @Throws(IOException::class) + override fun save(name: String, config: Config): Config { + Log.d(TAG, "Saving configuration for tunnel $name") + val file = fileFor(name) + if (!file.isFile) throw FileNotFoundException(context.getString(R.string.config_not_found_error, file.name)) + FileOutputStream(file, false).use { stream -> stream.write(config.toWgQuickString().toByteArray(StandardCharsets.UTF_8)) } + return config + } + + companion object { + private val TAG = "WireGuard/" + FileConfigStore::class.java.simpleName + } +} |