Compare commits

..

18 Commits

Author SHA1 Message Date
Zane Schepke 4d64d058de chore: bump version with notes 2025-04-11 21:19:11 -04:00
Weblate (bot) 7e9687aeb9 feat(lang): Translations update from Hosted Weblate (#671)
Co-authored-by: Matthaiks <kitynska@gmail.com>
Co-authored-by: Languages add-on <noreply-addon-languages@weblate.org>
Co-authored-by: solokot <solokot@gmail.com>
Co-authored-by: Kachelkaiser <kachelkaiser@htpst.de>
Co-authored-by: ssantos <ssantos@web.de>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
2025-04-11 21:16:41 -04:00
Zane Schepke a6e559ecec fix: logger toggle bug
closes #669
2025-04-11 21:06:47 -04:00
Zane Schepke c6bacf8e15 style: latest logo 2025-04-11 20:03:32 -04:00
Zane Schepke fdfc348e76 fix: kill switch launch on start
closes #686
2025-04-11 19:07:36 -04:00
Zane Schepke 77b83ea569 fix: AndroidTV language selection ui bug
fix: AndroidTV restart on boot

closes #673
closes #606
2025-04-11 18:42:11 -04:00
Zane Schepke 5ded556647 fix: ping job start by default
fix: kernel dns resolution on stop bug

closes #674
2025-04-11 17:54:40 -04:00
Zane Schepke b62e592ee9 chore: bump deps 2025-04-11 04:51:06 -04:00
dependabot[bot] 869e1ebf0d chore(deps): bump com.google.devtools.ksp from 2.1.20-1.0.32 to 2.1.20-2.0.0 (#677)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-11 04:50:04 -04:00
dependabot[bot] 352eae0b28 chore(deps): bump actions/checkout from 3 to 4 (#675)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-11 04:49:08 -04:00
dependabot[bot] 7cb91ecd94 chore(deps): bump androidx.compose.material3:material3 from 1.3.1 to 1.3.2 (#682)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-11 04:47:54 -04:00
dependabot[bot] 3291bb0718 chore(deps): bump roomVersion from 2.6.1 to 2.7.0 (#681)
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2025-04-11 04:47:09 -04:00
Zane Schepke ce3f0b85c1 fix: toggle resolution issue
#669
2025-04-11 04:29:03 -04:00
Zane Schepke f9768fc9f0 chore: update license 2025-04-11 02:36:17 -04:00
Zane Schepke 64db37648a style: new app icon 2025-04-11 01:29:21 -04:00
Zane Schepke cc5a2a972b feat(lang): weblate changes (#670)
Co-authored-by: solokot <solokot@gmail.com>
Co-authored-by: lateweb <weblate@techkoala.net>
Co-authored-by: Matthaiks <kitynska@gmail.com>
Co-authored-by: Kachelkaiser <kachelkaiser@outlook.com>
Co-authored-by: CyanWolf <hydemr@pm.me>
Co-authored-by: Henrik Sozzi <henrik_sozzi@hotmail.com>
Co-authored-by: 大王叫我来巡山 <hamburger2048@users.noreply.hosted.weblate.org>
Co-authored-by: x86_64-pc-linux-gnu <x86_64-pc-linux-gnu@proton.me>
Co-authored-by: mak7im01 <mak7im02@gmail.com>
Co-authored-by: heykanspor <meingithub@heykan.de>
Co-authored-by: Jan-Pascal van Best <janpascal@vanbest.org>
Co-authored-by: Faisal Gull <mail.faisalrehman.345@gmail.com>
Co-authored-by: தமிழ்நேரம் <anishprabu.t@gmail.com>
Co-authored-by: catelixor <catelixor+weblate@proton.me>
Co-authored-by: Deleted User <noreply+48943@weblate.org>
Co-authored-by: kometchtech <kometch@gmail.com>
2025-04-09 03:41:58 -04:00
Zane Schepke 6294c7372a ci: fix matrix 2025-04-08 22:19:00 -04:00
Zane Schepke d562f36652 ci: fix notification workflow on release 2025-04-08 22:02:10 -04:00
728 changed files with 11791 additions and 12691 deletions
-97
View File
@@ -1,97 +0,0 @@
root = true
[*]
charset = utf-8
indent_size = 4
indent_style = tab
max_line_length = 150
trim_trailing_whitespace = true
insert_final_newline = true
[{*.kt,*.kts}]
ij_continuation_indent_size = 4
ij_java_names_count_to_use_import_on_demand = 9999
ij_kotlin_align_in_columns_case_branch = false
ij_kotlin_align_multiline_binary_operation = false
ij_kotlin_align_multiline_extends_list = false
ij_kotlin_align_multiline_method_parentheses = false
ij_kotlin_align_multiline_parameters = true
ij_kotlin_align_multiline_parameters_in_calls = false
ij_kotlin_assignment_wrap = normal
ij_kotlin_blank_lines_after_class_header = 0
ij_kotlin_blank_lines_around_block_when_branches = 0
ij_kotlin_blank_lines_before_declaration_with_comment_or_annotation_on_separate_line = 1
ij_kotlin_block_comment_at_first_column = true
ij_kotlin_call_parameters_new_line_after_left_paren = true
ij_kotlin_call_parameters_right_paren_on_new_line = false
ij_kotlin_catch_on_new_line = false
ij_kotlin_continuation_indent_for_chained_calls = true
ij_kotlin_continuation_indent_for_expression_bodies = true
ij_kotlin_continuation_indent_in_argument_lists = true
ij_kotlin_continuation_indent_in_elvis = false
ij_kotlin_continuation_indent_in_if_conditions = false
ij_kotlin_continuation_indent_in_parameter_lists = false
ij_kotlin_continuation_indent_in_supertype_lists = false
ij_kotlin_else_on_new_line = false
ij_kotlin_enum_constants_wrap = off
ij_kotlin_extends_list_wrap = normal
ij_kotlin_field_annotation_wrap = split_into_lines
ij_kotlin_finally_on_new_line = false
ij_kotlin_if_rparen_on_new_line = false
ij_kotlin_import_nested_classes = false
ij_kotlin_insert_whitespaces_in_simple_one_line_method = true
ij_kotlin_keep_blank_lines_before_right_brace = 2
ij_kotlin_keep_blank_lines_in_code = 2
ij_kotlin_keep_blank_lines_in_declarations = 2
ij_kotlin_keep_first_column_comment = true
ij_kotlin_keep_indents_on_empty_lines = false
ij_kotlin_keep_line_breaks = true
ij_kotlin_lbrace_on_next_line = false
ij_kotlin_line_comment_add_space = false
ij_kotlin_line_comment_at_first_column = true
ij_kotlin_method_annotation_wrap = split_into_lines
ij_kotlin_method_call_chain_wrap = normal
ij_kotlin_method_parameters_new_line_after_left_paren = true
ij_kotlin_method_parameters_right_paren_on_new_line = true
ij_kotlin_name_count_to_use_star_import = 9999
ij_kotlin_name_count_to_use_star_import_for_members = 9999
ij_kotlin_parameter_annotation_wrap = off
ij_kotlin_space_after_comma = true
ij_kotlin_space_after_extend_colon = true
ij_kotlin_space_after_type_colon = true
ij_kotlin_space_before_catch_parentheses = true
ij_kotlin_space_before_comma = false
ij_kotlin_space_before_extend_colon = true
ij_kotlin_space_before_for_parentheses = true
ij_kotlin_space_before_if_parentheses = true
ij_kotlin_space_before_lambda_arrow = true
ij_kotlin_space_before_type_colon = false
ij_kotlin_space_before_when_parentheses = true
ij_kotlin_space_before_while_parentheses = true
ij_kotlin_spaces_around_additive_operators = true
ij_kotlin_spaces_around_assignment_operators = true
ij_kotlin_spaces_around_equality_operators = true
ij_kotlin_spaces_around_function_type_arrow = true
ij_kotlin_spaces_around_logical_operators = true
ij_kotlin_spaces_around_multiplicative_operators = true
ij_kotlin_spaces_around_range = false
ij_kotlin_spaces_around_relational_operators = true
ij_kotlin_spaces_around_unary_operator = false
ij_kotlin_spaces_around_when_arrow = true
ij_kotlin_variable_annotation_wrap = off
ij_kotlin_while_on_new_line = false
ij_kotlin_wrap_elvis_expressions = 1
ij_kotlin_wrap_expression_body_functions = 1
ij_kotlin_wrap_first_method_in_call_chain = false
#compose
ktlint_standard_filename = disabled
ktlint_standard_no-wildcard-imports = disabled
ktlint_standard_function-naming = disabled
ktlint_standard_property-naming = disabled
ktlint_standard_package-naming = disabled
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_code_style = android_studio
ktlint_standard_import-ordering = disabled
ktlint_standard_package-naming = disabled
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
+15 -13
View File
@@ -19,16 +19,18 @@ jobs:
curl -s -X POST 'https://api.telegram.org/bot${{ secrets.TELEGRAM_TOKEN }}/sendMessage' \ curl -s -X POST 'https://api.telegram.org/bot${{ secrets.TELEGRAM_TOKEN }}/sendMessage' \
-d "chat_id=${{ secrets.TELEGRAM_TO }}&text=${msg_text}&message_thread_id=${{ vars.TELEGRAM_ACTIVITY_TOPIC }}" -d "chat_id=${{ secrets.TELEGRAM_TO }}&text=${msg_text}&message_thread_id=${{ vars.TELEGRAM_ACTIVITY_TOPIC }}"
- name: Send Matrix Message - name: Send Matrix Message
run: | run: |
msg_text='${{ github.actor }} updated an issue: msg_text='${{ github.actor }} updated an issue:
status: ${{ github.event.issue.state }} - #${{ github.event.issue.number }} ${{ github.event.issue.title }} status: ${{ github.event.issue.state }} - #${{ github.event.issue.number }} ${{ github.event.issue.title }}
https://github.com/zaneschepke/wgtunnel/issues/${{ github.event.issue.number }}' https://github.com/zaneschepke/wgtunnel/issues/${{ github.event.issue.number }}'
curl -s -X POST \ # Escape newlines and quotes for JSON
-H "Authorization: Bearer ${{ secrets.MATRIX_TOKEN }}" \ formatted_msg=$(echo -n "$msg_text" | sed ':a;N;$ba;s/\n/\\n/g' | sed 's/"/\\"/g')
-H "Content-Type: application/json" \ curl -s -X POST \
-d '{ -H "Authorization: Bearer ${{ secrets.MATRIX_TOKEN }}" \
"msgtype": "m.text", -H "Content-Type: application/json" \
"body": "'"$msg_text"'" -d '{
}' \ "msgtype": "m.text",
"https://matrix.yourserver.com/_matrix/client/v3/rooms/${{ vars.MATRIX_ACTIVITY_TOPIC }}/send/m.room.message/$(date +%s)" "body": "'"$formatted_msg"'"
}' \
"https://matrix.org/_matrix/client/v3/rooms/${{ vars.MATRIX_ACTIVITY_TOPIC }}/send/m.room.message/$(date +%s)"
+2 -2
View File
@@ -19,5 +19,5 @@ jobs:
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x gradlew run: chmod +x gradlew
- name: Run ktlint - name: Run ktfmt
run: ./gradlew ktlintCheck run: ./gradlew ktfmtCheck
+17 -14
View File
@@ -1,8 +1,9 @@
name: on-publish name: on-publish
on: on:
repository_dispatch: release:
types: [ publish-release ] types: [ published ]
jobs: jobs:
on-publish: on-publish:
@@ -20,15 +21,17 @@ jobs:
- name: Send Matrix Message - name: Send Matrix Message
run: | run: |
msg_text='${{ github.actor }} published a new release: msg_text='${{ github.actor }} published a new release:
Release: ${{ github.event.release.tag_name }} Release: ${{ github.event.release.tag_name }}
${{ github.event.release.body }} ${{ github.event.release.body }}
https://github.com/zaneschepke/wgtunnel/releases/tag/${{ github.event.release.tag_name }}' https://github.com/zaneschepke/wgtunnel/releases/tag/${{ github.event.release.tag_name }}'
curl -s -X POST \ # Escape newlines and quotes for JSON
-H "Authorization: Bearer ${{ secrets.MATRIX_TOKEN }}" \ formatted_msg=$(echo -n "$msg_text" | sed ':a;N;$ba;s/\n/\\n/g' | sed 's/"/\\"/g')
-H "Content-Type: application/json" \ curl -s -X POST \
-d '{ -H "Authorization: Bearer ${{ secrets.MATRIX_TOKEN }}" \
"msgtype": "m.text", -H "Content-Type: application/json" \
"body": "'"$msg_text"'" -d '{
}' \ "msgtype": "m.text",
"https://matrix.yourserver.com/_matrix/client/v3/rooms/${{ vars.MATRIX_RELEASE_TOPIC }}/send/m.room.message/$(date +%s)" "body": "'"$formatted_msg"'"
}' \
"https://matrix.org/_matrix/client/v3/rooms/${{ vars.MATRIX_RELEASE_TOPIC }}/send/m.room.message/$(date +%s)"
+2 -2
View File
@@ -43,7 +43,7 @@ jobs:
steps: steps:
- name: Checkout Repository - name: Checkout Repository
uses: actions/checkout@v3 uses: actions/checkout@v4
with: with:
fetch-depth: 0 # This fetches all history so we can check commits fetch-depth: 0 # This fetches all history so we can check commits
@@ -168,7 +168,7 @@ jobs:
id: create_release id: create_release
uses: softprops/action-gh-release@v2 uses: softprops/action-gh-release@v2
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
with: with:
body: | body: |
${{ env.RELEASE_NOTES }} ${{ env.RELEASE_NOTES }}
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2023 WG Auto Tunnel Copyright © 2023-2025 Zane Schepke
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
+206 -205
View File
@@ -1,252 +1,253 @@
plugins { plugins {
alias(libs.plugins.android.application) alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android) alias(libs.plugins.kotlin.android)
alias(libs.plugins.hilt.android) alias(libs.plugins.hilt.android)
alias(libs.plugins.kotlinxSerialization) alias(libs.plugins.kotlinxSerialization)
alias(libs.plugins.ksp) alias(libs.plugins.ksp)
alias(libs.plugins.compose.compiler) alias(libs.plugins.compose.compiler)
alias(libs.plugins.grgit) alias(libs.plugins.grgit)
} }
val versionFile = file("$rootDir/versionCode.txt") val versionFile = file("$rootDir/versionCode.txt")
val versionCodeIncrement = with(getBuildTaskName().lowercase()) { val versionCodeIncrement =
when { with(getBuildTaskName().lowercase()) {
this.contains(Constants.NIGHTLY) || this.contains(Constants.PRERELEASE) -> { when {
if (versionFile.exists()) { this.contains(Constants.NIGHTLY) || this.contains(Constants.PRERELEASE) -> {
versionFile.readText().trim().toInt() + 1 if (versionFile.exists()) {
} else { versionFile.readText().trim().toInt() + 1
1 } else {
} 1
} }
else -> 0 }
} else -> 0
} }
}
android { android {
namespace = Constants.APP_ID namespace = Constants.APP_ID
compileSdk = Constants.TARGET_SDK compileSdk = Constants.TARGET_SDK
androidResources { androidResources { generateLocaleConfig = true }
generateLocaleConfig = true
}
// reproducibility // reproducibility
dependenciesInfo { dependenciesInfo {
// Disables dependency metadata when building APKs. // Disables dependency metadata when building APKs.
includeInApk = false includeInApk = false
// Disables dependency metadata when building Android App Bundles. // Disables dependency metadata when building Android App Bundles.
includeInBundle = false includeInBundle = false
} }
defaultConfig { defaultConfig {
applicationId = Constants.APP_ID applicationId = Constants.APP_ID
minSdk = Constants.MIN_SDK minSdk = Constants.MIN_SDK
targetSdk = Constants.TARGET_SDK targetSdk = Constants.TARGET_SDK
versionCode = Constants.VERSION_CODE + versionCodeIncrement versionCode = Constants.VERSION_CODE + versionCodeIncrement
versionName = determineVersionName() versionName = determineVersionName()
ksp { arg("room.schemaLocation", "$projectDir/schemas") } ksp { arg("room.schemaLocation", "$projectDir/schemas") }
sourceSets { sourceSets {
getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room getByName("debug").assets.srcDirs(files("$projectDir/schemas")) // Room
} }
buildConfigField("String[]", "LANGUAGES", "new String[]{ ${languageList().joinToString(separator = ", ") { "\"$it\"" }} }") buildConfigField(
"String[]",
"LANGUAGES",
"new String[]{ ${languageList().joinToString(separator = ", ") { "\"$it\"" }} }",
)
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables { useSupportLibrary = true } vectorDrawables { useSupportLibrary = true }
} }
signingConfigs { signingConfigs {
create(Constants.RELEASE) { create(Constants.RELEASE) {
storeFile = getStoreFile() storeFile = getStoreFile()
storePassword = getSigningProperty(Constants.STORE_PASS_VAR) storePassword = getSigningProperty(Constants.STORE_PASS_VAR)
keyAlias = getSigningProperty(Constants.KEY_ALIAS_VAR) keyAlias = getSigningProperty(Constants.KEY_ALIAS_VAR)
keyPassword = getSigningProperty(Constants.KEY_PASS_VAR) keyPassword = getSigningProperty(Constants.KEY_PASS_VAR)
} }
} }
buildTypes { buildTypes {
// don't strip // don't strip
packaging.jniLibs.keepDebugSymbols.addAll( packaging.jniLibs.keepDebugSymbols.addAll(
listOf("libwg-go.so", "libwg-quick.so", "libwg.so"), listOf("libwg-go.so", "libwg-quick.so", "libwg.so")
) )
release { release {
isDebuggable = false isDebuggable = false
isMinifyEnabled = true isMinifyEnabled = true
isShrinkResources = true isShrinkResources = true
proguardFiles( proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro", "proguard-rules.pro",
) )
signingConfig = signingConfigs.getByName(Constants.RELEASE) signingConfig = signingConfigs.getByName(Constants.RELEASE)
resValue("string", "provider", "\"${Constants.APP_NAME}.provider\"") resValue("string", "provider", "\"${Constants.APP_NAME}.provider\"")
} }
debug { debug {
applicationIdSuffix = ".debug" applicationIdSuffix = ".debug"
resValue("string", "app_name", "WG Tunnel - Debug") resValue("string", "app_name", "WG Tunnel - Debug")
isDebuggable = true isDebuggable = true
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"") resValue("string", "provider", "\"${Constants.APP_NAME}.provider.debug\"")
} }
create(Constants.PRERELEASE) { create(Constants.PRERELEASE) {
initWith(buildTypes.getByName(Constants.RELEASE)) initWith(buildTypes.getByName(Constants.RELEASE))
applicationIdSuffix = ".prerelease" applicationIdSuffix = ".prerelease"
resValue("string", "app_name", "WG Tunnel - Pre") resValue("string", "app_name", "WG Tunnel - Pre")
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"") resValue("string", "provider", "\"${Constants.APP_NAME}.provider.pre\"")
} }
create(Constants.NIGHTLY) { create(Constants.NIGHTLY) {
initWith(buildTypes.getByName(Constants.RELEASE)) initWith(buildTypes.getByName(Constants.RELEASE))
applicationIdSuffix = ".nightly" applicationIdSuffix = ".nightly"
resValue("string", "app_name", "WG Tunnel - Nightly") resValue("string", "app_name", "WG Tunnel - Nightly")
resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"") resValue("string", "provider", "\"${Constants.APP_NAME}.provider.nightly\"")
} }
applicationVariants.all { applicationVariants.all {
val variant = this val variant = this
variant.outputs variant.outputs
.map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl } .map { it as com.android.build.gradle.internal.api.BaseVariantOutputImpl }
.forEach { output -> .forEach { output ->
val outputFileName = val outputFileName =
"${Constants.APP_NAME}-${variant.flavorName}-" + "${Constants.APP_NAME}-${variant.flavorName}-" +
"${variant.buildType.name}-${variant.versionName}.apk" "${variant.buildType.name}-${variant.versionName}.apk"
output.outputFileName = outputFileName output.outputFileName = outputFileName
} }
} }
} }
flavorDimensions.add(Constants.TYPE) flavorDimensions.add(Constants.TYPE)
productFlavors { productFlavors {
create("fdroid") { create("fdroid") {
dimension = Constants.TYPE dimension = Constants.TYPE
proguardFile("fdroid-rules.pro") proguardFile("fdroid-rules.pro")
} }
create("general") { create("general") { dimension = Constants.TYPE }
dimension = Constants.TYPE }
} compileOptions {
} sourceCompatibility = JavaVersion.VERSION_17
compileOptions { targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_17 isCoreLibraryDesugaringEnabled = true
targetCompatibility = JavaVersion.VERSION_17 }
isCoreLibraryDesugaringEnabled = true kotlinOptions { jvmTarget = Constants.JVM_TARGET }
} buildFeatures {
kotlinOptions { jvmTarget = Constants.JVM_TARGET } compose = true
buildFeatures { buildConfig = true
compose = true }
buildConfig = true packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
}
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
} }
dependencies { dependencies {
implementation(project(":logcatter")) implementation(project(":logcatter"))
implementation(project(":networkmonitor")) implementation(project(":networkmonitor"))
implementation(libs.androidx.core.ktx) implementation(libs.androidx.core.ktx)
implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.runtime.ktx)
// helpers for implementing LifecycleOwner in a Service // helpers for implementing LifecycleOwner in a Service
implementation(libs.androidx.lifecycle.service) implementation(libs.androidx.lifecycle.service)
implementation(libs.androidx.activity.compose) implementation(libs.androidx.activity.compose)
implementation(platform(libs.androidx.compose.bom)) implementation(platform(libs.androidx.compose.bom))
implementation(libs.androidx.compose.ui) implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.graphics) implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.compose.ui.tooling.preview) implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.material3) implementation(libs.androidx.material3)
implementation(libs.androidx.appcompat) implementation(libs.androidx.appcompat)
implementation(libs.material) implementation(libs.material)
implementation(libs.androidx.storage) implementation(libs.androidx.storage)
// test // test
testImplementation(libs.junit) testImplementation(libs.junit)
testImplementation(libs.androidx.junit) testImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core) androidTestImplementation(libs.androidx.espresso.core)
androidTestImplementation(platform(libs.androidx.compose.bom)) androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.compose.ui.test) androidTestImplementation(libs.androidx.compose.ui.test)
androidTestImplementation(libs.androidx.room.testing) androidTestImplementation(libs.androidx.room.testing)
debugImplementation(libs.androidx.compose.ui.tooling) debugImplementation(libs.androidx.compose.ui.tooling)
debugImplementation(libs.androidx.compose.manifest) debugImplementation(libs.androidx.compose.manifest)
// tunnel // tunnel
implementation(libs.tunnel) implementation(libs.tunnel)
implementation(libs.amneziawg.android) implementation(libs.amneziawg.android)
coreLibraryDesugaring(libs.desugar.jdk.libs) coreLibraryDesugaring(libs.desugar.jdk.libs)
// logging // logging
implementation(libs.timber) implementation(libs.timber)
// compose navigation // compose navigation
implementation(libs.androidx.navigation.compose) implementation(libs.androidx.navigation.compose)
implementation(libs.androidx.hilt.navigation.compose) implementation(libs.androidx.hilt.navigation.compose)
// hilt // hilt
implementation(libs.hilt.android) implementation(libs.hilt.android)
ksp(libs.hilt.android.compiler) ksp(libs.hilt.android.compiler)
ksp(libs.androidx.hilt.compiler) ksp(libs.androidx.hilt.compiler)
// accompanist // accompanist
implementation(libs.accompanist.permissions) implementation(libs.accompanist.permissions)
implementation(libs.accompanist.drawablepainter) implementation(libs.accompanist.drawablepainter)
// storage // storage
implementation(libs.androidx.room.runtime) implementation(libs.androidx.room.runtime)
ksp(libs.androidx.room.compiler) ksp(libs.androidx.room.compiler)
implementation(libs.androidx.room.ktx) implementation(libs.androidx.room.ktx)
implementation(libs.androidx.datastore.preferences) implementation(libs.androidx.datastore.preferences)
// lifecycle // lifecycle
implementation(libs.lifecycle.runtime.compose) implementation(libs.lifecycle.runtime.compose)
implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.lifecycle.runtime.ktx)
implementation(libs.androidx.lifecycle.process) implementation(libs.androidx.lifecycle.process)
// serialization // serialization
implementation(libs.kotlinx.serialization.json) implementation(libs.kotlinx.serialization.json)
// ui // ui
implementation(libs.zxing.android.embedded) implementation(libs.zxing.android.embedded)
implementation(libs.material.icons.extended) implementation(libs.material.icons.extended)
// bio // bio
implementation(libs.androidx.biometric.ktx) implementation(libs.androidx.biometric.ktx)
implementation(libs.pin.lock.compose) implementation(libs.pin.lock.compose)
// shortcuts // shortcuts
implementation(libs.androidx.core) implementation(libs.androidx.core)
// splash // splash
implementation(libs.androidx.core.splashscreen) implementation(libs.androidx.core.splashscreen)
// worker // worker
implementation(libs.androidx.work.runtime) implementation(libs.androidx.work.runtime)
implementation(libs.androidx.hilt.work) implementation(libs.androidx.hilt.work)
} }
fun determineVersionName(): String { fun determineVersionName(): String {
return with(getBuildTaskName().lowercase()) { return with(getBuildTaskName().lowercase()) {
when { when {
contains(Constants.NIGHTLY) || contains(Constants.PRERELEASE) -> contains(Constants.NIGHTLY) || contains(Constants.PRERELEASE) ->
Constants.VERSION_NAME + Constants.VERSION_NAME + "-${grgitService.service.get().grgit.head().abbreviatedId}"
"-${grgitService.service.get().grgit.head().abbreviatedId}" else -> Constants.VERSION_NAME
else -> Constants.VERSION_NAME }
} }
}
} }
val incrementVersionCode by tasks.registering { val incrementVersionCode by
doLast { tasks.registering {
val versionFile = file("$rootDir/versionCode.txt") doLast {
if (versionFile.exists()) { val versionFile = file("$rootDir/versionCode.txt")
versionFile.writeText(versionCodeIncrement.toString()) if (versionFile.exists()) {
println("Incremented versionCode to $versionCodeIncrement") versionFile.writeText(versionCodeIncrement.toString())
} println("Incremented versionCode to $versionCodeIncrement")
} }
} }
}
tasks.whenTaskAdded { tasks.whenTaskAdded {
if (name.startsWith("assemble") && !name.lowercase().contains("debug")) { if (name.startsWith("assemble") && !name.lowercase().contains("debug")) {
dependsOn(incrementVersionCode) dependsOn(incrementVersionCode)
} }
} }
+1 -1
View File
@@ -39,4 +39,4 @@
-dontwarn org.joda.time.Instant -dontwarn org.joda.time.Instant
-dontwarn org.slf4j.impl.StaticLoggerBinder -dontwarn org.slf4j.impl.StaticLoggerBinder
-dontwarn org.slf4j.impl.StaticMDCBinder -dontwarn org.slf4j.impl.StaticMDCBinder
-dontwarn org.slf4j.impl.StaticMarkerBinder -dontwarn org.slf4j.impl.StaticMarkerBinder
@@ -13,10 +13,10 @@ import org.junit.runner.RunWith
*/ */
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest { class ExampleInstrumentedTest {
@Test @Test
fun useAppContext() { fun useAppContext() {
// Context of the app under test. // Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName) assertEquals("com.zaneschepke.wireguardautotunnel", appContext.packageName)
} }
} }
@@ -5,40 +5,35 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import com.zaneschepke.wireguardautotunnel.data.AppDatabase import com.zaneschepke.wireguardautotunnel.data.AppDatabase
import com.zaneschepke.wireguardautotunnel.data.Queries import com.zaneschepke.wireguardautotunnel.data.Queries
import java.io.IOException
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import java.io.IOException
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class MigrationTest { class MigrationTest {
private val dbName = "migration-test" private val dbName = "migration-test"
@get:Rule @get:Rule
val helper: MigrationTestHelper = val helper: MigrationTestHelper =
MigrationTestHelper( MigrationTestHelper(InstrumentationRegistry.getInstrumentation(), AppDatabase::class.java)
InstrumentationRegistry.getInstrumentation(),
AppDatabase::class.java,
)
@Test @Test
@Throws(IOException::class) @Throws(IOException::class)
fun migrate6To7() { fun migrate6To7() {
helper.createDatabase(dbName, 6).apply { helper.createDatabase(dbName, 6).apply {
// Database has schema version 1. Insert some data using SQL queries. // Database has schema version 1. Insert some data using SQL queries.
// You can't use DAO classes because they expect the latest schema. // You can't use DAO classes because they expect the latest schema.
execSQL(Queries.createDefaultSettings()) execSQL(Queries.createDefaultSettings())
execSQL( execSQL(Queries.createTunnelConfig())
Queries.createTunnelConfig(), // Prepare for the next version.
) close()
// Prepare for the next version. }
close()
}
// Re-open the database with version 2 and provide // Re-open the database with version 2 and provide
// MIGRATION_1_2 as the migration process. // MIGRATION_1_2 as the migration process.
helper.runMigrationsAndValidate(dbName, 7, true) helper.runMigrationsAndValidate(dbName, 7, true)
// MigrationTestHelper automatically verifies the schema changes, // MigrationTestHelper automatically verifies the schema changes,
// but you need to validate that the data was migrated properly. // but you need to validate that the data was migrated properly.
} }
} }
+3 -3
View File
@@ -53,7 +53,7 @@
<application <application
android:name=".WireGuardAutoTunnel" android:name=".WireGuardAutoTunnel"
android:allowBackup="false" android:allowBackup="false"
android:banner="@drawable/ic_banner" android:banner="@mipmap/ic_banner"
android:dataExtractionRules="@xml/data_extraction_rules" android:dataExtractionRules="@xml/data_extraction_rules"
android:enableOnBackInvokedCallback="true" android:enableOnBackInvokedCallback="true"
android:fullBackupContent="@xml/backup_rules" android:fullBackupContent="@xml/backup_rules"
@@ -114,7 +114,7 @@
<service <service
android:name=".core.service.tile.TunnelControlTile" android:name=".core.service.tile.TunnelControlTile"
android:exported="true" android:exported="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_notification"
android:label="@string/tunnel_control" android:label="@string/tunnel_control"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"> android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<meta-data <meta-data
@@ -131,7 +131,7 @@
<service <service
android:name=".core.service.tile.AutoTunnelControlTile" android:name=".core.service.tile.AutoTunnelControlTile"
android:exported="true" android:exported="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_notification"
android:label="@string/auto_tunnel" android:label="@string/auto_tunnel"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE"> android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<meta-data <meta-data
Binary file not shown.

Before

Width:  |  Height:  |  Size: 16 KiB

After

Width:  |  Height:  |  Size: 21 KiB

@@ -10,7 +10,6 @@ import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.provider.Settings import android.provider.Settings
import androidx.activity.SystemBarStyle import androidx.activity.SystemBarStyle
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
@@ -69,6 +68,8 @@ import com.zaneschepke.wireguardautotunnel.ui.common.navigation.DynamicTopAppBar
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController import com.zaneschepke.wireguardautotunnel.ui.common.navigation.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.common.navigation.currentNavBackStackEntryAsNavBarState import com.zaneschepke.wireguardautotunnel.ui.common.navigation.currentNavBackStackEntryAsNavBarState
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AutoTunnelAdvancedScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.TunnelAutoTunnelScreen import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.TunnelAutoTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigScreen import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigScreen
@@ -77,311 +78,317 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.main.splittunnel.SplitTunn
import com.zaneschepke.wireguardautotunnel.ui.screens.main.tunneloptions.TunnelOptionsScreen import com.zaneschepke.wireguardautotunnel.ui.screens.main.tunneloptions.TunnelOptionsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.pin.PinLockScreen import com.zaneschepke.wireguardautotunnel.ui.screens.pin.PinLockScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.SettingsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.advanced.SettingsAdvancedScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.AppearanceScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.AppearanceScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.display.DisplayScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.display.DisplayScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.language.LanguageScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.language.LanguageScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AutoTunnelAdvancedScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.advanced.SettingsAdvancedScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.LocationDisclosureScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.disclosure.LocationDisclosureScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.killswitch.KillSwitchScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.killswitch.KillSwitchScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.LogsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.logs.LogsScreen
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import org.amnezia.awg.backend.GoBackend.VpnService
import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
import kotlin.system.exitProcess import kotlin.system.exitProcess
import org.amnezia.awg.backend.GoBackend.VpnService
import timber.log.Timber
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@Inject @Inject lateinit var appStateRepository: AppStateRepository
lateinit var appStateRepository: AppStateRepository
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
@Inject @Inject lateinit var networkMonitor: NetworkMonitor
lateinit var networkMonitor: NetworkMonitor
private var lastLocationPermissionState: Boolean? = null private var lastLocationPermissionState: Boolean? = null
@SuppressLint("BatteryLife") @SuppressLint("BatteryLife")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge( enableEdgeToEdge(
statusBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT), statusBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
navigationBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT), navigationBarStyle = SystemBarStyle.Companion.auto(Color.TRANSPARENT, Color.TRANSPARENT),
) )
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
window.isNavigationBarContrastEnforced = false window.isNavigationBarContrastEnforced = false
} }
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val viewModel by viewModels<AppViewModel>() val viewModel by viewModels<AppViewModel>()
installSplashScreen().apply { installSplashScreen().apply {
setKeepOnScreenCondition { setKeepOnScreenCondition { !viewModel.appViewState.value.isAppReady }
!viewModel.appViewState.value.isAppReady }
}
}
setContent { setContent {
val appUiState by viewModel.uiState.collectAsStateWithLifecycle() val appUiState by viewModel.uiState.collectAsStateWithLifecycle()
val appViewState by viewModel.appViewState.collectAsStateWithLifecycle() val appViewState by viewModel.appViewState.collectAsStateWithLifecycle()
val navController = rememberNavController() val navController = rememberNavController()
val backStackEntry by navController.currentBackStackEntryAsState() val backStackEntry by navController.currentBackStackEntryAsState()
val navBarState by currentNavBackStackEntryAsNavBarState(navController, backStackEntry, viewModel, appUiState) val navBarState by
val snackbar = remember { SnackbarHostState() } currentNavBackStackEntryAsNavBarState(
var showVpnPermissionDialog by remember { mutableStateOf(false) } navController,
var vpnPermissionDenied by remember { mutableStateOf(false) } backStackEntry,
viewModel,
appUiState,
)
val snackbar = remember { SnackbarHostState() }
var showVpnPermissionDialog by remember { mutableStateOf(false) }
var vpnPermissionDenied by remember { mutableStateOf(false) }
val vpnActivity = val vpnActivity =
rememberLauncherForActivityResult( rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult(), ActivityResultContracts.StartActivityForResult(),
onResult = { onResult = {
if (it.resultCode != RESULT_OK) { if (it.resultCode != RESULT_OK) {
showVpnPermissionDialog = true showVpnPermissionDialog = true
vpnPermissionDenied = true vpnPermissionDenied = true
} else { } else {
vpnPermissionDenied = false vpnPermissionDenied = false
} }
}, },
) )
LaunchedEffect(appUiState.tunnels) { LaunchedEffect(appUiState.tunnels) {
if (!appViewState.isAppReady) { if (!appViewState.isAppReady) {
viewModel.handleEvent(AppEvent.AppReadyCheck(appUiState.tunnels)) viewModel.handleEvent(AppEvent.AppReadyCheck(appUiState.tunnels))
} }
} }
val batteryActivity = rememberLauncherForActivityResult( val batteryActivity =
ActivityResultContracts.StartActivityForResult(), rememberLauncherForActivityResult(
) { _: ActivityResult -> ActivityResultContracts.StartActivityForResult()
viewModel.handleEvent(AppEvent.SetBatteryOptimizeDisableShown) ) { _: ActivityResult ->
} viewModel.handleEvent(AppEvent.SetBatteryOptimizeDisableShown)
}
with(appViewState) { with(appViewState) {
LaunchedEffect(isConfigChanged) { LaunchedEffect(isConfigChanged) {
if (isConfigChanged) { if (isConfigChanged) {
Intent(this@MainActivity, MainActivity::class.java).also { Intent(this@MainActivity, MainActivity::class.java).also {
startActivity(it) startActivity(it)
exitProcess(0) exitProcess(0)
} }
} }
} }
LaunchedEffect(errorMessage) { LaunchedEffect(errorMessage) {
errorMessage?.let { errorMessage?.let {
snackbar.showSnackbar(it.asString(this@MainActivity)) snackbar.showSnackbar(it.asString(this@MainActivity))
viewModel.handleEvent(AppEvent.MessageShown) viewModel.handleEvent(AppEvent.MessageShown)
} }
} }
LaunchedEffect(appUiState.activeTunnels) { LaunchedEffect(appUiState.activeTunnels) {
appUiState.activeTunnels appUiState.activeTunnels.mapNotNull { (tunnelConf, tunnelState) ->
.mapNotNull { (tunnelConf, tunnelState) -> (tunnelState.status as? TunnelStatus.Error)?.let { error ->
(tunnelState.status as? TunnelStatus.Error)?.let { error -> val message = error.error.toStringRes()
val message = error.error.toStringRes() val context = this@MainActivity
val context = this@MainActivity snackbar.showSnackbar(
snackbar.showSnackbar(context.getString(R.string.tunnel_error_template, context.getString(message))) context.getString(
viewModel.handleEvent(AppEvent.ClearTunnelError(tunnelConf)) R.string.tunnel_error_template,
} context.getString(message),
} )
} )
LaunchedEffect(popBackStack) { viewModel.handleEvent(AppEvent.ClearTunnelError(tunnelConf))
if (popBackStack) { }
navController.popBackStack() }
viewModel.handleEvent(AppEvent.PopBackStack(false)) }
} LaunchedEffect(popBackStack) {
} if (popBackStack) {
LaunchedEffect(requestVpnPermission) { navController.popBackStack()
if (requestVpnPermission) { viewModel.handleEvent(AppEvent.PopBackStack(false))
if (!vpnPermissionDenied) { }
vpnActivity.launch(VpnService.prepare(this@MainActivity)) }
} else { LaunchedEffect(requestVpnPermission) {
showVpnPermissionDialog = true if (requestVpnPermission) {
} if (!vpnPermissionDenied) {
viewModel.handleEvent(AppEvent.VpnPermissionRequested) vpnActivity.launch(VpnService.prepare(this@MainActivity))
} } else {
} showVpnPermissionDialog = true
LaunchedEffect(requestBatteryPermission) { }
if (requestBatteryPermission) { viewModel.handleEvent(AppEvent.VpnPermissionRequested)
batteryActivity.launch( }
Intent().apply { }
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS LaunchedEffect(requestBatteryPermission) {
data = Uri.parse("package:${this@MainActivity.packageName}") if (requestBatteryPermission) {
}, batteryActivity.launch(
) Intent().apply {
} action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
} data = Uri.parse("package:${this@MainActivity.packageName}")
} }
)
}
}
}
CompositionLocalProvider(LocalNavController provides navController) { CompositionLocalProvider(LocalNavController provides navController) {
WireguardAutoTunnelTheme(theme = appUiState.appState.theme) { WireguardAutoTunnelTheme(theme = appUiState.appState.theme) {
VpnDeniedDialog(showVpnPermissionDialog, onDismiss = { showVpnPermissionDialog = false }) VpnDeniedDialog(
showVpnPermissionDialog,
onDismiss = { showVpnPermissionDialog = false },
)
Scaffold( Scaffold(
modifier = Modifier.pointerInput(Unit) { modifier =
detectTapGestures { Modifier.pointerInput(Unit) {
viewModel.handleEvent(AppEvent.SetSelectedTunnel(null)) detectTapGestures {
} viewModel.handleEvent(AppEvent.SetSelectedTunnel(null))
}, }
snackbarHost = { },
SnackbarHost(snackbar) { snackbarData: SnackbarData -> snackbarHost = {
CustomSnackBar( SnackbarHost(snackbar) { snackbarData: SnackbarData ->
snackbarData.visuals.message, CustomSnackBar(
isRtl = false, snackbarData.visuals.message,
containerColor = isRtl = false,
MaterialTheme.colorScheme.surfaceColorAtElevation( containerColor =
2.dp, MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp),
), )
) }
} },
}, topBar = { DynamicTopAppBar(navBarState) },
topBar = { bottomBar = {
DynamicTopAppBar(navBarState) AnimatedVisibility(
}, visible = navBarState.showBottom,
bottomBar = { enter = slideInVertically(initialOffsetY = { it }),
AnimatedVisibility( exit = slideOutVertically(targetOffsetY = { it }),
visible = navBarState.showBottom, ) {
enter = slideInVertically(initialOffsetY = { it }), CustomBottomNavbar(
exit = slideOutVertically(targetOffsetY = { it }), listOf(
) { BottomNavItem(
CustomBottomNavbar( name = stringResource(R.string.tunnels),
listOf( route = Route.Main,
BottomNavItem( icon = Icons.Rounded.Home,
name = stringResource(R.string.tunnels), onClick = { navController.goFromRoot(Route.Main) },
route = Route.Main, ),
icon = Icons.Rounded.Home, BottomNavItem(
onClick = { navController.goFromRoot(Route.Main) }, name = stringResource(R.string.auto_tunnel),
), route = Route.AutoTunnel,
BottomNavItem( icon = Icons.Rounded.Bolt,
name = stringResource(R.string.auto_tunnel), onClick = {
route = Route.AutoTunnel, val route =
icon = Icons.Rounded.Bolt, if (
onClick = { appUiState.appState
val route = if (appUiState.appState.isLocationDisclosureShown) Route.AutoTunnel else Route.LocationDisclosure .isLocationDisclosureShown
navController.goFromRoot(route) )
}, Route.AutoTunnel
active = appUiState.isAutoTunnelActive, else Route.LocationDisclosure
), navController.goFromRoot(route)
BottomNavItem( },
name = stringResource(R.string.settings), active = appUiState.isAutoTunnelActive,
route = Route.Settings, ),
icon = Icons.Rounded.Settings, BottomNavItem(
onClick = { navController.goFromRoot(Route.Settings) }, name = stringResource(R.string.settings),
), route = Route.Settings,
BottomNavItem( icon = Icons.Rounded.Settings,
name = stringResource(R.string.support), onClick = { navController.goFromRoot(Route.Settings) },
route = Route.Support, ),
icon = Icons.Rounded.QuestionMark, BottomNavItem(
onClick = { navController.goFromRoot(Route.Support) }, name = stringResource(R.string.support),
), route = Route.Support,
), icon = Icons.Rounded.QuestionMark,
navBarState = navBarState, onClick = { navController.goFromRoot(Route.Support) },
) ),
} ),
}, navBarState = navBarState,
) { padding -> )
Box( }
modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.surface) },
.padding(padding) ) { padding ->
.consumeWindowInsets(padding) Box(
.imePadding(), modifier =
) { Modifier.fillMaxSize()
NavHost( .background(MaterialTheme.colorScheme.surface)
navController, .padding(padding)
startDestination = (if (appUiState.appState.isPinLockEnabled) Route.Lock else Route.Main), .consumeWindowInsets(padding)
) { .imePadding()
composable<Route.Main> { ) {
MainScreen(appUiState, appViewState, viewModel) NavHost(
} navController,
composable<Route.Settings> { startDestination =
SettingsScreen(appUiState, appViewState, viewModel) (if (appUiState.appState.isPinLockEnabled) Route.Lock
} else Route.Main),
composable<Route.SettingsAdvanced> { ) {
SettingsAdvancedScreen(appUiState, viewModel) composable<Route.Main> {
} MainScreen(appUiState, appViewState, viewModel)
composable<Route.LocationDisclosure> { }
LocationDisclosureScreen(appUiState, viewModel) composable<Route.Settings> {
} SettingsScreen(appUiState, appViewState, viewModel)
composable<Route.AutoTunnel> { }
AutoTunnelScreen(appUiState, viewModel) composable<Route.SettingsAdvanced> {
} SettingsAdvancedScreen(appUiState, viewModel)
composable<Route.Appearance> { }
AppearanceScreen() composable<Route.LocationDisclosure> {
} LocationDisclosureScreen(appUiState, viewModel)
composable<Route.Language> { }
LanguageScreen(appUiState, viewModel) composable<Route.AutoTunnel> {
} AutoTunnelScreen(appUiState, viewModel)
composable<Route.Display> { }
DisplayScreen(appUiState, viewModel) composable<Route.Appearance> { AppearanceScreen() }
} composable<Route.Language> { LanguageScreen(appUiState, viewModel) }
composable<Route.Support> { composable<Route.Display> { DisplayScreen(appUiState, viewModel) }
SupportScreen() composable<Route.Support> { SupportScreen() }
} composable<Route.AutoTunnelAdvanced> {
composable<Route.AutoTunnelAdvanced> { AutoTunnelAdvancedScreen(appUiState, viewModel)
AutoTunnelAdvancedScreen(appUiState, viewModel) }
} composable<Route.Logs> { LogsScreen(appViewState, viewModel) }
composable<Route.Logs> { composable<Route.Config> { backStack ->
LogsScreen(appViewState, viewModel) val args = backStack.toRoute<Route.Config>()
} val config = appUiState.tunnels.firstOrNull { it.id == args.id }
composable<Route.Config> { backStack -> ConfigScreen(config, viewModel)
val args = backStack.toRoute<Route.Config>() }
val config = appUiState.tunnels.firstOrNull { it.id == args.id } composable<Route.TunnelOptions> { backStack ->
ConfigScreen(config, viewModel) val args = backStack.toRoute<Route.TunnelOptions>()
} appUiState.tunnels
composable<Route.TunnelOptions> { backStack -> .firstOrNull { it.id == args.id }
val args = backStack.toRoute<Route.TunnelOptions>() ?.let { config ->
appUiState.tunnels.firstOrNull { it.id == args.id }?.let { config -> TunnelOptionsScreen(config, appUiState, viewModel)
TunnelOptionsScreen(config, appUiState, viewModel) }
} }
} composable<Route.Lock> { PinLockScreen(viewModel) }
composable<Route.Lock> { composable<Route.Scanner> { ScannerScreen(viewModel) }
PinLockScreen(viewModel) composable<Route.KillSwitch> {
} KillSwitchScreen(appUiState, viewModel)
composable<Route.Scanner> { }
ScannerScreen(viewModel) composable<Route.SplitTunnel> { SplitTunnelScreen(viewModel) }
} composable<Route.TunnelAutoTunnel> { backStack ->
composable<Route.KillSwitch> { val args = backStack.toRoute<Route.TunnelOptions>()
KillSwitchScreen(appUiState, viewModel) appUiState.tunnels
} .firstOrNull { it.id == args.id }
composable<Route.SplitTunnel> { ?.let {
SplitTunnelScreen(viewModel) TunnelAutoTunnelScreen(
} it,
composable<Route.TunnelAutoTunnel> { backStack -> appUiState.appSettings,
val args = backStack.toRoute<Route.TunnelOptions>() viewModel,
appUiState.tunnels.firstOrNull { it.id == args.id }?.let { )
TunnelAutoTunnelScreen(it, appUiState.appSettings, viewModel) }
} }
} }
} }
} }
} }
} }
} }
} }
}
override fun onResume() {
super.onResume()
checkPermissionAndNotify()
}
private fun checkPermissionAndNotify() { override fun onResume() {
val hasLocation = ContextCompat.checkSelfPermission( super.onResume()
this, checkPermissionAndNotify()
Manifest.permission.ACCESS_FINE_LOCATION, }
) == PackageManager.PERMISSION_GRANTED
if (lastLocationPermissionState != hasLocation) { private fun checkPermissionAndNotify() {
Timber.d("Location permission changed to: $hasLocation") val hasLocation =
if (hasLocation) { ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) ==
networkMonitor.sendLocationPermissionsGrantedBroadcast() PackageManager.PERMISSION_GRANTED
} if (lastLocationPermissionState != hasLocation) {
lastLocationPermissionState = hasLocation Timber.d("Location permission changed to: $hasLocation")
} if (hasLocation) {
} networkMonitor.sendLocationPermissionsGrantedBroadcast()
}
lastLocationPermissionState = hasLocation
}
}
} }
@@ -20,134 +20,115 @@ import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.util.LocaleUtil import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
import com.zaneschepke.wireguardautotunnel.util.ReleaseTree import com.zaneschepke.wireguardautotunnel.util.ReleaseTree
import dagger.hilt.android.HiltAndroidApp import dagger.hilt.android.HiltAndroidApp
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@HiltAndroidApp @HiltAndroidApp
class WireGuardAutoTunnel : Application(), Configuration.Provider { class WireGuardAutoTunnel : Application(), Configuration.Provider {
@Inject @Inject lateinit var workerFactory: HiltWorkerFactory
lateinit var workerFactory: HiltWorkerFactory
override val workManagerConfiguration: Configuration override val workManagerConfiguration: Configuration
get() = Configuration.Builder() get() = Configuration.Builder().setWorkerFactory(workerFactory).build()
.setWorkerFactory(workerFactory)
.build()
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject @Inject lateinit var logReader: LogReader
lateinit var logReader: LogReader
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
@Inject @Inject @MainDispatcher lateinit var mainDispatcher: CoroutineDispatcher
@MainDispatcher
lateinit var mainDispatcher: CoroutineDispatcher
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
instance = this instance = this
ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver()) ProcessLifecycleOwner.get().lifecycle.addObserver(AppLifecycleObserver())
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Timber.plant(Timber.DebugTree()) Timber.plant(Timber.DebugTree())
StrictMode.setThreadPolicy( StrictMode.setThreadPolicy(
ThreadPolicy.Builder() ThreadPolicy.Builder()
.detectDiskReads() .detectDiskReads()
.detectDiskWrites() .detectDiskWrites()
.detectNetwork() .detectNetwork()
.penaltyLog() .penaltyLog()
.build(), .build()
) )
} else { } else {
Timber.plant(ReleaseTree()) Timber.plant(ReleaseTree())
} }
GoBackend.setAlwaysOnCallback { GoBackend.setAlwaysOnCallback {
applicationScope.launch { applicationScope.launch {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
if (settings.isAlwaysOnVpnEnabled) { if (settings.isAlwaysOnVpnEnabled) {
val tunnel = appDataRepository.getPrimaryOrFirstTunnel() val tunnel = appDataRepository.getPrimaryOrFirstTunnel()
tunnel?.let { tunnel?.let { tunnelManager.startTunnel(it) }
tunnelManager.startTunnel(it) } else {
} Timber.w("Always-on VPN is not enabled in app settings")
} else { }
Timber.w("Always-on VPN is not enabled in app settings") }
} }
}
}
ServiceWorker.start(this) ServiceWorker.start(this)
applicationScope.launch { applicationScope.launch {
if (!appDataRepository.settings.get().isKernelEnabled) { appDataRepository.appState.getLocale()?.let {
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList()) withContext(mainDispatcher) { LocaleUtil.changeLocale(it) }
} }
appDataRepository.appState.getLocale()?.let { appDataRepository.appState.isLocalLogsEnabled().let { enabled ->
withContext(mainDispatcher) { if (enabled) logReader.start()
LocaleUtil.changeLocale(it) }
} }
} }
appDataRepository.appState.isLocalLogsEnabled().let { enabled ->
if (enabled) logReader.start()
}
}
}
override fun onTerminate() { override fun onTerminate() {
applicationScope.launch { applicationScope.launch {
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptyList()) tunnelManager.setBackendState(BackendState.INACTIVE, emptyList())
} }
super.onTerminate() super.onTerminate()
} }
class AppLifecycleObserver : DefaultLifecycleObserver { class AppLifecycleObserver : DefaultLifecycleObserver {
override fun onStart(owner: LifecycleOwner) { override fun onStart(owner: LifecycleOwner) {
Timber.d("Application entered foreground") Timber.d("Application entered foreground")
foreground = true foreground = true
} }
override fun onPause(owner: LifecycleOwner) {
Timber.d("Application entered background")
foreground = false
}
}
companion object { override fun onPause(owner: LifecycleOwner) {
private var foreground = false Timber.d("Application entered background")
foreground = false
}
}
fun isForeground(): Boolean { companion object {
return foreground private var foreground = false
}
@Volatile fun isForeground(): Boolean {
private var lastActiveTunnels: List<Int> = emptyList() return foreground
}
@Synchronized @Volatile private var lastActiveTunnels: List<Int> = emptyList()
fun getLastActiveTunnels(): List<Int> {
return lastActiveTunnels
}
@Synchronized @Synchronized
fun setLastActiveTunnels(newTunnels: List<Int>) { fun getLastActiveTunnels(): List<Int> {
lastActiveTunnels = newTunnels return lastActiveTunnels
} }
lateinit var instance: WireGuardAutoTunnel @Synchronized
private set fun setLastActiveTunnels(newTunnels: List<Int>) {
} lastActiveTunnels = newTunnels
}
lateinit var instance: WireGuardAutoTunnel
private set
}
} }
@@ -8,42 +8,35 @@ import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class KernelReceiver : BroadcastReceiver() { class KernelReceiver : BroadcastReceiver() {
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject @Inject lateinit var tunnelRepository: TunnelRepository
lateinit var tunnelRepository: TunnelRepository
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val action = intent.action ?: return val action = intent.action ?: return
applicationScope.launch { applicationScope.launch {
if (action == REFRESH_TUNNELS_ACTION) { if (action == REFRESH_TUNNELS_ACTION) {
tunnelManager.runningTunnelNames().forEach { name -> tunnelManager.runningTunnelNames().forEach { name ->
val tunnel = tunnelRepository.findByTunnelName(name) val tunnel = tunnelRepository.findByTunnelName(name)
tunnel?.let { tunnel?.let { tunnelRepository.save(it.copy(isActive = true)) }
tunnelRepository.save(it.copy(isActive = true)) }
} serviceManager.updateTunnelTile()
} }
serviceManager.updateTunnelTile() }
} }
}
}
companion object { companion object {
const val REFRESH_TUNNELS_ACTION = "com.wireguard.android.action.REFRESH_TUNNEL_STATES" const val REFRESH_TUNNELS_ACTION = "com.wireguard.android.action.REFRESH_TUNNEL_STATES"
} }
} }
@@ -10,41 +10,36 @@ import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class NotificationActionReceiver : BroadcastReceiver() { class NotificationActionReceiver : BroadcastReceiver() {
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
@Inject @Inject lateinit var tunnelRepository: TunnelRepository
lateinit var tunnelRepository: TunnelRepository
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
applicationScope.launch { applicationScope.launch {
when (intent.action) { when (intent.action) {
NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel() NotificationAction.AUTO_TUNNEL_OFF.name -> serviceManager.stopAutoTunnel()
NotificationAction.TUNNEL_OFF.name -> { NotificationAction.TUNNEL_OFF.name -> {
val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0) val tunnelId = intent.getIntExtra(NotificationManager.EXTRA_ID, 0)
if (tunnelId == STOP_ALL_TUNNELS_ID) return@launch tunnelManager.stopTunnel() if (tunnelId == STOP_ALL_TUNNELS_ID) return@launch tunnelManager.stopTunnel()
val tunnel = tunnelRepository.getById(tunnelId) val tunnel = tunnelRepository.getById(tunnelId)
tunnelManager.stopTunnel(tunnel) tunnelManager.stopTunnel(tunnel)
} }
} }
} }
} }
companion object { companion object {
const val STOP_ALL_TUNNELS_ID = 0 const val STOP_ALL_TUNNELS_ID = 0
} }
} }
@@ -9,83 +9,88 @@ import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class RemoteControlReceiver : BroadcastReceiver() { class RemoteControlReceiver : BroadcastReceiver() {
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
enum class Action(private val suffix: String) { enum class Action(private val suffix: String) {
START_TUNNEL("START_TUNNEL"), START_TUNNEL("START_TUNNEL"),
STOP_TUNNEL("STOP_TUNNEL"), STOP_TUNNEL("STOP_TUNNEL"),
START_AUTO_TUNNEL("START_AUTO_TUNNEL"), START_AUTO_TUNNEL("START_AUTO_TUNNEL"),
STOP_AUTO_TUNNEL("STOP_AUTO_TUNNEL"), STOP_AUTO_TUNNEL("STOP_AUTO_TUNNEL");
;
fun getFullAction(): String { fun getFullAction(): String {
return "${Constants.BASE_PACKAGE}.$suffix" return "${Constants.BASE_PACKAGE}.$suffix"
} }
companion object { companion object {
fun fromAction(action: String): Action? { fun fromAction(action: String): Action? {
for (a in entries) { for (a in entries) {
if (a.getFullAction() == action) { if (a.getFullAction() == action) {
return a return a
} }
} }
return null return null
} }
} }
} }
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
Timber.i("onReceive") Timber.i("onReceive")
val action = intent.action ?: return val action = intent.action ?: return
val appAction = Action.fromAction(action) ?: return Timber.w("Unknown action $action") val appAction = Action.fromAction(action) ?: return Timber.w("Unknown action $action")
applicationScope.launch { applicationScope.launch {
if (!appDataRepository.appState.isRemoteControlEnabled()) return@launch Timber.w("Remote control disabled") if (!appDataRepository.appState.isRemoteControlEnabled())
val key = appDataRepository.appState.getRemoteKey() ?: return@launch Timber.w("Remote control key missing") return@launch Timber.w("Remote control disabled")
if (key != intent.getStringExtra(EXTRA_KEY)?.trim()) return@launch Timber.w("Invalid remote control key") val key =
when (appAction) { appDataRepository.appState.getRemoteKey()
Action.START_TUNNEL -> { ?: return@launch Timber.w("Remote control key missing")
val tunnelName = intent.getStringExtra(EXTRA_TUN_NAME) ?: return@launch startDefaultTunnel() if (key != intent.getStringExtra(EXTRA_KEY)?.trim())
val tunnel = appDataRepository.tunnels.findByTunnelName(tunnelName) ?: return@launch startDefaultTunnel() return@launch Timber.w("Invalid remote control key")
tunnelManager.startTunnel(tunnel) when (appAction) {
} Action.START_TUNNEL -> {
Action.STOP_TUNNEL -> { val tunnelName =
val tunnelName = intent.getStringExtra(EXTRA_TUN_NAME) ?: return@launch tunnelManager.stopTunnel() intent.getStringExtra(EXTRA_TUN_NAME) ?: return@launch startDefaultTunnel()
val tunnel = appDataRepository.tunnels.findByTunnelName(tunnelName) ?: return@launch tunnelManager.stopTunnel() val tunnel =
tunnelManager.stopTunnel(tunnel) appDataRepository.tunnels.findByTunnelName(tunnelName)
} ?: return@launch startDefaultTunnel()
Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel() tunnelManager.startTunnel(tunnel)
Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel() }
} Action.STOP_TUNNEL -> {
} val tunnelName =
} intent.getStringExtra(EXTRA_TUN_NAME)
?: return@launch tunnelManager.stopTunnel()
val tunnel =
appDataRepository.tunnels.findByTunnelName(tunnelName)
?: return@launch tunnelManager.stopTunnel()
tunnelManager.stopTunnel(tunnel)
}
Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel()
Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel()
}
}
}
private suspend fun startDefaultTunnel() { private suspend fun startDefaultTunnel() {
appDataRepository.getPrimaryOrFirstTunnel()?.let { tunnel -> appDataRepository.getPrimaryOrFirstTunnel()?.let { tunnel ->
tunnelManager.startTunnel(tunnel) tunnelManager.startTunnel(tunnel)
} }
} }
companion object { companion object {
const val EXTRA_TUN_NAME = "tunnelName" const val EXTRA_TUN_NAME = "tunnelName"
const val EXTRA_KEY = "key" const val EXTRA_KEY = "key"
} }
} }
@@ -9,48 +9,41 @@ import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class RestartReceiver : BroadcastReceiver() { class RestartReceiver : BroadcastReceiver() {
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
@Inject @Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
Timber.d("RestartReceiver triggered with action: ${intent.action}") Timber.d("RestartReceiver triggered with action: ${intent.action}")
serviceManager.updateTunnelTile() serviceManager.updateTunnelTile()
serviceManager.updateAutoTunnelTile() serviceManager.updateAutoTunnelTile()
applicationScope.launch(ioDispatcher) { applicationScope.launch(ioDispatcher) {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
if (settings.isRestoreOnBootEnabled) { if (settings.isRestoreOnBootEnabled) {
if (settings.isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) { if (settings.isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) {
Timber.d("Starting auto-tunnel on boot/update") Timber.d("Starting auto-tunnel on boot/update")
serviceManager.startAutoTunnel() serviceManager.startAutoTunnel()
} else { } else {
Timber.d("Restoring previous tunnel state") Timber.d("Restoring previous tunnel state")
tunnelManager.restorePreviousState() tunnelManager.restorePreviousState()
} }
} else { } else {
Timber.d("Restore on boot disabled, skipping") Timber.d("Restore on boot disabled, skipping")
} }
} }
} }
} }
@@ -9,38 +9,42 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.StringValue
interface NotificationManager { interface NotificationManager {
val context: Context val context: Context
fun createNotification(
channel: NotificationChannels,
title: String = "",
actions: Collection<NotificationCompat.Action> = emptyList(),
description: String = "",
showTimestamp: Boolean = false,
importance: Int = NotificationManager.IMPORTANCE_HIGH,
onGoing: Boolean = true,
onlyAlertOnce: Boolean = true,
): Notification
fun createNotification( fun createNotification(
channel: NotificationChannels, channel: NotificationChannels,
title: StringValue, title: String = "",
actions: Collection<NotificationCompat.Action> = emptyList(), actions: Collection<NotificationCompat.Action> = emptyList(),
description: StringValue, description: String = "",
showTimestamp: Boolean = false, showTimestamp: Boolean = false,
importance: Int = NotificationManager.IMPORTANCE_HIGH, importance: Int = NotificationManager.IMPORTANCE_HIGH,
onGoing: Boolean = true, onGoing: Boolean = true,
onlyAlertOnce: Boolean = true, onlyAlertOnce: Boolean = true,
): Notification ): Notification
fun createNotificationAction(notificationAction: NotificationAction, extraId: Int? = null): NotificationCompat.Action fun createNotification(
channel: NotificationChannels,
title: StringValue,
actions: Collection<NotificationCompat.Action> = emptyList(),
description: StringValue,
showTimestamp: Boolean = false,
importance: Int = NotificationManager.IMPORTANCE_HIGH,
onGoing: Boolean = true,
onlyAlertOnce: Boolean = true,
): Notification
fun remove(notificationId: Int) fun createNotificationAction(
notificationAction: NotificationAction,
extraId: Int? = null,
): NotificationCompat.Action
fun show(notificationId: Int, notification: Notification) fun remove(notificationId: Int)
companion object { fun show(notificationId: Int, notification: Notification)
const val AUTO_TUNNEL_NOTIFICATION_ID = 122
const val VPN_NOTIFICATION_ID = 100 companion object {
const val EXTRA_ID = "id" const val AUTO_TUNNEL_NOTIFICATION_ID = 122
} const val VPN_NOTIFICATION_ID = 100
const val EXTRA_ID = "id"
}
} }
@@ -21,149 +21,156 @@ import com.zaneschepke.wireguardautotunnel.util.StringValue
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject import javax.inject.Inject
class WireGuardNotification class WireGuardNotification @Inject constructor(@ApplicationContext override val context: Context) :
@Inject com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager {
constructor(
@ApplicationContext override val context: Context,
) : com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager {
enum class NotificationChannels { enum class NotificationChannels {
VPN, VPN,
AUTO_TUNNEL, AUTO_TUNNEL,
} }
private val notificationManager = NotificationManagerCompat.from(context) private val notificationManager = NotificationManagerCompat.from(context)
override fun createNotification( override fun createNotification(
channel: NotificationChannels, channel: NotificationChannels,
title: String, title: String,
actions: Collection<NotificationCompat.Action>, actions: Collection<NotificationCompat.Action>,
description: String, description: String,
showTimestamp: Boolean, showTimestamp: Boolean,
importance: Int, importance: Int,
onGoing: Boolean, onGoing: Boolean,
onlyAlertOnce: Boolean, onlyAlertOnce: Boolean,
): Notification { ): Notification {
notificationManager.createNotificationChannel(channel.asChannel()) notificationManager.createNotificationChannel(channel.asChannel())
return channel.asBuilder().apply { return channel
actions.forEach { .asBuilder()
addAction(it) .apply {
} actions.forEach { addAction(it) }
setContentTitle(title) setContentTitle(title)
setContentIntent( setContentIntent(
PendingIntent.getActivity( PendingIntent.getActivity(
context, context,
0, 0,
Intent(context, MainActivity::class.java), Intent(context, MainActivity::class.java),
PendingIntent.FLAG_IMMUTABLE, PendingIntent.FLAG_IMMUTABLE,
), )
) )
setContentText(description) setContentText(description)
setOnlyAlertOnce(onlyAlertOnce) setOnlyAlertOnce(onlyAlertOnce)
setOngoing(onGoing) setOngoing(onGoing)
setPriority(NotificationCompat.PRIORITY_HIGH) setPriority(NotificationCompat.PRIORITY_HIGH)
setShowWhen(showTimestamp) setShowWhen(showTimestamp)
setSmallIcon(R.drawable.ic_launcher) setSmallIcon(R.drawable.ic_notification)
}.build() }
} .build()
}
override fun createNotification( override fun createNotification(
channel: NotificationChannels, channel: NotificationChannels,
title: StringValue, title: StringValue,
actions: Collection<NotificationCompat.Action>, actions: Collection<NotificationCompat.Action>,
description: StringValue, description: StringValue,
showTimestamp: Boolean, showTimestamp: Boolean,
importance: Int, importance: Int,
onGoing: Boolean, onGoing: Boolean,
onlyAlertOnce: Boolean, onlyAlertOnce: Boolean,
): Notification { ): Notification {
return createNotification( return createNotification(
channel, channel,
title.asString(context), title.asString(context),
actions, actions,
description.asString(context), description.asString(context),
showTimestamp, showTimestamp,
importance, importance,
onGoing, onGoing,
onlyAlertOnce, onlyAlertOnce,
) )
} }
override fun createNotificationAction(notificationAction: NotificationAction, extraId: Int?): NotificationCompat.Action { override fun createNotificationAction(
val pendingIntent = PendingIntent.getBroadcast( notificationAction: NotificationAction,
context, extraId: Int?,
0, ): NotificationCompat.Action {
Intent(context, NotificationActionReceiver::class.java).apply { val pendingIntent =
action = notificationAction.name PendingIntent.getBroadcast(
if (extraId != null) putExtra(EXTRA_ID, extraId) context,
}, 0,
PendingIntent.FLAG_IMMUTABLE, Intent(context, NotificationActionReceiver::class.java).apply {
) action = notificationAction.name
return NotificationCompat.Action.Builder( if (extraId != null) putExtra(EXTRA_ID, extraId)
R.drawable.ic_launcher, },
notificationAction.title(context).uppercase(), PendingIntent.FLAG_IMMUTABLE,
pendingIntent, )
).build() return NotificationCompat.Action.Builder(
} R.drawable.ic_notification,
notificationAction.title(context).uppercase(),
pendingIntent,
)
.build()
}
override fun remove(notificationId: Int) { override fun remove(notificationId: Int) {
notificationManager.cancel(notificationId) notificationManager.cancel(notificationId)
} }
override fun show(notificationId: Int, notification: Notification) { override fun show(notificationId: Int, notification: Notification) {
with(notificationManager) { with(notificationManager) {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { if (
return ActivityCompat.checkSelfPermission(
} context,
notify(notificationId, notification) Manifest.permission.POST_NOTIFICATIONS,
} ) != PackageManager.PERMISSION_GRANTED
} ) {
return
}
notify(notificationId, notification)
}
}
private fun NotificationChannels.asBuilder(): NotificationCompat.Builder { private fun NotificationChannels.asBuilder(): NotificationCompat.Builder {
return when (this) { return when (this) {
NotificationChannels.AUTO_TUNNEL -> { NotificationChannels.AUTO_TUNNEL -> {
NotificationCompat.Builder( NotificationCompat.Builder(
context, context,
context.getString(R.string.auto_tunnel_channel_id), context.getString(R.string.auto_tunnel_channel_id),
) )
} }
NotificationChannels.VPN -> { NotificationChannels.VPN -> {
NotificationCompat.Builder( NotificationCompat.Builder(context, context.getString(R.string.vpn_channel_id))
context, }
context.getString(R.string.vpn_channel_id), }
) }
}
}
}
private fun NotificationChannels.asChannel(): NotificationChannel { private fun NotificationChannels.asChannel(): NotificationChannel {
return when (this) { return when (this) {
NotificationChannels.VPN -> { NotificationChannels.VPN -> {
NotificationChannel( NotificationChannel(
context.getString(R.string.vpn_channel_id), context.getString(R.string.vpn_channel_id),
context.getString(R.string.vpn_channel_name), context.getString(R.string.vpn_channel_name),
NotificationManager.IMPORTANCE_HIGH, NotificationManager.IMPORTANCE_HIGH,
).apply { )
description = context.getString(R.string.vpn_channel_description) .apply {
enableLights(true) description = context.getString(R.string.vpn_channel_description)
lightColor = Color.WHITE enableLights(true)
enableVibration(false) lightColor = Color.WHITE
vibrationPattern = longArrayOf(100, 200, 300) enableVibration(false)
} vibrationPattern = longArrayOf(100, 200, 300)
} }
NotificationChannels.AUTO_TUNNEL -> { }
NotificationChannel( NotificationChannels.AUTO_TUNNEL -> {
context.getString(R.string.auto_tunnel_channel_id), NotificationChannel(
context.getString(R.string.auto_tunnel_channel_name), context.getString(R.string.auto_tunnel_channel_id),
NotificationManager.IMPORTANCE_HIGH, context.getString(R.string.auto_tunnel_channel_name),
).apply { NotificationManager.IMPORTANCE_HIGH,
description = context.getString(R.string.auto_tunnel_channel_description) )
enableLights(true) .apply {
lightColor = Color.WHITE description = context.getString(R.string.auto_tunnel_channel_description)
enableVibration(false) enableLights(true)
vibrationPattern = longArrayOf(100, 200, 300) lightColor = Color.WHITE
} enableVibration(false)
} vibrationPattern = longArrayOf(100, 200, 300)
} }
} }
}
}
} }
@@ -25,106 +25,110 @@ import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
class ServiceManager @Inject constructor( class ServiceManager
private val context: Context, @Inject
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, constructor(
@ApplicationScope private val applicationScope: CoroutineScope, private val context: Context,
@MainDispatcher private val mainDispatcher: CoroutineDispatcher, @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
private val appDataRepository: AppDataRepository, @ApplicationScope private val applicationScope: CoroutineScope,
@MainDispatcher private val mainDispatcher: CoroutineDispatcher,
private val appDataRepository: AppDataRepository,
) { ) {
private val autoTunnelMutex = Mutex() private val autoTunnelMutex = Mutex()
private val _autoTunnelActive = MutableStateFlow(false) private val _autoTunnelActive = MutableStateFlow(false)
val autoTunnelActive = _autoTunnelActive.asStateFlow() val autoTunnelActive = _autoTunnelActive.asStateFlow()
var autoTunnelService = CompletableDeferred<AutoTunnelService>() var autoTunnelService = CompletableDeferred<AutoTunnelService>()
var backgroundService = CompletableDeferred<TunnelForegroundService>() var backgroundService = CompletableDeferred<TunnelForegroundService>()
private fun <T : Service> startService(cls: Class<T>, background: Boolean) { private fun <T : Service> startService(cls: Class<T>, background: Boolean) {
runCatching { runCatching {
val intent = Intent(context, cls) val intent = Intent(context, cls)
if (background) { if (background) {
context.startForegroundService(intent) context.startForegroundService(intent)
} else { } else {
context.startService(intent) context.startService(intent)
} }
}.onFailure { Timber.e(it) } }
} .onFailure { Timber.e(it) }
}
fun hasVpnPermission(): Boolean { fun hasVpnPermission(): Boolean {
return VpnService.prepare(context) == null return VpnService.prepare(context) == null
} }
suspend fun startAutoTunnel() { suspend fun startAutoTunnel() {
autoTunnelMutex.withLock { autoTunnelMutex.withLock {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true)) appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
if (autoTunnelService.isCompleted) { if (autoTunnelService.isCompleted) {
_autoTunnelActive.update { true } _autoTunnelActive.update { true }
return return
} }
runCatching { runCatching {
autoTunnelService = CompletableDeferred() autoTunnelService = CompletableDeferred()
startService(AutoTunnelService::class.java, !WireGuardAutoTunnel.isForeground()) startService(AutoTunnelService::class.java, !WireGuardAutoTunnel.isForeground())
_autoTunnelActive.update { true } _autoTunnelActive.update { true }
}.onFailure { }
Timber.e(it) .onFailure {
_autoTunnelActive.update { false } Timber.e(it)
} _autoTunnelActive.update { false }
withContext(mainDispatcher) { updateAutoTunnelTile() } }
} withContext(mainDispatcher) { updateAutoTunnelTile() }
} }
}
suspend fun stopAutoTunnel() { suspend fun stopAutoTunnel() {
autoTunnelMutex.withLock { autoTunnelMutex.withLock {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false)) appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
if (!autoTunnelService.isCompleted) return if (!autoTunnelService.isCompleted) return
runCatching { runCatching {
val service = autoTunnelService.await() val service = autoTunnelService.await()
service.stop() service.stop()
_autoTunnelActive.update { false } _autoTunnelActive.update { false }
autoTunnelService = CompletableDeferred() autoTunnelService = CompletableDeferred()
}.onFailure { }
Timber.e(it) .onFailure { Timber.e(it) }
} withContext(mainDispatcher) { updateAutoTunnelTile() }
withContext(mainDispatcher) { updateAutoTunnelTile() } }
} }
}
fun startTunnelForegroundService() { fun startTunnelForegroundService() {
if (backgroundService.isCompleted) return if (backgroundService.isCompleted) return
runCatching { runCatching {
backgroundService = CompletableDeferred() backgroundService = CompletableDeferred()
startService(TunnelForegroundService::class.java, !WireGuardAutoTunnel.isForeground()) startService(
}.onFailure { TunnelForegroundService::class.java,
Timber.e(it) !WireGuardAutoTunnel.isForeground(),
} )
} }
.onFailure { Timber.e(it) }
}
suspend fun stopTunnelForegroundService() { suspend fun stopTunnelForegroundService() {
if (!backgroundService.isCompleted) return if (!backgroundService.isCompleted) return
runCatching { runCatching {
val service = backgroundService.await() val service = backgroundService.await()
service.stop() service.stop()
backgroundService = CompletableDeferred() backgroundService = CompletableDeferred()
}.onFailure { }
Timber.e(it) .onFailure { Timber.e(it) }
} }
}
fun toggleAutoTunnel() { fun toggleAutoTunnel() {
applicationScope.launch(ioDispatcher) { applicationScope.launch(ioDispatcher) {
if (_autoTunnelActive.value) stopAutoTunnel() else startAutoTunnel() if (_autoTunnelActive.value) stopAutoTunnel() else startAutoTunnel()
} }
} }
fun updateAutoTunnelTile() { fun updateAutoTunnelTile() {
context.requestAutoTunnelTileServiceUpdate() context.requestAutoTunnelTileServiceUpdate()
} }
fun updateTunnelTile() { fun updateTunnelTile() {
context.requestTunnelTileServiceStateUpdate() context.requestTunnelTileServiceStateUpdate()
} }
} }
@@ -17,9 +17,12 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository import com.zaneschepke.wireguardautotunnel.domain.repository.TunnelRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
@@ -34,239 +37,286 @@ import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class TunnelForegroundService : LifecycleService() { class TunnelForegroundService : LifecycleService() {
@Inject @Inject lateinit var notificationManager: NotificationManager
lateinit var notificationManager: NotificationManager
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var networkMonitor: NetworkMonitor
lateinit var networkMonitor: NetworkMonitor
@Inject @Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
@Inject @Inject lateinit var tunnelRepo: TunnelRepository
lateinit var tunnelRepo: TunnelRepository
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
private val isNetworkConnected = MutableStateFlow(true) private val isNetworkConnected = MutableStateFlow(true)
private val tunnelJobs = ConcurrentHashMap<TunnelConf, Job>() private val tunnelJobs = ConcurrentHashMap<TunnelConf, Job>()
private val pingJobs = ConcurrentHashMap<TunnelConf, Job>()
override fun onCreate() { private val jobsMutex = Mutex()
super.onCreate()
serviceManager.backgroundService.complete(this)
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
override fun onBind(intent: Intent): IBinder? { override fun onCreate() {
super.onBind(intent) super.onCreate()
return null serviceManager.backgroundService.complete(this)
} ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onBind(intent: Intent): IBinder? {
super.onStartCommand(intent, flags, startId) super.onBind(intent)
serviceManager.backgroundService.complete(this) return null
ServiceCompat.startForeground( }
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
start()
return START_STICKY
}
fun start() = lifecycleScope.launch { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
tunnelManager.activeTunnels.distinctByKeys().collect { tuns -> super.onStartCommand(intent, flags, startId)
if (tuns.isEmpty() && tunnelJobs.isEmpty()) return@collect serviceManager.backgroundService.complete(this)
if (tuns.isEmpty() && tunnelJobs.isNotEmpty()) { ServiceCompat.startForeground(
return@collect tunnelJobs.forEach { (key, _) -> this@TunnelForegroundService,
Timber.d("Stopping all tunnel jobs") NotificationManager.VPN_NOTIFICATION_ID,
tunnelJobs[key]?.cancel() onCreateNotification(),
tunnelJobs.remove(key) Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
} )
} start()
val (jobsToStop, jobsToStart) = findMissingKeys(tuns, tunnelJobs) return START_STICKY
if (jobsToStop.isEmpty() && jobsToStart.isEmpty()) return@collect }
jobsToStop.forEach { tun ->
Timber.d("Stopping tunnel jobs for ${tun.tunName}")
tunnelJobs[tun]?.cancel()
tunnelJobs.remove(tun)
}
jobsToStart.forEach { tun ->
Timber.d("Starting tunnel jobs for ${tun.tunName}")
tunnelJobs += (tun to startTunnelJobs(tun))
}
updateServiceNotification()
}
}
// TODO Would be cool to have this include kill switch fun start() =
// TODO also we need to include errors lifecycleScope.launch(ioDispatcher) {
private fun updateServiceNotification() { tunnelManager.activeTunnels.distinctByKeys().collect { activeTunnels ->
val notification = when (tunnelJobs.size) { // No active tunnels and no jobs: nothing to do
0 -> onCreateNotification() if (activeTunnels.isEmpty() && tunnelJobs.isEmpty()) return@collect
1 -> createTunnelNotification(tunnelJobs.keys.first())
else -> createTunnelsNotification()
}
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
// use same scope so we can cancel all of these // Synchronize jobs with active tunnels
private fun startTunnelJobs(tunnelConf: TunnelConf) = lifecycleScope.launch { synchronizeJobs(activeTunnels)
// monitor if we have internet connectivity updateServiceNotification()
launch { startNetworkMonitorJob() } }
// job to trigger stats emit on interval }
launch { startTunnelStatsJob(tunnelConf) }
// monitor changes to the tunnel config
launch { startTunnelConfChangesJob(tunnelConf) }
// monitor tunnel ping
launch { startPingJob(tunnelConf) }
}
private fun findMissingKeys(map1: Map<TunnelConf, Any>, map2: Map<TunnelConf, Any>): Pair<Set<TunnelConf>, Set<TunnelConf>> { private suspend fun synchronizeJobs(activeTunnels: Map<TunnelConf, TunnelState>) {
val missingMap1 = map2.keys - map1.keys jobsMutex.withLock {
val missingMap2 = map1.keys - map2.keys // Stop jobs for tunnels that are no longer active
return missingMap1 to missingMap2 stopInactiveJobs(activeTunnels)
} // Start jobs for new tunnels
startNewJobs(activeTunnels)
}
}
private suspend fun startTunnelConfChangesJob(tunnelConf: TunnelConf) { private fun stopInactiveJobs(activeTunnels: Map<TunnelConf, TunnelState>) {
tunnelRepo.flow // If no active tunnels, clear all jobs
.flowOn(ioDispatcher) if (activeTunnels.isEmpty()) {
.map { storedTunnels -> clearAllJobs()
storedTunnels.firstOrNull { it.id == tunnelConf.id } return
} }
.filterNotNull() // Stop jobs for tunnels not in activeTunnels
// only emit when one of these 3 values change val tunnelsToStop = tunnelJobs.keys - activeTunnels.keys
.distinctUntilChanged { old, new -> tunnelsToStop.forEach { tun -> stopTunnelJobs(tun) }
old == new }
}
.collect { storedTunnel ->
if (tunnelConf != storedTunnel) {
Timber.d("Config changed for ${storedTunnel.tunName}, bouncing")
// let this complete, even after cancel
withContext(NonCancellable) {
tunnelManager.bounceTunnel(storedTunnel, TunnelStatus.StopReason.CONFIG_CHANGED)
}
}
}
}
private suspend fun startNetworkMonitorJob() { private fun clearAllJobs() {
networkMonitor.networkStatusFlow tunnelJobs.forEach { (tun, job) ->
.flowOn(ioDispatcher) Timber.d("Stopping tunnel job for ${tun.tunName}")
.collectLatest { status -> job.cancel()
val isAvailable = status !is NetworkStatus.Disconnected }
isNetworkConnected.value = isAvailable tunnelJobs.clear()
Timber.d("Network available: $status")
}
}
private suspend fun startTunnelStatsJob(tunnel: TunnelConf) = coroutineScope { pingJobs.forEach { (tun, job) ->
while (isActive) { if (isPingBounce(tun)) {
tunnelManager.updateTunnelStatistics(tunnel) Timber.d("Preserving ping job for ${tun.tunName} due to PING bounce")
delay(STATS_DELAY) return@forEach
} }
} Timber.d("Stopping ping job for ${tun.tunName}")
job.cancel()
}
pingJobs.entries.removeIf { (tun, _) -> !isPingBounce(tun) }
}
// TODO fix cooldown private fun stopTunnelJobs(tun: TunnelConf) {
private suspend fun startPingJob(tunnel: TunnelConf) = coroutineScope { tunnelJobs.remove(tun)?.cancel()
delay(PING_START_DELAY) Timber.d("Stopped tunnel job for ${tun.tunName}")
while (isActive) { if (isPingBounce(tun))
val shouldBounce = shouldBounceTunnel(tunnel) return Timber.d("Preserving ${tun.tunName} ping job due to ping bounce")
val delayMs = if (shouldBounce) { pingJobs.remove(tun)?.cancel()
// let this complete, even after cancel Timber.d("Stopped ping job for ${tun.tunName}")
withContext(NonCancellable) { }
tunnelManager.bounceTunnel(tunnel, TunnelStatus.StopReason.PING)
}
tunnel.pingCooldown ?: Constants.PING_COOLDOWN
} else {
tunnel.pingInterval ?: Constants.PING_INTERVAL
}
delay(delayMs)
}
}
private suspend fun shouldBounceTunnel(tunnel: TunnelConf): Boolean { private fun startNewJobs(activeTunnels: Map<TunnelConf, TunnelState>) {
if (!isNetworkConnected.value) { val tunnelsToStart = activeTunnels.keys - tunnelJobs.keys
Timber.d("Network disconnected, skipping ping for ${tunnel.tunName}") tunnelsToStart.forEach { tun ->
return false tunnelJobs[tun] = startTunnelJobs(tun)
} Timber.d("Started tunnel job for ${tun.tunName}")
return runCatching {
!tunnel.isTunnelPingable(ioDispatcher)
}.onFailure { e ->
Timber.e(e, "Ping check failed for ${tunnel.tunName}")
}.getOrDefault(true)
}
fun stop() { if (pingJobs[tun]?.isActive == true) {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE) Timber.d("Reusing active ping job for ${tun.tunName}")
stopSelf() } else {
} pingJobs[tun]?.cancel() // Cancel any stale job
if (tun.isPingEnabled) {
pingJobs[tun] = startPingJob(tun)
Timber.d("Started ping job for ${tun.tunName}")
}
}
}
}
override fun onDestroy() { private fun isPingBounce(tun: TunnelConf): Boolean =
serviceManager.backgroundService = CompletableDeferred() tunnelManager.bouncingTunnelIds[tun.id] == TunnelStatus.StopReason.PING
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
super.onDestroy()
}
private fun createTunnelNotification(tunnelConf: TunnelConf): Notification { // TODO Would be cool to have this include kill switch
return notificationManager.createNotification( // TODO also we need to include errors
WireGuardNotification.NotificationChannels.VPN, private fun updateServiceNotification() {
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}", val notification =
actions = listOf( when (tunnelJobs.size) {
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, tunnelConf.id), 0 -> onCreateNotification()
), 1 -> createTunnelNotification(tunnelJobs.keys.first())
) else -> createTunnelsNotification()
} }
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun createTunnelsNotification(): Notification { // use same scope so we can cancel all of these
return notificationManager.createNotification( private fun startTunnelJobs(tunnelConf: TunnelConf) =
WireGuardNotification.NotificationChannels.VPN, lifecycleScope.launch(ioDispatcher) {
title = "${getString(R.string.tunnel_running)} - ${getString(R.string.multiple)}", // monitor if we have internet connectivity
actions = listOf( launch { startNetworkMonitorJob() }
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0), // job to trigger stats emit on interval
), launch { startTunnelStatsJob(tunnelConf) }
) // monitor changes to the tunnel config
} launch { startTunnelConfChangesJob(tunnelConf) }
}
private fun onCreateNotification(): Notification { private suspend fun startTunnelConfChangesJob(tunnelConf: TunnelConf) {
return notificationManager.createNotification( tunnelRepo.flow
WireGuardNotification.NotificationChannels.VPN, .flowOn(ioDispatcher)
title = getString(R.string.tunnel_starting), .map { storedTunnels -> storedTunnels.firstOrNull { it.id == tunnelConf.id } }
) .filterNotNull()
} // only emit when one of these 3 values change
.distinctUntilChanged { old, new -> old == new }
.collect { storedTunnel ->
if (tunnelConf != storedTunnel) {
Timber.d("Config changed for ${storedTunnel.tunName}, bouncing")
// let this complete, even after cancel
withContext(NonCancellable) {
tunnelManager.bounceTunnel(
storedTunnel,
TunnelStatus.StopReason.CONFIG_CHANGED,
)
}
}
}
}
// TODO add notification handling and optional log reading for restart on handshake failures private suspend fun startNetworkMonitorJob() {
companion object { networkMonitor.networkStatusFlow.flowOn(ioDispatcher).collectLatest { status ->
const val STATS_DELAY = 1_000L val isAvailable = status !is NetworkStatus.Disconnected
const val PING_START_DELAY = 30_000L isNetworkConnected.value = isAvailable
// ipv6 disabled or block on network Timber.d("Network available: $status")
// const val userspaceStartFailed = "Failed to send handshake initiation: write udp [::]" }
// const val ipv6Fails = "Failed to send data packets: write udp [::]" }
// const val ipv4Fails = "Failed to send data packets: write udp 0.0.0.0:51820"
} private suspend fun startTunnelStatsJob(tunnel: TunnelConf) = coroutineScope {
while (isActive) {
tunnelManager.updateTunnelStatistics(tunnel)
delay(STATS_DELAY)
}
}
private fun startPingJob(tunnel: TunnelConf) =
lifecycleScope.launch(ioDispatcher) {
// delay for initial duration
delay(tunnel.pingInterval ?: Constants.PING_INTERVAL)
while (isActive) {
val shouldBounce = shouldBounceTunnel(tunnel)
val delayMs =
if (shouldBounce) {
// let this complete, even after cancel
withContext(NonCancellable) {
tunnelManager.bounceTunnel(tunnel, TunnelStatus.StopReason.PING)
}
tunnel.pingCooldown ?: Constants.PING_COOLDOWN
} else {
tunnel.pingInterval ?: Constants.PING_INTERVAL
}
delay(delayMs)
}
}
private suspend fun shouldBounceTunnel(tunnel: TunnelConf): Boolean {
if (!isNetworkConnected.value) {
Timber.d("Network disconnected, skipping ping for ${tunnel.tunName}")
return false
}
return runCatching { !tunnel.isTunnelPingable(ioDispatcher) }
.onFailure { e -> Timber.e(e, "Ping check failed for ${tunnel.tunName}") }
.getOrDefault(true)
}
fun stop() {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()
}
override fun onDestroy() {
serviceManager.backgroundService = CompletableDeferred()
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
super.onDestroy()
}
private fun createTunnelNotification(tunnelConf: TunnelConf): Notification {
return notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN,
title = "${getString(R.string.tunnel_running)} - ${tunnelConf.tunName}",
actions =
listOf(
notificationManager.createNotificationAction(
NotificationAction.TUNNEL_OFF,
tunnelConf.id,
)
),
)
}
private fun createTunnelsNotification(): Notification {
return notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN,
title = "${getString(R.string.tunnel_running)} - ${getString(R.string.multiple)}",
actions =
listOf(
notificationManager.createNotificationAction(NotificationAction.TUNNEL_OFF, 0)
),
)
}
private fun onCreateNotification(): Notification {
return notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN,
title = getString(R.string.tunnel_starting),
)
}
// TODO add notification handling and optional log reading for restart on handshake failures
companion object {
const val STATS_DELAY = 1_000L
// ipv6 disabled or block on network
// Failed to send handshake initiation: write udp [::]"
// Failed to send data packets: write udp [::]
// Failed to send data packets: write udp 0.0.0.0:51820
// Handshake did not complete after 5 seconds, retrying
}
} }
@@ -26,6 +26,8 @@ import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import javax.inject.Provider
import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -42,216 +44,233 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
@AndroidEntryPoint @AndroidEntryPoint
class AutoTunnelService : LifecycleService() { class AutoTunnelService : LifecycleService() {
@Inject @Inject lateinit var networkMonitor: NetworkMonitor
lateinit var networkMonitor: NetworkMonitor
@Inject @Inject lateinit var appDataRepository: Provider<AppDataRepository>
lateinit var appDataRepository: Provider<AppDataRepository>
@Inject @Inject lateinit var notificationManager: NotificationManager
lateinit var notificationManager: NotificationManager
@Inject @Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@IoDispatcher
lateinit var ioDispatcher: CoroutineDispatcher
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
private val defaultState = AutoTunnelState() private val defaultState = AutoTunnelState()
private val autoTunnelStateFlow = MutableStateFlow(defaultState) private val autoTunnelStateFlow = MutableStateFlow(defaultState)
private var wakeLock: PowerManager.WakeLock? = null private var wakeLock: PowerManager.WakeLock? = null
private var killSwitchJob: Job? = null private var killSwitchJob: Job? = null
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
serviceManager.autoTunnelService.complete(this) serviceManager.autoTunnelService.complete(this)
launchWatcherNotification() launchWatcherNotification()
} }
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
super.onBind(intent) super.onBind(intent)
return null return null
} }
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) super.onStartCommand(intent, flags, startId)
Timber.d("onStartCommand executed with startId: $startId") Timber.d("onStartCommand executed with startId: $startId")
serviceManager.autoTunnelService.complete(this) serviceManager.autoTunnelService.complete(this)
start() start()
return START_STICKY return START_STICKY
} }
fun start() { fun start() {
kotlin.runCatching { kotlin
launchWatcherNotification() .runCatching {
initWakeLock() launchWatcherNotification()
startAutoTunnelJob() initWakeLock()
startAutoTunnelStateJob() startAutoTunnelJob()
killSwitchJob = startKillSwitchJob() startAutoTunnelStateJob()
}.onFailure { killSwitchJob = startKillSwitchJob()
Timber.e(it) }
} .onFailure { Timber.e(it) }
} }
fun stop() { fun stop() {
wakeLock?.let { if (it.isHeld) it.release() } wakeLock?.let { if (it.isHeld) it.release() }
stopSelf() stopSelf()
} }
override fun onDestroy() { override fun onDestroy() {
serviceManager.autoTunnelService = CompletableDeferred() serviceManager.autoTunnelService = CompletableDeferred()
restoreVpnKillSwitch() restoreVpnKillSwitch()
super.onDestroy() super.onDestroy()
} }
private fun restoreVpnKillSwitch() { private fun restoreVpnKillSwitch() {
with(autoTunnelStateFlow.value) { with(autoTunnelStateFlow.value) {
if (settings.isVpnKillSwitchEnabled && tunnelManager.getBackendState() != BackendState.KILL_SWITCH_ACTIVE) { if (
killSwitchJob?.cancel() settings.isVpnKillSwitchEnabled &&
val allowedIps = if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList() tunnelManager.getBackendState() != BackendState.KILL_SWITCH_ACTIVE
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps) ) {
} killSwitchJob?.cancel()
} val allowedIps =
} if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
else emptyList()
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, allowedIps)
}
}
}
private fun launchWatcherNotification(description: String = getString(R.string.monitoring_state_changes)) { private fun launchWatcherNotification(
val notification = description: String = getString(R.string.monitoring_state_changes)
notificationManager.createNotification( ) {
WireGuardNotification.NotificationChannels.AUTO_TUNNEL, val notification =
title = getString(R.string.auto_tunnel_title), notificationManager.createNotification(
description = description, WireGuardNotification.NotificationChannels.AUTO_TUNNEL,
actions = listOf( title = getString(R.string.auto_tunnel_title),
notificationManager.createNotificationAction(NotificationAction.AUTO_TUNNEL_OFF), description = description,
), actions =
) listOf(
ServiceCompat.startForeground( notificationManager.createNotificationAction(
this, NotificationAction.AUTO_TUNNEL_OFF
NotificationManager.AUTO_TUNNEL_NOTIFICATION_ID, )
notification, ),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID, )
) ServiceCompat.startForeground(
} this,
NotificationManager.AUTO_TUNNEL_NOTIFICATION_ID,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
private fun initWakeLock() { private fun initWakeLock() {
wakeLock = (getSystemService(POWER_SERVICE) as PowerManager).run { wakeLock =
val tag = this.javaClass.name (getSystemService(POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply { val tag = this.javaClass.name
try { newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "$tag::lock").apply {
Timber.i("Initiating wakelock with 10 min timeout") try {
acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT) Timber.i("Initiating wakelock with 10 min timeout")
} finally { acquire(Constants.BATTERY_SAVER_WATCHER_WAKE_LOCK_TIMEOUT)
release() } finally {
} release()
} }
} }
} }
}
private fun buildNetworkState(networkStatus: NetworkStatus): NetworkState { private fun buildNetworkState(networkStatus: NetworkStatus): NetworkState {
return with(autoTunnelStateFlow.value.networkState) { return with(autoTunnelStateFlow.value.networkState) {
val wifiName = when (networkStatus) { val wifiName =
is NetworkStatus.Connected -> { when (networkStatus) {
networkStatus.wifiSsid is NetworkStatus.Connected -> {
} networkStatus.wifiSsid
else -> null }
} else -> null
copy( }
isWifiConnected = networkStatus.wifiConnected, copy(
isMobileDataConnected = networkStatus.cellularConnected, isWifiConnected = networkStatus.wifiConnected,
isEthernetConnected = networkStatus.ethernetConnected, isMobileDataConnected = networkStatus.cellularConnected,
wifiName = wifiName, isEthernetConnected = networkStatus.ethernetConnected,
) wifiName = wifiName,
} )
} }
}
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
private fun startAutoTunnelStateJob() = lifecycleScope.launch(ioDispatcher) { private fun startAutoTunnelStateJob() =
combine( lifecycleScope.launch(ioDispatcher) {
combineSettings(), combine(
appDataRepository.get().settings.flow combineSettings(),
.distinctUntilChanged { old, new -> old.isKernelEnabled == new.isKernelEnabled } // Only emit when isKernelEnabled changes appDataRepository
.flatMapLatest { .get()
networkMonitor.networkStatusFlow .settings
.flowOn(ioDispatcher) .flow
.map { buildNetworkState(it) } .distinctUntilChanged { old, new ->
} old.isKernelEnabled == new.isKernelEnabled
.distinctUntilChanged(), } // Only emit when isKernelEnabled changes
) { double, networkState -> .flatMapLatest {
AutoTunnelState( networkMonitor.networkStatusFlow.flowOn(ioDispatcher).map {
tunnelManager.activeTunnels.value, buildNetworkState(it)
networkState, }
double.first, }
double.second, .distinctUntilChanged(),
) ) { double, networkState ->
}.collect { state -> AutoTunnelState(
autoTunnelStateFlow.update { tunnelManager.activeTunnels.value,
it.copy( networkState,
activeTunnels = state.activeTunnels, double.first,
networkState = state.networkState, double.second,
settings = state.settings, )
tunnels = state.tunnels, }
) .collect { state ->
} autoTunnelStateFlow.update {
} it.copy(
} activeTunnels = state.activeTunnels,
networkState = state.networkState,
settings = state.settings,
tunnels = state.tunnels,
)
}
}
}
private fun combineSettings(): Flow<Pair<AppSettings, Tunnels>> { private fun combineSettings(): Flow<Pair<AppSettings, Tunnels>> {
return combine( return combine(
appDataRepository.get().settings.flow, appDataRepository.get().settings.flow,
appDataRepository.get().tunnels.flow.map { tunnels -> appDataRepository.get().tunnels.flow.map { tunnels ->
// isActive is ignored for equality checks so user can manually toggle off tunnel with auto-tunnel // isActive is ignored for equality checks so user can manually toggle off
tunnels.map { it.copy(isActive = false) } // tunnel with auto-tunnel
}, tunnels.map { it.copy(isActive = false) }
) { settings, tunnels -> },
Pair(settings, tunnels) ) { settings, tunnels ->
}.distinctUntilChanged() Pair(settings, tunnels)
} }
.distinctUntilChanged()
}
private fun startKillSwitchJob() = lifecycleScope.launch(ioDispatcher) { private fun startKillSwitchJob() =
autoTunnelStateFlow.collect { lifecycleScope.launch(ioDispatcher) {
if (it == defaultState) return@collect autoTunnelStateFlow.collect {
when (val event = it.asKillSwitchEvent()) { if (it == defaultState) return@collect
KillSwitchEvent.DoNothing -> Unit when (val event = it.asKillSwitchEvent()) {
is KillSwitchEvent.Start -> { KillSwitchEvent.DoNothing -> Unit
Timber.d("Starting kill switch") is KillSwitchEvent.Start -> {
tunnelManager.setBackendState(BackendState.KILL_SWITCH_ACTIVE, event.allowedIps) Timber.d("Starting kill switch")
} tunnelManager.setBackendState(
KillSwitchEvent.Stop -> { BackendState.KILL_SWITCH_ACTIVE,
Timber.d("Stopping kill switch") event.allowedIps,
tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptySet()) )
} }
} KillSwitchEvent.Stop -> {
} Timber.d("Stopping kill switch")
} tunnelManager.setBackendState(BackendState.SERVICE_ACTIVE, emptySet())
}
}
}
}
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
private fun startAutoTunnelJob() = lifecycleScope.launch(ioDispatcher) { private fun startAutoTunnelJob() =
Timber.i("Starting auto-tunnel network event watcher") lifecycleScope.launch(ioDispatcher) {
val settings = appDataRepository.get().settings.get() Timber.i("Starting auto-tunnel network event watcher")
Timber.d("Starting with debounce delay of: ${settings.debounceDelaySeconds} seconds") val settings = appDataRepository.get().settings.get()
autoTunnelStateFlow.debounce(settings.debounceDelayMillis()).collect { watcherState -> Timber.d("Starting with debounce delay of: ${settings.debounceDelaySeconds} seconds")
if (watcherState == defaultState) return@collect autoTunnelStateFlow.debounce(settings.debounceDelayMillis()).collect { watcherState ->
Timber.d("New auto tunnel state emitted ${watcherState.networkState}") if (watcherState == defaultState) return@collect
when (val event = watcherState.asAutoTunnelEvent()) { Timber.d("New auto tunnel state emitted ${watcherState.networkState}")
is AutoTunnelEvent.Start -> (event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())?.let { when (val event = watcherState.asAutoTunnelEvent()) {
tunnelManager.startTunnel(it) is AutoTunnelEvent.Start ->
} (event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())
// TODO improve this to target specific tunnels to better support multi-tunnel ?.let { tunnelManager.startTunnel(it) }
is AutoTunnelEvent.Stop -> tunnelManager.stopTunnel() // TODO improve this to target specific tunnels to better support multi-tunnel
AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: no condition met") is AutoTunnelEvent.Stop -> tunnelManager.stopTunnel()
} AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: no condition met")
} }
} }
}
} }
@@ -11,96 +11,94 @@ import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class AutoTunnelControlTile : TileService(), LifecycleOwner { class AutoTunnelControlTile : TileService(), LifecycleOwner {
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this) private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
} }
override fun onStartListening() { override fun onStartListening() {
super.onStartListening() super.onStartListening()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
Timber.d("Start listening called for auto tunnel tile") Timber.d("Start listening called for auto tunnel tile")
lifecycleScope.launch { lifecycleScope.launch {
serviceManager.autoTunnelActive.collect { serviceManager.autoTunnelActive.collect {
if (it) return@collect setActive() if (it) return@collect setActive()
setInactive() setInactive()
} }
} }
lifecycleScope.launch { lifecycleScope.launch {
appDataRepository.tunnels.flow.collect { appDataRepository.tunnels.flow.collect {
if (it.isEmpty()) { if (it.isEmpty()) {
setUnavailable() setUnavailable()
} }
} }
} }
} }
override fun onClick() { override fun onClick() {
super.onClick() super.onClick()
unlockAndRun { unlockAndRun {
lifecycleScope.launch { lifecycleScope.launch {
if (serviceManager.autoTunnelActive.value) { if (serviceManager.autoTunnelActive.value) {
serviceManager.stopAutoTunnel() serviceManager.stopAutoTunnel()
setInactive() setInactive()
} else { } else {
serviceManager.startAutoTunnel() serviceManager.startAutoTunnel()
setActive() setActive()
} }
} }
} }
} }
private fun setActive() { private fun setActive() {
runCatching { runCatching {
qsTile.state = Tile.STATE_ACTIVE qsTile.state = Tile.STATE_ACTIVE
qsTile.updateTile() qsTile.updateTile()
} }
} }
private fun setInactive() { private fun setInactive() {
runCatching { runCatching {
qsTile.state = Tile.STATE_INACTIVE qsTile.state = Tile.STATE_INACTIVE
qsTile.updateTile() qsTile.updateTile()
} }
} }
/* This works around an annoying unsolved frameworks bug some people are hitting. */ /* This works around an annoying unsolved frameworks bug some people are hitting. */
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
var ret: IBinder? = null var ret: IBinder? = null
try { try {
ret = super.onBind(intent) ret = super.onBind(intent)
} catch (_: Throwable) { } catch (_: Throwable) {
Timber.e("Failed to bind to TunnelControlTile") Timber.e("Failed to bind to TunnelControlTile")
} }
return ret return ret
} }
private fun setUnavailable() { private fun setUnavailable() {
runCatching { runCatching {
qsTile.state = Tile.STATE_UNAVAILABLE qsTile.state = Tile.STATE_UNAVAILABLE
qsTile.updateTile() qsTile.updateTile()
} }
} }
override val lifecycle: Lifecycle override val lifecycle: Lifecycle
get() = lifecycleRegistry get() = lifecycleRegistry
} }
@@ -17,175 +17,167 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class TunnelControlTile : TileService(), LifecycleOwner { class TunnelControlTile : TileService(), LifecycleOwner {
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this) private val lifecycleRegistry: LifecycleRegistry = LifecycleRegistry(this)
private var isCollecting = false private var isCollecting = false
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
} }
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
} }
override fun onStartListening() { override fun onStartListening() {
super.onStartListening() super.onStartListening()
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START) lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
Timber.d("Start listening called for tunnel tile") Timber.d("Start listening called for tunnel tile")
if (isCollecting) return if (isCollecting) return
isCollecting = true isCollecting = true
lifecycleScope.launch { lifecycleScope.launch { tunnelManager.activeTunnels.collect { updateTileState() } }
tunnelManager.activeTunnels.collect { }
updateTileState()
}
}
}
private suspend fun updateTileState() { private suspend fun updateTileState() {
try { try {
val tunnels = appDataRepository.tunnels.getAll() val tunnels = appDataRepository.tunnels.getAll()
if (tunnels.isEmpty()) { if (tunnels.isEmpty()) {
setUnavailable() setUnavailable()
return return
} }
val activeTunnels = tunnelManager.activeTunnels.value val activeTunnels =
.filter { it.value.status.isUpOrStarting() } tunnelManager.activeTunnels.value.filter { it.value.status.isUpOrStarting() }
when { when {
activeTunnels.isNotEmpty() -> { activeTunnels.isNotEmpty() -> {
val activeIds = activeTunnels.map { it.key.id } val activeIds = activeTunnels.map { it.key.id }
// TODO improvements would be needed to make this work well with toggling multiple tunnels // TODO improvements would be needed to make this work well with toggling
// this would be better managed elsewhere // multiple tunnels
WireGuardAutoTunnel.setLastActiveTunnels(activeIds) // this would be better managed elsewhere
updateTileForActiveTunnels(activeTunnels) WireGuardAutoTunnel.setLastActiveTunnels(activeIds)
} updateTileForActiveTunnels(activeTunnels)
else -> updateTileForLastActiveTunnels() }
} else -> updateTileForLastActiveTunnels()
} catch (e: Exception) { }
setUnavailable() } catch (e: Exception) {
} setUnavailable()
} }
}
private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) { private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) {
val tileName = when (activeTunnels.size) { val tileName =
1 -> activeTunnels.keys.first().tunName when (activeTunnels.size) {
else -> getString(R.string.multiple) 1 -> activeTunnels.keys.first().tunName
} else -> getString(R.string.multiple)
updateTile(tileName, true) }
} updateTile(tileName, true)
}
private suspend fun updateTileForLastActiveTunnels() { private suspend fun updateTileForLastActiveTunnels() {
val lastActiveIds = WireGuardAutoTunnel.getLastActiveTunnels() val lastActiveIds = WireGuardAutoTunnel.getLastActiveTunnels()
when { when {
lastActiveIds.isEmpty() -> { lastActiveIds.isEmpty() -> {
appDataRepository.getStartTunnelConfig()?.let { config -> appDataRepository.getStartTunnelConfig()?.let { config ->
updateTile(config.tunName, false) updateTile(config.tunName, false)
} ?: setUnavailable() } ?: setUnavailable()
} }
lastActiveIds.size > 1 -> updateTile(getString(R.string.multiple), false) lastActiveIds.size > 1 -> updateTile(getString(R.string.multiple), false)
else -> { else -> {
val tunnelId = lastActiveIds.first() val tunnelId = lastActiveIds.first()
appDataRepository.tunnels.getById(tunnelId)?.let { tunnel -> appDataRepository.tunnels.getById(tunnelId)?.let { tunnel ->
updateTile(tunnel.tunName, false) updateTile(tunnel.tunName, false)
} ?: setUnavailable() } ?: setUnavailable()
} }
} }
} }
override fun onClick() { override fun onClick() {
super.onClick() super.onClick()
unlockAndRun { unlockAndRun {
lifecycleScope.launch { lifecycleScope.launch {
if (tunnelManager.activeTunnels.value.isNotEmpty()) return@launch tunnelManager.stopTunnel() if (tunnelManager.activeTunnels.value.isNotEmpty())
val lastActive = WireGuardAutoTunnel.getLastActiveTunnels() return@launch tunnelManager.stopTunnel()
if (lastActive.isEmpty()) { val lastActive = WireGuardAutoTunnel.getLastActiveTunnels()
appDataRepository.getStartTunnelConfig()?.let { if (lastActive.isEmpty()) {
tunnelManager.startTunnel(it) appDataRepository.getStartTunnelConfig()?.let { tunnelManager.startTunnel(it) }
} } else {
} else { lastActive.forEach { id ->
lastActive.forEach { id -> appDataRepository.tunnels.getById(id)?.let { tunnelManager.startTunnel(it) }
appDataRepository.tunnels.getById(id)?.let { }
tunnelManager.startTunnel(it) }
} }
} }
} }
}
}
}
private fun setActive() { private fun setActive() {
runCatching { runCatching {
qsTile.state = Tile.STATE_ACTIVE qsTile.state = Tile.STATE_ACTIVE
qsTile.updateTile() qsTile.updateTile()
} }
} }
private fun setInactive() { private fun setInactive() {
runCatching { runCatching {
qsTile.state = Tile.STATE_INACTIVE qsTile.state = Tile.STATE_INACTIVE
qsTile.updateTile() qsTile.updateTile()
} }
} }
private fun setUnavailable() { private fun setUnavailable() {
runCatching { runCatching {
qsTile.state = Tile.STATE_UNAVAILABLE qsTile.state = Tile.STATE_UNAVAILABLE
setTileDescription("") setTileDescription("")
qsTile.updateTile() qsTile.updateTile()
} }
} }
private fun setTileDescription(description: String) { private fun setTileDescription(description: String) {
runCatching { runCatching {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
qsTile.subtitle = description qsTile.subtitle = description
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
qsTile.stateDescription = description qsTile.stateDescription = description
} }
qsTile.updateTile() qsTile.updateTile()
} }
} }
/* This works around an annoying unsolved frameworks bug some people are hitting. */ /* This works around an annoying unsolved frameworks bug some people are hitting. */
override fun onBind(intent: Intent): IBinder? { override fun onBind(intent: Intent): IBinder? {
var ret: IBinder? = null var ret: IBinder? = null
try { try {
ret = super.onBind(intent) ret = super.onBind(intent)
} catch (_: Throwable) { } catch (_: Throwable) {
Timber.e("Failed to bind to TunnelControlTile") Timber.e("Failed to bind to TunnelControlTile")
} }
return ret return ret
} }
private fun updateTile(name: String, active: Boolean) { private fun updateTile(name: String, active: Boolean) {
runCatching { runCatching {
setTileDescription(name) setTileDescription(name)
if (active) return setActive() if (active) return setActive()
setInactive() setInactive()
}.onFailure { }
Timber.e(it) .onFailure { Timber.e(it) }
} }
}
override val lifecycle: Lifecycle override val lifecycle: Lifecycle
get() = lifecycleRegistry get() = lifecycleRegistry
} }
@@ -10,70 +10,83 @@ import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class DynamicShortcutManager(private val context: Context, @IoDispatcher private val ioDispatcher: CoroutineDispatcher) : ShortcutManager { class DynamicShortcutManager(
override suspend fun addShortcuts() { private val context: Context,
withContext(ioDispatcher) { @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
ShortcutManagerCompat.setDynamicShortcuts(context, createShortcuts()) ) : ShortcutManager {
} override suspend fun addShortcuts() {
} withContext(ioDispatcher) {
ShortcutManagerCompat.setDynamicShortcuts(context, createShortcuts())
}
}
override suspend fun removeShortcuts() { override suspend fun removeShortcuts() {
withContext(ioDispatcher) { withContext(ioDispatcher) {
ShortcutManagerCompat.removeDynamicShortcuts(context, createShortcuts().map { it.id }) ShortcutManagerCompat.removeDynamicShortcuts(context, createShortcuts().map { it.id })
} }
} }
private fun createShortcuts(): List<ShortcutInfoCompat> { private fun createShortcuts(): List<ShortcutInfoCompat> {
return listOf( return listOf(
buildShortcut( buildShortcut(
context.getString(R.string.vpn_off), context.getString(R.string.vpn_off),
context.getString(R.string.vpn_off), context.getString(R.string.vpn_off),
context.getString(R.string.vpn_off), context.getString(R.string.vpn_off),
intent = Intent(context, ShortcutsActivity::class.java).apply { intent =
putExtra("className", "WireGuardTunnelService") Intent(context, ShortcutsActivity::class.java).apply {
action = ShortcutsActivity.Action.STOP.name putExtra("className", "WireGuardTunnelService")
}, action = ShortcutsActivity.Action.STOP.name
shortcutIcon = R.drawable.vpn_off, },
), shortcutIcon = R.drawable.vpn_off,
buildShortcut( ),
context.getString(R.string.vpn_on), buildShortcut(
context.getString(R.string.vpn_on), context.getString(R.string.vpn_on),
context.getString(R.string.vpn_on), context.getString(R.string.vpn_on),
intent = Intent(context, ShortcutsActivity::class.java).apply { context.getString(R.string.vpn_on),
putExtra("className", "WireGuardTunnelService") intent =
action = ShortcutsActivity.Action.START.name Intent(context, ShortcutsActivity::class.java).apply {
}, putExtra("className", "WireGuardTunnelService")
shortcutIcon = R.drawable.vpn_on, action = ShortcutsActivity.Action.START.name
), },
buildShortcut( shortcutIcon = R.drawable.vpn_on,
context.getString(R.string.start_auto), ),
context.getString(R.string.start_auto), buildShortcut(
context.getString(R.string.start_auto), context.getString(R.string.start_auto),
intent = Intent(context, ShortcutsActivity::class.java).apply { context.getString(R.string.start_auto),
putExtra("className", "WireGuardConnectivityWatcherService") context.getString(R.string.start_auto),
action = ShortcutsActivity.Action.START.name intent =
}, Intent(context, ShortcutsActivity::class.java).apply {
shortcutIcon = R.drawable.auto_play, putExtra("className", "WireGuardConnectivityWatcherService")
), action = ShortcutsActivity.Action.START.name
buildShortcut( },
context.getString(R.string.stop_auto), shortcutIcon = R.drawable.auto_play,
context.getString(R.string.stop_auto), ),
context.getString(R.string.stop_auto), buildShortcut(
intent = Intent(context, ShortcutsActivity::class.java).apply { context.getString(R.string.stop_auto),
putExtra("className", "WireGuardConnectivityWatcherService") context.getString(R.string.stop_auto),
action = ShortcutsActivity.Action.STOP.name context.getString(R.string.stop_auto),
}, intent =
shortcutIcon = R.drawable.auto_pause, Intent(context, ShortcutsActivity::class.java).apply {
), putExtra("className", "WireGuardConnectivityWatcherService")
) action = ShortcutsActivity.Action.STOP.name
} },
shortcutIcon = R.drawable.auto_pause,
),
)
}
private fun buildShortcut(id: String, shortLabel: String, longLabel: String, intent: Intent, shortcutIcon: Int): ShortcutInfoCompat { private fun buildShortcut(
return ShortcutInfoCompat.Builder(context, id) id: String,
.setShortLabel(shortLabel) shortLabel: String,
.setLongLabel(longLabel) longLabel: String,
.setIntent(intent) intent: Intent,
.setIcon(IconCompat.createWithResource(context, shortcutIcon)) shortcutIcon: Int,
.build() ): ShortcutInfoCompat {
} return ShortcutInfoCompat.Builder(context, id)
.setShortLabel(shortLabel)
.setLongLabel(longLabel)
.setIntent(intent)
.setIcon(IconCompat.createWithResource(context, shortcutIcon))
.build()
}
} }
@@ -1,6 +1,7 @@
package com.zaneschepke.wireguardautotunnel.core.shortcut package com.zaneschepke.wireguardautotunnel.core.shortcut
interface ShortcutManager { interface ShortcutManager {
suspend fun addShortcuts() suspend fun addShortcuts()
suspend fun removeShortcuts()
suspend fun removeShortcuts()
} }
@@ -9,69 +9,68 @@ import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelProvider
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
@AndroidEntryPoint @AndroidEntryPoint
class ShortcutsActivity : ComponentActivity() { class ShortcutsActivity : ComponentActivity() {
@Inject @Inject lateinit var appDataRepository: AppDataRepository
lateinit var appDataRepository: AppDataRepository
@Inject @Inject lateinit var serviceManager: ServiceManager
lateinit var serviceManager: ServiceManager
@Inject @Inject lateinit var tunnelManager: TunnelManager
lateinit var tunnelManager: TunnelManager
@Inject @Inject @ApplicationScope lateinit var applicationScope: CoroutineScope
@ApplicationScope
lateinit var applicationScope: CoroutineScope
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
applicationScope.launch { applicationScope.launch {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
if (settings.isShortcutsEnabled) { if (settings.isShortcutsEnabled) {
when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) { when (intent.getStringExtra(CLASS_NAME_EXTRA_KEY)) {
LEGACY_TUNNEL_SERVICE_NAME, TunnelProvider::class.java.simpleName -> { LEGACY_TUNNEL_SERVICE_NAME,
val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY) TunnelProvider::class.java.simpleName -> {
Timber.d("Tunnel name extra: $tunnelName") val tunnelName = intent.getStringExtra(TUNNEL_NAME_EXTRA_KEY)
val tunnelConfig = tunnelName?.let { Timber.d("Tunnel name extra: $tunnelName")
appDataRepository.tunnels.getAll() val tunnelConfig =
.firstOrNull { it.tunName == tunnelName } tunnelName?.let {
} ?: appDataRepository.getStartTunnelConfig() appDataRepository.tunnels.getAll().firstOrNull {
Timber.d("Shortcut action on name: ${tunnelConfig?.tunName}") it.tunName == tunnelName
tunnelConfig?.let { }
when (intent.action) { } ?: appDataRepository.getStartTunnelConfig()
Action.START.name -> tunnelManager.startTunnel(it) Timber.d("Shortcut action on name: ${tunnelConfig?.tunName}")
Action.STOP.name -> tunnelManager.stopTunnel() tunnelConfig?.let {
else -> Unit when (intent.action) {
} Action.START.name -> tunnelManager.startTunnel(it)
} Action.STOP.name -> tunnelManager.stopTunnel()
} else -> Unit
AutoTunnelService::class.java.simpleName, LEGACY_AUTO_TUNNEL_SERVICE_NAME -> { }
when (intent.action) { }
Action.START.name -> serviceManager.startAutoTunnel() }
Action.STOP.name -> serviceManager.stopAutoTunnel() AutoTunnelService::class.java.simpleName,
} LEGACY_AUTO_TUNNEL_SERVICE_NAME -> {
} when (intent.action) {
} Action.START.name -> serviceManager.startAutoTunnel()
} Action.STOP.name -> serviceManager.stopAutoTunnel()
} }
finish() }
} }
}
}
finish()
}
enum class Action { enum class Action {
START, START,
STOP, STOP,
} }
companion object { companion object {
const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService" const val LEGACY_TUNNEL_SERVICE_NAME = "WireGuardTunnelService"
const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService" const val LEGACY_AUTO_TUNNEL_SERVICE_NAME = "WireGuardConnectivityWatcherService"
const val TUNNEL_NAME_EXTRA_KEY = "tunnelName" const val TUNNEL_NAME_EXTRA_KEY = "tunnelName"
const val CLASS_NAME_EXTRA_KEY = "className" const val CLASS_NAME_EXTRA_KEY = "className"
} }
} }
@@ -1,5 +1,6 @@
package com.zaneschepke.wireguardautotunnel.core.tunnel package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.wireguard.android.backend.BackendException
import com.wireguard.android.backend.Tunnel import com.wireguard.android.backend.Tunnel
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
@@ -10,7 +11,11 @@ import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
import java.util.concurrent.ConcurrentHashMap
import kotlin.concurrent.thread
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
@@ -19,185 +24,215 @@ import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.concurrent.thread
abstract class BaseTunnel( abstract class BaseTunnel(
@ApplicationScope private val applicationScope: CoroutineScope, @ApplicationScope private val applicationScope: CoroutineScope,
private val appDataRepository: AppDataRepository, private val appDataRepository: AppDataRepository,
private val serviceManager: ServiceManager, private val serviceManager: ServiceManager,
) : TunnelProvider { ) : TunnelProvider {
private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap()) private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap())
private val tunThreads = ConcurrentHashMap<Int, Thread>() private val tunThreads = ConcurrentHashMap<Int, Thread>()
override val activeTunnels = activeTuns.asStateFlow() override val activeTunnels = activeTuns.asStateFlow()
private val tunMutex = Mutex() private val tunMutex = Mutex()
private val tunStatusMutex = Mutex() private val tunStatusMutex = Mutex()
private val bounceTunnelMutex = Mutex()
private val isBouncing = AtomicBoolean(false) override val bouncingTunnelIds = ConcurrentHashMap<Int, TunnelStatus.StopReason>()
abstract suspend fun startBackend(tunnel: TunnelConf) abstract suspend fun startBackend(tunnel: TunnelConf)
abstract fun stopBackend(tunnel: TunnelConf) abstract fun stopBackend(tunnel: TunnelConf)
override suspend fun clearError(tunnelConf: TunnelConf) = updateTunnelStatus(tunnelConf, TunnelStatus.Down) override suspend fun clearError(tunnelConf: TunnelConf) =
updateTunnelStatus(tunnelConf, TunnelStatus.Down)
override fun hasVpnPermission(): Boolean { override fun hasVpnPermission(): Boolean {
return serviceManager.hasVpnPermission() return serviceManager.hasVpnPermission()
} }
protected suspend fun updateTunnelStatus(tunnelConf: TunnelConf, state: TunnelStatus? = null, stats: TunnelStatistics? = null) { protected suspend fun updateTunnelStatus(
tunStatusMutex.withLock { tunnelConf: TunnelConf,
activeTuns.update { current -> state: TunnelStatus? = null,
val originalConf = current.getKeyById(tunnelConf.id) ?: tunnelConf stats: TunnelStatistics? = null,
val existingState = current.getValueById(tunnelConf.id) ?: TunnelState() ) {
val newState = state ?: existingState.status tunStatusMutex.withLock {
if (newState == TunnelStatus.Down) { activeTuns.update { current ->
Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN") val originalConf = current.getKeyById(tunnelConf.id) ?: tunnelConf
cleanUpTunThread(tunnelConf) val existingState = current.getValueById(tunnelConf.id) ?: TunnelState()
current - originalConf val newState = state ?: existingState.status
} else if (existingState.status == newState && stats == null) { if (newState == TunnelStatus.Down) {
Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newState") Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN")
current cleanUpTunThread(tunnelConf)
} else { current - originalConf
val updated = existingState.copy( } else if (existingState.status == newState && stats == null) {
status = newState, Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newState")
statistics = stats ?: existingState.statistics, current
) } else {
current + (originalConf to updated) val updated =
} existingState.copy(
} status = newState,
} statistics = stats ?: existingState.statistics,
} )
current + (originalConf to updated)
}
}
}
}
private suspend fun stopActiveTunnels() { private suspend fun stopActiveTunnels() {
activeTunnels.value.forEach { (config, state) -> activeTunnels.value.forEach { (config, state) ->
if (state.status.isUpOrStarting()) { if (state.status.isUpOrStarting()) {
stopTunnel(config) stopTunnel(config)
} }
} }
} }
private fun configureTunnelCallbacks(tunnelConf: TunnelConf) { private fun configureTunnelCallbacks(tunnelConf: TunnelConf) {
Timber.d("Configuring TunnelConf instance: ${tunnelConf.hashCode()}") Timber.d("Configuring TunnelConf instance: ${tunnelConf.hashCode()}")
tunnelConf.setStateChangeCallback { state -> tunnelConf.setStateChangeCallback { state ->
applicationScope.launch { applicationScope.launch {
Timber.d("State change callback triggered for tunnel ${tunnelConf.id}: ${tunnelConf.tunName} with state $state at ${System.currentTimeMillis()}") Timber.d(
when (state) { "State change callback triggered for tunnel ${tunnelConf.id}: ${tunnelConf.tunName} with state $state at ${System.currentTimeMillis()}"
is Tunnel.State -> updateTunnelStatus(tunnelConf, state.asTunnelState()) )
is org.amnezia.awg.backend.Tunnel.State -> updateTunnelStatus(tunnelConf, state.asTunnelState()) when (state) {
} is Tunnel.State -> updateTunnelStatus(tunnelConf, state.asTunnelState())
handleServiceChangesOnStop() is org.amnezia.awg.backend.Tunnel.State ->
} updateTunnelStatus(tunnelConf, state.asTunnelState())
serviceManager.updateTunnelTile() }
} handleServiceStateOnChange()
} }
serviceManager.updateTunnelTile()
}
}
override suspend fun updateTunnelStatistics(tunnel: TunnelConf) { override suspend fun updateTunnelStatistics(tunnel: TunnelConf) {
val stats = getStatistics(tunnel) val stats = getStatistics(tunnel)
updateTunnelStatus(tunnel, null, stats) updateTunnelStatus(tunnel, null, stats)
} }
override suspend fun startTunnel(tunnelConf: TunnelConf) { override suspend fun startTunnel(tunnelConf: TunnelConf) {
if (activeTuns.exists(tunnelConf.id) || tunThreads.containsKey(tunnelConf.id)) return if (activeTuns.exists(tunnelConf.id) || tunThreads.containsKey(tunnelConf.id)) return
// stop active tunnels if we are userspace if (this@BaseTunnel is UserspaceTunnel) stopActiveTunnels()
if (this@BaseTunnel is UserspaceTunnel) stopActiveTunnels() tunMutex.withLock {
tunMutex.withLock { tunThreads[tunnelConf.id] = thread {
// use thread to interrupt java backend if stuck (like in dns resolution) runCatching {
tunThreads += tunnelConf.id to thread { runBlocking {
runBlocking { try {
try { Timber.d("Starting tunnel ${tunnelConf.id}...")
Timber.d("Starting tunnel ${tunnelConf.id}...") startTunnelInner(tunnelConf)
startTunnelInner(tunnelConf) Timber.d("Started complete for tunnel ${tunnelConf.name}...")
Timber.d("Started complete for tunnel ${tunnelConf.name}...") } catch (e: BackendError) {
} catch (e: BackendError) { Timber.e(e, "Failed to start tunnel ${tunnelConf.name} userspace")
Timber.e(e, "Failed to start tunnel ${tunnelConf.name} userspace") updateTunnelStatus(tunnelConf, TunnelStatus.Error(e))
updateTunnelStatus(tunnelConf, TunnelStatus.Error(e)) } catch (e: InterruptedException) {
} catch (e: InterruptedException) { Timber.w(
Timber.i("Tunnel start has been interrupted as ${tunnelConf.name} failed to start") "Tunnel start has been interrupted as ${tunnelConf.name} failed to start"
} )
} }
} }
} }
} .onFailure { Timber.w("Tunnel start has been interrupted") }
}
}
}
private suspend fun startTunnelInner(tunnelConf: TunnelConf) { private suspend fun startTunnelInner(tunnelConf: TunnelConf) {
configureTunnelCallbacks(tunnelConf) configureTunnelCallbacks(tunnelConf)
Timber.d("Started backend for tunnel ${tunnelConf.id}...") Timber.d("Starting backend for tunnel ${tunnelConf.id}...")
startBackend(tunnelConf) try {
updateTunnelStatus(tunnelConf, TunnelStatus.Up) startBackend(tunnelConf)
Timber.d("DONE for tun ${tunnelConf.id}...") updateTunnelStatus(tunnelConf, TunnelStatus.Up)
saveTunnelActiveState(tunnelConf, true) Timber.d("Started for tun ${tunnelConf.id}...")
serviceManager.startTunnelForegroundService() saveTunnelActiveState(tunnelConf, true)
} serviceManager.startTunnelForegroundService()
} catch (e: BackendException) {
Timber.e(e, "Failed to start backend for ${tunnelConf.name}")
val backendError = e.toBackendError()
updateTunnelStatus(tunnelConf, TunnelStatus.Error(backendError))
throw backendError
}
}
private suspend fun saveTunnelActiveState(tunnelConf: TunnelConf, active: Boolean) { private suspend fun saveTunnelActiveState(tunnelConf: TunnelConf, active: Boolean) {
val tunnelCopy = tunnelConf.copyWithCallback(isActive = active) val tunnelCopy = tunnelConf.copyWithCallback(isActive = active)
appDataRepository.tunnels.save(tunnelCopy) appDataRepository.tunnels.save(tunnelCopy)
} }
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) { override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
if (tunnelConf == null) return stopActiveTunnels() if (tunnelConf == null) return stopActiveTunnels()
tunMutex.withLock { tunMutex.withLock {
try { try {
if (activeTuns.isStarting(tunnelConf.id)) return handleStuckStartingTunnelShutdown(tunnelConf) if (activeTuns.isStarting(tunnelConf.id))
updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason)) return handleStuckStartingTunnelShutdown(tunnelConf)
stopTunnelInner(tunnelConf) updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason))
} catch (e: BackendError) { stopTunnelInner(tunnelConf)
Timber.e(e, "Failed to stop tunnel ${tunnelConf.id}") } catch (e: BackendError) {
updateTunnelStatus(tunnelConf, TunnelStatus.Error(e)) Timber.e(e, "Failed to stop tunnel ${tunnelConf.id}")
} updateTunnelStatus(tunnelConf, TunnelStatus.Error(e))
} }
} }
}
private suspend fun stopTunnelInner(tunnelConf: TunnelConf) { private suspend fun stopTunnelInner(tunnelConf: TunnelConf) {
val tunnel = activeTuns.findTunnel(tunnelConf.id) ?: return val tunnel = activeTuns.findTunnel(tunnelConf.id) ?: return
stopBackend(tunnel) stopBackend(tunnel)
saveTunnelActiveState(tunnelConf, false) saveTunnelActiveState(tunnelConf, false)
removeActiveTunnel(tunnel) removeActiveTunnel(tunnel)
} }
private suspend fun handleServiceChangesOnStop() { private suspend fun handleServiceStateOnChange() {
if (activeTuns.value.isEmpty() && !isBouncing.get()) return serviceManager.stopTunnelForegroundService() if (activeTuns.value.isEmpty() && bouncingTunnelIds.isEmpty())
} serviceManager.stopTunnelForegroundService()
}
private suspend fun handleStuckStartingTunnelShutdown(tunnel: TunnelConf) { private suspend fun handleStuckStartingTunnelShutdown(tunnel: TunnelConf) {
Timber.d("Stuck in starting state so shutting down tunnel thread for tunnel ${tunnel.name}") Timber.d("Stuck in starting state so shutting down tunnel thread for tunnel ${tunnel.name}")
try { try {
tunThreads[tunnel.id]?.let { tunThreads[tunnel.id]?.let {
if (it.state != Thread.State.TERMINATED) { if (it.state != Thread.State.TERMINATED) {
it.interrupt() it.interrupt()
updateTunnelStatus(tunnel, TunnelStatus.Down) updateTunnelStatus(tunnel, TunnelStatus.Down)
} else { } else {
Timber.d("Thread already terminated") Timber.d("Thread already terminated")
} }
} }
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Failed to stop tunnel thread for ${tunnel.name}") Timber.e(e, "Failed to stop tunnel thread for ${tunnel.name}")
} }
cleanUpTunThread(tunnel) cleanUpTunThread(tunnel)
} }
private fun cleanUpTunThread(tunnel: TunnelConf) { private fun cleanUpTunThread(tunnel: TunnelConf) {
Timber.d("Removing thread for ${tunnel.name}") Timber.d("Removing thread for ${tunnel.name}")
tunThreads -= tunnel.id tunThreads -= tunnel.id
} }
private fun removeActiveTunnel(tunnelConf: TunnelConf) { private fun removeActiveTunnel(tunnelConf: TunnelConf) {
activeTuns.update { current -> activeTuns.update { current -> current.toMutableMap().apply { remove(tunnelConf) } }
current.toMutableMap().apply { remove(tunnelConf) } }
}
}
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) { override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
Timber.i("Bounce tunnel ${tunnelConf.name}") bounceTunnelMutex.withLock {
isBouncing.set(true) Timber.i(
stopTunnel(tunnelConf, reason) "Bounce tunnel ${tunnelConf.name} for reason: $reason, current bouncing: ${bouncingTunnelIds.size}"
startTunnel(tunnelConf) )
isBouncing.set(false) bouncingTunnelIds[tunnelConf.id] = reason
} try {
stopTunnel(tunnelConf, reason)
delay(300L)
startTunnel(tunnelConf)
} finally {
bouncingTunnelIds.remove(tunnelConf.id)
handleServiceStateOnChange()
Timber.d(
"Cleared bounce state for ${tunnelConf.name}, remaining: ${bouncingTunnelIds.size}"
)
}
}
}
override suspend fun runningTunnelNames(): Set<String> = activeTuns.value.keys.map { it.tunName }.toSet() override suspend fun runningTunnelNames(): Set<String> =
activeTuns.value.keys.map { it.tunName }.toSet()
} }
@@ -6,46 +6,45 @@ import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
fun Map<TunnelConf, TunnelState>.allDown(): Boolean { fun Map<TunnelConf, TunnelState>.allDown(): Boolean {
return this.all { it.value.status.isDown() } return this.all { it.value.status.isDown() }
} }
fun Map<TunnelConf, TunnelState>.hasActive(): Boolean { fun Map<TunnelConf, TunnelState>.hasActive(): Boolean {
return this.any { it.value.status.isUp() } return this.any { it.value.status.isUp() }
} }
fun Map<TunnelConf, TunnelState>.getValueById(id: Int): TunnelState? { fun Map<TunnelConf, TunnelState>.getValueById(id: Int): TunnelState? {
val key = this.keys.find { it.id == id } val key = this.keys.find { it.id == id }
return key?.let { this@getValueById[it] } return key?.let { this@getValueById[it] }
} }
fun Map<TunnelConf, TunnelState>.getKeyById(id: Int): TunnelConf? { fun Map<TunnelConf, TunnelState>.getKeyById(id: Int): TunnelConf? {
return this.keys.find { it.id == id } return this.keys.find { it.id == id }
} }
fun Map<TunnelConf, TunnelState>.isUp(tunnelConf: TunnelConf): Boolean { fun Map<TunnelConf, TunnelState>.isUp(tunnelConf: TunnelConf): Boolean {
return this.getValueById(tunnelConf.id)?.status?.isUp() ?: false return this.getValueById(tunnelConf.id)?.status?.isUp() ?: false
} }
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.exists(id: Int): Boolean { fun MutableStateFlow<Map<TunnelConf, TunnelState>>.exists(id: Int): Boolean {
return this.value.any { it.key.id == id } return this.value.any { it.key.id == id }
} }
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isUp(id: Int): Boolean { fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isUp(id: Int): Boolean {
return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Up } return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Up }
} }
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isStarting(id: Int): Boolean { fun MutableStateFlow<Map<TunnelConf, TunnelState>>.isStarting(id: Int): Boolean {
return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Starting } return this.value.any { it.key.id == id && it.value.status == TunnelStatus.Starting }
} }
fun MutableStateFlow<Map<TunnelConf, TunnelState>>.findTunnel(id: Int): TunnelConf? { fun MutableStateFlow<Map<TunnelConf, TunnelState>>.findTunnel(id: Int): TunnelConf? {
return this.value.keys.find { it.id == id } return this.value.keys.find { it.id == id }
} }
private val URL_PATTERN = Regex( private val URL_PATTERN =
"""^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}:[0-9]{1,5}$""", Regex("""^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,}:[0-9]{1,5}$""")
)
fun String.isUrl(): Boolean { fun String.isUrl(): Boolean {
return URL_PATTERN.matches(this) return URL_PATTERN.matches(this)
} }
@@ -12,53 +12,55 @@ import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.domain.state.WireGuardStatistics import com.zaneschepke.wireguardautotunnel.domain.state.WireGuardStatistics
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
class KernelTunnel @Inject constructor( class KernelTunnel
@ApplicationScope private val applicationScope: CoroutineScope, @Inject
serviceManager: ServiceManager, constructor(
appDataRepository: AppDataRepository, @ApplicationScope private val applicationScope: CoroutineScope,
private val backend: Backend, serviceManager: ServiceManager,
appDataRepository: AppDataRepository,
private val backend: Backend,
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) { ) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? { override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
return try { return try {
WireGuardStatistics(backend.getStatistics(tunnelConf)) WireGuardStatistics(backend.getStatistics(tunnelConf))
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e) Timber.e(e)
null null
} }
} }
override suspend fun startBackend(tunnel: TunnelConf) { override suspend fun startBackend(tunnel: TunnelConf) {
try { try {
updateTunnelStatus(tunnel, TunnelStatus.Starting) updateTunnelStatus(tunnel, TunnelStatus.Starting)
backend.setState(tunnel, Tunnel.State.UP, tunnel.toWgConfig()) backend.setState(tunnel, Tunnel.State.UP, tunnel.toWgConfig())
} catch (e: BackendException) { } catch (e: BackendException) {
throw e.toBackendError() throw e.toBackendError()
} }
} }
override fun stopBackend(tunnel: TunnelConf) { override fun stopBackend(tunnel: TunnelConf) {
Timber.i("Stopping tunnel ${tunnel.id} kernel") Timber.i("Stopping tunnel ${tunnel.id} kernel")
try { try {
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toWgConfig()) backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toWgConfig())
} catch (e: BackendException) { } catch (e: BackendException) {
throw e.toBackendError() throw e.toBackendError()
} }
} }
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) { override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
Timber.w("Not yet implemented for kernel") Timber.w("Not yet implemented for kernel")
} }
override fun getBackendState(): BackendState { override fun getBackendState(): BackendState {
return BackendState.INACTIVE return BackendState.INACTIVE
} }
override suspend fun runningTunnelNames(): Set<String> { override suspend fun runningTunnelNames(): Set<String> {
return backend.runningTunnelNames return backend.runningTunnelNames
} }
} }
@@ -9,6 +9,8 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -19,96 +21,104 @@ import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.plus import kotlinx.coroutines.plus
import javax.inject.Inject
class TunnelManager @Inject constructor( class TunnelManager
@Kernel private val kernelTunnel: TunnelProvider, @Inject
@Userspace private val userspaceTunnel: TunnelProvider, constructor(
private val appDataRepository: AppDataRepository, @Kernel private val kernelTunnel: TunnelProvider,
@ApplicationScope private val applicationScope: CoroutineScope, @Userspace private val userspaceTunnel: TunnelProvider,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, private val appDataRepository: AppDataRepository,
@ApplicationScope private val applicationScope: CoroutineScope,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : TunnelProvider { ) : TunnelProvider {
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
private val tunnelProviderFlow = appDataRepository.settings.flow private val tunnelProviderFlow =
.filterNotNull() appDataRepository.settings.flow
.flatMapLatest { settings -> .filterNotNull()
MutableStateFlow(if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel) .flatMapLatest { settings ->
} MutableStateFlow(if (settings.isKernelEnabled) kernelTunnel else userspaceTunnel)
.stateIn( }
scope = applicationScope.plus(ioDispatcher), .stateIn(
started = SharingStarted.Eagerly, scope = applicationScope.plus(ioDispatcher),
initialValue = userspaceTunnel, started = SharingStarted.Eagerly,
) initialValue = userspaceTunnel,
)
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
override val activeTunnels = appDataRepository.settings.flow override val activeTunnels =
.filterNotNull() appDataRepository.settings.flow
.flatMapLatest { settings -> .filterNotNull()
if (settings.isKernelEnabled) { .flatMapLatest { settings ->
kernelTunnel.activeTunnels if (settings.isKernelEnabled) {
} else { kernelTunnel.activeTunnels
userspaceTunnel.activeTunnels } else {
} userspaceTunnel.activeTunnels
} }
.stateIn( }
scope = applicationScope.plus(ioDispatcher), .stateIn(
started = SharingStarted.Eagerly, scope = applicationScope.plus(ioDispatcher),
initialValue = emptyMap(), started = SharingStarted.Eagerly,
) initialValue = emptyMap(),
)
override fun hasVpnPermission(): Boolean { override val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason> =
return userspaceTunnel.hasVpnPermission() tunnelProviderFlow.value.bouncingTunnelIds
}
override suspend fun clearError(tunnelConf: TunnelConf) { override fun hasVpnPermission(): Boolean {
tunnelProviderFlow.value.clearError(tunnelConf) return userspaceTunnel.hasVpnPermission()
} }
override suspend fun updateTunnelStatistics(tunnel: TunnelConf) { override suspend fun clearError(tunnelConf: TunnelConf) {
tunnelProviderFlow.value.updateTunnelStatistics(tunnel) tunnelProviderFlow.value.clearError(tunnelConf)
} }
override suspend fun startTunnel(tunnelConf: TunnelConf) { override suspend fun updateTunnelStatistics(tunnel: TunnelConf) {
tunnelProviderFlow.value.startTunnel(tunnelConf) tunnelProviderFlow.value.updateTunnelStatistics(tunnel)
} }
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) { override suspend fun startTunnel(tunnelConf: TunnelConf) {
tunnelProviderFlow.value.stopTunnel(tunnelConf) tunnelProviderFlow.value.startTunnel(tunnelConf)
} }
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) { override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
tunnelProviderFlow.value.bounceTunnel(tunnelConf) tunnelProviderFlow.value.stopTunnel(tunnelConf, reason)
} }
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) { override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
tunnelProviderFlow.value.setBackendState(backendState, allowedIps) tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason)
} }
override fun getBackendState(): BackendState { override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
return tunnelProviderFlow.value.getBackendState() tunnelProviderFlow.value.setBackendState(backendState, allowedIps)
} }
override suspend fun runningTunnelNames(): Set<String> { override fun getBackendState(): BackendState {
return tunnelProviderFlow.value.runningTunnelNames() return tunnelProviderFlow.value.getBackendState()
} }
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? { override suspend fun runningTunnelNames(): Set<String> {
return tunnelProviderFlow.value.getStatistics(tunnelConf) return tunnelProviderFlow.value.runningTunnelNames()
} }
fun restorePreviousState() = applicationScope.launch(ioDispatcher) { override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
val settings = appDataRepository.settings.get() return tunnelProviderFlow.value.getStatistics(tunnelConf)
if (settings.isRestoreOnBootEnabled) { }
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
val tunsToStart = previouslyActiveTuns.filterNot { tun -> activeTunnels.value.any { tun.id == it.key.id } } fun restorePreviousState() =
if (settings.isKernelEnabled) { applicationScope.launch(ioDispatcher) {
return@launch tunsToStart.forEach { val settings = appDataRepository.settings.get()
startTunnel(it) if (settings.isRestoreOnBootEnabled) {
} val previouslyActiveTuns = appDataRepository.tunnels.getActive()
} else { val tunsToStart =
tunsToStart.firstOrNull()?.let { startTunnel(it) } previouslyActiveTuns.filterNot { tun ->
} activeTunnels.value.any { tun.id == it.key.id }
} }
} if (settings.isKernelEnabled) {
return@launch tunsToStart.forEach { startTunnel(it) }
} else {
tunsToStart.firstOrNull()?.let { startTunnel(it) }
}
}
}
} }
@@ -5,18 +5,52 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import java.util.concurrent.ConcurrentHashMap
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
interface TunnelProvider { interface TunnelProvider {
suspend fun startTunnel(tunnelConf: TunnelConf) /** Starts the specified tunnel configuration. */
suspend fun stopTunnel(tunnelConf: TunnelConf? = null, reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER) suspend fun startTunnel(tunnelConf: TunnelConf)
suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER)
fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) /**
fun getBackendState(): BackendState * Stops the specified tunnel, or all tunnels if none is provided.
suspend fun runningTunnelNames(): Set<String> *
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? * @param tunnelConf The tunnel to stop, or null to stop all active tunnels.
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>> * @param reason The reason for stopping, defaults to USER for manual stops. Callers should
fun hasVpnPermission(): Boolean * override with specific reasons (e.g., PING, CONFIG_CHANGED) when applicable.
suspend fun clearError(tunnelConf: TunnelConf) */
suspend fun updateTunnelStatistics(tunnel: TunnelConf) suspend fun stopTunnel(
tunnelConf: TunnelConf? = null,
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER,
)
/**
* Bounces (stops and restarts) the specified tunnel.
*
* @param tunnelConf The tunnel to bounce.
* @param reason The reason for bouncing, defaults to USER for manual actions. Callers should
* override with specific reasons (e.g., PING, CONFIG_CHANGED) when applicable.
*/
suspend fun bounceTunnel(
tunnelConf: TunnelConf,
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.USER,
)
fun setBackendState(backendState: BackendState, allowedIps: Collection<String>)
fun getBackendState(): BackendState
suspend fun runningTunnelNames(): Set<String>
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics?
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>>
val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason>
fun hasVpnPermission(): Boolean
suspend fun clearError(tunnelConf: TunnelConf)
suspend fun updateTunnelStatistics(tunnel: TunnelConf)
} }
@@ -11,94 +11,97 @@ import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendState
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendState
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError
import javax.inject.Inject
import kotlin.jvm.optionals.getOrNull
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import org.amnezia.awg.backend.Backend import org.amnezia.awg.backend.Backend
import org.amnezia.awg.backend.BackendException import org.amnezia.awg.backend.BackendException
import org.amnezia.awg.backend.Tunnel import org.amnezia.awg.backend.Tunnel
import org.amnezia.awg.config.Config import org.amnezia.awg.config.Config
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
import kotlin.jvm.optionals.getOrNull
class UserspaceTunnel @Inject constructor( class UserspaceTunnel
@ApplicationScope private val applicationScope: CoroutineScope, @Inject
val serviceManager: ServiceManager, constructor(
val appDataRepository: AppDataRepository, @ApplicationScope private val applicationScope: CoroutineScope,
private val backend: Backend, val serviceManager: ServiceManager,
val appDataRepository: AppDataRepository,
private val backend: Backend,
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) { ) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
private var previousBackendState: Pair<BackendState, Boolean>? = null private var previousBackendState: Pair<BackendState, Boolean>? = null
override suspend fun startBackend(tunnel: TunnelConf) { override suspend fun startBackend(tunnel: TunnelConf) {
try { try {
updateTunnelStatus(tunnel, TunnelStatus.Starting) updateTunnelStatus(tunnel, TunnelStatus.Starting)
val amConfig = tunnel.toAmConfig() val amConfig = tunnel.toAmConfig()
handleVpnKillSwitchWithDomainEndpoints(amConfig) handleVpnKillSwitchWithDomainEndpoints(amConfig)
backend.setState(tunnel, Tunnel.State.UP, amConfig) backend.setState(tunnel, Tunnel.State.UP, amConfig)
} catch (e: BackendException) { } catch (e: BackendException) {
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}") Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}")
throw e.toBackendError() throw e.toBackendError()
} }
} }
override fun stopBackend(tunnel: TunnelConf) { override fun stopBackend(tunnel: TunnelConf) {
Timber.i("Stopping tunnel ${tunnel.name} userspace") Timber.i("Stopping tunnel ${tunnel.name} userspace")
try { try {
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toAmConfig()) backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toAmConfig())
} catch (e: BackendException) { } catch (e: BackendException) {
Timber.e(e, "Failed to stop tunnel ${tunnel.id}") Timber.e(e, "Failed to stop tunnel ${tunnel.id}")
throw e.toBackendError() throw e.toBackendError()
} }
handlePreviouslyEnabledVpnKillSwitch() handlePreviouslyEnabledVpnKillSwitch()
} }
// stop vpn kill switch if we need to resolve DNS for peer endpoints // stop vpn kill switch if we need to resolve DNS for peer endpoints
private suspend fun handleVpnKillSwitchWithDomainEndpoints(config: Config) { private suspend fun handleVpnKillSwitchWithDomainEndpoints(config: Config) {
if (config.peers.any { it.endpoint.getOrNull()?.toString()?.isUrl() == true } && if (
backend.backendState.asBackendState() == BackendState.KILL_SWITCH_ACTIVE config.peers.any { it.endpoint.getOrNull()?.toString()?.isUrl() == true } &&
) { backend.backendState.asBackendState() == BackendState.KILL_SWITCH_ACTIVE
val bypassLan = appDataRepository.settings.get().isLanOnKillSwitchEnabled ) {
previousBackendState = Pair(BackendState.KILL_SWITCH_ACTIVE, bypassLan) val bypassLan = appDataRepository.settings.get().isLanOnKillSwitchEnabled
setBackendState(BackendState.SERVICE_ACTIVE, emptyList()) previousBackendState = Pair(BackendState.KILL_SWITCH_ACTIVE, bypassLan)
} setBackendState(BackendState.SERVICE_ACTIVE, emptyList())
} }
}
// restore vpn kill switch if needed // restore vpn kill switch if needed
private fun handlePreviouslyEnabledVpnKillSwitch() { private fun handlePreviouslyEnabledVpnKillSwitch() {
// let auto tunnel handle this if it is active // let auto tunnel handle this if it is active
if (!serviceManager.autoTunnelActive.value) { if (!serviceManager.autoTunnelActive.value) {
previousBackendState?.let { (state, lanEnabled) -> previousBackendState?.let { (state, lanEnabled) ->
Timber.d("Restoring kill switch configuration") Timber.d("Restoring kill switch configuration")
val lan = if (lanEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList() val lan = if (lanEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList()
backend.setBackendState(state.asAmBackendState(), lan) backend.setBackendState(state.asAmBackendState(), lan)
} }
} }
previousBackendState = null previousBackendState = null
} }
override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) { override fun setBackendState(backendState: BackendState, allowedIps: Collection<String>) {
Timber.d("Setting backend state: $backendState with allowedIps: $allowedIps") Timber.d("Setting backend state: $backendState with allowedIps: $allowedIps")
try { try {
backend.setBackendState(backendState.asAmBackendState(), allowedIps) backend.setBackendState(backendState.asAmBackendState(), allowedIps)
} catch (e: BackendException) { } catch (e: BackendException) {
throw e.toBackendError() throw e.toBackendError()
} }
} }
override fun getBackendState(): BackendState { override fun getBackendState(): BackendState {
return backend.backendState.asBackendState() return backend.backendState.asBackendState()
} }
override suspend fun runningTunnelNames(): Set<String> { override suspend fun runningTunnelNames(): Set<String> {
return backend.runningTunnelNames return backend.runningTunnelNames
} }
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? { override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? {
return try { return try {
AmneziaStatistics(backend.getStatistics(tunnelConf)) AmneziaStatistics(backend.getStatistics(tunnelConf))
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}") Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}")
null null
} }
} }
} }
@@ -13,48 +13,55 @@ import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import dagger.assisted.Assisted import dagger.assisted.Assisted
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.util.concurrent.TimeUnit
@HiltWorker @HiltWorker
class ServiceWorker @AssistedInject constructor( class ServiceWorker
@Assisted private val context: Context, @AssistedInject
@Assisted private val params: WorkerParameters, constructor(
private val serviceManager: ServiceManager, @Assisted private val context: Context,
private val appDataRepository: AppDataRepository, @Assisted private val params: WorkerParameters,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, private val serviceManager: ServiceManager,
private val tunnelManager: TunnelManager, private val appDataRepository: AppDataRepository,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
private val tunnelManager: TunnelManager,
) : CoroutineWorker(context, params) { ) : CoroutineWorker(context, params) {
companion object { companion object {
private const val TAG = "service_worker" private const val TAG = "service_worker"
fun stop(context: Context) { fun stop(context: Context) {
WorkManager.getInstance(context).cancelAllWorkByTag(TAG) WorkManager.getInstance(context).cancelAllWorkByTag(TAG)
} }
fun start(context: Context) { fun start(context: Context) {
val periodicWorkRequest = PeriodicWorkRequestBuilder<ServiceWorker>( val periodicWorkRequest =
repeatInterval = 15, PeriodicWorkRequestBuilder<ServiceWorker>(
repeatIntervalTimeUnit = TimeUnit.MINUTES, repeatInterval = 15,
).build() repeatIntervalTimeUnit = TimeUnit.MINUTES,
WorkManager.getInstance(context) )
.enqueueUniquePeriodicWork( .build()
TAG, WorkManager.getInstance(context)
ExistingPeriodicWorkPolicy.KEEP, .enqueueUniquePeriodicWork(
periodicWorkRequest, TAG,
) ExistingPeriodicWorkPolicy.KEEP,
} periodicWorkRequest,
} )
}
}
override suspend fun doWork(): Result = withContext(ioDispatcher) { override suspend fun doWork(): Result =
Timber.i("Service worker started") withContext(ioDispatcher) {
with(appDataRepository.settings.get()) { Timber.i("Service worker started")
if (isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value) return@with serviceManager.startAutoTunnel() with(appDataRepository.settings.get()) {
if (tunnelManager.activeTunnels.value.isEmpty()) tunnelManager.restorePreviousState() if (isAutoTunnelEnabled && !serviceManager.autoTunnelActive.value)
} return@with serviceManager.startAutoTunnel()
Result.success() if (tunnelManager.activeTunnels.value.isEmpty())
} tunnelManager.restorePreviousState()
}
Result.success()
}
} }
@@ -12,79 +12,38 @@ import com.zaneschepke.wireguardautotunnel.data.model.Settings
import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig import com.zaneschepke.wireguardautotunnel.data.model.TunnelConfig
@Database( @Database(
entities = [Settings::class, TunnelConfig::class], entities = [Settings::class, TunnelConfig::class],
version = 16, version = 16,
autoMigrations = autoMigrations =
[ [
AutoMigration(from = 1, to = 2), AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3), AutoMigration(from = 2, to = 3),
AutoMigration( AutoMigration(from = 3, to = 4),
from = 3, AutoMigration(from = 4, to = 5),
to = 4, AutoMigration(from = 5, to = 6),
), AutoMigration(from = 6, to = 7, spec = RemoveLegacySettingColumnsMigration::class),
AutoMigration( AutoMigration(7, 8),
from = 4, AutoMigration(8, 9),
to = 5, AutoMigration(9, 10),
), AutoMigration(from = 10, to = 11, spec = RemoveTunnelPauseMigration::class),
AutoMigration( AutoMigration(from = 11, to = 12),
from = 5, AutoMigration(from = 12, to = 13),
to = 6, AutoMigration(from = 13, to = 14),
), AutoMigration(from = 14, to = 15),
AutoMigration( AutoMigration(from = 15, to = 16),
from = 6, ],
to = 7, exportSchema = true,
spec = RemoveLegacySettingColumnsMigration::class,
),
AutoMigration(7, 8),
AutoMigration(8, 9),
AutoMigration(9, 10),
AutoMigration(
from = 10,
to = 11,
spec = RemoveTunnelPauseMigration::class,
),
AutoMigration(
from = 11,
to = 12,
),
AutoMigration(
from = 12,
to = 13,
),
AutoMigration(
from = 13,
to = 14,
),
AutoMigration(
from = 14,
to = 15,
),
AutoMigration(
from = 15,
to = 16,
),
],
exportSchema = true,
) )
@TypeConverters(DatabaseListConverters::class) @TypeConverters(DatabaseListConverters::class)
abstract class AppDatabase : RoomDatabase() { abstract class AppDatabase : RoomDatabase() {
abstract fun settingDao(): SettingsDao abstract fun settingDao(): SettingsDao
abstract fun tunnelConfigDoa(): TunnelConfigDao abstract fun tunnelConfigDoa(): TunnelConfigDao
} }
@DeleteColumn( @DeleteColumn(tableName = "Settings", columnName = "default_tunnel")
tableName = "Settings", @DeleteColumn(tableName = "Settings", columnName = "is_battery_saver_enabled")
columnName = "default_tunnel",
)
@DeleteColumn(
tableName = "Settings",
columnName = "is_battery_saver_enabled",
)
class RemoveLegacySettingColumnsMigration : AutoMigrationSpec class RemoveLegacySettingColumnsMigration : AutoMigrationSpec
@DeleteColumn( @DeleteColumn(tableName = "Settings", columnName = "is_auto_tunnel_paused")
tableName = "Settings",
columnName = "is_auto_tunnel_paused",
)
class RemoveTunnelPauseMigration : AutoMigrationSpec class RemoveTunnelPauseMigration : AutoMigrationSpec
@@ -7,6 +7,7 @@ import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore import androidx.datastore.preferences.preferencesDataStore
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import java.io.IOException
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
@@ -15,82 +16,77 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
import java.io.IOException
class DataStoreManager( class DataStoreManager(
private val context: Context, private val context: Context,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) { ) {
companion object { companion object {
val locationDisclosureShown = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN") val locationDisclosureShown = booleanPreferencesKey("LOCATION_DISCLOSURE_SHOWN")
val batteryDisableShown = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN") val batteryDisableShown = booleanPreferencesKey("BATTERY_OPTIMIZE_DISABLE_SHOWN")
val currentSSID = stringPreferencesKey("CURRENT_SSID") val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED")
val pinLockEnabled = booleanPreferencesKey("PIN_LOCK_ENABLED") val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED")
val tunnelStatsExpanded = booleanPreferencesKey("TUNNEL_STATS_EXPANDED") val isLocalLogsEnabled = booleanPreferencesKey("LOCAL_LOGS_ENABLED")
val isLocalLogsEnabled = booleanPreferencesKey("LOCAL_LOGS_ENABLED") val locale = stringPreferencesKey("LOCALE")
val locale = stringPreferencesKey("LOCALE") val theme = stringPreferencesKey("THEME")
val theme = stringPreferencesKey("THEME") val isRemoteControlEnabled = booleanPreferencesKey("IS_REMOTE_CONTROL_ENABLED")
val isRemoteControlEnabled = booleanPreferencesKey("IS_REMOTE_CONTROL_ENABLED") val remoteKey = stringPreferencesKey("REMOTE_KEY")
val remoteKey = stringPreferencesKey("REMOTE_KEY") }
}
// preferences // preferences
private val preferencesKey = "preferences" private val preferencesKey = "preferences"
private val Context.dataStore by private val Context.dataStore by preferencesDataStore(name = preferencesKey)
preferencesDataStore(
name = preferencesKey,
)
suspend fun init() { suspend fun init() {
withContext(ioDispatcher) { withContext(ioDispatcher) {
try { try {
context.dataStore.data.first() context.dataStore.data.first()
} catch (e: IOException) { } catch (e: IOException) {
Timber.Forest.e(e) Timber.Forest.e(e)
} }
} }
} }
suspend fun <T> saveToDataStore(key: Preferences.Key<T>, value: T) { suspend fun <T> saveToDataStore(key: Preferences.Key<T>, value: T) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
try { try {
context.dataStore.edit { it[key] = value } context.dataStore.edit { it[key] = value }
} catch (e: IOException) { } catch (e: IOException) {
Timber.Forest.e(e) Timber.Forest.e(e)
} catch (e: Exception) { } catch (e: Exception) {
Timber.Forest.e(e) Timber.Forest.e(e)
} }
} }
} }
suspend fun <T> removeFromDataStore(key: Preferences.Key<T>) { suspend fun <T> removeFromDataStore(key: Preferences.Key<T>) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
try { try {
context.dataStore.edit { it.remove(key) } context.dataStore.edit { it.remove(key) }
} catch (e: IOException) { } catch (e: IOException) {
Timber.Forest.e(e) Timber.Forest.e(e)
} catch (e: Exception) { } catch (e: Exception) {
Timber.Forest.e(e) Timber.Forest.e(e)
} }
} }
} }
fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] } fun <T> getFromStoreFlow(key: Preferences.Key<T>) = context.dataStore.data.map { it[key] }
suspend fun <T> getFromStore(key: Preferences.Key<T>): T? { suspend fun <T> getFromStore(key: Preferences.Key<T>): T? {
return withContext(ioDispatcher) { return withContext(ioDispatcher) {
try { try {
context.dataStore.data.map { it[key] }.first() context.dataStore.data.map { it[key] }.first()
} catch (e: IOException) { } catch (e: IOException) {
Timber.Forest.e(e) Timber.Forest.e(e)
null null
} }
} }
} }
fun <T> getFromStoreBlocking(key: Preferences.Key<T>) = runBlocking { fun <T> getFromStoreBlocking(key: Preferences.Key<T>) = runBlocking {
context.dataStore.data.map { it[key] }.first() context.dataStore.data.map { it[key] }.first()
} }
val preferencesFlow: Flow<Preferences?> = context.dataStore.data.flowOn(ioDispatcher) val preferencesFlow: Flow<Preferences?> = context.dataStore.data.flowOn(ioDispatcher)
} }
@@ -5,17 +5,18 @@ import androidx.sqlite.db.SupportSQLiteDatabase
import timber.log.Timber import timber.log.Timber
class DatabaseCallback : RoomDatabase.Callback() { class DatabaseCallback : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) = db.run { override fun onCreate(db: SupportSQLiteDatabase) =
// Notice non-ui thread is here db.run {
beginTransaction() // Notice non-ui thread is here
try { beginTransaction()
execSQL(Queries.createDefaultSettings()) try {
Timber.i("Bootstrapping settings data") execSQL(Queries.createDefaultSettings())
setTransactionSuccessful() Timber.i("Bootstrapping settings data")
} catch (e: Exception) { setTransactionSuccessful()
Timber.e(e) } catch (e: Exception) {
} finally { Timber.e(e)
endTransaction() } finally {
} endTransaction()
} }
}
} }
@@ -4,20 +4,20 @@ import androidx.room.TypeConverter
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
class DatabaseListConverters { class DatabaseListConverters {
@TypeConverter @TypeConverter
fun listToString(value: MutableList<String>): String { fun listToString(value: MutableList<String>): String {
return Json.encodeToString(value) return Json.encodeToString(value)
} }
@TypeConverter @TypeConverter
fun stringToList(value: String): MutableList<String> { fun stringToList(value: String): MutableList<String> {
if (value.isBlank() || value.isEmpty()) return mutableListOf() if (value.isBlank() || value.isEmpty()) return mutableListOf()
return try { return try {
Json.decodeFromString<MutableList<String>>(value) Json.decodeFromString<MutableList<String>>(value)
} catch (e: Exception) { } catch (e: Exception) {
val list = value.split(",").toMutableList() val list = value.split(",").toMutableList()
val json = listToString(list) val json = listToString(list)
Json.decodeFromString<MutableList<String>>(json) Json.decodeFromString<MutableList<String>>(json)
} }
} }
} }
@@ -1,8 +1,8 @@
package com.zaneschepke.wireguardautotunnel.data package com.zaneschepke.wireguardautotunnel.data
object Queries { object Queries {
fun createDefaultSettings(): String { fun createDefaultSettings(): String {
return """ return """
INSERT INTO Settings (is_tunnel_enabled, INSERT INTO Settings (is_tunnel_enabled,
is_tunnel_on_mobile_data_enabled, is_tunnel_on_mobile_data_enabled,
trusted_network_ssids, trusted_network_ssids,
@@ -24,12 +24,14 @@ object Queries {
'false', 'false',
'false', 'false',
'false') 'false')
""".trimIndent() """
} .trimIndent()
}
fun createTunnelConfig(): String { fun createTunnelConfig(): String {
return """ return """
INSERT INTO TunnelConfig (name, wg_quick) VALUES ('test', 'test') INSERT INTO TunnelConfig (name, wg_quick) VALUES ('test', 'test')
""".trimIndent() """
} .trimIndent()
}
} }
@@ -10,27 +10,19 @@ import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface SettingsDao { interface SettingsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun save(t: Settings)
suspend fun save(t: Settings)
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveAll(t: List<Settings>)
suspend fun saveAll(t: List<Settings>)
@Query("SELECT * FROM settings WHERE id=:id") @Query("SELECT * FROM settings WHERE id=:id") suspend fun getById(id: Long): Settings?
suspend fun getById(id: Long): Settings?
@Query("SELECT * FROM settings") @Query("SELECT * FROM settings") suspend fun getAll(): List<Settings>
suspend fun getAll(): List<Settings>
@Query("SELECT * FROM settings LIMIT 1") @Query("SELECT * FROM settings LIMIT 1") fun getSettingsFlow(): Flow<Settings>
fun getSettingsFlow(): Flow<Settings>
@Query("SELECT * FROM settings") @Query("SELECT * FROM settings") fun getAllFlow(): Flow<MutableList<Settings>>
fun getAllFlow(): Flow<MutableList<Settings>>
@Delete @Delete suspend fun delete(t: Settings)
suspend fun delete(t: Settings)
@Query("SELECT COUNT('id') FROM settings") @Query("SELECT COUNT('id') FROM settings") suspend fun count(): Long
suspend fun count(): Long
} }
@@ -11,48 +11,40 @@ import kotlinx.coroutines.flow.Flow
@Dao @Dao
interface TunnelConfigDao { interface TunnelConfigDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun save(t: TunnelConfig)
suspend fun save(t: TunnelConfig)
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun saveAll(t: TunnelConfigs)
suspend fun saveAll(t: TunnelConfigs)
@Query("SELECT * FROM TunnelConfig WHERE id=:id") @Query("SELECT * FROM TunnelConfig WHERE id=:id") suspend fun getById(id: Long): TunnelConfig?
suspend fun getById(id: Long): TunnelConfig?
@Query("SELECT * FROM TunnelConfig WHERE name=:name") @Query("SELECT * FROM TunnelConfig WHERE name=:name")
suspend fun getByName(name: String): TunnelConfig? suspend fun getByName(name: String): TunnelConfig?
@Query("SELECT * FROM TunnelConfig WHERE is_Active=1") @Query("SELECT * FROM TunnelConfig WHERE is_Active=1") suspend fun getActive(): TunnelConfigs
suspend fun getActive(): TunnelConfigs
@Query("SELECT * FROM TunnelConfig") @Query("SELECT * FROM TunnelConfig") suspend fun getAll(): TunnelConfigs
suspend fun getAll(): TunnelConfigs
@Delete @Delete suspend fun delete(t: TunnelConfig)
suspend fun delete(t: TunnelConfig)
@Query("SELECT COUNT('id') FROM TunnelConfig") @Query("SELECT COUNT('id') FROM TunnelConfig") suspend fun count(): Long
suspend fun count(): Long
@Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'") @Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'")
suspend fun findByTunnelNetworkName(name: String): TunnelConfigs suspend fun findByTunnelNetworkName(name: String): TunnelConfigs
@Query("UPDATE TunnelConfig SET is_primary_tunnel = 0 WHERE is_primary_tunnel =1") @Query("UPDATE TunnelConfig SET is_primary_tunnel = 0 WHERE is_primary_tunnel =1")
suspend fun resetPrimaryTunnel() suspend fun resetPrimaryTunnel()
@Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1") @Query("UPDATE TunnelConfig SET is_mobile_data_tunnel = 0 WHERE is_mobile_data_tunnel =1")
suspend fun resetMobileDataTunnel() suspend fun resetMobileDataTunnel()
@Query("UPDATE TunnelConfig SET is_ethernet_tunnel = 0 WHERE is_ethernet_tunnel =1") @Query("UPDATE TunnelConfig SET is_ethernet_tunnel = 0 WHERE is_ethernet_tunnel =1")
suspend fun resetEthernetTunnel() suspend fun resetEthernetTunnel()
@Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1") @Query("SELECT * FROM TUNNELCONFIG WHERE is_primary_tunnel=1")
suspend fun findByPrimary(): TunnelConfigs suspend fun findByPrimary(): TunnelConfigs
@Query("SELECT * FROM TUNNELCONFIG WHERE is_mobile_data_tunnel=1") @Query("SELECT * FROM TUNNELCONFIG WHERE is_mobile_data_tunnel=1")
suspend fun findByMobileDataTunnel(): TunnelConfigs suspend fun findByMobileDataTunnel(): TunnelConfigs
@Query("SELECT * FROM tunnelconfig") @Query("SELECT * FROM tunnelconfig") fun getAllFlow(): Flow<MutableList<TunnelConfig>>
fun getAllFlow(): Flow<MutableList<TunnelConfig>>
} }
@@ -4,50 +4,52 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.AppState
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
data class GeneralState( data class GeneralState(
val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT, val isLocationDisclosureShown: Boolean = LOCATION_DISCLOSURE_SHOWN_DEFAULT,
val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT, val isBatteryOptimizationDisableShown: Boolean = BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT, val isPinLockEnabled: Boolean = PIN_LOCK_ENABLED_DEFAULT,
val isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED, val isTunnelStatsExpanded: Boolean = IS_TUNNEL_STATS_EXPANDED,
val isLocalLogsEnabled: Boolean = IS_LOGS_ENABLED_DEFAULT, val isLocalLogsEnabled: Boolean = IS_LOGS_ENABLED_DEFAULT,
val isRemoteControlEnabled: Boolean = IS_REMOTE_CONTROL_ENABLED, val isRemoteControlEnabled: Boolean = IS_REMOTE_CONTROL_ENABLED,
val remoteKey: String? = null, val remoteKey: String? = null,
val locale: String? = null, val locale: String? = null,
val theme: Theme = Theme.AUTOMATIC, val theme: Theme = Theme.AUTOMATIC,
) { ) {
fun toAppState(): AppState = AppState( fun toAppState(): AppState =
isLocationDisclosureShown, AppState(
isBatteryOptimizationDisableShown, isLocationDisclosureShown,
isPinLockEnabled, isBatteryOptimizationDisableShown,
isTunnelStatsExpanded, isPinLockEnabled,
isLocationDisclosureShown, isTunnelStatsExpanded,
isRemoteControlEnabled, isLocalLogsEnabled,
remoteKey, isRemoteControlEnabled,
locale, remoteKey,
theme, locale,
) theme,
)
companion object { companion object {
fun from(appState: AppState): GeneralState { fun from(appState: AppState): GeneralState {
return with(appState) { return with(appState) {
GeneralState( GeneralState(
isLocationDisclosureShown, isLocationDisclosureShown,
isBatteryOptimizationDisableShown, isBatteryOptimizationDisableShown,
isPinLockEnabled, isPinLockEnabled,
isTunnelStatsExpanded, isTunnelStatsExpanded,
isLocalLogsEnabled, isLocalLogsEnabled,
isRemoteControlEnabled, isRemoteControlEnabled,
remoteKey, remoteKey,
locale, locale,
theme, theme,
) )
} }
} }
const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false const val LOCATION_DISCLOSURE_SHOWN_DEFAULT = false
const val PIN_LOCK_ENABLED_DEFAULT = false const val BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT = false
const val IS_TUNNEL_STATS_EXPANDED = false const val PIN_LOCK_ENABLED_DEFAULT = false
const val IS_LOGS_ENABLED_DEFAULT = false const val IS_TUNNEL_STATS_EXPANDED = false
const val IS_REMOTE_CONTROL_ENABLED = false const val IS_LOGS_ENABLED_DEFAULT = false
} const val IS_REMOTE_CONTROL_ENABLED = false
}
} }
@@ -7,112 +7,100 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
@Entity @Entity
data class Settings( data class Settings(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "is_tunnel_enabled") val isAutoTunnelEnabled: Boolean = false, @ColumnInfo(name = "is_tunnel_enabled") val isAutoTunnelEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled") @ColumnInfo(name = "is_tunnel_on_mobile_data_enabled")
val isTunnelOnMobileDataEnabled: Boolean = false, val isTunnelOnMobileDataEnabled: Boolean = false,
@ColumnInfo(name = "trusted_network_ssids") @ColumnInfo(name = "trusted_network_ssids")
val trustedNetworkSSIDs: MutableList<String> = mutableListOf(), val trustedNetworkSSIDs: MutableList<String> = mutableListOf(),
@ColumnInfo(name = "is_always_on_vpn_enabled") val isAlwaysOnVpnEnabled: Boolean = false, @ColumnInfo(name = "is_always_on_vpn_enabled") val isAlwaysOnVpnEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled") @ColumnInfo(name = "is_tunnel_on_ethernet_enabled")
val isTunnelOnEthernetEnabled: Boolean = false, val isTunnelOnEthernetEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(name = "is_shortcuts_enabled", defaultValue = "false")
name = "is_shortcuts_enabled", val isShortcutsEnabled: Boolean = false,
defaultValue = "false", @ColumnInfo(name = "is_tunnel_on_wifi_enabled", defaultValue = "false")
) val isTunnelOnWifiEnabled: Boolean = false,
val isShortcutsEnabled: Boolean = false, @ColumnInfo(name = "is_kernel_enabled", defaultValue = "false")
@ColumnInfo( val isKernelEnabled: Boolean = false,
name = "is_tunnel_on_wifi_enabled", @ColumnInfo(name = "is_restore_on_boot_enabled", defaultValue = "false")
defaultValue = "false", val isRestoreOnBootEnabled: Boolean = false,
) @ColumnInfo(name = "is_multi_tunnel_enabled", defaultValue = "false")
val isTunnelOnWifiEnabled: Boolean = false, val isMultiTunnelEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(name = "is_ping_enabled", defaultValue = "false")
name = "is_kernel_enabled", val isPingEnabled: Boolean = false,
defaultValue = "false", @ColumnInfo(name = "is_amnezia_enabled", defaultValue = "false")
) val isAmneziaEnabled: Boolean = false,
val isKernelEnabled: Boolean = false, @ColumnInfo(name = "is_wildcards_enabled", defaultValue = "false")
@ColumnInfo( val isWildcardsEnabled: Boolean = false,
name = "is_restore_on_boot_enabled", @ColumnInfo(name = "is_wifi_by_shell_enabled", defaultValue = "false")
defaultValue = "false", val isWifiNameByShellEnabled: Boolean = false,
) @ColumnInfo(name = "is_stop_on_no_internet_enabled", defaultValue = "false")
val isRestoreOnBootEnabled: Boolean = false, val isStopOnNoInternetEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(name = "is_vpn_kill_switch_enabled", defaultValue = "false")
name = "is_multi_tunnel_enabled", val isVpnKillSwitchEnabled: Boolean = false,
defaultValue = "false", @ColumnInfo(name = "is_kernel_kill_switch_enabled", defaultValue = "false")
) val isKernelKillSwitchEnabled: Boolean = false,
val isMultiTunnelEnabled: Boolean = false, @ColumnInfo(name = "is_lan_on_kill_switch_enabled", defaultValue = "false")
@ColumnInfo( val isLanOnKillSwitchEnabled: Boolean = false,
name = "is_ping_enabled", @ColumnInfo(name = "debounce_delay_seconds", defaultValue = "3")
defaultValue = "false", val debounceDelaySeconds: Int = 3,
) @ColumnInfo(name = "is_disable_kill_switch_on_trusted_enabled", defaultValue = "false")
val isPingEnabled: Boolean = false, val isDisableKillSwitchOnTrustedEnabled: Boolean = false,
@ColumnInfo(
name = "is_amnezia_enabled",
defaultValue = "false",
)
val isAmneziaEnabled: Boolean = false,
@ColumnInfo(
name = "is_wildcards_enabled",
defaultValue = "false",
)
val isWildcardsEnabled: Boolean = false,
@ColumnInfo(
name = "is_wifi_by_shell_enabled",
defaultValue = "false",
)
val isWifiNameByShellEnabled: Boolean = false,
@ColumnInfo(
name = "is_stop_on_no_internet_enabled",
defaultValue = "false",
)
val isStopOnNoInternetEnabled: Boolean = false,
@ColumnInfo(
name = "is_vpn_kill_switch_enabled",
defaultValue = "false",
)
val isVpnKillSwitchEnabled: Boolean = false,
@ColumnInfo(
name = "is_kernel_kill_switch_enabled",
defaultValue = "false",
)
val isKernelKillSwitchEnabled: Boolean = false,
@ColumnInfo(
name = "is_lan_on_kill_switch_enabled",
defaultValue = "false",
)
val isLanOnKillSwitchEnabled: Boolean = false,
@ColumnInfo(
name = "debounce_delay_seconds",
defaultValue = "3",
)
val debounceDelaySeconds: Int = 3,
@ColumnInfo(
name = "is_disable_kill_switch_on_trusted_enabled",
defaultValue = "false",
)
val isDisableKillSwitchOnTrustedEnabled: Boolean = false,
) { ) {
fun toAppSettings(): AppSettings { fun toAppSettings(): AppSettings {
return AppSettings( return AppSettings(
id, isAutoTunnelEnabled, isTunnelOnMobileDataEnabled, trustedNetworkSSIDs, isAlwaysOnVpnEnabled, isTunnelOnEthernetEnabled, id,
isShortcutsEnabled, isTunnelOnWifiEnabled, isKernelEnabled, isRestoreOnBootEnabled, isMultiTunnelEnabled, isPingEnabled, isAutoTunnelEnabled,
isAmneziaEnabled, isWildcardsEnabled, isWifiNameByShellEnabled, isStopOnNoInternetEnabled, isVpnKillSwitchEnabled, isTunnelOnMobileDataEnabled,
isKernelKillSwitchEnabled, isLanOnKillSwitchEnabled, debounceDelaySeconds, isDisableKillSwitchOnTrustedEnabled, trustedNetworkSSIDs,
) isAlwaysOnVpnEnabled,
} isTunnelOnEthernetEnabled,
isShortcutsEnabled,
isTunnelOnWifiEnabled,
isKernelEnabled,
isRestoreOnBootEnabled,
isMultiTunnelEnabled,
isPingEnabled,
isAmneziaEnabled,
isWildcardsEnabled,
isWifiNameByShellEnabled,
isStopOnNoInternetEnabled,
isVpnKillSwitchEnabled,
isKernelKillSwitchEnabled,
isLanOnKillSwitchEnabled,
debounceDelaySeconds,
isDisableKillSwitchOnTrustedEnabled,
)
}
companion object { companion object {
fun from(appSettings: AppSettings): Settings { fun from(appSettings: AppSettings): Settings {
return with(appSettings) { return with(appSettings) {
Settings( Settings(
id, isAutoTunnelEnabled, isTunnelOnMobileDataEnabled, trustedNetworkSSIDs.toMutableList(), isAlwaysOnVpnEnabled, id,
isTunnelOnEthernetEnabled, isShortcutsEnabled, isTunnelOnWifiEnabled, isKernelEnabled, isRestoreOnBootEnabled, isAutoTunnelEnabled,
isMultiTunnelEnabled, isPingEnabled, isAmneziaEnabled, isWildcardsEnabled, isWifiNameByShellEnabled, isTunnelOnMobileDataEnabled,
isStopOnNoInternetEnabled, isVpnKillSwitchEnabled, isKernelKillSwitchEnabled, isLanOnKillSwitchEnabled, trustedNetworkSSIDs.toMutableList(),
debounceDelaySeconds, isDisableKillSwitchOnTrustedEnabled, isAlwaysOnVpnEnabled,
) isTunnelOnEthernetEnabled,
} isShortcutsEnabled,
} isTunnelOnWifiEnabled,
} isKernelEnabled,
isRestoreOnBootEnabled,
isMultiTunnelEnabled,
isPingEnabled,
isAmneziaEnabled,
isWildcardsEnabled,
isWifiNameByShellEnabled,
isStopOnNoInternetEnabled,
isVpnKillSwitchEnabled,
isKernelKillSwitchEnabled,
isLanOnKillSwitchEnabled,
debounceDelaySeconds,
isDisableKillSwitchOnTrustedEnabled,
)
}
}
}
} }
@@ -8,86 +8,70 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
@Entity(indices = [Index(value = ["name"], unique = true)]) @Entity(indices = [Index(value = ["name"], unique = true)])
data class TunnelConfig( data class TunnelConfig(
@PrimaryKey(autoGenerate = true) val id: Int = 0, @PrimaryKey(autoGenerate = true) val id: Int = 0,
@ColumnInfo(name = "name") val name: String, @ColumnInfo(name = "name") val name: String,
@ColumnInfo(name = "wg_quick") val wgQuick: String, @ColumnInfo(name = "wg_quick") val wgQuick: String,
@ColumnInfo( @ColumnInfo(name = "tunnel_networks", defaultValue = "")
name = "tunnel_networks", val tunnelNetworks: MutableList<String> = mutableListOf(),
defaultValue = "", @ColumnInfo(name = "is_mobile_data_tunnel", defaultValue = "false")
) val isMobileDataTunnel: Boolean = false,
val tunnelNetworks: MutableList<String> = mutableListOf(), @ColumnInfo(name = "is_primary_tunnel", defaultValue = "false")
@ColumnInfo( val isPrimaryTunnel: Boolean = false,
name = "is_mobile_data_tunnel", @ColumnInfo(name = "am_quick", defaultValue = "") val amQuick: String = AM_QUICK_DEFAULT,
defaultValue = "false", @ColumnInfo(name = "is_Active", defaultValue = "false") val isActive: Boolean = false,
) @ColumnInfo(name = "is_ping_enabled", defaultValue = "false")
val isMobileDataTunnel: Boolean = false, val isPingEnabled: Boolean = false,
@ColumnInfo( @ColumnInfo(name = "ping_interval", defaultValue = "null") val pingInterval: Long? = null,
name = "is_primary_tunnel", @ColumnInfo(name = "ping_cooldown", defaultValue = "null") val pingCooldown: Long? = null,
defaultValue = "false", @ColumnInfo(name = "ping_ip", defaultValue = "null") var pingIp: String? = null,
) @ColumnInfo(name = "is_ethernet_tunnel", defaultValue = "false")
val isPrimaryTunnel: Boolean = false, var isEthernetTunnel: Boolean = false,
@ColumnInfo( @ColumnInfo(name = "is_ipv4_preferred", defaultValue = "true")
name = "am_quick", var isIpv4Preferred: Boolean = true,
defaultValue = "",
)
val amQuick: String = AM_QUICK_DEFAULT,
@ColumnInfo(
name = "is_Active",
defaultValue = "false",
)
val isActive: Boolean = false,
@ColumnInfo(
name = "is_ping_enabled",
defaultValue = "false",
)
val isPingEnabled: Boolean = false,
@ColumnInfo(
name = "ping_interval",
defaultValue = "null",
)
val pingInterval: Long? = null,
@ColumnInfo(
name = "ping_cooldown",
defaultValue = "null",
)
val pingCooldown: Long? = null,
@ColumnInfo(
name = "ping_ip",
defaultValue = "null",
)
var pingIp: String? = null,
@ColumnInfo(
name = "is_ethernet_tunnel",
defaultValue = "false",
)
var isEthernetTunnel: Boolean = false,
@ColumnInfo(
name = "is_ipv4_preferred",
defaultValue = "true",
)
var isIpv4Preferred: Boolean = true,
) { ) {
fun toTunnel(): TunnelConf { fun toTunnel(): TunnelConf {
return TunnelConf( return TunnelConf(
id, name, wgQuick, tunnelNetworks, isMobileDataTunnel, id,
isPrimaryTunnel, amQuick, isActive, isPingEnabled, pingInterval, name,
pingCooldown, pingIp, isEthernetTunnel, isIpv4Preferred, wgQuick,
) tunnelNetworks,
} isMobileDataTunnel,
isPrimaryTunnel,
amQuick,
isActive,
isPingEnabled,
pingInterval,
pingCooldown,
pingIp,
isEthernetTunnel,
isIpv4Preferred,
)
}
companion object { companion object {
const val AM_QUICK_DEFAULT = "" const val AM_QUICK_DEFAULT = ""
fun from(tunnelConf: TunnelConf): TunnelConfig { fun from(tunnelConf: TunnelConf): TunnelConfig {
return with(tunnelConf) { return with(tunnelConf) {
return TunnelConfig( return TunnelConfig(
id, tunName, wgQuick, tunnelNetworks.toMutableList(), isMobileDataTunnel, id,
isPrimaryTunnel, amQuick, isActive, isPingEnabled, pingInterval, tunName,
pingCooldown, pingIp, isEthernetTunnel, isIpv4Preferred, wgQuick,
) tunnelNetworks.toMutableList(),
} isMobileDataTunnel,
} isPrimaryTunnel,
} amQuick,
isActive,
isPingEnabled,
pingInterval,
pingCooldown,
pingIp,
isEthernetTunnel,
isIpv4Preferred,
)
}
}
}
} }
@@ -10,19 +10,19 @@ import javax.inject.Inject
class AppDataRoomRepository class AppDataRoomRepository
@Inject @Inject
constructor( constructor(
override val settings: AppSettingRepository, override val settings: AppSettingRepository,
override val tunnels: TunnelRepository, override val tunnels: TunnelRepository,
override val appState: AppStateRepository, override val appState: AppStateRepository,
) : AppDataRepository { ) : AppDataRepository {
override suspend fun getPrimaryOrFirstTunnel(): TunnelConf? { override suspend fun getPrimaryOrFirstTunnel(): TunnelConf? {
return tunnels.findPrimary().firstOrNull() ?: tunnels.getAll().firstOrNull() return tunnels.findPrimary().firstOrNull() ?: tunnels.getAll().firstOrNull()
} }
override suspend fun getStartTunnelConfig(): TunnelConf? { override suspend fun getStartTunnelConfig(): TunnelConf? {
tunnels.getActive().let { tunnels.getActive().let {
if (it.isNotEmpty()) return it.first() if (it.isNotEmpty()) return it.first()
return getPrimaryOrFirstTunnel() return getPrimaryOrFirstTunnel()
} }
} }
} }
@@ -9,117 +9,125 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import timber.log.Timber import timber.log.Timber
class DataStoreAppStateRepository( class DataStoreAppStateRepository(private val dataStoreManager: DataStoreManager) :
private val dataStoreManager: DataStoreManager, AppStateRepository {
) : override suspend fun isLocationDisclosureShown(): Boolean {
AppStateRepository { return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown)
override suspend fun isLocationDisclosureShown(): Boolean { ?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown) }
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
}
override suspend fun setLocationDisclosureShown(shown: Boolean) { override suspend fun setLocationDisclosureShown(shown: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.locationDisclosureShown, shown) dataStoreManager.saveToDataStore(DataStoreManager.locationDisclosureShown, shown)
} }
override suspend fun isPinLockEnabled(): Boolean { override suspend fun isPinLockEnabled(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.pinLockEnabled) return dataStoreManager.getFromStore(DataStoreManager.pinLockEnabled)
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT ?: GeneralState.PIN_LOCK_ENABLED_DEFAULT
} }
override suspend fun setPinLockEnabled(enabled: Boolean) { override suspend fun setPinLockEnabled(enabled: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.pinLockEnabled, enabled) dataStoreManager.saveToDataStore(DataStoreManager.pinLockEnabled, enabled)
} }
override suspend fun isBatteryOptimizationDisableShown(): Boolean { override suspend fun isBatteryOptimizationDisableShown(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.batteryDisableShown) return dataStoreManager.getFromStore(DataStoreManager.batteryDisableShown)
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT ?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT
} }
override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) { override suspend fun setBatteryOptimizationDisableShown(shown: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown) dataStoreManager.saveToDataStore(DataStoreManager.batteryDisableShown, shown)
} }
override suspend fun isTunnelStatsExpanded(): Boolean { override suspend fun isTunnelStatsExpanded(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded) return dataStoreManager.getFromStore(DataStoreManager.tunnelStatsExpanded)
?: GeneralState.IS_TUNNEL_STATS_EXPANDED ?: GeneralState.IS_TUNNEL_STATS_EXPANDED
} }
override suspend fun setTunnelStatsExpanded(expanded: Boolean) { override suspend fun setTunnelStatsExpanded(expanded: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.tunnelStatsExpanded, expanded) dataStoreManager.saveToDataStore(DataStoreManager.tunnelStatsExpanded, expanded)
} }
override suspend fun setTheme(theme: Theme) { override suspend fun setTheme(theme: Theme) {
dataStoreManager.saveToDataStore(DataStoreManager.theme, theme.name) dataStoreManager.saveToDataStore(DataStoreManager.theme, theme.name)
} }
override suspend fun getTheme(): Theme { override suspend fun getTheme(): Theme {
return dataStoreManager.getFromStore(DataStoreManager.theme)?.let { return dataStoreManager.getFromStore(DataStoreManager.theme)?.let {
try { try {
Theme.valueOf(it) Theme.valueOf(it)
} catch (_: IllegalArgumentException) { } catch (_: IllegalArgumentException) {
Theme.AUTOMATIC Theme.AUTOMATIC
} }
} ?: Theme.AUTOMATIC } ?: Theme.AUTOMATIC
} }
override suspend fun isLocalLogsEnabled(): Boolean { override suspend fun isLocalLogsEnabled(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.isLocalLogsEnabled) ?: GeneralState.IS_LOGS_ENABLED_DEFAULT return dataStoreManager.getFromStore(DataStoreManager.isLocalLogsEnabled)
} ?: GeneralState.IS_LOGS_ENABLED_DEFAULT
}
override suspend fun setLocalLogsEnabled(enabled: Boolean) { override suspend fun setLocalLogsEnabled(enabled: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.isLocalLogsEnabled, enabled) dataStoreManager.saveToDataStore(DataStoreManager.isLocalLogsEnabled, enabled)
} }
override suspend fun setLocale(localeTag: String) { override suspend fun setLocale(localeTag: String) {
dataStoreManager.saveToDataStore(DataStoreManager.locale, localeTag) dataStoreManager.saveToDataStore(DataStoreManager.locale, localeTag)
} }
override suspend fun getLocale(): String? { override suspend fun getLocale(): String? {
return dataStoreManager.getFromStore(DataStoreManager.locale) return dataStoreManager.getFromStore(DataStoreManager.locale)
} }
override suspend fun setIsRemoteControlEnabled(enabled: Boolean) { override suspend fun setIsRemoteControlEnabled(enabled: Boolean) {
dataStoreManager.saveToDataStore(DataStoreManager.isRemoteControlEnabled, enabled) dataStoreManager.saveToDataStore(DataStoreManager.isRemoteControlEnabled, enabled)
} }
override suspend fun isRemoteControlEnabled(): Boolean { override suspend fun isRemoteControlEnabled(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.isRemoteControlEnabled) ?: GeneralState.IS_REMOTE_CONTROL_ENABLED return dataStoreManager.getFromStore(DataStoreManager.isRemoteControlEnabled)
} ?: GeneralState.IS_REMOTE_CONTROL_ENABLED
}
override suspend fun setRemoteKey(key: String) { override suspend fun setRemoteKey(key: String) {
dataStoreManager.saveToDataStore(DataStoreManager.remoteKey, key) dataStoreManager.saveToDataStore(DataStoreManager.remoteKey, key)
} }
override suspend fun getRemoteKey(): String? { override suspend fun getRemoteKey(): String? {
return dataStoreManager.getFromStore(DataStoreManager.remoteKey) return dataStoreManager.getFromStore(DataStoreManager.remoteKey)
} }
override val flow: Flow<AppState> = override val flow: Flow<AppState> =
dataStoreManager.preferencesFlow.map { prefs -> dataStoreManager.preferencesFlow
prefs?.let { pref -> .map { prefs ->
try { prefs?.let { pref ->
GeneralState( try {
isLocationDisclosureShown = GeneralState(
pref[DataStoreManager.locationDisclosureShown] isLocationDisclosureShown =
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT, pref[DataStoreManager.locationDisclosureShown]
isBatteryOptimizationDisableShown = ?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT,
pref[DataStoreManager.batteryDisableShown] isBatteryOptimizationDisableShown =
?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT, pref[DataStoreManager.batteryDisableShown]
isPinLockEnabled = ?: GeneralState.BATTERY_OPTIMIZATION_DISABLE_SHOWN_DEFAULT,
pref[DataStoreManager.pinLockEnabled] isPinLockEnabled =
?: GeneralState.PIN_LOCK_ENABLED_DEFAULT, pref[DataStoreManager.pinLockEnabled]
isTunnelStatsExpanded = pref[DataStoreManager.tunnelStatsExpanded] ?: GeneralState.IS_TUNNEL_STATS_EXPANDED, ?: GeneralState.PIN_LOCK_ENABLED_DEFAULT,
isLocalLogsEnabled = pref[DataStoreManager.isLocalLogsEnabled] ?: GeneralState.IS_LOGS_ENABLED_DEFAULT, isTunnelStatsExpanded =
isRemoteControlEnabled = pref[DataStoreManager.isRemoteControlEnabled] ?: GeneralState.IS_REMOTE_CONTROL_ENABLED, pref[DataStoreManager.tunnelStatsExpanded]
remoteKey = pref[DataStoreManager.remoteKey], ?: GeneralState.IS_TUNNEL_STATS_EXPANDED,
locale = pref[DataStoreManager.locale], isLocalLogsEnabled =
theme = getTheme(), pref[DataStoreManager.isLocalLogsEnabled]
) ?: GeneralState.IS_LOGS_ENABLED_DEFAULT,
} catch (e: IllegalArgumentException) { isRemoteControlEnabled =
Timber.e(e) pref[DataStoreManager.isRemoteControlEnabled]
GeneralState() ?: GeneralState.IS_REMOTE_CONTROL_ENABLED,
} remoteKey = pref[DataStoreManager.remoteKey],
} ?: GeneralState() locale = pref[DataStoreManager.locale],
}.map { it.toAppState() } theme = getTheme(),
)
} catch (e: IllegalArgumentException) {
Timber.e(e)
GeneralState()
}
} ?: GeneralState()
}
.map { it.toAppState() }
} }
@@ -11,21 +11,20 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class RoomSettingsRepository( class RoomSettingsRepository(
private val settingsDoa: SettingsDao, private val settingsDoa: SettingsDao,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : AppSettingRepository { ) : AppSettingRepository {
override suspend fun save(appSettings: AppSettings) { override suspend fun save(appSettings: AppSettings) {
withContext(ioDispatcher) { withContext(ioDispatcher) { settingsDoa.save(Settings.from(appSettings)) }
settingsDoa.save(Settings.from(appSettings)) }
}
}
override val flow = settingsDoa.getSettingsFlow().flowOn(ioDispatcher).map { it.toAppSettings() } override val flow =
settingsDoa.getSettingsFlow().flowOn(ioDispatcher).map { it.toAppSettings() }
override suspend fun get(): AppSettings { override suspend fun get(): AppSettings {
return withContext(ioDispatcher) { return withContext(ioDispatcher) {
(settingsDoa.getAll().firstOrNull() ?: Settings()).toAppSettings() (settingsDoa.getAll().firstOrNull() ?: Settings()).toAppSettings()
} }
} }
} }
@@ -12,102 +12,81 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class RoomTunnelRepository( class RoomTunnelRepository(
private val tunnelConfigDao: TunnelConfigDao, private val tunnelConfigDao: TunnelConfigDao,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher, @IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : TunnelRepository { ) : TunnelRepository {
override val flow = tunnelConfigDao.getAllFlow().flowOn(ioDispatcher).map { it.map { it.toTunnel() } } override val flow =
tunnelConfigDao.getAllFlow().flowOn(ioDispatcher).map { it.map { it.toTunnel() } }
override suspend fun getAll(): Tunnels { override suspend fun getAll(): Tunnels {
return withContext(ioDispatcher) { return withContext(ioDispatcher) { tunnelConfigDao.getAll().map { it.toTunnel() } }
tunnelConfigDao.getAll().map { it.toTunnel() } }
}
}
override suspend fun save(tunnelConf: TunnelConf) { override suspend fun save(tunnelConf: TunnelConf) {
withContext(ioDispatcher) { withContext(ioDispatcher) { tunnelConfigDao.save(TunnelConfig.from(tunnelConf)) }
tunnelConfigDao.save(TunnelConfig.from(tunnelConf)) }
}
}
override suspend fun saveAll(tunnelConfList: List<TunnelConf>) { override suspend fun saveAll(tunnelConfList: List<TunnelConf>) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
tunnelConfigDao.saveAll(tunnelConfList.map(TunnelConfig::from)) tunnelConfigDao.saveAll(tunnelConfList.map(TunnelConfig::from))
} }
} }
override suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) { override suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
tunnelConfigDao.resetPrimaryTunnel() tunnelConfigDao.resetPrimaryTunnel()
tunnelConf?.let { tunnelConf?.let { save(it.copy(isPrimaryTunnel = true)) }
save( }
it.copy( }
isPrimaryTunnel = true,
),
)
}
}
}
override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) { override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
tunnelConfigDao.resetMobileDataTunnel() tunnelConfigDao.resetMobileDataTunnel()
tunnelConf?.let { tunnelConf?.let { save(it.copy(isMobileDataTunnel = true)) }
save( }
it.copy( }
isMobileDataTunnel = true,
),
)
}
}
}
override suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?) { override suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
tunnelConfigDao.resetEthernetTunnel() tunnelConfigDao.resetEthernetTunnel()
tunnelConf?.let { tunnelConf?.let { save(it.copy(isEthernetTunnel = true)) }
save( }
it.copy( }
isEthernetTunnel = true,
),
)
}
}
}
override suspend fun delete(tunnelConf: TunnelConf) { override suspend fun delete(tunnelConf: TunnelConf) {
withContext(ioDispatcher) { withContext(ioDispatcher) { tunnelConfigDao.delete(TunnelConfig.from(tunnelConf)) }
tunnelConfigDao.delete(TunnelConfig.from(tunnelConf)) }
}
}
override suspend fun getById(id: Int): TunnelConf? { override suspend fun getById(id: Int): TunnelConf? {
return withContext(ioDispatcher) { tunnelConfigDao.getById(id.toLong())?.toTunnel() } return withContext(ioDispatcher) { tunnelConfigDao.getById(id.toLong())?.toTunnel() }
} }
override suspend fun getActive(): Tunnels { override suspend fun getActive(): Tunnels {
return withContext(ioDispatcher) { return withContext(ioDispatcher) { tunnelConfigDao.getActive().map { it.toTunnel() } }
tunnelConfigDao.getActive().map { it.toTunnel() } }
}
}
override suspend fun count(): Int { override suspend fun count(): Int {
return withContext(ioDispatcher) { tunnelConfigDao.count().toInt() } return withContext(ioDispatcher) { tunnelConfigDao.count().toInt() }
} }
override suspend fun findByTunnelName(name: String): TunnelConf? { override suspend fun findByTunnelName(name: String): TunnelConf? {
return withContext(ioDispatcher) { tunnelConfigDao.getByName(name)?.toTunnel() } return withContext(ioDispatcher) { tunnelConfigDao.getByName(name)?.toTunnel() }
} }
override suspend fun findByTunnelNetworksName(name: String): Tunnels { override suspend fun findByTunnelNetworksName(name: String): Tunnels {
return withContext(ioDispatcher) { tunnelConfigDao.findByTunnelNetworkName(name).map { it.toTunnel() } } return withContext(ioDispatcher) {
} tunnelConfigDao.findByTunnelNetworkName(name).map { it.toTunnel() }
}
}
override suspend fun findByMobileDataTunnel(): Tunnels { override suspend fun findByMobileDataTunnel(): Tunnels {
return withContext(ioDispatcher) { tunnelConfigDao.findByMobileDataTunnel().map { it.toTunnel() } } return withContext(ioDispatcher) {
} tunnelConfigDao.findByMobileDataTunnel().map { it.toTunnel() }
}
}
override suspend fun findPrimary(): Tunnels { override suspend fun findPrimary(): Tunnels {
return withContext(ioDispatcher) { tunnelConfigDao.findByPrimary().map { it.toTunnel() } } return withContext(ioDispatcher) { tunnelConfigDao.findByPrimary().map { it.toTunnel() } }
} }
} }
@@ -12,35 +12,39 @@ import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.SupervisorJob
import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class AppModule { class AppModule {
@Singleton @Singleton
@ApplicationScope @ApplicationScope
@Provides @Provides
fun providesApplicationScope(@DefaultDispatcher defaultDispatcher: CoroutineDispatcher): CoroutineScope = fun providesApplicationScope(
CoroutineScope(SupervisorJob() + defaultDispatcher) @DefaultDispatcher defaultDispatcher: CoroutineDispatcher
): CoroutineScope = CoroutineScope(SupervisorJob() + defaultDispatcher)
@Singleton @Singleton
@Provides @Provides
fun provideLogCollect(@ApplicationContext context: Context): LogReader { fun provideLogCollect(@ApplicationContext context: Context): LogReader {
return LogcatReader.init(storageDir = context.filesDir.absolutePath) return LogcatReader.init(storageDir = context.filesDir.absolutePath)
} }
@Singleton @Singleton
@Provides @Provides
fun provideNotificationService(@ApplicationContext context: Context): NotificationManager { fun provideNotificationService(@ApplicationContext context: Context): NotificationManager {
return WireGuardNotification(context) return WireGuardNotification(context)
} }
@Singleton @Singleton
@Provides @Provides
fun provideShortcutManager(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): ShortcutManager { fun provideShortcutManager(
return DynamicShortcutManager(context, ioDispatcher) @ApplicationContext context: Context,
} @IoDispatcher ioDispatcher: CoroutineDispatcher,
): ShortcutManager {
return DynamicShortcutManager(context, ioDispatcher)
}
} }
@@ -2,18 +2,10 @@ package com.zaneschepke.wireguardautotunnel.di
import javax.inject.Qualifier import javax.inject.Qualifier
@Qualifier @Qualifier @Retention(AnnotationRetention.BINARY) annotation class TunnelShell
@Retention(AnnotationRetention.BINARY)
annotation class TunnelShell
@Qualifier @Qualifier @Retention(AnnotationRetention.BINARY) annotation class AppShell
@Retention(AnnotationRetention.BINARY)
annotation class AppShell
@Qualifier @Qualifier @Retention(AnnotationRetention.BINARY) annotation class Kernel
@Retention(AnnotationRetention.BINARY)
annotation class Kernel
@Qualifier @Qualifier @Retention(AnnotationRetention.BINARY) annotation class Userspace
@Retention(AnnotationRetention.BINARY)
annotation class Userspace
@@ -2,26 +2,14 @@ package com.zaneschepke.wireguardautotunnel.di
import javax.inject.Qualifier import javax.inject.Qualifier
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class DefaultDispatcher
@Qualifier
annotation class DefaultDispatcher
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class IoDispatcher
@Qualifier
annotation class IoDispatcher
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class MainDispatcher
@Qualifier
annotation class MainDispatcher
@Retention(AnnotationRetention.BINARY) @Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainImmediateDispatcher
@Qualifier
annotation class MainImmediateDispatcher
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ApplicationScope
@Qualifier
annotation class ApplicationScope
@Retention(AnnotationRetention.RUNTIME) @Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ServiceScope
@Qualifier
annotation class ServiceScope
@@ -10,19 +10,15 @@ import kotlinx.coroutines.Dispatchers
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
object CoroutinesDispatchersModule { object CoroutinesDispatchersModule {
@DefaultDispatcher @DefaultDispatcher
@Provides @Provides
fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default fun providesDefaultDispatcher(): CoroutineDispatcher = Dispatchers.Default
@IoDispatcher @IoDispatcher @Provides fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
@Provides
fun providesIoDispatcher(): CoroutineDispatcher = Dispatchers.IO
@MainDispatcher @MainDispatcher @Provides fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
@Provides
fun providesMainDispatcher(): CoroutineDispatcher = Dispatchers.Main
@MainImmediateDispatcher @MainImmediateDispatcher
@Provides @Provides
fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate fun providesMainImmediateDispatcher(): CoroutineDispatcher = Dispatchers.Main.immediate
} }
@@ -21,68 +21,77 @@ import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import kotlinx.coroutines.CoroutineDispatcher
import javax.inject.Singleton import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class RepositoryModule { class RepositoryModule {
@Provides @Provides
@Singleton @Singleton
fun provideDatabase(@ApplicationContext context: Context): AppDatabase { fun provideDatabase(@ApplicationContext context: Context): AppDatabase {
return Room.databaseBuilder( return Room.databaseBuilder(
context, context,
AppDatabase::class.java, AppDatabase::class.java,
context.getString(R.string.db_name), context.getString(R.string.db_name),
) )
.fallbackToDestructiveMigration() .fallbackToDestructiveMigration()
.addCallback(DatabaseCallback()) .addCallback(DatabaseCallback())
.build() .build()
} }
@Singleton @Singleton
@Provides @Provides
fun provideSettingsDoa(appDatabase: AppDatabase): SettingsDao { fun provideSettingsDoa(appDatabase: AppDatabase): SettingsDao {
return appDatabase.settingDao() return appDatabase.settingDao()
} }
@Singleton @Singleton
@Provides @Provides
fun provideTunnelConfigDoa(appDatabase: AppDatabase): TunnelConfigDao { fun provideTunnelConfigDoa(appDatabase: AppDatabase): TunnelConfigDao {
return appDatabase.tunnelConfigDoa() return appDatabase.tunnelConfigDoa()
} }
@Singleton @Singleton
@Provides @Provides
fun provideTunnelConfigRepository(tunnelConfigDao: TunnelConfigDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): TunnelRepository { fun provideTunnelConfigRepository(
return RoomTunnelRepository(tunnelConfigDao, ioDispatcher) tunnelConfigDao: TunnelConfigDao,
} @IoDispatcher ioDispatcher: CoroutineDispatcher,
): TunnelRepository {
return RoomTunnelRepository(tunnelConfigDao, ioDispatcher)
}
@Singleton @Singleton
@Provides @Provides
fun provideSettingsRepository(settingsDao: SettingsDao, @IoDispatcher ioDispatcher: CoroutineDispatcher): AppSettingRepository { fun provideSettingsRepository(
return RoomSettingsRepository(settingsDao, ioDispatcher) settingsDao: SettingsDao,
} @IoDispatcher ioDispatcher: CoroutineDispatcher,
): AppSettingRepository {
return RoomSettingsRepository(settingsDao, ioDispatcher)
}
@Singleton @Singleton
@Provides @Provides
fun providePreferencesDataStore(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): DataStoreManager { fun providePreferencesDataStore(
return DataStoreManager(context, ioDispatcher) @ApplicationContext context: Context,
} @IoDispatcher ioDispatcher: CoroutineDispatcher,
): DataStoreManager {
return DataStoreManager(context, ioDispatcher)
}
@Provides @Provides
@Singleton @Singleton
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager): AppStateRepository { fun provideGeneralStateRepository(dataStoreManager: DataStoreManager): AppStateRepository {
return DataStoreAppStateRepository(dataStoreManager) return DataStoreAppStateRepository(dataStoreManager)
} }
@Provides @Provides
@Singleton @Singleton
fun provideAppDataRepository( fun provideAppDataRepository(
settingsRepository: AppSettingRepository, settingsRepository: AppSettingRepository,
tunnelRepository: TunnelRepository, tunnelRepository: TunnelRepository,
appStateRepository: AppStateRepository, appStateRepository: AppStateRepository,
): AppDataRepository { ): AppDataRepository {
return AppDataRoomRepository(settingsRepository, tunnelRepository, appStateRepository) return AppDataRoomRepository(settingsRepository, tunnelRepository, appStateRepository)
} }
} }
@@ -18,97 +18,121 @@ import dagger.Provides
import dagger.hilt.InstallIn import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import org.amnezia.awg.backend.Backend import org.amnezia.awg.backend.Backend
import org.amnezia.awg.backend.GoBackend import org.amnezia.awg.backend.GoBackend
import org.amnezia.awg.backend.RootTunnelActionHandler import org.amnezia.awg.backend.RootTunnelActionHandler
import javax.inject.Singleton
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class TunnelModule { class TunnelModule {
@Provides @Provides
@Singleton @Singleton
@TunnelShell @TunnelShell
fun provideTunnelRootShell(@ApplicationContext context: Context): RootShell { fun provideTunnelRootShell(@ApplicationContext context: Context): RootShell {
return RootShell(context) return RootShell(context)
} }
@Provides @Provides
@Singleton @Singleton
@AppShell @AppShell
fun provideAppRootShell(@ApplicationContext context: Context): RootShell { fun provideAppRootShell(@ApplicationContext context: Context): RootShell {
return RootShell(context) return RootShell(context)
} }
@Provides @Provides
@Singleton @Singleton
fun provideAmneziaBackend(@ApplicationContext context: Context): Backend { fun provideAmneziaBackend(@ApplicationContext context: Context): Backend {
return GoBackend(context, RootTunnelActionHandler(org.amnezia.awg.util.RootShell(context))) return GoBackend(context, RootTunnelActionHandler(org.amnezia.awg.util.RootShell(context)))
} }
@Provides @Provides
@Singleton @Singleton
fun provideKernelBackend(@ApplicationContext context: Context, @TunnelShell shell: RootShell): com.wireguard.android.backend.Backend { fun provideKernelBackend(
return WgQuickBackend(context, shell, ToolsInstaller(context, shell), com.wireguard.android.backend.RootTunnelActionHandler(shell)).also { @ApplicationContext context: Context,
it.setMultipleTunnels(true) @TunnelShell shell: RootShell,
} ): com.wireguard.android.backend.Backend {
} return WgQuickBackend(
context,
shell,
ToolsInstaller(context, shell),
com.wireguard.android.backend.RootTunnelActionHandler(shell),
)
.also { it.setMultipleTunnels(true) }
}
@Provides @Provides
@Singleton @Singleton
@Kernel @Kernel
fun provideKernelProvider( fun provideKernelProvider(
@ApplicationScope applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
serviceManager: ServiceManager, serviceManager: ServiceManager,
appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
backend: com.wireguard.android.backend.Backend, backend: com.wireguard.android.backend.Backend,
): TunnelProvider { ): TunnelProvider {
return KernelTunnel(applicationScope, serviceManager, appDataRepository, backend) return KernelTunnel(applicationScope, serviceManager, appDataRepository, backend)
} }
@Provides @Provides
@Singleton @Singleton
@Userspace @Userspace
fun provideUserspaceProvider( fun provideUserspaceProvider(
@ApplicationScope applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
serviceManager: ServiceManager, serviceManager: ServiceManager,
appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
backend: Backend, backend: Backend,
): TunnelProvider { ): TunnelProvider {
return UserspaceTunnel(applicationScope, serviceManager, appDataRepository, backend) return UserspaceTunnel(applicationScope, serviceManager, appDataRepository, backend)
} }
@Provides @Provides
@Singleton @Singleton
fun provideTunnelManager( fun provideTunnelManager(
@Kernel kernelTunnel: TunnelProvider, @Kernel kernelTunnel: TunnelProvider,
@Userspace userspaceTunnel: TunnelProvider, @Userspace userspaceTunnel: TunnelProvider,
appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
@IoDispatcher ioDispatcher: CoroutineDispatcher, @IoDispatcher ioDispatcher: CoroutineDispatcher,
@ApplicationScope applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
): TunnelManager { ): TunnelManager {
return TunnelManager(kernelTunnel, userspaceTunnel, appDataRepository, applicationScope, ioDispatcher) return TunnelManager(
} kernelTunnel,
userspaceTunnel,
appDataRepository,
applicationScope,
ioDispatcher,
)
}
@Provides @Provides
@Singleton @Singleton
fun provideNetworkMonitor(@ApplicationContext context: Context, settingsRepository: AppSettingRepository): NetworkMonitor { fun provideNetworkMonitor(
return AndroidNetworkMonitor(context) { runBlocking { settingsRepository.get().isWifiNameByShellEnabled } } @ApplicationContext context: Context,
} settingsRepository: AppSettingRepository,
): NetworkMonitor {
return AndroidNetworkMonitor(context) {
runBlocking { settingsRepository.get().isWifiNameByShellEnabled }
}
}
@Singleton @Singleton
@Provides @Provides
fun provideServiceManager( fun provideServiceManager(
@ApplicationContext context: Context, @ApplicationContext context: Context,
@IoDispatcher ioDispatcher: CoroutineDispatcher, @IoDispatcher ioDispatcher: CoroutineDispatcher,
@MainDispatcher mainCoroutineDispatcher: CoroutineDispatcher, @MainDispatcher mainCoroutineDispatcher: CoroutineDispatcher,
@ApplicationScope applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
): ServiceManager { ): ServiceManager {
return ServiceManager(context, ioDispatcher, applicationScope, mainCoroutineDispatcher, appDataRepository) return ServiceManager(
} context,
ioDispatcher,
applicationScope,
mainCoroutineDispatcher,
appDataRepository,
)
}
} }
@@ -13,9 +13,12 @@ import kotlinx.coroutines.CoroutineDispatcher
@Module @Module
@InstallIn(ViewModelComponent::class) @InstallIn(ViewModelComponent::class)
class ViewModelModule { class ViewModelModule {
@ViewModelScoped @ViewModelScoped
@Provides @Provides
fun provideFileUtils(@ApplicationContext context: Context, @IoDispatcher ioDispatcher: CoroutineDispatcher): FileUtils { fun provideFileUtils(
return FileUtils(context, ioDispatcher) @ApplicationContext context: Context,
} @IoDispatcher ioDispatcher: CoroutineDispatcher,
): FileUtils {
return FileUtils(context, ioDispatcher)
}
} }
@@ -1,29 +1,29 @@
package com.zaneschepke.wireguardautotunnel.domain.entity package com.zaneschepke.wireguardautotunnel.domain.entity
data class AppSettings( data class AppSettings(
val id: Int = 0, val id: Int = 0,
val isAutoTunnelEnabled: Boolean = false, val isAutoTunnelEnabled: Boolean = false,
val isTunnelOnMobileDataEnabled: Boolean = false, val isTunnelOnMobileDataEnabled: Boolean = false,
val trustedNetworkSSIDs: List<String> = emptyList(), val trustedNetworkSSIDs: List<String> = emptyList(),
val isAlwaysOnVpnEnabled: Boolean = false, val isAlwaysOnVpnEnabled: Boolean = false,
val isTunnelOnEthernetEnabled: Boolean = false, val isTunnelOnEthernetEnabled: Boolean = false,
val isShortcutsEnabled: Boolean = false, val isShortcutsEnabled: Boolean = false,
val isTunnelOnWifiEnabled: Boolean = false, val isTunnelOnWifiEnabled: Boolean = false,
val isKernelEnabled: Boolean = false, val isKernelEnabled: Boolean = false,
val isRestoreOnBootEnabled: Boolean = false, val isRestoreOnBootEnabled: Boolean = false,
val isMultiTunnelEnabled: Boolean = false, val isMultiTunnelEnabled: Boolean = false,
val isPingEnabled: Boolean = false, val isPingEnabled: Boolean = false,
val isAmneziaEnabled: Boolean = false, val isAmneziaEnabled: Boolean = false,
val isWildcardsEnabled: Boolean = false, val isWildcardsEnabled: Boolean = false,
val isWifiNameByShellEnabled: Boolean = false, val isWifiNameByShellEnabled: Boolean = false,
val isStopOnNoInternetEnabled: Boolean = false, val isStopOnNoInternetEnabled: Boolean = false,
val isVpnKillSwitchEnabled: Boolean = false, val isVpnKillSwitchEnabled: Boolean = false,
val isKernelKillSwitchEnabled: Boolean = false, val isKernelKillSwitchEnabled: Boolean = false,
val isLanOnKillSwitchEnabled: Boolean = false, val isLanOnKillSwitchEnabled: Boolean = false,
val debounceDelaySeconds: Int = 3, val debounceDelaySeconds: Int = 3,
val isDisableKillSwitchOnTrustedEnabled: Boolean = false, val isDisableKillSwitchOnTrustedEnabled: Boolean = false,
) { ) {
fun debounceDelayMillis(): Long { fun debounceDelayMillis(): Long {
return debounceDelaySeconds * 1000L return debounceDelaySeconds * 1000L
} }
} }
@@ -3,13 +3,13 @@ package com.zaneschepke.wireguardautotunnel.domain.entity
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
data class AppState( data class AppState(
val isLocationDisclosureShown: Boolean, val isLocationDisclosureShown: Boolean,
val isBatteryOptimizationDisableShown: Boolean, val isBatteryOptimizationDisableShown: Boolean,
val isPinLockEnabled: Boolean, val isPinLockEnabled: Boolean,
val isTunnelStatsExpanded: Boolean, val isTunnelStatsExpanded: Boolean,
val isLocalLogsEnabled: Boolean, val isLocalLogsEnabled: Boolean,
val isRemoteControlEnabled: Boolean, val isRemoteControlEnabled: Boolean,
val remoteKey: String?, val remoteKey: String?,
val locale: String?, val locale: String?,
val theme: Theme, val theme: Theme,
) )
@@ -8,177 +8,227 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable import com.zaneschepke.wireguardautotunnel.util.extensions.isReachable
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.InputStream import java.io.InputStream
import java.net.InetAddress import java.net.InetAddress
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.withContext
import timber.log.Timber
data class TunnelConf( data class TunnelConf(
val id: Int = 0, val id: Int = 0,
val tunName: String, val tunName: String,
val wgQuick: String, val wgQuick: String,
val tunnelNetworks: List<String> = emptyList(), val tunnelNetworks: List<String> = emptyList(),
val isMobileDataTunnel: Boolean = false, val isMobileDataTunnel: Boolean = false,
val isPrimaryTunnel: Boolean = false, val isPrimaryTunnel: Boolean = false,
val amQuick: String, val amQuick: String,
val isActive: Boolean = false, val isActive: Boolean = false,
val isPingEnabled: Boolean = false, val isPingEnabled: Boolean = false,
val pingInterval: Long? = null, val pingInterval: Long? = null,
val pingCooldown: Long? = null, val pingCooldown: Long? = null,
val pingIp: String? = null, val pingIp: String? = null,
val isEthernetTunnel: Boolean = false, val isEthernetTunnel: Boolean = false,
val isIpv4Preferred: Boolean = true, val isIpv4Preferred: Boolean = true,
@Transient @Transient private var stateChangeCallback: ((Any) -> Unit)? = null,
private var stateChangeCallback: ((Any) -> Unit)? = null,
) : Tunnel, org.amnezia.awg.backend.Tunnel { ) : Tunnel, org.amnezia.awg.backend.Tunnel {
fun setStateChangeCallback(callback: (Any) -> Unit) { fun setStateChangeCallback(callback: (Any) -> Unit) {
stateChangeCallback = callback stateChangeCallback = callback
} }
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
if (other !is TunnelConf) return false if (other !is TunnelConf) return false
return id == other.id && tunName == other.tunName && wgQuick == other.wgQuick && amQuick == other.amQuick && return id == other.id &&
isPrimaryTunnel == other.isPrimaryTunnel && isMobileDataTunnel == other.isMobileDataTunnel && tunName == other.tunName &&
isEthernetTunnel == other.isEthernetTunnel && isPingEnabled == other.isPingEnabled && pingIp == other.pingIp && wgQuick == other.wgQuick &&
pingCooldown == other.pingCooldown && pingInterval == other.pingInterval && tunnelNetworks == other.tunnelNetworks && amQuick == other.amQuick &&
isIpv4Preferred == other.isIpv4Preferred isPrimaryTunnel == other.isPrimaryTunnel &&
} isMobileDataTunnel == other.isMobileDataTunnel &&
isEthernetTunnel == other.isEthernetTunnel &&
isPingEnabled == other.isPingEnabled &&
pingIp == other.pingIp &&
pingCooldown == other.pingCooldown &&
pingInterval == other.pingInterval &&
tunnelNetworks == other.tunnelNetworks &&
isIpv4Preferred == other.isIpv4Preferred
}
override fun hashCode(): Int { override fun hashCode(): Int {
var result = id var result = id
result = 31 * result + tunName.hashCode() result = 31 * result + tunName.hashCode()
result = 31 * result + wgQuick.hashCode() result = 31 * result + wgQuick.hashCode()
result = 31 * result + amQuick.hashCode() result = 31 * result + amQuick.hashCode()
return result return result
} }
fun copyWithCallback( fun copyWithCallback(
id: Int = this.id, id: Int = this.id,
tunName: String = this.tunName, tunName: String = this.tunName,
wgQuick: String = this.wgQuick, wgQuick: String = this.wgQuick,
tunnelNetworks: List<String> = this.tunnelNetworks, tunnelNetworks: List<String> = this.tunnelNetworks,
isMobileDataTunnel: Boolean = this.isMobileDataTunnel, isMobileDataTunnel: Boolean = this.isMobileDataTunnel,
isPrimaryTunnel: Boolean = this.isPrimaryTunnel, isPrimaryTunnel: Boolean = this.isPrimaryTunnel,
amQuick: String = this.amQuick, amQuick: String = this.amQuick,
isActive: Boolean = this.isActive, isActive: Boolean = this.isActive,
isPingEnabled: Boolean = this.isPingEnabled, isPingEnabled: Boolean = this.isPingEnabled,
pingInterval: Long? = this.pingInterval, pingInterval: Long? = this.pingInterval,
pingCooldown: Long? = this.pingCooldown, pingCooldown: Long? = this.pingCooldown,
pingIp: String? = this.pingIp, pingIp: String? = this.pingIp,
isEthernetTunnel: Boolean = this.isEthernetTunnel, isEthernetTunnel: Boolean = this.isEthernetTunnel,
isIpv4Preferred: Boolean = this.isIpv4Preferred, isIpv4Preferred: Boolean = this.isIpv4Preferred,
): TunnelConf { ): TunnelConf {
return TunnelConf( return TunnelConf(
id, tunName, wgQuick, tunnelNetworks, isMobileDataTunnel, isPrimaryTunnel, id,
amQuick, isActive, isPingEnabled, pingInterval, pingCooldown, pingIp, tunName,
isEthernetTunnel, isIpv4Preferred, wgQuick,
).apply { tunnelNetworks,
stateChangeCallback = this@TunnelConf.stateChangeCallback isMobileDataTunnel,
// tunnelStatsCallback = this@TunnelConf.tunnelStatsCallback isPrimaryTunnel,
// bounceTunnelCallback = this@TunnelConf.bounceTunnelCallback amQuick,
} isActive,
} isPingEnabled,
pingInterval,
pingCooldown,
pingIp,
isEthernetTunnel,
isIpv4Preferred,
)
.apply {
stateChangeCallback = this@TunnelConf.stateChangeCallback
// tunnelStatsCallback = this@TunnelConf.tunnelStatsCallback
// bounceTunnelCallback = this@TunnelConf.bounceTunnelCallback
}
}
// fun onUpdateStatistics() { // fun onUpdateStatistics() {
// tunnelStatsCallback?.invoke() // tunnelStatsCallback?.invoke()
// } // }
// //
// fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) { // fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
// bounceTunnelCallback?.invoke(tunnelConf, reason) // bounceTunnelCallback?.invoke(tunnelConf, reason)
// } // }
fun toAmConfig(): org.amnezia.awg.config.Config { fun toAmConfig(): org.amnezia.awg.config.Config {
return configFromAmQuick(amQuick.ifBlank { wgQuick }) return configFromAmQuick(amQuick.ifBlank { wgQuick })
} }
fun toWgConfig(): Config { fun toWgConfig(): Config {
return configFromWgQuick(wgQuick) return configFromWgQuick(wgQuick)
} }
override fun getName(): String = tunName override fun getName(): String = tunName
override fun isIpv4ResolutionPreferred(): Boolean = isIpv4Preferred override fun isIpv4ResolutionPreferred(): Boolean = isIpv4Preferred
override fun onStateChange(newState: org.amnezia.awg.backend.Tunnel.State) { override fun onStateChange(newState: org.amnezia.awg.backend.Tunnel.State) {
stateChangeCallback?.invoke(newState) stateChangeCallback?.invoke(newState)
} }
override fun onStateChange(newState: Tunnel.State) { override fun onStateChange(newState: Tunnel.State) {
stateChangeCallback?.invoke(newState) stateChangeCallback?.invoke(newState)
} }
fun isTunnelConfigChanged(updatedConf: TunnelConf): Boolean { fun isTunnelConfigChanged(updatedConf: TunnelConf): Boolean {
return updatedConf.wgQuick != wgQuick || updatedConf.amQuick != amQuick || updatedConf.name != name return updatedConf.wgQuick != wgQuick ||
} updatedConf.amQuick != amQuick ||
updatedConf.name != name
}
fun generateUniqueName(tunnelNames: List<String>): String { fun generateUniqueName(tunnelNames: List<String>): String {
var tunnelName = this.tunName var tunnelName = this.tunName
var num = 1 var num = 1
while (tunnelNames.any { it == tunnelName }) { while (tunnelNames.any { it == tunnelName }) {
tunnelName = if (!tunnelName.hasNumberInParentheses()) { tunnelName =
"$name($num)" if (!tunnelName.hasNumberInParentheses()) {
} else { "$name($num)"
val pair = tunnelName.extractNameAndNumber() } else {
"${pair?.first}($num)" val pair = tunnelName.extractNameAndNumber()
} "${pair?.first}($num)"
num++ }
} num++
return tunnelName }
} return tunnelName
}
suspend fun isTunnelPingable(context: CoroutineContext): Boolean { suspend fun isTunnelPingable(context: CoroutineContext): Boolean {
return withContext(context) { return withContext(context) {
val config = toWgConfig() val config = toWgConfig()
if (pingIp != null) { if (pingIp != null) {
return@withContext InetAddress.getByName(pingIp) return@withContext InetAddress.getByName(pingIp)
.isReachable(Constants.PING_TIMEOUT.toInt()).also { .isReachable(Constants.PING_TIMEOUT.toInt())
Timber.i("Ping reachable $pingIp: $it") .also { Timber.i("Ping reachable $pingIp: $it") }
} }
} config.peers
config.peers.map { peer -> .map { peer -> peer.isReachable() }
peer.isReachable() .all { true }
}.all { true }.also { .also { Timber.i("Ping of all peers reachable: $it") }
Timber.i("Ping of all peers reachable: $it") }
} }
}
}
companion object { companion object {
fun configFromWgQuick(wgQuick: String): Config { fun configFromWgQuick(wgQuick: String): Config {
val inputStream: InputStream = wgQuick.byteInputStream() val inputStream: InputStream = wgQuick.byteInputStream()
return inputStream.bufferedReader(StandardCharsets.UTF_8).use { return inputStream.bufferedReader(StandardCharsets.UTF_8).use { Config.parse(it) }
Config.parse(it) }
}
}
fun configFromAmQuick(amQuick: String): org.amnezia.awg.config.Config { fun configFromAmQuick(amQuick: String): org.amnezia.awg.config.Config {
val inputStream: InputStream = amQuick.byteInputStream() val inputStream: InputStream = amQuick.byteInputStream()
return inputStream.bufferedReader(StandardCharsets.UTF_8).use { return inputStream.bufferedReader(StandardCharsets.UTF_8).use {
org.amnezia.awg.config.Config.parse(it) org.amnezia.awg.config.Config.parse(it)
} }
} }
fun tunnelConfigFromAmConfig(config: org.amnezia.awg.config.Config, name: String? = null): TunnelConf { fun tunnelConfigFromAmConfig(
val amQuick = config.toAwgQuickString(true) config: org.amnezia.awg.config.Config,
val wgQuick = config.toWgQuickString() name: String? = null,
return TunnelConf(tunName = name ?: config.defaultName(), wgQuick = wgQuick, amQuick = amQuick) ): TunnelConf {
} val amQuick = config.toAwgQuickString(true)
val wgQuick = config.toWgQuickString()
return TunnelConf(
tunName = name ?: config.defaultName(),
wgQuick = wgQuick,
amQuick = amQuick,
)
}
private const val IPV6_ALL_NETWORKS = "::/0" private const val IPV6_ALL_NETWORKS = "::/0"
private const val IPV4_ALL_NETWORKS = "0.0.0.0/0" private const val IPV4_ALL_NETWORKS = "0.0.0.0/0"
val ALL_IPS = listOf(IPV4_ALL_NETWORKS, IPV6_ALL_NETWORKS) val ALL_IPS = listOf(IPV4_ALL_NETWORKS, IPV6_ALL_NETWORKS)
private val IPV4_PUBLIC_NETWORKS = listOf( private val IPV4_PUBLIC_NETWORKS =
"0.0.0.0/5", "8.0.0.0/7", "11.0.0.0/8", "12.0.0.0/6", "16.0.0.0/4", "32.0.0.0/3", listOf(
"64.0.0.0/2", "128.0.0.0/3", "160.0.0.0/5", "168.0.0.0/6", "172.0.0.0/12", "0.0.0.0/5",
"172.32.0.0/11", "172.64.0.0/10", "172.128.0.0/9", "173.0.0.0/8", "174.0.0.0/7", "8.0.0.0/7",
"176.0.0.0/4", "192.0.0.0/9", "192.128.0.0/11", "192.160.0.0/13", "192.169.0.0/16", "11.0.0.0/8",
"192.170.0.0/15", "192.172.0.0/14", "192.176.0.0/12", "192.192.0.0/10", "12.0.0.0/6",
"193.0.0.0/8", "194.0.0.0/7", "196.0.0.0/6", "200.0.0.0/5", "208.0.0.0/4", "16.0.0.0/4",
) "32.0.0.0/3",
val LAN_BYPASS_ALLOWED_IPS = listOf(IPV6_ALL_NETWORKS) + IPV4_PUBLIC_NETWORKS "64.0.0.0/2",
} "128.0.0.0/3",
"160.0.0.0/5",
"168.0.0.0/6",
"172.0.0.0/12",
"172.32.0.0/11",
"172.64.0.0/10",
"172.128.0.0/9",
"173.0.0.0/8",
"174.0.0.0/7",
"176.0.0.0/4",
"192.0.0.0/9",
"192.128.0.0/11",
"192.160.0.0/13",
"192.169.0.0/16",
"192.170.0.0/15",
"192.172.0.0/14",
"192.176.0.0/12",
"192.192.0.0/10",
"193.0.0.0/8",
"194.0.0.0/7",
"196.0.0.0/6",
"200.0.0.0/5",
"208.0.0.0/4",
)
val LAN_BYPASS_ALLOWED_IPS = listOf(IPV6_ALL_NETWORKS) + IPV4_PUBLIC_NETWORKS
}
} }
@@ -3,22 +3,31 @@ package com.zaneschepke.wireguardautotunnel.domain.enums
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
sealed class BackendError : Exception() { sealed class BackendError : Exception() {
data object DNS : BackendError() data object DNS : BackendError()
data object Unauthorized : BackendError()
data object Config : BackendError()
data object KernelModuleName : BackendError()
data object InvalidConfig : BackendError()
data object NotAuthorized : BackendError()
data object ServiceNotRunning : BackendError()
data object Unknown : BackendError()
fun toStringRes() = when (this) { data object Unauthorized : BackendError()
Config -> R.string.config_error
DNS -> R.string.dns_resolve_error data object Config : BackendError()
InvalidConfig -> R.string.invalid_config_error
KernelModuleName -> R.string.kernel_name_error data object KernelModuleName : BackendError()
NotAuthorized, Unauthorized -> R.string.auth_error
ServiceNotRunning -> R.string.service_running_error data object InvalidConfig : BackendError()
Unknown -> R.string.unknown_error
} data object NotAuthorized : BackendError()
data object ServiceNotRunning : BackendError()
data object Unknown : BackendError()
fun toStringRes() =
when (this) {
Config -> R.string.config_error
DNS -> R.string.dns_resolve_error
InvalidConfig -> R.string.invalid_config_error
KernelModuleName -> R.string.kernel_name_error
NotAuthorized,
Unauthorized -> R.string.auth_error
ServiceNotRunning -> R.string.service_running_error
Unknown -> R.string.unknown_error
}
} }
@@ -1,7 +1,7 @@
package com.zaneschepke.wireguardautotunnel.domain.enums package com.zaneschepke.wireguardautotunnel.domain.enums
enum class BackendState { enum class BackendState {
KILL_SWITCH_ACTIVE, KILL_SWITCH_ACTIVE,
SERVICE_ACTIVE, SERVICE_ACTIVE,
INACTIVE, INACTIVE,
} }
@@ -1,6 +1,6 @@
package com.zaneschepke.wireguardautotunnel.domain.enums package com.zaneschepke.wireguardautotunnel.domain.enums
enum class ConfigType { enum class ConfigType {
AMNEZIA, AMNEZIA,
WG, WG,
} }
@@ -1,17 +1,16 @@
package com.zaneschepke.wireguardautotunnel.domain.enums package com.zaneschepke.wireguardautotunnel.domain.enums
enum class HandshakeStatus { enum class HandshakeStatus {
HEALTHY, HEALTHY,
STALE, STALE,
UNKNOWN, UNKNOWN,
NOT_STARTED, NOT_STARTED;
;
companion object { companion object {
private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 180 private const val WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC = 180
const val STATUS_CHANGE_TIME_BUFFER = 30 const val STATUS_CHANGE_TIME_BUFFER = 30
const val STALE_TIME_LIMIT_SEC = const val STALE_TIME_LIMIT_SEC =
WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC + STATUS_CHANGE_TIME_BUFFER WG_TYPICAL_HANDSHAKE_INTERVAL_WHEN_HEALTHY_SEC + STATUS_CHANGE_TIME_BUFFER
const val NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC = 30 const val NEVER_CONNECTED_TO_UNHEALTHY_TIME_LIMIT_SEC = 30
} }
} }
@@ -4,14 +4,13 @@ import android.content.Context
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
enum class NotificationAction { enum class NotificationAction {
TUNNEL_OFF, TUNNEL_OFF,
AUTO_TUNNEL_OFF, AUTO_TUNNEL_OFF;
;
fun title(context: Context): String { fun title(context: Context): String {
return when (this) { return when (this) {
TUNNEL_OFF -> context.getString(R.string.stop) TUNNEL_OFF -> context.getString(R.string.stop)
AUTO_TUNNEL_OFF -> context.getString(R.string.stop) AUTO_TUNNEL_OFF -> context.getString(R.string.stop)
} }
} }
} }
@@ -1,31 +1,35 @@
package com.zaneschepke.wireguardautotunnel.domain.enums package com.zaneschepke.wireguardautotunnel.domain.enums
sealed class TunnelStatus { sealed class TunnelStatus {
data class Error(val error: BackendError) : TunnelStatus() data class Error(val error: BackendError) : TunnelStatus()
data object Up : TunnelStatus()
data object Down : TunnelStatus()
data class Stopping(val reason: StopReason) : TunnelStatus()
data object Starting : TunnelStatus()
enum class StopReason { data object Up : TunnelStatus()
USER,
PING,
CONFIG_CHANGED,
}
fun isDown(): Boolean { data object Down : TunnelStatus()
return this == Down
}
fun isUp(): Boolean { data class Stopping(val reason: StopReason) : TunnelStatus()
return this == Up
}
fun isUpOrStarting(): Boolean { data object Starting : TunnelStatus()
return this == Up || this == Starting
}
fun isDownOrStopping(): Boolean { enum class StopReason {
return this == Down || this is Stopping USER,
} PING,
CONFIG_CHANGED,
}
fun isDown(): Boolean {
return this == Down
}
fun isUp(): Boolean {
return this == Up
}
fun isUpOrStarting(): Boolean {
return this == Up || this == Starting
}
fun isDownOrStopping(): Boolean {
return this == Down || this is Stopping
}
} }
@@ -3,7 +3,9 @@ package com.zaneschepke.wireguardautotunnel.domain.events
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
sealed class AutoTunnelEvent { sealed class AutoTunnelEvent {
data class Start(val tunnelConf: TunnelConf? = null) : AutoTunnelEvent() data class Start(val tunnelConf: TunnelConf? = null) : AutoTunnelEvent()
data object Stop : AutoTunnelEvent()
data object DoNothing : AutoTunnelEvent() data object Stop : AutoTunnelEvent()
data object DoNothing : AutoTunnelEvent()
} }
@@ -1,7 +1,9 @@
package com.zaneschepke.wireguardautotunnel.domain.events package com.zaneschepke.wireguardautotunnel.domain.events
sealed class KillSwitchEvent { sealed class KillSwitchEvent {
data class Start(val allowedIps: List<String>) : KillSwitchEvent() data class Start(val allowedIps: List<String>) : KillSwitchEvent()
data object Stop : KillSwitchEvent()
data object DoNothing : KillSwitchEvent() data object Stop : KillSwitchEvent()
data object DoNothing : KillSwitchEvent()
} }
@@ -3,11 +3,11 @@ package com.zaneschepke.wireguardautotunnel.domain.repository
import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.entity.TunnelConf
interface AppDataRepository { interface AppDataRepository {
suspend fun getPrimaryOrFirstTunnel(): TunnelConf? suspend fun getPrimaryOrFirstTunnel(): TunnelConf?
suspend fun getStartTunnelConfig(): TunnelConf? suspend fun getStartTunnelConfig(): TunnelConf?
val settings: AppSettingRepository val settings: AppSettingRepository
val tunnels: TunnelRepository val tunnels: TunnelRepository
val appState: AppStateRepository val appState: AppStateRepository
} }
@@ -4,7 +4,9 @@ import com.zaneschepke.wireguardautotunnel.domain.entity.AppSettings
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface AppSettingRepository { interface AppSettingRepository {
suspend fun save(appSettings: AppSettings) suspend fun save(appSettings: AppSettings)
val flow: Flow<AppSettings>
suspend fun get(): AppSettings val flow: Flow<AppSettings>
suspend fun get(): AppSettings
} }
@@ -5,41 +5,41 @@ import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface AppStateRepository { interface AppStateRepository {
suspend fun isLocationDisclosureShown(): Boolean suspend fun isLocationDisclosureShown(): Boolean
suspend fun setLocationDisclosureShown(shown: Boolean) suspend fun setLocationDisclosureShown(shown: Boolean)
suspend fun isPinLockEnabled(): Boolean suspend fun isPinLockEnabled(): Boolean
suspend fun setPinLockEnabled(enabled: Boolean) suspend fun setPinLockEnabled(enabled: Boolean)
suspend fun isBatteryOptimizationDisableShown(): Boolean suspend fun isBatteryOptimizationDisableShown(): Boolean
suspend fun setBatteryOptimizationDisableShown(shown: Boolean) suspend fun setBatteryOptimizationDisableShown(shown: Boolean)
suspend fun isTunnelStatsExpanded(): Boolean suspend fun isTunnelStatsExpanded(): Boolean
suspend fun setTunnelStatsExpanded(expanded: Boolean) suspend fun setTunnelStatsExpanded(expanded: Boolean)
suspend fun setTheme(theme: Theme) suspend fun setTheme(theme: Theme)
suspend fun getTheme(): Theme suspend fun getTheme(): Theme
suspend fun isLocalLogsEnabled(): Boolean suspend fun isLocalLogsEnabled(): Boolean
suspend fun setLocalLogsEnabled(enabled: Boolean) suspend fun setLocalLogsEnabled(enabled: Boolean)
suspend fun setLocale(localeTag: String) suspend fun setLocale(localeTag: String)
suspend fun getLocale(): String? suspend fun getLocale(): String?
suspend fun setIsRemoteControlEnabled(enabled: Boolean) suspend fun setIsRemoteControlEnabled(enabled: Boolean)
suspend fun isRemoteControlEnabled(): Boolean suspend fun isRemoteControlEnabled(): Boolean
suspend fun setRemoteKey(key: String) suspend fun setRemoteKey(key: String)
suspend fun getRemoteKey(): String? suspend fun getRemoteKey(): String?
val flow: Flow<AppState> val flow: Flow<AppState>
} }
@@ -5,33 +5,33 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
interface TunnelRepository { interface TunnelRepository {
val flow: Flow<List<TunnelConf>> val flow: Flow<List<TunnelConf>>
suspend fun getAll(): Tunnels suspend fun getAll(): Tunnels
suspend fun save(tunnelConf: TunnelConf) suspend fun save(tunnelConf: TunnelConf)
suspend fun saveAll(tunnelConfList: List<TunnelConf>) suspend fun saveAll(tunnelConfList: List<TunnelConf>)
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?) suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?)
suspend fun delete(tunnelConf: TunnelConf) suspend fun delete(tunnelConf: TunnelConf)
suspend fun getById(id: Int): TunnelConf? suspend fun getById(id: Int): TunnelConf?
suspend fun getActive(): Tunnels suspend fun getActive(): Tunnels
suspend fun count(): Int suspend fun count(): Int
suspend fun findByTunnelName(name: String): TunnelConf? suspend fun findByTunnelName(name: String): TunnelConf?
suspend fun findByTunnelNetworksName(name: String): Tunnels suspend fun findByTunnelNetworksName(name: String): Tunnels
suspend fun findByMobileDataTunnel(): Tunnels suspend fun findByMobileDataTunnel(): Tunnels
suspend fun findPrimary(): Tunnels suspend fun findPrimary(): Tunnels
} }
@@ -4,31 +4,31 @@ import org.amnezia.awg.backend.Statistics
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
class AmneziaStatistics(private val statistics: Statistics) : TunnelStatistics() { class AmneziaStatistics(private val statistics: Statistics) : TunnelStatistics() {
override fun peerStats(peer: Key): PeerStats? { override fun peerStats(peer: Key): PeerStats? {
val key = Key.fromBase64(peer.toBase64()) val key = Key.fromBase64(peer.toBase64())
val stats = statistics.peer(key) val stats = statistics.peer(key)
return stats?.let { return stats?.let {
PeerStats( PeerStats(
rxBytes = stats.rxBytes, rxBytes = stats.rxBytes,
txBytes = stats.txBytes, txBytes = stats.txBytes,
latestHandshakeEpochMillis = stats.latestHandshakeEpochMillis, latestHandshakeEpochMillis = stats.latestHandshakeEpochMillis,
) )
} }
} }
override fun isTunnelStale(): Boolean { override fun isTunnelStale(): Boolean {
return statistics.isStale return statistics.isStale
} }
override fun getPeers(): Array<Key> { override fun getPeers(): Array<Key> {
return statistics.peers() return statistics.peers()
} }
override fun rx(): Long { override fun rx(): Long {
return statistics.totalRx() return statistics.totalRx()
} }
override fun tx(): Long { override fun tx(): Long {
return statistics.totalTx() return statistics.totalTx()
} }
} }
@@ -10,150 +10,189 @@ import com.zaneschepke.wireguardautotunnel.domain.events.KillSwitchEvent
import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList import com.zaneschepke.wireguardautotunnel.util.extensions.isMatchingToWildcardList
data class AutoTunnelState( data class AutoTunnelState(
val activeTunnels: Map<TunnelConf, TunnelState> = emptyMap(), val activeTunnels: Map<TunnelConf, TunnelState> = emptyMap(),
val networkState: NetworkState = NetworkState(), val networkState: NetworkState = NetworkState(),
val settings: AppSettings = AppSettings(), val settings: AppSettings = AppSettings(),
val tunnels: List<TunnelConf> = emptyList(), val tunnels: List<TunnelConf> = emptyList(),
) { ) {
private fun isMobileDataActive(): Boolean { private fun isMobileDataActive(): Boolean {
return !networkState.isEthernetConnected && !networkState.isWifiConnected && networkState.isMobileDataConnected return !networkState.isEthernetConnected &&
} !networkState.isWifiConnected &&
networkState.isMobileDataConnected
}
private fun isMobileTunnelDataChangeNeeded(): Boolean { private fun isMobileTunnelDataChangeNeeded(): Boolean {
val preferredTunnel = preferredMobileDataTunnel() val preferredTunnel = preferredMobileDataTunnel()
return preferredTunnel != null && return preferredTunnel != null &&
activeTunnels.isNotEmpty() && !activeTunnels.isUp(preferredTunnel) activeTunnels.isNotEmpty() &&
} !activeTunnels.isUp(preferredTunnel)
}
private fun isEthernetTunnelChangeNeeded(): Boolean { private fun isEthernetTunnelChangeNeeded(): Boolean {
val preferredTunnel = preferredEthernetTunnel() val preferredTunnel = preferredEthernetTunnel()
return preferredTunnel != null && activeTunnels.isNotEmpty() && !activeTunnels.isUp(preferredTunnel) return preferredTunnel != null &&
} activeTunnels.isNotEmpty() &&
!activeTunnels.isUp(preferredTunnel)
}
private fun preferredMobileDataTunnel(): TunnelConf? { private fun preferredMobileDataTunnel(): TunnelConf? {
return tunnels.firstOrNull { it.isMobileDataTunnel } ?: tunnels.firstOrNull { it.isPrimaryTunnel } return tunnels.firstOrNull { it.isMobileDataTunnel }
} ?: tunnels.firstOrNull { it.isPrimaryTunnel }
}
private fun preferredEthernetTunnel(): TunnelConf? { private fun preferredEthernetTunnel(): TunnelConf? {
return tunnels.firstOrNull { it.isEthernetTunnel } ?: tunnels.firstOrNull { it.isPrimaryTunnel } return tunnels.firstOrNull { it.isEthernetTunnel }
} ?: tunnels.firstOrNull { it.isPrimaryTunnel }
}
private fun preferredWifiTunnel(): TunnelConf? { private fun preferredWifiTunnel(): TunnelConf? {
return getTunnelWithMatchingTunnelNetwork() ?: tunnels.firstOrNull { it.isPrimaryTunnel } return getTunnelWithMatchingTunnelNetwork() ?: tunnels.firstOrNull { it.isPrimaryTunnel }
} }
private fun isWifiActive(): Boolean { private fun isWifiActive(): Boolean {
return !networkState.isEthernetConnected && networkState.isWifiConnected return !networkState.isEthernetConnected && networkState.isWifiConnected
} }
private fun startOnEthernet(): Boolean { private fun startOnEthernet(): Boolean {
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && activeTunnels.allDown() return networkState.isEthernetConnected &&
} settings.isTunnelOnEthernetEnabled &&
activeTunnels.allDown()
}
private fun stopOnEthernet(): Boolean { private fun stopOnEthernet(): Boolean {
return networkState.isEthernetConnected && !settings.isTunnelOnEthernetEnabled && activeTunnels.hasActive() return networkState.isEthernetConnected &&
} !settings.isTunnelOnEthernetEnabled &&
activeTunnels.hasActive()
}
// TODO test removed kill switch state check // TODO test removed kill switch state check
private fun stopKillSwitchOnTrusted(): Boolean { private fun stopKillSwitchOnTrusted(): Boolean {
return networkState.isWifiConnected && settings.isVpnKillSwitchEnabled && settings.isDisableKillSwitchOnTrustedEnabled && isCurrentSSIDTrusted() return networkState.isWifiConnected &&
} settings.isVpnKillSwitchEnabled &&
settings.isDisableKillSwitchOnTrustedEnabled &&
isCurrentSSIDTrusted()
}
// TODO test, removed kill switch state check // TODO test, removed kill switch state check
private fun startKillSwitch(): Boolean { private fun startKillSwitch(): Boolean {
return settings.isVpnKillSwitchEnabled && (!settings.isDisableKillSwitchOnTrustedEnabled || !isCurrentSSIDTrusted()) return settings.isVpnKillSwitchEnabled &&
} (!settings.isDisableKillSwitchOnTrustedEnabled || !isCurrentSSIDTrusted())
}
private fun isNoConnectivity(): Boolean { private fun isNoConnectivity(): Boolean {
return !networkState.isEthernetConnected && !networkState.isWifiConnected && !networkState.isMobileDataConnected return !networkState.isEthernetConnected &&
} !networkState.isWifiConnected &&
!networkState.isMobileDataConnected
}
private fun stopOnMobileData(): Boolean { private fun stopOnMobileData(): Boolean {
return isMobileDataActive() && !settings.isTunnelOnMobileDataEnabled && activeTunnels.hasActive() return isMobileDataActive() &&
} !settings.isTunnelOnMobileDataEnabled &&
activeTunnels.hasActive()
}
private fun startOnMobileData(): Boolean { private fun startOnMobileData(): Boolean {
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && activeTunnels.allDown() return isMobileDataActive() &&
} settings.isTunnelOnMobileDataEnabled &&
activeTunnels.allDown()
}
private fun changeOnMobileData(): Boolean { private fun changeOnMobileData(): Boolean {
return isMobileDataActive() && settings.isTunnelOnMobileDataEnabled && isMobileTunnelDataChangeNeeded() return isMobileDataActive() &&
} settings.isTunnelOnMobileDataEnabled &&
isMobileTunnelDataChangeNeeded()
}
private fun changeOnEthernet(): Boolean { private fun changeOnEthernet(): Boolean {
return networkState.isEthernetConnected && settings.isTunnelOnEthernetEnabled && isEthernetTunnelChangeNeeded() return networkState.isEthernetConnected &&
} settings.isTunnelOnEthernetEnabled &&
isEthernetTunnelChangeNeeded()
}
private fun stopOnWifi(): Boolean { private fun stopOnWifi(): Boolean {
return isWifiActive() && !settings.isTunnelOnWifiEnabled && activeTunnels.hasActive() return isWifiActive() && !settings.isTunnelOnWifiEnabled && activeTunnels.hasActive()
} }
private fun stopOnTrustedWifi(): Boolean { private fun stopOnTrustedWifi(): Boolean {
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.hasActive() && isCurrentSSIDTrusted() return isWifiActive() &&
} settings.isTunnelOnWifiEnabled &&
activeTunnels.hasActive() &&
isCurrentSSIDTrusted()
}
private fun startOnUntrustedWifi(): Boolean { private fun startOnUntrustedWifi(): Boolean {
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.allDown() && !isCurrentSSIDTrusted() return isWifiActive() &&
} settings.isTunnelOnWifiEnabled &&
activeTunnels.allDown() &&
!isCurrentSSIDTrusted()
}
private fun changeOnUntrustedWifi(): Boolean { private fun changeOnUntrustedWifi(): Boolean {
return isWifiActive() && settings.isTunnelOnWifiEnabled && activeTunnels.hasActive() && !isCurrentSSIDTrusted() && !isWifiTunnelPreferred() return isWifiActive() &&
} settings.isTunnelOnWifiEnabled &&
activeTunnels.hasActive() &&
!isCurrentSSIDTrusted() &&
!isWifiTunnelPreferred()
}
private fun isWifiTunnelPreferred(): Boolean { private fun isWifiTunnelPreferred(): Boolean {
val preferred = preferredWifiTunnel() val preferred = preferredWifiTunnel()
return preferred?.let { activeTunnels.isUp(it) } ?: true return preferred?.let { activeTunnels.isUp(it) } ?: true
} }
fun asAutoTunnelEvent(): AutoTunnelEvent { fun asAutoTunnelEvent(): AutoTunnelEvent {
return when { return when {
// ethernet scenarios // ethernet scenarios
stopOnEthernet() -> AutoTunnelEvent.Stop stopOnEthernet() -> AutoTunnelEvent.Stop
startOnEthernet() || changeOnEthernet() -> AutoTunnelEvent.Start(preferredEthernetTunnel()) startOnEthernet() || changeOnEthernet() ->
// mobile data scenarios AutoTunnelEvent.Start(preferredEthernetTunnel())
stopOnMobileData() -> AutoTunnelEvent.Stop // mobile data scenarios
startOnMobileData() || changeOnMobileData() -> AutoTunnelEvent.Start(preferredMobileDataTunnel()) stopOnMobileData() -> AutoTunnelEvent.Stop
// wifi scenarios startOnMobileData() || changeOnMobileData() ->
stopOnWifi() -> AutoTunnelEvent.Stop AutoTunnelEvent.Start(preferredMobileDataTunnel())
stopOnTrustedWifi() -> AutoTunnelEvent.Stop // wifi scenarios
startOnUntrustedWifi() || changeOnUntrustedWifi() -> AutoTunnelEvent.Start(preferredWifiTunnel()) stopOnWifi() -> AutoTunnelEvent.Stop
// no connectivity stopOnTrustedWifi() -> AutoTunnelEvent.Stop
isNoConnectivity() && settings.isStopOnNoInternetEnabled -> AutoTunnelEvent.Stop startOnUntrustedWifi() || changeOnUntrustedWifi() ->
else -> AutoTunnelEvent.DoNothing AutoTunnelEvent.Start(preferredWifiTunnel())
} // no connectivity
} isNoConnectivity() && settings.isStopOnNoInternetEnabled -> AutoTunnelEvent.Stop
else -> AutoTunnelEvent.DoNothing
}
}
fun asKillSwitchEvent(): KillSwitchEvent { fun asKillSwitchEvent(): KillSwitchEvent {
return when { return when {
stopKillSwitchOnTrusted() -> KillSwitchEvent.Stop stopKillSwitchOnTrusted() -> KillSwitchEvent.Stop
startKillSwitch() -> { startKillSwitch() -> {
val allowedIps = if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS else emptyList() val allowedIps =
KillSwitchEvent.Start(allowedIps) if (settings.isLanOnKillSwitchEnabled) TunnelConf.LAN_BYPASS_ALLOWED_IPS
} else emptyList()
else -> KillSwitchEvent.DoNothing KillSwitchEvent.Start(allowedIps)
} }
} else -> KillSwitchEvent.DoNothing
}
}
private fun isCurrentSSIDTrusted(): Boolean { private fun isCurrentSSIDTrusted(): Boolean {
return networkState.wifiName?.let { return networkState.wifiName?.let { hasTrustedWifiName(it) } == true
hasTrustedWifiName(it) }
} == true
}
private fun hasTrustedWifiName(wifiName: String, wifiNames: List<String> = settings.trustedNetworkSSIDs): Boolean { private fun hasTrustedWifiName(
return if (settings.isWildcardsEnabled) { wifiName: String,
wifiNames.isMatchingToWildcardList(wifiName) wifiNames: List<String> = settings.trustedNetworkSSIDs,
} else { ): Boolean {
wifiNames.contains(wifiName) return if (settings.isWildcardsEnabled) {
} wifiNames.isMatchingToWildcardList(wifiName)
} } else {
wifiNames.contains(wifiName)
}
}
private fun getTunnelWithMatchingTunnelNetwork(): TunnelConf? { private fun getTunnelWithMatchingTunnelNetwork(): TunnelConf? {
return networkState.wifiName?.let { wifiName -> return networkState.wifiName?.let { wifiName ->
tunnels.firstOrNull { tunnels.firstOrNull { hasTrustedWifiName(wifiName, it.tunnelNetworks) }
hasTrustedWifiName(wifiName, it.tunnelNetworks) }
} }
}
}
} }
@@ -1,9 +1,9 @@
package com.zaneschepke.wireguardautotunnel.domain.state package com.zaneschepke.wireguardautotunnel.domain.state
data class ConnectivityState( data class ConnectivityState(
val wifiAvailable: Boolean, val wifiAvailable: Boolean,
val ethernetAvailable: Boolean, val ethernetAvailable: Boolean,
val cellularAvailable: Boolean, val cellularAvailable: Boolean,
) { ) {
val allOffline = !wifiAvailable && !ethernetAvailable && !cellularAvailable val allOffline = !wifiAvailable && !ethernetAvailable && !cellularAvailable
} }
@@ -1,12 +1,12 @@
package com.zaneschepke.wireguardautotunnel.domain.state package com.zaneschepke.wireguardautotunnel.domain.state
data class NetworkState( data class NetworkState(
val isWifiConnected: Boolean = false, val isWifiConnected: Boolean = false,
val isMobileDataConnected: Boolean = false, val isMobileDataConnected: Boolean = false,
val isEthernetConnected: Boolean = false, val isEthernetConnected: Boolean = false,
val wifiName: String? = null, val wifiName: String? = null,
) { ) {
fun hasNoCapabilities(): Boolean { fun hasNoCapabilities(): Boolean {
return !isWifiConnected && !isMobileDataConnected && !isEthernetConnected return !isWifiConnected && !isMobileDataConnected && !isEthernetConnected
} }
} }
@@ -4,7 +4,7 @@ import com.zaneschepke.wireguardautotunnel.domain.enums.BackendState
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
data class TunnelState( data class TunnelState(
val status: TunnelStatus = TunnelStatus.Down, val status: TunnelStatus = TunnelStatus.Down,
val backendState: BackendState = BackendState.INACTIVE, val backendState: BackendState = BackendState.INACTIVE,
val statistics: TunnelStatistics? = null, val statistics: TunnelStatistics? = null,
) )
@@ -3,16 +3,20 @@ package com.zaneschepke.wireguardautotunnel.domain.state
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
abstract class TunnelStatistics { abstract class TunnelStatistics {
@JvmRecord @JvmRecord
data class PeerStats(val rxBytes: Long, val txBytes: Long, val latestHandshakeEpochMillis: Long) data class PeerStats(
val rxBytes: Long,
val txBytes: Long,
val latestHandshakeEpochMillis: Long,
)
abstract fun peerStats(peer: Key): PeerStats? abstract fun peerStats(peer: Key): PeerStats?
abstract fun isTunnelStale(): Boolean abstract fun isTunnelStale(): Boolean
abstract fun getPeers(): Array<Key> abstract fun getPeers(): Array<Key>
abstract fun rx(): Long abstract fun rx(): Long
abstract fun tx(): Long abstract fun tx(): Long
} }
@@ -4,33 +4,31 @@ import com.wireguard.android.backend.Statistics
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
class WireGuardStatistics(private val statistics: Statistics) : TunnelStatistics() { class WireGuardStatistics(private val statistics: Statistics) : TunnelStatistics() {
override fun peerStats(peer: Key): PeerStats? { override fun peerStats(peer: Key): PeerStats? {
val key = com.wireguard.crypto.Key.fromBase64(peer.toBase64()) val key = com.wireguard.crypto.Key.fromBase64(peer.toBase64())
val peerStats = statistics.peer(key) val peerStats = statistics.peer(key)
return peerStats?.let { return peerStats?.let {
PeerStats( PeerStats(
txBytes = peerStats.txBytes, txBytes = peerStats.txBytes,
rxBytes = peerStats.rxBytes, rxBytes = peerStats.rxBytes,
latestHandshakeEpochMillis = peerStats.latestHandshakeEpochMillis, latestHandshakeEpochMillis = peerStats.latestHandshakeEpochMillis,
) )
} }
} }
override fun isTunnelStale(): Boolean { override fun isTunnelStale(): Boolean {
return statistics.isStale return statistics.isStale
} }
override fun getPeers(): Array<Key> { override fun getPeers(): Array<Key> {
return statistics.peers().map { return statistics.peers().map { Key.fromBase64(it.toBase64()) }.toTypedArray()
Key.fromBase64(it.toBase64()) }
}.toTypedArray()
}
override fun rx(): Long { override fun rx(): Long {
return statistics.totalRx() return statistics.totalRx()
} }
override fun tx(): Long { override fun tx(): Long {
return statistics.totalTx() return statistics.totalTx()
} }
} }
@@ -3,69 +3,44 @@ package com.zaneschepke.wireguardautotunnel.ui
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
sealed class Route { sealed class Route {
@Serializable @Serializable data object Support : Route()
data object Support : Route()
@Serializable @Serializable data object Settings : Route()
data object Settings : Route()
@Serializable @Serializable data object SettingsAdvanced : Route()
data object SettingsAdvanced : Route()
@Serializable @Serializable data object AutoTunnel : Route()
data object AutoTunnel : Route()
@Serializable @Serializable data object AutoTunnelAdvanced : Route()
data object AutoTunnelAdvanced : Route()
@Serializable @Serializable data object LocationDisclosure : Route()
data object LocationDisclosure : Route()
@Serializable @Serializable data object Appearance : Route()
data object Appearance : Route()
@Serializable @Serializable data object Display : Route()
data object Display : Route()
@Serializable @Serializable data object KillSwitch : Route()
data object KillSwitch : Route()
@Serializable @Serializable data object Language : Route()
data object Language : Route()
@Serializable @Serializable data object Main : Route()
data object Main : Route()
@Serializable @Serializable data class TunnelOptions(val id: Int) : Route()
data class TunnelOptions(
val id: Int,
) : Route()
@Serializable @Serializable data object Lock : Route()
data object Lock : Route()
@Serializable @Serializable data object Scanner : Route()
data object Scanner : Route()
@Serializable @Serializable data class Config(val id: Int) : Route()
data class Config(
val id: Int,
) : Route()
@Serializable @Serializable
data class SplitTunnel( data class SplitTunnel(val id: Int) : Route() {
val id: Int, companion object {
) : Route() { const val KEY_ID = "id"
companion object { }
const val KEY_ID = "id" }
}
}
@Serializable @Serializable data class TunnelAutoTunnel(val id: Int) : Route()
data class TunnelAutoTunnel(
val id: Int,
) : Route()
@Serializable @Serializable data object Logs : Route()
data object Logs : Route()
} }
@@ -20,40 +20,37 @@ import com.zaneschepke.wireguardautotunnel.R
@Composable @Composable
fun <T> DropdownSelector( fun <T> DropdownSelector(
currentValue: T, currentValue: T,
options: List<T>, options: List<T>,
onValueSelected: (T) -> Unit, onValueSelected: (T) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
label: @Composable (() -> Unit)? = null, label: @Composable (() -> Unit)? = null,
isExpanded: Boolean = false, isExpanded: Boolean = false,
onDismiss: () -> Unit = {}, onDismiss: () -> Unit = {},
) { ) {
Row( Row(
horizontalArrangement = Arrangement.spacedBy(5.dp), horizontalArrangement = Arrangement.spacedBy(5.dp),
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
if (label != null) label() if (label != null) label()
Text( Text(text = currentValue.toString(), style = MaterialTheme.typography.bodyMedium)
text = currentValue.toString(), Icon(Icons.Default.ArrowDropDown, contentDescription = stringResource(R.string.dropdown))
style = MaterialTheme.typography.bodyMedium, }
) DropdownMenu(
Icon(Icons.Default.ArrowDropDown, contentDescription = stringResource(R.string.dropdown)) modifier = modifier.height(250.dp),
} scrollState = rememberScrollState(),
DropdownMenu( containerColor = MaterialTheme.colorScheme.surface,
modifier = modifier.height(250.dp), expanded = isExpanded,
scrollState = rememberScrollState(), onDismissRequest = onDismiss,
containerColor = MaterialTheme.colorScheme.surface, ) {
expanded = isExpanded, options.forEach { option ->
onDismissRequest = onDismiss, DropdownMenuItem(
) { text = { Text(text = option.toString()) },
options.forEach { option -> onClick = {
DropdownMenuItem( onValueSelected(option)
text = { Text(text = option.toString()) }, onDismiss() // Close dropdown after selection
onClick = { },
onValueSelected(option) )
onDismiss() // Close dropdown after selection }
}, }
)
}
}
} }
@@ -22,50 +22,43 @@ import androidx.compose.ui.unit.dp
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
fun ExpandingRowListItem( fun ExpandingRowListItem(
leading: @Composable () -> Unit, leading: @Composable () -> Unit,
text: String, text: String,
onHold: () -> Unit = {}, onHold: () -> Unit = {},
onClick: () -> Unit, onClick: () -> Unit,
trailing: @Composable () -> Unit, trailing: @Composable () -> Unit,
isExpanded: Boolean, isExpanded: Boolean,
expanded: @Composable () -> Unit = {}, expanded: @Composable () -> Unit = {},
) { ) {
Box( Box(
modifier = modifier =
Modifier Modifier.animateContentSize()
.animateContentSize() .clip(RoundedCornerShape(8.dp))
.clip(RoundedCornerShape(30.dp)) .combinedClickable(onClick = { onClick() }, onLongClick = { onHold() })
.combinedClickable( ) {
onClick = { onClick() }, Column {
onLongClick = { onHold() }, Row(
), modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
) { verticalAlignment = Alignment.CenterVertically,
Column { horizontalArrangement = Arrangement.SpaceBetween,
Row( ) {
modifier = Row(
Modifier verticalAlignment = Alignment.CenterVertically,
.fillMaxWidth() horizontalArrangement = Arrangement.spacedBy(16.dp),
.padding(horizontal = 15.dp), modifier = Modifier.fillMaxWidth(13 / 20f),
verticalAlignment = Alignment.CenterVertically, ) {
horizontalArrangement = Arrangement.SpaceBetween, leading()
) { Text(
Row( text,
verticalAlignment = Alignment.CenterVertically, maxLines = 1,
horizontalArrangement = Arrangement.spacedBy(15.dp), overflow = TextOverflow.Ellipsis,
modifier = Modifier.fillMaxWidth(13 / 20f), style = MaterialTheme.typography.labelLarge,
) { color = MaterialTheme.colorScheme.onBackground,
leading() )
Text( }
text, trailing()
maxLines = 1, }
overflow = TextOverflow.Ellipsis, if (isExpanded) expanded()
style = MaterialTheme.typography.labelLarge, }
color = MaterialTheme.colorScheme.onBackground, }
)
}
trailing()
}
if (isExpanded) expanded()
}
}
} }
@@ -14,24 +14,25 @@ import androidx.compose.ui.graphics.Color
@Composable @Composable
fun ShimmerEffect(modifier: Modifier = Modifier): Brush { fun ShimmerEffect(modifier: Modifier = Modifier): Brush {
val shimmerColors = listOf( val shimmerColors =
Color.LightGray.copy(alpha = 0.9f), listOf(
Color.LightGray.copy(alpha = 0.3f), Color.LightGray.copy(alpha = 0.9f),
Color.LightGray.copy(alpha = 0.9f), Color.LightGray.copy(alpha = 0.3f),
) Color.LightGray.copy(alpha = 0.9f),
)
val transition = rememberInfiniteTransition() val transition = rememberInfiniteTransition()
val translateAnim by transition.animateFloat( val translateAnim by
initialValue = 0f, transition.animateFloat(
targetValue = 1000f, initialValue = 0f,
animationSpec = infiniteRepeatable( targetValue = 1000f,
animation = tween(durationMillis = 1200, easing = LinearEasing), animationSpec =
), infiniteRepeatable(animation = tween(durationMillis = 1200, easing = LinearEasing)),
) )
return Brush.linearGradient( return Brush.linearGradient(
colors = shimmerColors, colors = shimmerColors,
start = Offset(0f, 0f), start = Offset(0f, 0f),
end = Offset(translateAnim, translateAnim), end = Offset(translateAnim, translateAnim),
) )
} }
@@ -13,25 +13,30 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
@Composable @Composable
fun ClickableIconButton(onClick: () -> Unit, onIconClick: () -> Unit, text: String, icon: ImageVector, enabled: Boolean = true) { fun ClickableIconButton(
TextButton( onClick: () -> Unit,
onClick = onClick, onIconClick: () -> Unit,
enabled = enabled, text: String,
) { icon: ImageVector,
Text(text, Modifier.weight(1f, false), style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.primary) enabled: Boolean = true,
Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing)) ) {
Icon( TextButton(onClick = onClick, enabled = enabled) {
imageVector = icon, Text(
contentDescription = icon.name, text,
modifier = Modifier.weight(1f, false),
Modifier style = MaterialTheme.typography.bodySmall,
.size(ButtonDefaults.IconSize) color = MaterialTheme.colorScheme.primary,
.weight(1f, false) )
.clickable { Spacer(modifier = Modifier.size(ButtonDefaults.IconSpacing))
if (enabled) { Icon(
onIconClick() imageVector = icon,
} contentDescription = icon.name,
}, modifier =
) Modifier.size(ButtonDefaults.IconSize).weight(1f, false).clickable {
} if (enabled) {
onIconClick()
}
},
)
}
} }
@@ -12,11 +12,8 @@ import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
@Composable @Composable
fun ForwardButton(modifier: Modifier = Modifier.focusable(), onClick: () -> Unit) { fun ForwardButton(modifier: Modifier = Modifier.focusable(), onClick: () -> Unit) {
IconButton( IconButton(modifier = modifier, onClick = onClick) {
modifier = modifier, val icon = Icons.AutoMirrored.Outlined.ArrowForward
onClick = onClick, Icon(icon, icon.name, Modifier.size(iconSize))
) { }
val icon = Icons.AutoMirrored.Outlined.ArrowForward
Icon(icon, icon.name, Modifier.size(iconSize))
}
} }
@@ -25,73 +25,67 @@ import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
@androidx.compose.runtime.Composable @androidx.compose.runtime.Composable
fun IconSurfaceButton(title: String, onClick: () -> Unit, selected: Boolean, leadingIcon: ImageVector? = null, description: String? = null) { fun IconSurfaceButton(
val border: BorderStroke? = title: String,
if (selected) { onClick: () -> Unit,
BorderStroke( selected: Boolean,
1.dp, leadingIcon: ImageVector? = null,
MaterialTheme.colorScheme.primary, description: String? = null,
) ) {
} else { val border: BorderStroke? =
null if (selected) {
} BorderStroke(1.dp, MaterialTheme.colorScheme.primary)
Card( } else {
modifier = null
Modifier }
.fillMaxWidth() Card(
.height(IntrinsicSize.Min), modifier = Modifier.fillMaxWidth().height(IntrinsicSize.Min),
shape = RoundedCornerShape(8.dp), shape = RoundedCornerShape(8.dp),
border = border, border = border,
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
) { ) {
Box( Box(modifier = Modifier.clickable { onClick() }.fillMaxWidth()) {
modifier = Modifier.clickable { onClick() } Column(
.fillMaxWidth(), modifier =
) { Modifier.padding(horizontal = 8.dp, vertical = 10.dp)
Column( .padding(end = 16.dp)
modifier = .padding(start = 8.dp)
Modifier .fillMaxSize(),
.padding(horizontal = 8.dp, vertical = 10.dp) verticalArrangement = Arrangement.Center,
.padding(end = 16.dp).padding(start = 8.dp) horizontalAlignment = Alignment.Start,
.fillMaxSize(), ) {
verticalArrangement = Arrangement.Center, Row(
horizontalAlignment = Alignment.Start, verticalAlignment = Alignment.Companion.CenterVertically,
) { horizontalArrangement = Arrangement.spacedBy(16.dp),
Row( ) {
verticalAlignment = Alignment.Companion.CenterVertically, Row(
horizontalArrangement = Arrangement.spacedBy(16.dp), horizontalArrangement = Arrangement.spacedBy(16.dp),
) { verticalAlignment = Alignment.Companion.CenterVertically,
Row( modifier =
horizontalArrangement = Arrangement.spacedBy( Modifier.padding(vertical = if (description == null) 10.dp else 0.dp),
16.dp, ) {
), leadingIcon?.let {
verticalAlignment = Alignment.Companion.CenterVertically, Icon(
modifier = Modifier.padding(vertical = if (description == null) 10.dp else 0.dp), leadingIcon,
) { leadingIcon.name,
leadingIcon?.let { Modifier.size(iconSize),
Icon( if (selected) MaterialTheme.colorScheme.primary
leadingIcon, else MaterialTheme.colorScheme.onSurface,
leadingIcon.name, )
Modifier.size(iconSize), }
if (selected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface, Column {
) Text(title, style = MaterialTheme.typography.titleMedium)
} description?.let {
Column { Text(
Text( description,
title, color = MaterialTheme.colorScheme.onSurfaceVariant,
style = MaterialTheme.typography.titleMedium, style = MaterialTheme.typography.bodyMedium,
) )
description?.let { }
Text( }
description, }
color = MaterialTheme.colorScheme.onSurfaceVariant, }
style = MaterialTheme.typography.bodyMedium, }
) }
} }
}
}
}
}
}
}
} }
@@ -9,19 +9,26 @@ import androidx.compose.ui.draw.scale
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@Composable @Composable
fun ScaledSwitch(checked: Boolean, onClick: (checked: Boolean) -> Unit, enabled: Boolean = true, modifier: Modifier = Modifier) { fun ScaledSwitch(
Switch( checked: Boolean,
checked, onClick: (checked: Boolean) -> Unit,
{ onClick(it) }, enabled: Boolean = true,
modifier.scale((52.dp / 52.dp)), modifier: Modifier = Modifier,
enabled = enabled, ) {
colors = SwitchDefaults.colors().copy( Switch(
checkedThumbColor = MaterialTheme.colorScheme.background, checked,
checkedIconColor = MaterialTheme.colorScheme.background, { onClick(it) },
uncheckedTrackColor = MaterialTheme.colorScheme.surface, modifier.scale((52.dp / 52.dp)),
uncheckedBorderColor = MaterialTheme.colorScheme.outline, enabled = enabled,
uncheckedThumbColor = MaterialTheme.colorScheme.outline, colors =
uncheckedIconColor = MaterialTheme.colorScheme.outline, SwitchDefaults.colors()
), .copy(
) checkedThumbColor = MaterialTheme.colorScheme.background,
checkedIconColor = MaterialTheme.colorScheme.background,
uncheckedTrackColor = MaterialTheme.colorScheme.surface,
uncheckedBorderColor = MaterialTheme.colorScheme.outline,
uncheckedThumbColor = MaterialTheme.colorScheme.outline,
uncheckedIconColor = MaterialTheme.colorScheme.outline,
),
)
} }
@@ -25,49 +25,40 @@ import androidx.compose.ui.unit.dp
@Composable @Composable
fun SelectionItemButton( fun SelectionItemButton(
leading: (@Composable () -> Unit)? = null, leading: (@Composable () -> Unit)? = null,
buttonText: String, buttonText: String,
trailing: (@Composable () -> Unit)? = null, trailing: (@Composable () -> Unit)? = null,
onClick: () -> Unit, onClick: () -> Unit,
ripple: Boolean = true, ripple: Boolean = true,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
Card( Card(
modifier = modifier =
modifier modifier
.clip(RoundedCornerShape(8.dp)) .clip(RoundedCornerShape(8.dp))
.clickable( .clickable(
indication = if (ripple) ripple() else null, indication = if (ripple) ripple() else null,
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
onClick = { onClick() }, onClick = { onClick() },
) )
.height(56.dp), .height(56.dp),
colors = colors = CardDefaults.cardColors(containerColor = Color.Transparent),
CardDefaults.cardColors( ) {
containerColor = Color.Transparent, Row(
), verticalAlignment = Alignment.CenterVertically,
) { horizontalArrangement = Arrangement.Start,
Row( modifier = Modifier.fillMaxSize().padding(horizontal = 12.dp),
verticalAlignment = Alignment.CenterVertically, ) {
horizontalArrangement = Arrangement.Start, leading?.let { it() }
modifier = Modifier Text(
.fillMaxSize() buttonText,
.padding(end = 10.dp), style = MaterialTheme.typography.labelMedium,
) { color = MaterialTheme.colorScheme.onSurface,
leading?.let { modifier = Modifier.fillMaxWidth(3 / 4f),
it() maxLines = 2,
} overflow = TextOverflow.Ellipsis,
Text( )
buttonText, trailing?.let { it() }
style = MaterialTheme.typography.labelMedium, }
color = MaterialTheme.colorScheme.onSurface, }
modifier = Modifier.fillMaxWidth(3 / 4f),
maxLines = 2,
overflow = TextOverflow.Ellipsis,
)
trailing?.let {
it()
}
}
}
} }
@@ -4,10 +4,10 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
data class SelectionItem( data class SelectionItem(
val leadingIcon: ImageVector? = null, val leadingIcon: ImageVector? = null,
val trailing: (@Composable () -> Unit)? = null, val trailing: (@Composable () -> Unit)? = null,
val title: (@Composable () -> Unit), val title: (@Composable () -> Unit),
val description: (@Composable () -> Unit)? = null, val description: (@Composable () -> Unit)? = null,
val onClick: (() -> Unit)? = null, val onClick: (() -> Unit)? = null,
val height: Int = 64, val height: Int = 64,
) )
@@ -7,14 +7,17 @@ import androidx.compose.ui.res.stringResource
@Composable @Composable
fun SelectionItemLabel( fun SelectionItemLabel(
textResId: Int, textResId: Int,
style: androidx.compose.ui.text.TextStyle = MaterialTheme.typography.bodyMedium, style: androidx.compose.ui.text.TextStyle = MaterialTheme.typography.bodyMedium,
isDescription: Boolean = false, isDescription: Boolean = false,
) { ) {
Text( Text(
text = stringResource(textResId), text = stringResource(textResId),
style = style.copy( style =
color = if (isDescription) MaterialTheme.colorScheme.outline else MaterialTheme.colorScheme.onSurface, style.copy(
), color =
) if (isDescription) MaterialTheme.colorScheme.outline
else MaterialTheme.colorScheme.onSurface
),
)
} }
@@ -1,84 +1,76 @@
package com.zaneschepke.wireguardautotunnel.ui.common.button.surface package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults import androidx.compose.material3.CardDefaults
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
@Composable @Composable
fun SurfaceSelectionGroupButton(items: List<SelectionItem>) { fun SurfaceSelectionGroupButton(items: List<SelectionItem>) {
Card( Card(
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
shape = RoundedCornerShape(8.dp), shape = RoundedCornerShape(8.dp),
colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface),
) { ) {
items.mapIndexed { index, item -> items.map { item ->
Box( Box(
contentAlignment = Alignment.Center, contentAlignment = Alignment.Center,
modifier = Modifier modifier =
.then(item.onClick?.let { Modifier.clickable { it() } } ?: Modifier) Modifier.fillMaxWidth()
.fillMaxWidth(), .clip(RoundedCornerShape(8.dp))
) { .then(item.onClick?.let { Modifier.clickable { it() } } ?: Modifier),
Row( ) {
verticalAlignment = Alignment.CenterVertically, Row(
modifier = Modifier.fillMaxWidth().padding(vertical = 4.dp), verticalAlignment = Alignment.CenterVertically,
) { modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
Row( ) {
verticalAlignment = Alignment.CenterVertically, Row(
modifier = Modifier verticalAlignment = Alignment.CenterVertically,
.weight(4f, false) modifier = Modifier.weight(4f, false).fillMaxWidth(),
.fillMaxWidth(), ) {
) { item.leadingIcon?.let { icon ->
item.leadingIcon?.let { icon -> Icon(
Icon( icon,
icon, icon.name,
icon.name, modifier = Modifier.size(iconSize),
modifier = Modifier.size(iconSize), tint = MaterialTheme.colorScheme.onSurface,
tint = MaterialTheme.colorScheme.onSurface, )
) }
} Column(
Column( horizontalAlignment = Alignment.Start,
horizontalAlignment = Alignment.Start, verticalArrangement =
verticalArrangement = Arrangement.spacedBy(2.dp, Alignment.CenterVertically), Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
modifier = Modifier modifier =
.fillMaxWidth() Modifier.fillMaxWidth()
.padding(start = if (item.leadingIcon != null) 16.dp else 0.dp) .padding(start = if (item.leadingIcon != null) 16.dp else 0.dp)
.padding(vertical = if (item.description == null) 16.dp else 6.dp), .weight(1f)
) { .padding(
item.title() vertical = if (item.description == null) 16.dp else 6.dp
item.description?.let { ),
it() ) {
} item.title()
} item.description?.let { it() }
} }
item.trailing?.let { }
Box( item.trailing?.let {
contentAlignment = Alignment.CenterEnd, Box(
modifier = Modifier contentAlignment = Alignment.CenterEnd,
.padding(start = 16.dp) modifier = Modifier.padding(start = 16.dp),
.weight(1f), ) {
) { it()
it() }
} }
} }
} }
} }
if (index + 1 != items.size) HorizontalDivider(color = MaterialTheme.colorScheme.outline.copy(0.30f)) }
}
}
} }
@@ -13,33 +13,43 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
@Composable @Composable
fun ConfigurationTextBox( fun ConfigurationTextBox(
value: String, value: String,
hint: String, hint: String,
onValueChange: (String) -> Unit, onValueChange: (String) -> Unit,
keyboardActions: KeyboardActions = KeyboardActions(), keyboardActions: KeyboardActions = KeyboardActions(),
label: String, label: String,
modifier: Modifier, modifier: Modifier,
isError: Boolean = false, isError: Boolean = false,
keyboardOptions: KeyboardOptions = KeyboardOptions( keyboardOptions: KeyboardOptions =
capitalization = KeyboardCapitalization.None, KeyboardOptions(capitalization = KeyboardCapitalization.None, imeAction = ImeAction.Done),
imeAction = ImeAction.Done, trailing: (@Composable () -> Unit)? = null,
), interactionSource: MutableInteractionSource? = null,
trailing: (@Composable () -> Unit)? = null,
interactionSource: MutableInteractionSource? = null,
) { ) {
OutlinedTextField( OutlinedTextField(
isError = isError, isError = isError,
textStyle = MaterialTheme.typography.labelLarge, textStyle = MaterialTheme.typography.labelLarge,
modifier = modifier, modifier = modifier,
value = value, value = value,
singleLine = true, singleLine = true,
interactionSource = interactionSource, interactionSource = interactionSource,
onValueChange = { onValueChange(it) }, onValueChange = { onValueChange(it) },
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.bodyMedium) }, label = {
maxLines = 1, Text(
placeholder = { Text(hint, color = MaterialTheme.colorScheme.outline, style = MaterialTheme.typography.bodyMedium) }, label,
keyboardOptions = keyboardOptions, color = MaterialTheme.colorScheme.onSurface,
keyboardActions = keyboardActions, style = MaterialTheme.typography.bodyMedium,
trailingIcon = trailing, )
) },
maxLines = 1,
placeholder = {
Text(
hint,
color = MaterialTheme.colorScheme.outline,
style = MaterialTheme.typography.bodyMedium,
)
},
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
trailingIcon = trailing,
)
} }
@@ -13,36 +13,29 @@ import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
@Composable @Composable
fun ConfigurationToggle( fun ConfigurationToggle(
label: String, label: String,
enabled: Boolean = true, enabled: Boolean = true,
checked: Boolean, checked: Boolean,
onCheckChanged: (checked: Boolean) -> Unit, onCheckChanged: (checked: Boolean) -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
Row( Row(
modifier = modifier = Modifier.fillMaxWidth(),
Modifier verticalAlignment = Alignment.CenterVertically,
.fillMaxWidth(), horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically, ) {
horizontalArrangement = Arrangement.SpaceBetween, Text(
) { label,
Text( textAlign = TextAlign.Start,
label, style = MaterialTheme.typography.labelLarge,
textAlign = TextAlign.Start, modifier = Modifier.weight(weight = 1.0f, fill = false),
style = MaterialTheme.typography.labelLarge, softWrap = true,
modifier = )
Modifier ScaledSwitch(
.weight( modifier = modifier,
weight = 1.0f, enabled = enabled,
fill = false, checked = checked,
), onClick = { onCheckChanged(it) },
softWrap = true, )
) }
ScaledSwitch(
modifier = modifier,
enabled = enabled,
checked = checked,
onClick = { onCheckChanged(it) },
)
}
} }
@@ -27,61 +27,70 @@ import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
@Composable @Composable
fun SubmitConfigurationTextBox( fun SubmitConfigurationTextBox(
value: String?, value: String?,
label: String, label: String,
hint: String, hint: String,
isErrorValue: (value: String?) -> Boolean, isErrorValue: (value: String?) -> Boolean,
onSubmit: (value: String) -> Unit, onSubmit: (value: String) -> Unit,
keyboardOptions: KeyboardOptions = KeyboardOptions( keyboardOptions: KeyboardOptions =
capitalization = KeyboardCapitalization.None, KeyboardOptions(capitalization = KeyboardCapitalization.None, imeAction = ImeAction.Done),
imeAction = ImeAction.Done,
),
) { ) {
val focusManager = LocalFocusManager.current val focusManager = LocalFocusManager.current
val interactionSource = remember { MutableInteractionSource() } val interactionSource = remember { MutableInteractionSource() }
val isFocused by interactionSource.collectIsFocusedAsState() val isFocused by interactionSource.collectIsFocusedAsState()
val keyboardController = LocalSoftwareKeyboardController.current val keyboardController = LocalSoftwareKeyboardController.current
var stateValue by remember { mutableStateOf(value ?: "") } var stateValue by remember { mutableStateOf(value ?: "") }
CustomTextField( CustomTextField(
isError = isErrorValue(stateValue), isError = isErrorValue(stateValue),
textStyle = MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.onSurface), textStyle =
value = stateValue, MaterialTheme.typography.bodySmall.copy(color = MaterialTheme.colorScheme.onSurface),
onValueChange = { stateValue = it }, value = stateValue,
interactionSource = interactionSource, onValueChange = { stateValue = it },
label = { Text(label, color = MaterialTheme.colorScheme.onSurface, style = MaterialTheme.typography.labelMedium) }, interactionSource = interactionSource,
containerColor = MaterialTheme.colorScheme.surface, label = {
placeholder = { Text(hint, style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline) }, Text(
modifier = label,
Modifier color = MaterialTheme.colorScheme.onSurface,
.padding( style = MaterialTheme.typography.labelMedium,
top = 5.dp, )
bottom = 10.dp, },
).fillMaxWidth().padding(end = 16.dp), containerColor = MaterialTheme.colorScheme.surface,
singleLine = true, placeholder = {
keyboardOptions = keyboardOptions, Text(
keyboardActions = KeyboardActions( hint,
onDone = { style = MaterialTheme.typography.bodySmall,
onSubmit(stateValue) color = MaterialTheme.colorScheme.outline,
keyboardController?.hide() )
}, },
), modifier = Modifier.padding(top = 5.dp, bottom = 10.dp).fillMaxWidth().padding(end = 16.dp),
trailing = { singleLine = true,
if (!isErrorValue(stateValue) && isFocused) { keyboardOptions = keyboardOptions,
IconButton(onClick = { keyboardActions =
onSubmit(stateValue) KeyboardActions(
keyboardController?.hide() onDone = {
focusManager.clearFocus() onSubmit(stateValue)
}) { keyboardController?.hide()
val icon = Icons.Outlined.Save }
Icon( ),
imageVector = icon, trailing = {
contentDescription = icon.name, if (!isErrorValue(stateValue) && isFocused) {
tint = MaterialTheme.colorScheme.primary, IconButton(
) onClick = {
} onSubmit(stateValue)
} keyboardController?.hide()
}, focusManager.clearFocus()
) }
) {
val icon = Icons.Outlined.Save
Icon(
imageVector = icon,
contentDescription = icon.name,
tint = MaterialTheme.colorScheme.primary,
)
}
}
},
)
} }
@@ -13,38 +13,27 @@ import com.zaneschepke.wireguardautotunnel.R
@Composable @Composable
fun InfoDialog( fun InfoDialog(
onAttest: () -> Unit, onAttest: () -> Unit,
onDismiss: () -> Unit, onDismiss: () -> Unit,
title: @Composable () -> Unit, title: @Composable () -> Unit,
body: @Composable () -> Unit, body: @Composable () -> Unit,
confirmText: @Composable () -> Unit, confirmText: @Composable () -> Unit,
) { ) {
MaterialTheme( MaterialTheme(colorScheme = MaterialTheme.colorScheme.copy()) {
colorScheme = MaterialTheme.colorScheme.copy(), Surface(color = MaterialTheme.colorScheme.surface, tonalElevation = 0.dp) {
) { AlertDialog(
Surface( onDismissRequest = { onDismiss() },
color = MaterialTheme.colorScheme.surface, confirmButton = { TextButton(onClick = { onAttest() }) { confirmText() } },
tonalElevation = 0.dp, dismissButton = {
) { TextButton(onClick = { onDismiss() }) {
AlertDialog( Text(text = stringResource(R.string.cancel))
onDismissRequest = { onDismiss() }, }
confirmButton = { },
TextButton(onClick = { onAttest() }) { containerColor = MaterialTheme.colorScheme.surface,
confirmText() title = { title() },
} text = { body() },
}, properties = DialogProperties(usePlatformDefaultWidth = true),
dismissButton = { )
TextButton(onClick = { onDismiss() }) { }
Text(text = stringResource(R.string.cancel)) }
}
},
containerColor = MaterialTheme.colorScheme.surface,
title = { title() },
text = { body() },
properties = DialogProperties(
usePlatformDefaultWidth = true,
),
)
}
}
} }
@@ -14,35 +14,39 @@ import com.zaneschepke.wireguardautotunnel.util.extensions.launchVpnSettings
@Composable @Composable
fun VpnDeniedDialog(show: Boolean, onDismiss: () -> Unit) { fun VpnDeniedDialog(show: Boolean, onDismiss: () -> Unit) {
val context = LocalContext.current val context = LocalContext.current
if (show) { if (show) {
val alwaysOnDescription = buildAnnotatedString { val alwaysOnDescription = buildAnnotatedString {
append(stringResource(R.string.always_on_message)) append(stringResource(R.string.always_on_message))
append(" ") append(" ")
pushStringAnnotation(tag = "vpnSettings", annotation = "") pushStringAnnotation(tag = "vpnSettings", annotation = "")
withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.primary)) { withStyle(style = SpanStyle(color = MaterialTheme.colorScheme.primary)) {
append(stringResource(id = R.string.vpn_settings)) append(stringResource(id = R.string.vpn_settings))
} }
pop() pop()
append(" ") append(" ")
append(stringResource(R.string.always_on_message2)) append(stringResource(R.string.always_on_message2))
append(".") append(".")
} }
InfoDialog( InfoDialog(
onDismiss = { onDismiss() }, onDismiss = { onDismiss() },
onAttest = { onDismiss() }, onAttest = { onDismiss() },
title = { Text(text = stringResource(R.string.vpn_denied_dialog_title)) }, title = { Text(text = stringResource(R.string.vpn_denied_dialog_title)) },
body = { body = {
ClickableText( ClickableText(
text = alwaysOnDescription, text = alwaysOnDescription,
style = MaterialTheme.typography.bodyMedium.copy(color = MaterialTheme.colorScheme.outline), style =
) { MaterialTheme.typography.bodyMedium.copy(
alwaysOnDescription.getStringAnnotations(tag = "vpnSettings", it, it).firstOrNull()?.let { color = MaterialTheme.colorScheme.outline
context.launchVpnSettings() ),
} ) {
} alwaysOnDescription
}, .getStringAnnotations(tag = "vpnSettings", it, it)
confirmText = { Text(text = stringResource(R.string.okay)) }, .firstOrNull()
) ?.let { context.launchVpnSettings() }
} }
},
confirmText = { Text(text = stringResource(R.string.okay)) },
)
}
} }
@@ -13,48 +13,53 @@ import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
@Composable @Composable
fun rememberFileImportLauncherForResult(onNoFileExplorer: () -> Unit, onData: (data: Uri) -> Unit): ManagedActivityResultLauncher<String, Uri?> { fun rememberFileImportLauncherForResult(
return rememberLauncherForActivityResult( onNoFileExplorer: () -> Unit,
object : ActivityResultContracts.GetContent() { onData: (data: Uri) -> Unit,
override fun createIntent(context: Context, input: String): Intent { ): ManagedActivityResultLauncher<String, Uri?> {
val intent = super.createIntent(context, input).apply { return rememberLauncherForActivityResult(
type = if (context.isRunningOnTv()) { object : ActivityResultContracts.GetContent() {
Constants.ALLOWED_TV_FILE_TYPES override fun createIntent(context: Context, input: String): Intent {
} else { val intent =
Constants.ALL_FILE_TYPES super.createIntent(context, input).apply {
} type =
} if (context.isRunningOnTv()) {
Constants.ALLOWED_TV_FILE_TYPES
} else {
Constants.ALL_FILE_TYPES
}
}
/* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than /* AndroidTV now comes with stubs that do nothing but display a Toast less helpful than
* what we can do, so detect this and throw an exception that we can catch later. */ * what we can do, so detect this and throw an exception that we can catch later. */
val activitiesToResolveIntent = val activitiesToResolveIntent =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager.queryIntentActivities( context.packageManager.queryIntentActivities(
intent, intent,
PackageManager.ResolveInfoFlags.of( PackageManager.ResolveInfoFlags.of(
PackageManager.MATCH_DEFAULT_ONLY.toLong(), PackageManager.MATCH_DEFAULT_ONLY.toLong()
), ),
) )
} else { } else {
context.packageManager.queryIntentActivities( context.packageManager.queryIntentActivities(
intent, intent,
PackageManager.MATCH_DEFAULT_ONLY, PackageManager.MATCH_DEFAULT_ONLY,
) )
} }
if ( if (
activitiesToResolveIntent.all { activitiesToResolveIntent.all {
val name = it.activityInfo.packageName val name = it.activityInfo.packageName
name.startsWith(Constants.GOOGLE_TV_EXPLORER_STUB) || name.startsWith(Constants.GOOGLE_TV_EXPLORER_STUB) ||
name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB) name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB)
} }
) { ) {
onNoFileExplorer() onNoFileExplorer()
} }
return intent return intent
} }
}, }
) { data -> ) { data ->
if (data == null) return@rememberLauncherForActivityResult if (data == null) return@rememberLauncherForActivityResult
onData(data) onData(data)
} }
} }
@@ -6,19 +6,21 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
@Composable @Composable
fun GroupLabel(title: String) { fun GroupLabel(title: String, modifier: Modifier = Modifier) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.Start, horizontalArrangement = Arrangement.Start,
) { modifier = modifier,
Text( ) {
title, Text(
style = MaterialTheme.typography.titleMedium, title,
fontWeight = FontWeight.Bold, style = MaterialTheme.typography.titleMedium,
color = MaterialTheme.colorScheme.onBackground, fontWeight = FontWeight.Bold,
) color = MaterialTheme.colorScheme.onBackground,
} )
}
} }
@@ -12,12 +12,12 @@ import androidx.compose.ui.Modifier
@Composable @Composable
fun SelectedLabel() { fun SelectedLabel() {
Row( Row(
modifier = Modifier.fillMaxSize(), modifier = Modifier.fillMaxSize(),
horizontalArrangement = Arrangement.End, horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
) { ) {
val icon = Icons.Outlined.CheckBox val icon = Icons.Outlined.CheckBox
Icon(icon, icon.name) Icon(icon, icon.name)
} }
} }
@@ -32,80 +32,84 @@ import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv
@Composable @Composable
fun BottomBarTabs(tabs: List<BottomNavItem>, selectedTabIndex: Int, isChildRoute: Boolean, onTabSelected: (BottomNavItem) -> Unit) { fun BottomBarTabs(
val context = LocalContext.current tabs: List<BottomNavItem>,
val isRunningOnTv = remember { context.isRunningOnTv() } selectedTabIndex: Int,
isChildRoute: Boolean,
onTabSelected: (BottomNavItem) -> Unit,
) {
val context = LocalContext.current
val isRunningOnTv = remember { context.isRunningOnTv() }
Row( Row(
modifier = Modifier modifier =
.fillMaxWidth() Modifier.fillMaxWidth().height(64.dp).padding(horizontal = 8.dp).padding(top = 12.dp),
.height(64.dp) horizontalArrangement = Arrangement.SpaceEvenly,
.padding(horizontal = 8.dp) verticalAlignment = Alignment.CenterVertically,
.padding(top = 12.dp), ) {
horizontalArrangement = Arrangement.SpaceEvenly, tabs.forEachIndexed { index, tab ->
verticalAlignment = Alignment.CenterVertically, Column(
) { modifier =
tabs.forEachIndexed { index, tab -> Modifier.weight(1f)
Column( .fillMaxHeight()
modifier = Modifier .background(Color.Transparent)
.weight(1f) .then(
.fillMaxHeight() if (isRunningOnTv) {
.background(Color.Transparent) Modifier.clickable {
.then( if (index == selectedTabIndex && !isChildRoute) return@clickable
if (isRunningOnTv) { tab.onClick.invoke()
Modifier.clickable { onTabSelected(tab)
if (index == selectedTabIndex && !isChildRoute) return@clickable }
tab.onClick.invoke() } else {
onTabSelected(tab) Modifier
} }
} else { )
Modifier .pointerInput(Unit) {
}, detectTapGestures {
) if (index == selectedTabIndex && !isChildRoute)
.pointerInput(Unit) { return@detectTapGestures
detectTapGestures { tab.onClick.invoke()
if (index == selectedTabIndex && !isChildRoute) return@detectTapGestures onTabSelected(tab)
tab.onClick.invoke() }
onTabSelected(tab) },
} horizontalAlignment = Alignment.CenterHorizontally,
}, verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally, ) {
verticalArrangement = Arrangement.Center, val animatedColor by
) { animateColorAsState(
val animatedColor by animateColorAsState( targetValue = MaterialTheme.colorScheme.primary,
targetValue = MaterialTheme.colorScheme.primary, animationSpec = spring(stiffness = Spring.StiffnessLow),
animationSpec = spring(stiffness = Spring.StiffnessLow), label = "animatedColor",
label = "animatedColor", )
) val color =
val color = if (selectedTabIndex == index) animatedColor else MaterialTheme.colorScheme.onSurface if (selectedTabIndex == index) animatedColor
else MaterialTheme.colorScheme.onSurface
if (tab.active) { if (tab.active) {
BadgedBox( BadgedBox(
badge = { badge = {
Badge( Badge(
modifier = Modifier modifier = Modifier.offset(x = 8.dp, y = ((-8).dp)).size(6.dp),
.offset(x = 8.dp, y = ((-8).dp)) containerColor = SilverTree,
.size(6.dp), )
containerColor = SilverTree, }
) ) {
}, Icon(
) { imageVector = tab.icon,
Icon( contentDescription = tab.name,
imageVector = tab.icon, tint = color,
contentDescription = tab.name, modifier = Modifier.size(24.dp),
tint = color, )
modifier = Modifier.size(24.dp), }
) } else {
} Icon(
} else { imageVector = tab.icon,
Icon( contentDescription = tab.name,
imageVector = tab.icon, tint = color,
contentDescription = tab.name, modifier = Modifier.size(24.dp),
tint = color, )
modifier = Modifier.size(24.dp), }
) }
} }
} }
}
}
} }
@@ -4,9 +4,9 @@ import androidx.compose.ui.graphics.vector.ImageVector
import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.Route
data class BottomNavItem( data class BottomNavItem(
val name: String, val name: String,
val route: Route, val route: Route,
val icon: ImageVector, val icon: ImageVector,
val onClick: () -> Unit, val onClick: () -> Unit,
val active: Boolean = false, val active: Boolean = false,
) )

Some files were not shown because too many files have changed in this diff Show More