Compare commits

..

24 Commits

Author SHA1 Message Date
dependabot[bot] 747dba6c1e chore(deps): bump lifecycle-runtime-compose from 2.9.2 to 2.9.3
Bumps `lifecycle-runtime-compose` from 2.9.2 to 2.9.3.

Updates `androidx.lifecycle:lifecycle-process` from 2.9.2 to 2.9.3

Updates `androidx.lifecycle:lifecycle-service` from 2.9.2 to 2.9.3

Updates `androidx.lifecycle:lifecycle-runtime-ktx` from 2.9.2 to 2.9.3

Updates `androidx.lifecycle:lifecycle-runtime-compose` from 2.9.2 to 2.9.3

---
updated-dependencies:
- dependency-name: androidx.lifecycle:lifecycle-process
  dependency-version: 2.9.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: androidx.lifecycle:lifecycle-service
  dependency-version: 2.9.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: androidx.lifecycle:lifecycle-runtime-ktx
  dependency-version: 2.9.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
- dependency-name: androidx.lifecycle:lifecycle-runtime-compose
  dependency-version: 2.9.3
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-09-10 13:13:22 +00:00
Zane Schepke ee808e0b4d refactor: add addreses and allow show address
#842
2025-09-10 05:21:37 -04:00
Zane Schepke 5749f06229 fix: non vpnservice mode crashes
closes #935

Fixes issue where foreground service exempt type causes security exception crash on latest android API version without an added vpn profile. Switched to special use type for kernel/proxy foreground service.
2025-09-10 01:29:06 -04:00
Zane Schepke 207c1a4d1a fix: remove biometrics requirement
This is already redundant with pin lock feature, removing it.

closes #933
2025-09-09 11:47:59 -04:00
Zane Schepke 050efe2fb3 feat: add in app donation details, crypto addresses
closes #842
2025-09-09 10:15:06 -04:00
Zane Schepke 5bd497c8bb refactor: disbale unsupported features on mode changes, copy app version
closes #930
2025-09-08 21:28:10 -04:00
Zane Schepke 82b39695de fix: improve tun state icon to reflect health 2025-09-08 06:44:42 -04:00
Zane Schepke 67eacc576c refactor: make launch options more obvious 2025-09-08 05:10:17 -04:00
Zane Schepke c9c4bbf3bf fix: tunnel screen nav bar action rendering bug 2025-09-08 03:31:11 -04:00
Zane Schepke 297e8c1f93 fix: fmt, mode icon button alignment 2025-09-08 02:00:28 -04:00
Zane Schepke 5f8bc7b4f6 feat!: ddns re-resolve and dynamic peer update without bouncing tunnel
This feature allows users to enable DDNS re-resolve that will check for a DDNS change on handshake and/or ping failures without the need for auto tunnel and will dynamically update peers without bouncing the tunnel for added security/privacy.

- Fixes tile toggling random/wrong tunnel.
- Fixes native thread socket bypass crash with lockdown mode
- Removes bouncing logic as DDNS change can now be resolved via a bypassed DoH socket and update the peers endpoints dynamically.
- Restarting active tunnel on manual config changes removed. Will be replaced soon with a modal prompt asking users if they would like to restart (to preserve privacy and prevent leaks).
- Refactor of tunnel start and stop logic for cleaner tunnel startup and shutdowns.
- Various other fixes and changes.
2025-09-08 01:36:17 -04:00
Zane Schepke 37845f2e77 fix: proxy ui bugs 2025-09-04 05:10:41 -04:00
Zane Schepke 4cb65c5d30 fix: improve tunnel shutdown, fix bounce bug
closes #913
2025-09-04 03:41:55 -04:00
Zane Schepke ee1fcc6b24 fix: proxy setting save bug 2025-09-03 20:24:58 -04:00
Zane Schepke 875eae29e7 fix: tunnel ipv4 toggle bug 2025-09-03 18:41:59 -04:00
Zane Schepke 0ea84b3e31 fix: navigation sync bug 2025-09-03 18:39:28 -04:00
Zane Schepke 7a0af2462b fix: go parser 2025-09-01 18:17:29 -04:00
Zane Schepke df7154d0c1 fix: amnezia config parse 2025-09-01 17:30:47 -04:00
Zane Schepke fa4cc84c0e refactor: navigation and viewmodels (#925)
- Added UI support for Amnezia 1.5
- Fix for http proxy race
- Fix for db migration race
- Split tunnel app packages caching
- Detailed config parsing error messages
- Added navgraph with viewmodel scoping
- Pin minor ui enhancements
2025-09-01 16:08:40 -04:00
Zane Schepke d07cf7a24b chore: bump deps 2025-08-24 01:35:26 -04:00
Zane Schepke 74c4efe477 fix: proguard rules consume bug 2025-08-23 20:08:16 -04:00
Zane Schepke 3256da1cfa fix: proguard minification bug 2025-08-23 19:11:42 -04:00
Zane Schepke 132728f5dd fix: add missing db migration query 2025-08-23 15:32:51 -04:00
Zane Schepke 3eb72cd43c feat!: proxied backend and lockdown mode (#911)
Another big one. 

- SOCKS5/HTTP proxy integration via "proxy mode"
- Kill switch (which was hacky and leaky) has been replace by a robust "lockdown mode" which keeps a dummy vpnservice tunnel active, capturing all device traffic and fowarding it to a netstack/gvisor tunnel via the SOCKS5 proxy. If a tunnel requires DNS resolution for peer endpoints, we punch a hole by bypassing a socket for DoH resolution to keep things secure and private. 
- DoH support for peer endpoint resolutions for proxy, lockdown, and regular vpn mode (not kernel, yet) with support for cloudflare and Adguard DoH providers.
2025-08-23 03:56:13 -04:00
254 changed files with 8864 additions and 5624 deletions
+6 -1
View File
@@ -9,6 +9,7 @@ plugins {
alias(libs.plugins.compose.compiler) alias(libs.plugins.compose.compiler)
alias(libs.plugins.grgit) alias(libs.plugins.grgit)
alias(libs.plugins.licensee) alias(libs.plugins.licensee)
id("kotlin-parcelize")
} }
android { android {
@@ -200,7 +201,6 @@ dependencies {
implementation(libs.material.icons.core) implementation(libs.material.icons.core)
implementation(libs.material.icons.extended) implementation(libs.material.icons.extended)
implementation(libs.androidx.biometric.ktx)
implementation(libs.pin.lock.compose) implementation(libs.pin.lock.compose)
implementation(libs.androidx.core) implementation(libs.androidx.core)
@@ -229,6 +229,11 @@ dependencies {
implementation(libs.roomdatabasebackup) { implementation(libs.roomdatabasebackup) {
exclude(group = "org.reactivestreams", module = "reactive-streams") exclude(group = "org.reactivestreams", module = "reactive-streams")
} }
// state management
implementation(libs.orbit.compose)
implementation(libs.orbit.viewmodel)
implementation(libs.orbit.core)
} }
tasks.register<Copy>("copyLicenseeJsonToAssets") { tasks.register<Copy>("copyLicenseeJsonToAssets") {
@@ -0,0 +1,359 @@
{
"formatVersion": 1,
"database": {
"version": 21,
"identityHash": "51f828868c0ea2f0f5c987410ff5c5a1",
"entities": [
{
"tableName": "Settings",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `is_tunnel_enabled` INTEGER NOT NULL, `is_tunnel_on_mobile_data_enabled` INTEGER NOT NULL, `trusted_network_ssids` TEXT NOT NULL, `is_always_on_vpn_enabled` INTEGER NOT NULL, `is_tunnel_on_ethernet_enabled` INTEGER NOT NULL, `is_shortcuts_enabled` INTEGER NOT NULL DEFAULT false, `is_tunnel_on_wifi_enabled` INTEGER NOT NULL DEFAULT false, `is_restore_on_boot_enabled` INTEGER NOT NULL DEFAULT false, `is_multi_tunnel_enabled` INTEGER NOT NULL DEFAULT false, `is_ping_enabled` INTEGER NOT NULL DEFAULT false, `is_wildcards_enabled` INTEGER NOT NULL DEFAULT false, `is_stop_on_no_internet_enabled` INTEGER NOT NULL DEFAULT false, `is_lan_on_kill_switch_enabled` INTEGER NOT NULL DEFAULT false, `debounce_delay_seconds` INTEGER NOT NULL DEFAULT 3, `is_disable_kill_switch_on_trusted_enabled` INTEGER NOT NULL DEFAULT false, `is_tunnel_on_unsecure_enabled` INTEGER NOT NULL DEFAULT false, `wifi_detection_method` INTEGER NOT NULL DEFAULT 0, `is_ping_monitoring_enabled` INTEGER NOT NULL DEFAULT true, `tunnel_ping_interval_sec` INTEGER NOT NULL DEFAULT 30, `tunnel_ping_attempts` INTEGER NOT NULL DEFAULT 3, `tunnel_ping_timeout_sec` INTEGER, `app_mode` INTEGER NOT NULL DEFAULT 0, `dns_protocol` INTEGER NOT NULL DEFAULT 0, `dns_endpoint` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isAutoTunnelEnabled",
"columnName": "is_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isTunnelOnMobileDataEnabled",
"columnName": "is_tunnel_on_mobile_data_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "trustedNetworkSSIDs",
"columnName": "trusted_network_ssids",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "isAlwaysOnVpnEnabled",
"columnName": "is_always_on_vpn_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isTunnelOnEthernetEnabled",
"columnName": "is_tunnel_on_ethernet_enabled",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isShortcutsEnabled",
"columnName": "is_shortcuts_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isTunnelOnWifiEnabled",
"columnName": "is_tunnel_on_wifi_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isRestoreOnBootEnabled",
"columnName": "is_restore_on_boot_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isMultiTunnelEnabled",
"columnName": "is_multi_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isPingEnabled",
"columnName": "is_ping_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isWildcardsEnabled",
"columnName": "is_wildcards_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isStopOnNoInternetEnabled",
"columnName": "is_stop_on_no_internet_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isLanOnKillSwitchEnabled",
"columnName": "is_lan_on_kill_switch_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "debounceDelaySeconds",
"columnName": "debounce_delay_seconds",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "3"
},
{
"fieldPath": "isDisableKillSwitchOnTrustedEnabled",
"columnName": "is_disable_kill_switch_on_trusted_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isTunnelOnUnsecureEnabled",
"columnName": "is_tunnel_on_unsecure_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "wifiDetectionMethod",
"columnName": "wifi_detection_method",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isPingMonitoringEnabled",
"columnName": "is_ping_monitoring_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "true"
},
{
"fieldPath": "tunnelPingIntervalSeconds",
"columnName": "tunnel_ping_interval_sec",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "30"
},
{
"fieldPath": "tunnelPingAttempts",
"columnName": "tunnel_ping_attempts",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "3"
},
{
"fieldPath": "tunnelPingTimeoutSeconds",
"columnName": "tunnel_ping_timeout_sec",
"affinity": "INTEGER"
},
{
"fieldPath": "appMode",
"columnName": "app_mode",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "dnsProtocol",
"columnName": "dns_protocol",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "dnsEndpoint",
"columnName": "dns_endpoint",
"affinity": "TEXT"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
},
{
"tableName": "TunnelConfig",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `wg_quick` TEXT NOT NULL, `tunnel_networks` TEXT NOT NULL DEFAULT '', `is_mobile_data_tunnel` INTEGER NOT NULL DEFAULT false, `is_primary_tunnel` INTEGER NOT NULL DEFAULT false, `am_quick` TEXT NOT NULL DEFAULT '', `is_Active` INTEGER NOT NULL DEFAULT false, `restart_on_ping_failure` INTEGER NOT NULL DEFAULT false, `ping_target` TEXT DEFAULT null, `is_ethernet_tunnel` INTEGER NOT NULL DEFAULT false, `is_ipv4_preferred` INTEGER NOT NULL DEFAULT true, `position` INTEGER NOT NULL DEFAULT 0, `auto_tunnel_apps` TEXT NOT NULL DEFAULT '[]')",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "wgQuick",
"columnName": "wg_quick",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "tunnelNetworks",
"columnName": "tunnel_networks",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isMobileDataTunnel",
"columnName": "is_mobile_data_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isPrimaryTunnel",
"columnName": "is_primary_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "amQuick",
"columnName": "am_quick",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isActive",
"columnName": "is_Active",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "restartOnPingFailure",
"columnName": "restart_on_ping_failure",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "pingTarget",
"columnName": "ping_target",
"affinity": "TEXT",
"defaultValue": "null"
},
{
"fieldPath": "isEthernetTunnel",
"columnName": "is_ethernet_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isIpv4Preferred",
"columnName": "is_ipv4_preferred",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "true"
},
{
"fieldPath": "position",
"columnName": "position",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "autoTunnelApps",
"columnName": "auto_tunnel_apps",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "'[]'"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_TunnelConfig_name",
"unique": true,
"columnNames": [
"name"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_TunnelConfig_name` ON `${TABLE_NAME}` (`name`)"
}
]
},
{
"tableName": "proxy_settings",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `socks5_proxy_enabled` INTEGER NOT NULL DEFAULT false, `socks5_proxy_bind_address` TEXT, `http_proxy_enable` INTEGER NOT NULL DEFAULT false, `http_proxy_bind_address` TEXT, `proxy_username` TEXT, `proxy_password` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "socks5ProxyEnabled",
"columnName": "socks5_proxy_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "socks5ProxyBindAddress",
"columnName": "socks5_proxy_bind_address",
"affinity": "TEXT"
},
{
"fieldPath": "httpProxyEnabled",
"columnName": "http_proxy_enable",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "httpProxyBindAddress",
"columnName": "http_proxy_bind_address",
"affinity": "TEXT"
},
{
"fieldPath": "proxyUsername",
"columnName": "proxy_username",
"affinity": "TEXT"
},
{
"fieldPath": "proxyPassword",
"columnName": "proxy_password",
"affinity": "TEXT"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '51f828868c0ea2f0f5c987410ff5c5a1')"
]
}
}
@@ -0,0 +1,364 @@
{
"formatVersion": 1,
"database": {
"version": 22,
"identityHash": "db93d0490401ccbef25ca39f27bafa29",
"entities": [
{
"tableName": "Settings",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `is_tunnel_enabled` INTEGER NOT NULL DEFAULT 0, `is_tunnel_on_mobile_data_enabled` INTEGER NOT NULL DEFAULT 0, `trusted_network_ssids` TEXT NOT NULL DEFAULT '', `is_always_on_vpn_enabled` INTEGER NOT NULL DEFAULT 0, `is_tunnel_on_ethernet_enabled` INTEGER NOT NULL DEFAULT 0, `is_shortcuts_enabled` INTEGER NOT NULL DEFAULT 0, `is_tunnel_on_wifi_enabled` INTEGER NOT NULL DEFAULT 0, `is_restore_on_boot_enabled` INTEGER NOT NULL DEFAULT 0, `is_multi_tunnel_enabled` INTEGER NOT NULL DEFAULT 0, `is_ping_enabled` INTEGER NOT NULL DEFAULT 0, `is_wildcards_enabled` INTEGER NOT NULL DEFAULT 0, `is_stop_on_no_internet_enabled` INTEGER NOT NULL DEFAULT 0, `is_lan_on_kill_switch_enabled` INTEGER NOT NULL DEFAULT 0, `debounce_delay_seconds` INTEGER NOT NULL DEFAULT 3, `is_disable_kill_switch_on_trusted_enabled` INTEGER NOT NULL DEFAULT 0, `is_tunnel_on_unsecure_enabled` INTEGER NOT NULL DEFAULT 0, `wifi_detection_method` INTEGER NOT NULL DEFAULT 0, `is_ping_monitoring_enabled` INTEGER NOT NULL DEFAULT 1, `tunnel_ping_interval_sec` INTEGER NOT NULL DEFAULT 30, `tunnel_ping_attempts` INTEGER NOT NULL DEFAULT 3, `tunnel_ping_timeout_sec` INTEGER, `app_mode` INTEGER NOT NULL DEFAULT 0, `dns_protocol` INTEGER NOT NULL DEFAULT 0, `dns_endpoint` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isAutoTunnelEnabled",
"columnName": "is_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isTunnelOnMobileDataEnabled",
"columnName": "is_tunnel_on_mobile_data_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "trustedNetworkSSIDs",
"columnName": "trusted_network_ssids",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isAlwaysOnVpnEnabled",
"columnName": "is_always_on_vpn_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isTunnelOnEthernetEnabled",
"columnName": "is_tunnel_on_ethernet_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isShortcutsEnabled",
"columnName": "is_shortcuts_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isTunnelOnWifiEnabled",
"columnName": "is_tunnel_on_wifi_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isRestoreOnBootEnabled",
"columnName": "is_restore_on_boot_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isMultiTunnelEnabled",
"columnName": "is_multi_tunnel_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isPingEnabled",
"columnName": "is_ping_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isWildcardsEnabled",
"columnName": "is_wildcards_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isStopOnNoInternetEnabled",
"columnName": "is_stop_on_no_internet_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isLanOnKillSwitchEnabled",
"columnName": "is_lan_on_kill_switch_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "debounceDelaySeconds",
"columnName": "debounce_delay_seconds",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "3"
},
{
"fieldPath": "isDisableKillSwitchOnTrustedEnabled",
"columnName": "is_disable_kill_switch_on_trusted_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isTunnelOnUnsecureEnabled",
"columnName": "is_tunnel_on_unsecure_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "wifiDetectionMethod",
"columnName": "wifi_detection_method",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "isPingMonitoringEnabled",
"columnName": "is_ping_monitoring_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "1"
},
{
"fieldPath": "tunnelPingIntervalSeconds",
"columnName": "tunnel_ping_interval_sec",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "30"
},
{
"fieldPath": "tunnelPingAttempts",
"columnName": "tunnel_ping_attempts",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "3"
},
{
"fieldPath": "tunnelPingTimeoutSeconds",
"columnName": "tunnel_ping_timeout_sec",
"affinity": "INTEGER"
},
{
"fieldPath": "appMode",
"columnName": "app_mode",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "dnsProtocol",
"columnName": "dns_protocol",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "dnsEndpoint",
"columnName": "dns_endpoint",
"affinity": "TEXT"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
},
{
"tableName": "TunnelConfig",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `wg_quick` TEXT NOT NULL, `tunnel_networks` TEXT NOT NULL DEFAULT '', `is_mobile_data_tunnel` INTEGER NOT NULL DEFAULT false, `is_primary_tunnel` INTEGER NOT NULL DEFAULT false, `am_quick` TEXT NOT NULL DEFAULT '', `is_Active` INTEGER NOT NULL DEFAULT false, `restart_on_ping_failure` INTEGER NOT NULL DEFAULT false, `ping_target` TEXT DEFAULT null, `is_ethernet_tunnel` INTEGER NOT NULL DEFAULT false, `is_ipv4_preferred` INTEGER NOT NULL DEFAULT true, `position` INTEGER NOT NULL DEFAULT 0, `auto_tunnel_apps` TEXT NOT NULL DEFAULT '[]')",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "wgQuick",
"columnName": "wg_quick",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "tunnelNetworks",
"columnName": "tunnel_networks",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isMobileDataTunnel",
"columnName": "is_mobile_data_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isPrimaryTunnel",
"columnName": "is_primary_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "amQuick",
"columnName": "am_quick",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "''"
},
{
"fieldPath": "isActive",
"columnName": "is_Active",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "restartOnPingFailure",
"columnName": "restart_on_ping_failure",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "pingTarget",
"columnName": "ping_target",
"affinity": "TEXT",
"defaultValue": "null"
},
{
"fieldPath": "isEthernetTunnel",
"columnName": "is_ethernet_tunnel",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "false"
},
{
"fieldPath": "isIpv4Preferred",
"columnName": "is_ipv4_preferred",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "true"
},
{
"fieldPath": "position",
"columnName": "position",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "autoTunnelApps",
"columnName": "auto_tunnel_apps",
"affinity": "TEXT",
"notNull": true,
"defaultValue": "'[]'"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
},
"indices": [
{
"name": "index_TunnelConfig_name",
"unique": true,
"columnNames": [
"name"
],
"orders": [],
"createSql": "CREATE UNIQUE INDEX IF NOT EXISTS `index_TunnelConfig_name` ON `${TABLE_NAME}` (`name`)"
}
]
},
{
"tableName": "proxy_settings",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `socks5_proxy_enabled` INTEGER NOT NULL DEFAULT 0, `socks5_proxy_bind_address` TEXT, `http_proxy_enable` INTEGER NOT NULL DEFAULT 0, `http_proxy_bind_address` TEXT, `proxy_username` TEXT, `proxy_password` TEXT)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "socks5ProxyEnabled",
"columnName": "socks5_proxy_enabled",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "socks5ProxyBindAddress",
"columnName": "socks5_proxy_bind_address",
"affinity": "TEXT"
},
{
"fieldPath": "httpProxyEnabled",
"columnName": "http_proxy_enable",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
},
{
"fieldPath": "httpProxyBindAddress",
"columnName": "http_proxy_bind_address",
"affinity": "TEXT"
},
{
"fieldPath": "proxyUsername",
"columnName": "proxy_username",
"affinity": "TEXT"
},
{
"fieldPath": "proxyPassword",
"columnName": "proxy_password",
"affinity": "TEXT"
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"id"
]
}
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'db93d0490401ccbef25ca39f27bafa29')"
]
}
}
+32 -7
View File
@@ -5,10 +5,11 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<!--foreground service exempt android 14--> <!--foreground service special use for non VPN service tunnels, android 14-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
<!--foreground service special use for VPN service tunnels, android 14-->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SYSTEM_EXEMPTED" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
tools:ignore="ProtectedPermissions" />
<!--foreground service permissions--> <!--foreground service permissions-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
@@ -151,21 +152,45 @@
android:name=".core.service.autotunnel.AutoTunnelService" android:name=".core.service.autotunnel.AutoTunnelService"
android:enabled="true" android:enabled="true"
android:exported="false" android:exported="false"
android:foregroundServiceType="systemExempted" android:foregroundServiceType="specialUse"
android:persistent="true" android:persistent="true"
android:stopWithTask="false" android:stopWithTask="false"
tools:node="merge" /> tools:node="merge">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="This service monitors network changes to automatically
establish and maintain WireGuard VPN tunnels on demand, ensuring seamless connectivity.
It requires persistent foreground execution to detect real-time events,
which cannot be achieved with standard background APIs due to timing and reliability needs for
network connectivity monitoring."/>
</service>
<service
android:name=".core.service.TunnelForegroundService"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="specialUse"
android:persistent="true"
android:stopWithTask="false"
tools:node="merge">
<property android:name="android.app.PROPERTY_SPECIAL_USE_FGS_SUBTYPE"
android:value="This service sustains non-VpnService virtual tunnels (using gVisor/netstack for
isolated networking), keeping connections alive for continuous secure data routing.
Persistent foreground operation is essential to handle
low-level tunnel maintenance and avoid interruptions, beyond the capabilities of other
service types or background work."/>
</service>
<service <service
android:name=".core.service.TunnelForegroundService" android:name=".core.service.VpnForegroundService"
android:exported="false" android:exported="false"
android:persistent="true" android:persistent="true"
android:foregroundServiceType="systemExempted" android:foregroundServiceType="systemExempted"
android:permission="android.permission.BIND_VPN_SERVICE"> android:permission="android.permission.BIND_VPN_SERVICE">
<intent-filter> <intent-filter>
<action android:name="android.net.VpnService" /> <action android:name="android.net.VpnService" />
</intent-filter> </intent-filter>
</service> </service>
<receiver <receiver
android:name=".core.broadcast.RestartReceiver" android:name=".core.broadcast.RestartReceiver"
android:enabled="true" android:enabled="true"
@@ -1,5 +1,6 @@
package com.zaneschepke.wireguardautotunnel package com.zaneschepke.wireguardautotunnel
import ProxySettingsScreen
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
@@ -15,9 +16,6 @@ import androidx.activity.result.ActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
@@ -25,6 +23,7 @@ import androidx.compose.material3.*
import androidx.compose.runtime.* import androidx.compose.runtime.*
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.zIndex import androidx.compose.ui.zIndex
@@ -34,35 +33,30 @@ import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.rememberNavController import androidx.navigation.compose.rememberNavController
import androidx.navigation.navigation
import androidx.navigation.toRoute import androidx.navigation.toRoute
import com.zaneschepke.networkmonitor.NetworkMonitor import com.zaneschepke.networkmonitor.NetworkMonitor
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.data.AppDatabase import com.zaneschepke.wireguardautotunnel.data.AppDatabase
import com.zaneschepke.wireguardautotunnel.data.model.AppMode import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.domain.sideeffect.GlobalSideEffect
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
import com.zaneschepke.wireguardautotunnel.ui.common.banner.AppAlertBanner import com.zaneschepke.wireguardautotunnel.ui.common.banner.AppAlertBanner
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.VpnDeniedDialog import com.zaneschepke.wireguardautotunnel.ui.common.dialog.VpnDeniedDialog
import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar import com.zaneschepke.wireguardautotunnel.ui.common.snackbar.CustomSnackBar
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalIsAndroidTV import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.BottomNavbar import com.zaneschepke.wireguardautotunnel.ui.navigation.components.BottomNavbar
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.DynamicTopAppBar import com.zaneschepke.wireguardautotunnel.ui.navigation.components.DynamicTopAppBar
import com.zaneschepke.wireguardautotunnel.ui.navigation.components.currentNavBackStackEntryAsNavBarState import com.zaneschepke.wireguardautotunnel.ui.navigation.components.currentBackStackEntryAsNavbarState
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.AutoTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AutoTunnelAdvancedScreen import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.advanced.AutoTunnelAdvancedScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.detection.WifiDetectionMethodScreen import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.detection.WifiDetectionMethodScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.LocationDisclosureScreen import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.LocationDisclosureScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.main.MainScreen
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.sort.SortScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.main.splittunnel.SplitTunnelScreen
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.appearance.AppearanceScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.AppearanceScreen
@@ -71,47 +65,41 @@ import com.zaneschepke.wireguardautotunnel.ui.screens.settings.appearance.langua
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.dns.DnsSettingsScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.dns.DnsSettingsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.LogsScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.logs.LogsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.monitoring.TunnelMonitoringScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.monitoring.TunnelMonitoringScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.proxy.ProxySettingsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.system.SystemFeaturesScreen import com.zaneschepke.wireguardautotunnel.ui.screens.settings.system.SystemFeaturesScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen import com.zaneschepke.wireguardautotunnel.ui.screens.support.SupportScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.DonateScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.donate.crypto.AddressesScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.support.license.LicenseScreen import com.zaneschepke.wireguardautotunnel.ui.screens.support.license.LicenseScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.TunnelsScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.autotunnel.TunnelAutoTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.config.ConfigScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.sort.SortScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.splittunnel.SplitTunnelScreen
import com.zaneschepke.wireguardautotunnel.ui.screens.tunnels.tunneloptions.TunnelOptionsScreen
import com.zaneschepke.wireguardautotunnel.ui.theme.AlertRed import com.zaneschepke.wireguardautotunnel.ui.theme.AlertRed
import com.zaneschepke.wireguardautotunnel.ui.theme.OffWhite import com.zaneschepke.wireguardautotunnel.ui.theme.OffWhite
import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme import com.zaneschepke.wireguardautotunnel.ui.theme.WireguardAutoTunnelTheme
import com.zaneschepke.wireguardautotunnel.util.extensions.isRunningOnTv import com.zaneschepke.wireguardautotunnel.util.LocaleUtil
import com.zaneschepke.wireguardautotunnel.util.extensions.restartApp import com.zaneschepke.wireguardautotunnel.util.extensions.*
import com.zaneschepke.wireguardautotunnel.util.extensions.showToast import com.zaneschepke.wireguardautotunnel.viewmodel.*
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import de.raphaelebner.roomdatabasebackup.core.RoomBackup import de.raphaelebner.roomdatabasebackup.core.RoomBackup
import java.util.Locale import java.util.*
import javax.inject.Inject import javax.inject.Inject
import kotlin.system.exitProcess
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.teamgravity.pin_lock_compose.PinManager
@AndroidEntryPoint @AndroidEntryPoint
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
@Inject lateinit var appStateRepository: AppStateRepository @Inject lateinit var appStateRepository: AppStateRepository
@Inject lateinit var tunnelManager: TunnelManager @Inject lateinit var tunnelManager: TunnelManager
@Inject lateinit var networkMonitor: NetworkMonitor @Inject lateinit var networkMonitor: NetworkMonitor
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@Inject @MainDispatcher lateinit var mainDispatcher: CoroutineDispatcher
@Inject lateinit var appDatabase: AppDatabase @Inject lateinit var appDatabase: AppDatabase
private var lastLocationPermissionState: Boolean? = null
private lateinit var roomBackup: RoomBackup private lateinit var roomBackup: RoomBackup
val REQUEST_CODE = 123
@SuppressLint("BatteryLife") @SuppressLint("BatteryLife")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge( enableEdgeToEdge(
@@ -122,32 +110,44 @@ class MainActivity : AppCompatActivity() {
window.isNavigationBarContrastEnforced = false window.isNavigationBarContrastEnforced = false
} }
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
roomBackup = RoomBackup(this) roomBackup = RoomBackup(this)
val viewModel by viewModels<AppViewModel>() val viewModel by viewModels<SharedAppViewModel>()
installSplashScreen().apply { installSplashScreen().apply {
setKeepOnScreenCondition { !viewModel.appViewState.value.isAppReady } setKeepOnScreenCondition { !viewModel.container.stateFlow.value.isAppLoaded }
} }
setContent { setContent {
val context = LocalContext.current
val isTv = isRunningOnTv() val isTv = isRunningOnTv()
val appUiState by viewModel.uiState.collectAsStateWithLifecycle() val appState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
val appViewState by viewModel.appViewState.collectAsStateWithLifecycle()
val navController = rememberNavController() val navController = rememberNavController()
val backStackEntry by navController.currentBackStackEntryAsState() val scope = rememberCoroutineScope()
val navBarState by
currentNavBackStackEntryAsNavBarState( var pinManagerInitialized by remember { mutableStateOf(false) }
navController,
backStackEntry, LaunchedEffect(appState.isAppLoaded) {
viewModel, if (appState.isAppLoaded) {
appUiState, if (appState.pinLockEnabled && !pinManagerInitialized) {
appViewState, PinManager.initialize(this@MainActivity)
) pinManagerInitialized = true
}
appState.locale.let { LocaleUtil.changeLocale(it) }
}
}
val navState by
navController.currentBackStackEntryAsNavbarState(viewModel, navController)
val snackbar = remember { SnackbarHostState() } val snackbar = remember { SnackbarHostState() }
var showVpnPermissionDialog by remember { mutableStateOf(false) } var showVpnPermissionDialog by remember { mutableStateOf(false) }
var vpnPermissionDenied by remember { mutableStateOf(false) } var vpnPermissionDenied by remember { mutableStateOf(false) }
var requestingAppMode by remember {
mutableStateOf<Pair<AppMode?, TunnelConf?>>(Pair(null, null))
}
LaunchedEffect(navState) { Timber.d("New navbar state $navState") }
val vpnActivity = val vpnActivity =
rememberLauncherForActivityResult( rememberLauncherForActivityResult(
@@ -159,214 +159,239 @@ class MainActivity : AppCompatActivity() {
} else { } else {
vpnPermissionDenied = false vpnPermissionDenied = false
showVpnPermissionDialog = false showVpnPermissionDialog = false
val (appMode, config) = requestingAppMode
when (appMode) {
AppMode.VPN -> if (config != null) viewModel.startTunnel(config)
AppMode.LOCK_DOWN -> viewModel.setAppMode(AppMode.LOCK_DOWN)
else -> Unit
}
} }
requestingAppMode = Pair(null, null)
}, },
) )
LaunchedEffect(appUiState.tunnels) {
if (!appViewState.isAppReady) {
viewModel.handleEvent(AppEvent.AppReadyCheck(appUiState.tunnels))
}
}
val batteryActivity = val batteryActivity =
rememberLauncherForActivityResult( rememberLauncherForActivityResult(
ActivityResultContracts.StartActivityForResult() ActivityResultContracts.StartActivityForResult()
) { _: ActivityResult -> ) { _: ActivityResult ->
viewModel.handleEvent(AppEvent.SetBatteryOptimizeDisableShown) viewModel.disableBatteryOptimizationsShown()
} }
with(appViewState) { fun requestDisableBatteryOptimizations() {
LaunchedEffect(isConfigChanged) { batteryActivity.launch(
if (isConfigChanged) { Intent().apply {
Intent(this@MainActivity, MainActivity::class.java).also { action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
startActivity(it) data = "package:${this@MainActivity.packageName}".toUri()
exitProcess(0)
}
} }
} )
LaunchedEffect(errorMessage) { }
errorMessage?.let {
snackbar.showSnackbar(it.asString(this@MainActivity)) LaunchedEffect(Unit) {
viewModel.handleEvent(AppEvent.MessageShown) viewModel.globalSideEffect.collect { sideEffect ->
} when (sideEffect) {
} GlobalSideEffect.ConfigChanged -> restartApp()
LaunchedEffect(popBackStack) { GlobalSideEffect.PopBackStack -> navController.popBackStack()
if (popBackStack) { GlobalSideEffect.RequestBatteryOptimizationDisabled ->
navController.popBackStack() requestDisableBatteryOptimizations()
viewModel.handleEvent(AppEvent.PopBackStack(false)) is GlobalSideEffect.RequestVpnPermission -> {
} requestingAppMode = Pair(sideEffect.requestingMode, sideEffect.config)
}
LaunchedEffect(requestVpnPermission) {
if (requestVpnPermission) {
if (!vpnPermissionDenied) {
vpnActivity.launch(VpnService.prepare(this@MainActivity)) vpnActivity.launch(VpnService.prepare(this@MainActivity))
} else {
showVpnPermissionDialog = true
} }
viewModel.handleEvent(AppEvent.VpnPermissionRequested) is GlobalSideEffect.ShareFile -> context.launchShareFile(sideEffect.file)
} is GlobalSideEffect.Snackbar ->
} scope.launch {
LaunchedEffect(requestBatteryPermission) { snackbar.showSnackbar(sideEffect.message.asString(context))
if (requestBatteryPermission) {
batteryActivity.launch(
Intent().apply {
action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
data = "package:${this@MainActivity.packageName}".toUri()
} }
) is GlobalSideEffect.Toast ->
scope.launch { context.showToast(sideEffect.message.asString(context)) }
is GlobalSideEffect.LaunchUrl -> context.openWebUrl(sideEffect.url)
is GlobalSideEffect.InstallApk -> context.installApk(sideEffect.apk)
} }
} }
} }
CompositionLocalProvider(LocalIsAndroidTV provides isTv) { if (!appState.isAppLoaded) return@setContent
CompositionLocalProvider(LocalNavController provides navController) {
WireguardAutoTunnelTheme(theme = appUiState.appState.theme) { CompositionLocalProvider(
VpnDeniedDialog( LocalIsAndroidTV provides isTv,
showVpnPermissionDialog, LocalSharedVm provides viewModel,
onDismiss = { LocalNavController provides navController,
showVpnPermissionDialog = false ) {
vpnPermissionDenied = false WireguardAutoTunnelTheme(theme = appState.theme) {
VpnDeniedDialog(
showVpnPermissionDialog,
onDismiss = {
showVpnPermissionDialog = false
vpnPermissionDenied = false
},
)
Box(modifier = Modifier.fillMaxSize()) {
if (appState.settings.appMode == AppMode.LOCK_DOWN) {
AppAlertBanner(
stringResource(R.string.locked_down).uppercase(Locale.getDefault()),
OffWhite,
AlertRed,
modifier = Modifier.fillMaxWidth().zIndex(2f),
)
}
Scaffold(
snackbarHost = {
SnackbarHost(snackbar) { snackbarData ->
CustomSnackBar(
snackbarData.visuals.message,
isRtl = false,
containerColor =
MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp),
)
}
}, },
) topBar = { DynamicTopAppBar(navState) },
bottomBar = {
Box(modifier = Modifier.fillMaxSize()) { BottomNavbar(appState.isAutoTunnelActive, navState, navController)
// Top banner if in locked down mode },
if (appUiState.appSettings.appMode == AppMode.LOCK_DOWN) { modifier =
AppAlertBanner( Modifier.pointerInput(Unit) {
stringResource(R.string.locked_down) detectTapGestures { viewModel.clearSelectedTunnels() }
.uppercase(Locale.getDefault()), },
OffWhite, ) { padding ->
AlertRed, Box(
modifier =
Modifier.fillMaxWidth().zIndex(2f), // Draw above everything
)
}
Scaffold(
modifier = modifier =
Modifier.pointerInput(Unit) { Modifier.fillMaxSize()
detectTapGestures { .background(MaterialTheme.colorScheme.surface)
viewModel.handleEvent(AppEvent.ClearSelectedTunnels) .padding(padding)
} .consumeWindowInsets(padding)
}, .imePadding()
snackbarHost = { ) {
SnackbarHost(snackbar) { snackbarData: SnackbarData -> NavHost(
CustomSnackBar( navController = navController,
snackbarData.visuals.message, startDestination =
isRtl = false, if (appState.pinLockEnabled && !appState.isAuthorized)
containerColor = Route.Lock
MaterialTheme.colorScheme.surfaceColorAtElevation( else Route.TunnelsGraph,
2.dp
),
)
}
},
topBar = { DynamicTopAppBar(navBarState) },
bottomBar = {
AnimatedVisibility(
visible = navBarState.showBottom,
enter = slideInVertically(initialOffsetY = { it }),
exit = slideOutVertically(targetOffsetY = { it }),
) {
BottomNavbar(appUiState = appUiState)
}
},
) { padding ->
Box(
modifier =
Modifier.fillMaxSize()
.background(MaterialTheme.colorScheme.surface)
.padding(padding)
.consumeWindowInsets(padding)
.imePadding()
) { ) {
NavHost( composable<Route.Lock> { PinLockScreen() }
navController, navigation<Route.TunnelsGraph>(
startDestination = startDestination = Route.Tunnels
(if (appUiState.appState.isPinLockEnabled) Route.Lock
else Route.Main),
) { ) {
composable<Route.Main> { composable<Route.Tunnels> {
MainScreen(appUiState, appViewState, viewModel) val viewModel =
it.sharedViewModel<TunnelsViewModel>(navController)
TunnelsScreen(viewModel)
} }
composable<Route.Settings> { composable<Route.Sort> {
SettingsScreen(appUiState, appViewState, viewModel) val viewModel =
it.sharedViewModel<TunnelsViewModel>(navController)
SortScreen(viewModel)
} }
composable<Route.TunnelOptions> { backStackEntry ->
val args = backStackEntry.toRoute<Route.TunnelOptions>()
val viewModel =
backStackEntry.sharedViewModel<TunnelsViewModel>(
navController
)
TunnelOptionsScreen(args.id, viewModel)
}
composable<Route.SplitTunnel> { backStackEntry ->
val args = backStackEntry.toRoute<Route.SplitTunnel>()
SplitTunnelScreen(args.id)
}
composable<Route.TunnelAutoTunnel> { backStackEntry ->
val args =
backStackEntry.toRoute<Route.TunnelAutoTunnel>()
val viewModel =
backStackEntry.sharedViewModel<TunnelsViewModel>(
navController
)
TunnelAutoTunnelScreen(args.id, viewModel)
}
composable<Route.Config> { backStackEntry ->
val args = backStackEntry.toRoute<Route.Config>()
val viewModel =
backStackEntry.sharedViewModel<TunnelsViewModel>(
navController
)
ConfigScreen(args.id, viewModel)
}
}
navigation<Route.AutoTunnelGraph>(
startDestination =
if (appState.isLocationDisclosureShown) Route.AutoTunnel
else Route.LocationDisclosure
) {
composable<Route.LocationDisclosure> { composable<Route.LocationDisclosure> {
val viewModel =
it.sharedViewModel<AutoTunnelViewModel>(
navController
)
LocationDisclosureScreen(viewModel) LocationDisclosureScreen(viewModel)
} }
composable<Route.AutoTunnel> { composable<Route.AutoTunnel> {
AutoTunnelScreen(appUiState, viewModel) val viewModel =
it.sharedViewModel<AutoTunnelViewModel>(
navController
)
AutoTunnelScreen(viewModel)
} }
composable<Route.Appearance> { AppearanceScreen() } composable<Route.AdvancedAutoTunnel> {
composable<Route.Language> { val viewModel =
LanguageScreen(appUiState, viewModel) it.sharedViewModel<AutoTunnelViewModel>(
} navController
composable<Route.Display> { )
DisplayScreen(appUiState, viewModel) AutoTunnelAdvancedScreen(viewModel)
}
composable<Route.Support> {
SupportScreen(appViewModel = viewModel)
}
composable<Route.License> { LicenseScreen() }
composable<Route.AutoTunnelAdvanced> {
AutoTunnelAdvancedScreen(appUiState, viewModel)
} }
composable<Route.WifiDetectionMethod> { composable<Route.WifiDetectionMethod> {
WifiDetectionMethodScreen(appUiState, viewModel) val viewModel =
it.sharedViewModel<AutoTunnelViewModel>(
navController
)
WifiDetectionMethodScreen(viewModel)
} }
composable<Route.Logs> { }
LogsScreen(appViewState, viewModel)
navigation<Route.SettingsGraph>(
startDestination = Route.Settings
) {
composable<Route.Settings> {
val viewModel =
it.sharedViewModel<SettingsViewModel>(navController)
SettingsScreen(viewModel)
} }
composable<Route.Config> { backStack ->
val args = backStack.toRoute<Route.Config>()
val config =
appUiState.tunnels.firstOrNull { it.id == args.id }
ConfigScreen(config, appUiState, viewModel)
}
composable<Route.TunnelOptions> { backStack ->
val args = backStack.toRoute<Route.TunnelOptions>()
appUiState.tunnels
.firstOrNull { it.id == args.id }
?.let { config ->
TunnelOptionsScreen(
config,
viewModel,
appViewState,
appUiState.appSettings,
)
}
}
composable<Route.Lock> { PinLockScreen(viewModel) }
composable<Route.SplitTunnel> {
SplitTunnelScreen(viewModel)
}
composable<Route.TunnelAutoTunnel> { backStack ->
val args = backStack.toRoute<Route.TunnelOptions>()
appUiState.tunnels
.firstOrNull { it.id == args.id }
?.let {
TunnelAutoTunnelScreen(
it,
appUiState.appSettings,
viewModel,
)
}
}
composable<Route.Sort> { SortScreen(appUiState, viewModel) }
composable<Route.TunnelMonitoring> { composable<Route.TunnelMonitoring> {
TunnelMonitoringScreen(appUiState, viewModel) val viewModel =
} it.sharedViewModel<SettingsViewModel>(navController)
composable<Route.ProxySettings> { TunnelMonitoringScreen(viewModel)
ProxySettingsScreen(appUiState, viewModel)
} }
composable<Route.SystemFeatures> { composable<Route.SystemFeatures> {
SystemFeaturesScreen(appUiState, viewModel) val viewModel =
it.sharedViewModel<SettingsViewModel>(navController)
SystemFeaturesScreen(viewModel)
} }
composable<Route.Dns> { composable<Route.Dns> {
DnsSettingsScreen(appUiState, viewModel) val viewModel =
it.sharedViewModel<SettingsViewModel>(navController)
DnsSettingsScreen(viewModel)
} }
composable<Route.ProxySettings> { ProxySettingsScreen() }
composable<Route.Appearance> { AppearanceScreen() }
composable<Route.Language> { LanguageScreen() }
composable<Route.Display> { DisplayScreen() }
composable<Route.Logs> { LogsScreen() }
}
navigation<Route.SupportGraph>(
startDestination = Route.Support
) {
composable<Route.Support> {
val viewModel =
it.sharedViewModel<SupportViewModel>(navController)
SupportScreen(viewModel)
}
composable<Route.License> { LicenseScreen() }
composable<Route.Donate> { DonateScreen(navController) }
composable<Route.Addresses> { AddressesScreen() }
} }
} }
} }
@@ -389,15 +414,15 @@ class MainActivity : AppCompatActivity() {
} }
fun performBackup() = fun performBackup() =
lifecycleScope.launch(ioDispatcher) { lifecycleScope.launch {
roomBackup roomBackup
.database(appDatabase) .database(appDatabase)
.backupLocation(RoomBackup.BACKUP_FILE_LOCATION_CUSTOM_DIALOG) .backupLocation(RoomBackup.BACKUP_FILE_LOCATION_CUSTOM_DIALOG)
.enableLogDebug(true) .enableLogDebug(true)
.maxFileCount(5) .maxFileCount(5)
.apply { .apply {
onCompleteListener { success, message, exitCode -> onCompleteListener { success, _, _ ->
lifecycleScope.launch(mainDispatcher) { lifecycleScope.launch {
if (success) { if (success) {
showToast( showToast(
getString( getString(
@@ -406,7 +431,9 @@ class MainActivity : AppCompatActivity() {
) )
) )
restartApp() restartApp()
} else showToast(R.string.backup_failed) } else {
showToast(R.string.backup_failed)
}
} }
} }
} }
@@ -420,8 +447,8 @@ class MainActivity : AppCompatActivity() {
.enableLogDebug(true) .enableLogDebug(true)
.backupLocation(RoomBackup.BACKUP_FILE_LOCATION_CUSTOM_DIALOG) .backupLocation(RoomBackup.BACKUP_FILE_LOCATION_CUSTOM_DIALOG)
.apply { .apply {
onCompleteListener { success, message, exitCode -> onCompleteListener { success, _, _ ->
lifecycleScope.launch(mainDispatcher) { lifecycleScope.launch {
if (success) { if (success) {
showToast( showToast(
getString( getString(
@@ -430,7 +457,9 @@ class MainActivity : AppCompatActivity() {
) )
) )
restartApp() restartApp()
} else showToast(R.string.restore_failed) } else {
showToast(R.string.restore_failed)
}
} }
} }
} }
@@ -5,7 +5,6 @@ import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy import android.os.StrictMode.ThreadPolicy
import androidx.hilt.work.HiltWorkerFactory import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration import androidx.work.Configuration
import com.wireguard.android.backend.GoBackend
import com.zaneschepke.logcatter.LogReader import com.zaneschepke.logcatter.LogReader
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationMonitor import com.zaneschepke.wireguardautotunnel.core.notification.NotificationMonitor
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
@@ -15,14 +14,17 @@ import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.di.MainDispatcher import com.zaneschepke.wireguardautotunnel.di.MainDispatcher
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
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 javax.inject.Inject
import kotlinx.coroutines.* import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import org.amnezia.awg.backend.GoBackend
import timber.log.Timber import timber.log.Timber
@HiltAndroidApp @HiltAndroidApp
@@ -64,6 +66,11 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
Timber.plant(ReleaseTree()) Timber.plant(ReleaseTree())
} }
applicationScope.launch(ioDispatcher) {
launch { if (appDataRepository.appState.isLocalLogsEnabled()) logReader.start() }
launch { notificationMonitor.handleApplicationNotifications() }
}
GoBackend.setAlwaysOnCallback { GoBackend.setAlwaysOnCallback {
applicationScope.launch { applicationScope.launch {
val settings = appDataRepository.settings.get() val settings = appDataRepository.settings.get()
@@ -77,16 +84,6 @@ class WireGuardAutoTunnel : Application(), Configuration.Provider {
} }
ServiceWorker.start(this) ServiceWorker.start(this)
applicationScope.launch {
launch { notificationMonitor.handleApplicationNotifications() }
appDataRepository.appState.getLocale()?.let {
withContext(mainDispatcher) { LocaleUtil.changeLocale(it) }
}
appDataRepository.appState.isLocalLogsEnabled().let { enabled ->
if (enabled) logReader.start()
}
}
} }
override fun onTerminate() { override fun onTerminate() {
@@ -31,9 +31,9 @@ class NotificationActionReceiver : BroadcastReceiver() {
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)
val tunnel = tunnelRepository.getById(tunnelId) return@launch tunnelManager.stopActiveTunnels()
tunnelManager.stopTunnel(tunnel) tunnelRepository.getById(tunnelId)?.let { tunnelManager.stopTunnel(it.id) }
} }
} }
} }
@@ -71,11 +71,11 @@ class RemoteControlReceiver : BroadcastReceiver() {
Action.STOP_TUNNEL -> { Action.STOP_TUNNEL -> {
val tunnelName = val tunnelName =
intent.getStringExtra(EXTRA_TUN_NAME) intent.getStringExtra(EXTRA_TUN_NAME)
?: return@launch tunnelManager.stopTunnel() ?: return@launch tunnelManager.stopActiveTunnels()
val tunnel = val tunnel =
appDataRepository.tunnels.findByTunnelName(tunnelName) appDataRepository.tunnels.findByTunnelName(tunnelName)
?: return@launch tunnelManager.stopTunnel() ?: return@launch tunnelManager.stopActiveTunnels()
tunnelManager.stopTunnel(tunnel) tunnelManager.stopTunnel(tunnel.id)
} }
Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel() Action.START_AUTO_TUNNEL -> serviceManager.startAutoTunnel()
Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel() Action.STOP_AUTO_TUNNEL -> serviceManager.stopAutoTunnel()
@@ -16,9 +16,9 @@ interface NotificationManager {
title: String = "", title: String = "",
actions: Collection<NotificationCompat.Action> = emptyList(), actions: Collection<NotificationCompat.Action> = emptyList(),
description: String = "", description: String = "",
showTimestamp: Boolean = false, showTimestamp: Boolean = true,
importance: Int = NotificationManager.IMPORTANCE_HIGH, importance: Int = NotificationManager.IMPORTANCE_HIGH,
onGoing: Boolean = true, onGoing: Boolean = false,
onlyAlertOnce: Boolean = true, onlyAlertOnce: Boolean = true,
): Notification ): Notification
@@ -27,9 +27,9 @@ interface NotificationManager {
title: StringValue, title: StringValue,
actions: Collection<NotificationCompat.Action> = emptyList(), actions: Collection<NotificationCompat.Action> = emptyList(),
description: StringValue, description: StringValue,
showTimestamp: Boolean = false, showTimestamp: Boolean = true,
importance: Int = NotificationManager.IMPORTANCE_HIGH, importance: Int = NotificationManager.IMPORTANCE_HIGH,
onGoing: Boolean = true, onGoing: Boolean = false,
onlyAlertOnce: Boolean = true, onlyAlertOnce: Boolean = true,
): Notification ): Notification
@@ -3,7 +3,7 @@ package com.zaneschepke.wireguardautotunnel.core.notification
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.StringValue
import jakarta.inject.Inject import jakarta.inject.Inject
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.coroutineScope
@@ -22,15 +22,15 @@ constructor(
} }
private suspend fun handleTunnelErrors() = private suspend fun handleTunnelErrors() =
tunnelManager.errorEvents.collectLatest { (tunnelConf, error) -> tunnelManager.errorEvents.collectLatest { (tunName, error) ->
if (!WireGuardAutoTunnel.uiActive.value) { if (!WireGuardAutoTunnel.uiActive.value) {
val notification = val notification =
notificationManager.createNotification( notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN, WireGuardNotification.NotificationChannels.VPN,
title = StringValue.DynamicString(tunnelConf.name), title = StringValue.DynamicString(tunName),
description = description =
when (error) { when (error) {
is BackendError.BounceFailed -> error.toStringValue() is BackendCoreException.BounceFailed -> error.toStringValue()
else -> else ->
StringValue.StringResource( StringValue.StringResource(
R.string.tunnel_error_template, R.string.tunnel_error_template,
@@ -46,12 +46,12 @@ constructor(
} }
private suspend fun handleTunnelMessages() = private suspend fun handleTunnelMessages() =
tunnelManager.messageEvents.collectLatest { (tunnelConf, message) -> tunnelManager.messageEvents.collectLatest { (tunName, message) ->
if (!WireGuardAutoTunnel.uiActive.value) { if (!WireGuardAutoTunnel.uiActive.value) {
val notification = val notification =
notificationManager.createNotification( notificationManager.createNotification(
WireGuardNotification.NotificationChannels.VPN, WireGuardNotification.NotificationChannels.VPN,
title = StringValue.DynamicString(tunnelConf.name), title = StringValue.DynamicString(tunName),
description = message.toStringValue(), description = message.toStringValue(),
) )
notificationManager.show( notificationManager.show(
@@ -0,0 +1,153 @@
package com.zaneschepke.wireguardautotunnel.core.service
import android.app.Notification
import android.content.Intent
import android.os.IBinder
import androidx.core.app.ServiceCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.util.extensions.distinctByKeys
import dagger.hilt.android.AndroidEntryPoint
import io.ktor.util.collections.*
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import timber.log.Timber
@AndroidEntryPoint
abstract class BaseTunnelForegroundService : LifecycleService(), TunnelService {
@Inject lateinit var notificationManager: NotificationManager
@Inject lateinit var serviceManager: ServiceManager
@Inject lateinit var tunnelManager: TunnelManager
@Inject lateinit var tunnelMonitor: TunnelMonitor
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@Inject lateinit var appDataRepository: AppDataRepository
private val tunnelJobs = ConcurrentMap<Int, Job>()
protected abstract val fgsType: Int
override fun onBind(intent: Intent): IBinder {
super.onBind(intent)
return LocalBinder(this)
}
override fun onCreate() {
super.onCreate()
ServiceCompat.startForeground(
this,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
fgsType,
)
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
ServiceCompat.startForeground(
this,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
fgsType,
)
start()
return START_STICKY
}
override fun start() {
lifecycleScope.launch(ioDispatcher) {
tunnelManager.activeTunnels.distinctByKeys().collect { activeTunnels ->
val activeTunConfigs = activeTunnels.keys
val obsoleteJobs = tunnelJobs.keys - activeTunConfigs
obsoleteJobs.forEach { tunId -> tunnelJobs[tunId]?.cancel() }
activeTunConfigs.forEach { tunId ->
if (tunnelJobs.contains(tunId)) return@forEach
tunnelJobs[tunId] = launch { tunnelMonitor.startMonitoring(tunId, true) }
}
val tunnels = appDataRepository.tunnels.getAll()
val activeConfigs = tunnels.filter { activeTunConfigs.contains(it.id) }
updateServiceNotification(activeConfigs)
}
}
}
// TODO Would be cool to have this include kill switch
private fun updateServiceNotification(activeConfigs: List<TunnelConf>) {
val notification =
when (activeConfigs.size) {
0 -> onCreateNotification()
1 -> createTunnelNotification(activeConfigs.first())
else -> createTunnelsNotification()
}
ServiceCompat.startForeground(
this,
NotificationManager.VPN_NOTIFICATION_ID,
notification,
fgsType,
)
}
override fun stop() {
Timber.d("Stop called")
tunnelJobs.forEach { it.value.cancel() }
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()
}
override fun onDestroy() {
tunnelJobs.forEach { it.value.cancel() }
serviceManager.handleTunnelServiceDestroy()
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
Timber.d("onDestroy")
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,
)
),
onGoing = true,
)
}
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),
)
}
}
@@ -0,0 +1,5 @@
package com.zaneschepke.wireguardautotunnel.core.service
import android.os.Binder
class LocalBinder(val service: TunnelService) : Binder()
@@ -7,15 +7,16 @@ import android.content.ServiceConnection
import android.net.VpnService import android.net.VpnService
import android.os.IBinder import android.os.IBinder
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.AutoTunnelService
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate import com.zaneschepke.wireguardautotunnel.util.extensions.requestAutoTunnelTileServiceUpdate
import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate import com.zaneschepke.wireguardautotunnel.util.extensions.requestTunnelTileServiceStateUpdate
import jakarta.inject.Inject import jakarta.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.*
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@@ -26,29 +27,45 @@ class ServiceManager
@Inject @Inject
constructor( constructor(
private val context: Context, private val context: Context,
private val ioDispatcher: CoroutineDispatcher, @IoDispatcher ioDispatcher: CoroutineDispatcher,
private val applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
private val mainDispatcher: CoroutineDispatcher, private val mainDispatcher: CoroutineDispatcher,
private val appDataRepository: AppDataRepository, private val appDataRepository: AppDataRepository,
) { ) {
private val autoTunnelMutex = Mutex() private val autoTunnelMutex = Mutex()
private val tunnelMutex = Mutex()
private val _tunnelService = MutableStateFlow<TunnelForegroundService?>(null) private val _tunnelService = MutableStateFlow<TunnelService?>(null)
private val _autoTunnelService = MutableStateFlow<AutoTunnelService?>(null) private val _autoTunnelService = MutableStateFlow<AutoTunnelService?>(null)
val autoTunnelService = _autoTunnelService.asStateFlow() val autoTunnelService = _autoTunnelService.asStateFlow()
val tunnelService = _tunnelService.asStateFlow()
private val tunnelServiceConnection = private val tunnelServiceConnection =
object : ServiceConnection { object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) { override fun onServiceConnected(name: ComponentName, service: IBinder) {
val binder = service as? TunnelForegroundService.LocalBinder val binder = service as? LocalBinder
_tunnelService.value = binder?.service _tunnelService.value = binder?.service
Timber.d("TunnelForegroundService connected") val serviceClass =
when {
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
name.className.contains("TunnelForegroundService") ->
"TunnelForegroundService"
else -> "Unknown"
}
Timber.d("$serviceClass connected")
} }
override fun onServiceDisconnected(name: ComponentName) { override fun onServiceDisconnected(name: ComponentName) {
_tunnelService.value = null _tunnelService.value = null
Timber.d("TunnelForegroundService disconnected") val serviceClass =
when {
name.className.contains("VpnForegroundService") -> "VpnForegroundService"
name.className.contains("TunnelForegroundService") ->
"TunnelForegroundService"
else -> "Unknown"
}
Timber.d("$serviceClass disconnected")
} }
} }
@@ -66,72 +83,74 @@ constructor(
} }
} }
init {
// Observe changes to the AutoTunnelService and trigger side effects
applicationScope.launch(ioDispatcher) {
_autoTunnelService
.onEach { service ->
withContext(mainDispatcher) { updateAutoTunnelTile() }
if (service == null) {
// The service is disconnected, update the DB state
val settings = appDataRepository.settings.get()
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
}
}
.launchIn(this)
}
}
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()
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = true))
if (_autoTunnelService.value != null) return if (_autoTunnelService.value != null) return
withContext(ioDispatcher) { val intent = Intent(context, AutoTunnelService::class.java)
val intent = Intent(context, AutoTunnelService::class.java) context.startForegroundService(intent)
context.startForegroundService(intent) context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
context.bindService(intent, autoTunnelServiceConnection, Context.BIND_AUTO_CREATE)
withContext(mainDispatcher) { updateAutoTunnelTile() }
}
} }
} }
suspend fun stopAutoTunnel() { suspend fun stopAutoTunnel() =
autoTunnelMutex.withLock { autoTunnelMutex.withLock {
val settings = appDataRepository.settings.get() if (_autoTunnelService.value == null) return@withLock
appDataRepository.settings.save(settings.copy(isAutoTunnelEnabled = false))
if (_autoTunnelService.value == null) return
_autoTunnelService.value?.let { service -> _autoTunnelService.value?.let { service ->
service.stop() service.stop()
try { try {
context.unbindService(autoTunnelServiceConnection) context.unbindService(autoTunnelServiceConnection)
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Failed to unbind AutoTunnelService") Timber.e(e, "Failed to unbind AutoTunnelService")
} finally {
_tunnelService.value = null
} }
} }
withContext(mainDispatcher) { updateAutoTunnelTile() }
} }
}
suspend fun startTunnelForegroundService() { suspend fun startTunnelService(appMode: AppMode) =
if (_tunnelService.value != null) return tunnelMutex.withLock {
withContext(ioDispatcher) { if (_tunnelService.value != null) return@withLock
applicationScope.launch(ioDispatcher) { val serviceClass =
val intent = Intent(context, TunnelForegroundService::class.java) when (appMode) {
context.startForegroundService(intent) AppMode.VPN,
context.bindService(intent, tunnelServiceConnection, Context.BIND_AUTO_CREATE) AppMode.LOCK_DOWN -> VpnForegroundService::class.java
AppMode.KERNEL,
AppMode.PROXY -> TunnelForegroundService::class.java
}
val intent = Intent(context, serviceClass)
context.startForegroundService(intent)
context.bindService(intent, tunnelServiceConnection, Context.BIND_AUTO_CREATE)
}
suspend fun stopTunnelService() =
tunnelMutex.withLock {
_tunnelService.value?.let { service ->
service.stop()
try {
context.unbindService(tunnelServiceConnection)
} catch (e: Exception) {
Timber.e(e, "Failed to stop Tunnel Service")
}
} }
} }
}
fun stopTunnelForegroundService() {
_tunnelService.value?.let { service ->
service.stop()
try {
context.unbindService(tunnelServiceConnection)
} catch (e: Exception) {
Timber.e(e, "Failed to stop TunnelForegroundService")
} finally {
_tunnelService.value = null
}
}
}
fun toggleAutoTunnel() {
applicationScope.launch(ioDispatcher) {
if (_autoTunnelService.value != null) stopAutoTunnel() else startAutoTunnel()
}
}
fun updateAutoTunnelTile() { fun updateAutoTunnelTile() {
context.requestAutoTunnelTileServiceUpdate() context.requestAutoTunnelTileServiceUpdate()
@@ -1,154 +1,8 @@
package com.zaneschepke.wireguardautotunnel.core.service package com.zaneschepke.wireguardautotunnel.core.service
import android.app.Notification
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import androidx.core.app.ServiceCompat
import androidx.lifecycle.LifecycleService
import androidx.lifecycle.lifecycleScope
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.core.notification.NotificationManager
import com.zaneschepke.wireguardautotunnel.core.notification.WireGuardNotification
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
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 dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import io.ktor.util.collections.*
import javax.inject.Inject
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import timber.log.Timber
@AndroidEntryPoint @AndroidEntryPoint
class TunnelForegroundService : LifecycleService() { class TunnelForegroundService(override val fgsType: Int = Constants.SPECIAL_USE_SERVICE_TYPE_ID) :
BaseTunnelForegroundService()
@Inject lateinit var notificationManager: NotificationManager
@Inject lateinit var serviceManager: ServiceManager
@Inject lateinit var tunnelManager: TunnelManager
@Inject lateinit var tunnelMonitor: TunnelMonitor
@Inject @IoDispatcher lateinit var ioDispatcher: CoroutineDispatcher
@Inject lateinit var appDataRepository: AppDataRepository
class LocalBinder(val service: TunnelForegroundService) : Binder()
private val tunnelJobs = ConcurrentMap<TunnelConf, Job>()
private val binder = LocalBinder(this)
override fun onCreate() {
super.onCreate()
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
override fun onBind(intent: Intent): IBinder {
super.onBind(intent)
return binder
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId)
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
onCreateNotification(),
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
start()
return START_STICKY
}
fun start() =
lifecycleScope.launch(ioDispatcher) {
tunnelManager.activeTunnels.distinctByKeys().collect { activeTunnels ->
val activeTunConfigs = activeTunnels.keys
val obsoleteJobs = tunnelJobs.keys - activeTunConfigs
obsoleteJobs.forEach { tunnelConf -> tunnelJobs[tunnelConf]?.cancel() }
activeTunConfigs.forEach { tun ->
if (tunnelJobs.containsKey(tun)) return@forEach
tunnelJobs[tun] = launch { tunnelMonitor.startMonitoring(tun, true) }
}
updateServiceNotification(activeTunnels)
}
}
// TODO Would be cool to have this include kill switch
private fun updateServiceNotification(activeTunnels: Map<TunnelConf, TunnelState>) {
val notification =
when (activeTunnels.size) {
0 -> onCreateNotification()
1 -> createTunnelNotification(activeTunnels.keys.first())
else -> createTunnelsNotification()
}
ServiceCompat.startForeground(
this@TunnelForegroundService,
NotificationManager.VPN_NOTIFICATION_ID,
notification,
Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID,
)
}
fun stop() {
Timber.d("Stop called")
tunnelJobs.forEach { it.value.cancel() }
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
stopSelf()
}
override fun onDestroy() {
tunnelJobs.forEach { it.value.cancel() }
serviceManager.handleTunnelServiceDestroy()
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
Timber.d("onDestroy")
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),
)
}
}
@@ -0,0 +1,7 @@
package com.zaneschepke.wireguardautotunnel.core.service
interface TunnelService {
fun start()
fun stop()
}
@@ -0,0 +1,8 @@
package com.zaneschepke.wireguardautotunnel.core.service
import com.zaneschepke.wireguardautotunnel.util.Constants
import dagger.hilt.android.AndroidEntryPoint
@AndroidEntryPoint
class VpnForegroundService(override val fgsType: Int = Constants.SYSTEM_EXEMPT_SERVICE_TYPE_ID) :
BaseTunnelForegroundService()
@@ -17,20 +17,18 @@ import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelMonitor
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction import com.zaneschepke.wireguardautotunnel.domain.enums.NotificationAction
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus.StopReason.Ping
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.AutoTunnelState import com.zaneschepke.wireguardautotunnel.domain.state.AutoTunnelState
import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState 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 com.zaneschepke.wireguardautotunnel.util.extensions.to
import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider import javax.inject.Provider
import kotlin.math.pow
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@@ -60,12 +58,6 @@ class AutoTunnelService : LifecycleService() {
private val autoTunnelStateFlow = MutableStateFlow(defaultState) private val autoTunnelStateFlow = MutableStateFlow(defaultState)
private val bounceCounts = MutableStateFlow<Map<Int, Int>>(emptyMap())
private var eventHandlerJob: Job? = null
private val lastBounceTimes = mutableMapOf<Int, Long>()
class LocalBinder(val service: AutoTunnelService) : Binder() class LocalBinder(val service: AutoTunnelService) : Binder()
private val binder = LocalBinder(this) private val binder = LocalBinder(this)
@@ -117,6 +109,7 @@ class AutoTunnelService : LifecycleService() {
NotificationAction.AUTO_TUNNEL_OFF NotificationAction.AUTO_TUNNEL_OFF
) )
), ),
onGoing = true,
) )
ServiceCompat.startForeground( ServiceCompat.startForeground(
this, this,
@@ -141,20 +134,10 @@ class AutoTunnelService : LifecycleService() {
val tunnelsFlow = val tunnelsFlow =
tunnelManager.activeTunnels.map { StateChange.ActiveTunnelsChange(it) } tunnelManager.activeTunnels.map { StateChange.ActiveTunnelsChange(it) }
val monitoringFlow =
tunnelManager.activeTunnels
.map { map -> map.mapValues { (_, state) -> state.pingStates } }
.distinctUntilChanged()
.map { StateChange.MonitoringChange(it) }
var reevaluationJob: Job? = null var reevaluationJob: Job? = null
// get everything in sync before we use merge // get everything in sync before we use merge
combine(networkFlow, settingsFlow, tunnelsFlow, monitoringFlow) { combine(networkFlow, settingsFlow, tunnelsFlow) { network, settings, tunnels ->
network,
settings,
tunnels,
monitoring ->
autoTunnelStateFlow.update { autoTunnelStateFlow.update {
it.copy( it.copy(
activeTunnels = tunnels.activeTunnels, activeTunnels = tunnels.activeTunnels,
@@ -168,7 +151,7 @@ class AutoTunnelService : LifecycleService() {
// use merge to limit the noise of a combine and also increase the scalability of auto // use merge to limit the noise of a combine and also increase the scalability of auto
// tunnel handling new states // tunnel handling new states
merge(networkFlow, settingsFlow, tunnelsFlow, monitoringFlow).collect { change -> merge(networkFlow, settingsFlow, tunnelsFlow).collect { change ->
if (change !is StateChange.ActiveTunnelsChange) { if (change !is StateChange.ActiveTunnelsChange) {
Timber.d("New state changed to ${change.javaClass.simpleName}") Timber.d("New state changed to ${change.javaClass.simpleName}")
} }
@@ -199,22 +182,6 @@ class AutoTunnelService : LifecycleService() {
autoTunnelStateFlow.update { it.copy(activeTunnels = change.activeTunnels) } autoTunnelStateFlow.update { it.copy(activeTunnels = change.activeTunnels) }
return@collect return@collect
} }
is StateChange.MonitoringChange -> {
change.pingStates.forEach { (config, pingState) ->
Timber.d("Ping state $pingState")
if (pingState?.all { it.value.isReachable } == true) {
Timber.d("Clearing bounce count on success")
bounceCounts.update { current ->
current.toMutableMap().apply { remove(config.id) }
}
}
}
return@collect handleAutoTunnelEvent(
autoTunnelStateFlow.value.determineAutoTunnelEvent(
StateChange.MonitoringChange(change.pingStates)
)
)
}
} }
handleAutoTunnelEvent(autoTunnelStateFlow.value.determineAutoTunnelEvent(change)) handleAutoTunnelEvent(autoTunnelStateFlow.value.determineAutoTunnelEvent(change))
@@ -240,7 +207,7 @@ class AutoTunnelService : LifecycleService() {
} }
// all relevant settings to auto tunnel // all relevant settings to auto tunnel
private fun areAutoTunnelSettingsTheSame(old: AppSettings, new: AppSettings): Boolean { private fun areAutoTunnelSettingsTheSame(old: GeneralSettings, new: GeneralSettings): Boolean {
return (old.isTunnelOnWifiEnabled == new.isTunnelOnWifiEnabled && return (old.isTunnelOnWifiEnabled == new.isTunnelOnWifiEnabled &&
old.isTunnelOnMobileDataEnabled == new.isTunnelOnMobileDataEnabled && old.isTunnelOnMobileDataEnabled == new.isTunnelOnMobileDataEnabled &&
old.isTunnelOnEthernetEnabled == new.isTunnelOnEthernetEnabled && old.isTunnelOnEthernetEnabled == new.isTunnelOnEthernetEnabled &&
@@ -254,7 +221,7 @@ class AutoTunnelService : LifecycleService() {
old.isStopOnNoInternetEnabled == new.isStopOnNoInternetEnabled) old.isStopOnNoInternetEnabled == new.isStopOnNoInternetEnabled)
} }
private fun combineSettings(): Flow<Pair<AppSettings, Tunnels>> { private fun combineSettings(): Flow<Pair<GeneralSettings, Tunnels>> {
return combine( return combine(
appDataRepository appDataRepository
.get() .get()
@@ -302,7 +269,7 @@ class AutoTunnelService : LifecycleService() {
.distinctUntilChanged(::areAutoTunnelPermissionsRequiredTheSame) .distinctUntilChanged(::areAutoTunnelPermissionsRequiredTheSame)
.map { .map {
NetworkPermissionState( NetworkPermissionState(
it.settings.wifiDetectionMethod, it.settings.wifiDetectionMethod.to(),
it.networkState.locationServicesEnabled == true, it.networkState.locationServicesEnabled == true,
it.networkState.locationPermissionGranted == true, it.networkState.locationPermissionGranted == true,
(it.tunnels.any { tunnel -> tunnel.tunnelNetworks.isNotEmpty() } || (it.tunnels.any { tunnel -> tunnel.tunnelNetworks.isNotEmpty() } ||
@@ -379,39 +346,8 @@ class AutoTunnelService : LifecycleService() {
(event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())?.let { (event.tunnelConf ?: appDataRepository.get().getPrimaryOrFirstTunnel())?.let {
tunnelManager.startTunnel(it) tunnelManager.startTunnel(it)
} }
is AutoTunnelEvent.Stop -> tunnelManager.stopTunnel() is AutoTunnelEvent.Stop -> tunnelManager.stopActiveTunnels()
AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: nothing to do") AutoTunnelEvent.DoNothing -> Timber.i("Auto-tunneling: nothing to do")
is AutoTunnelEvent.Bounce ->
handleBounceWithBackoff(event.configsPeerKeyResolvedMap)
}
}
}
private suspend fun handleBounceWithBackoff(
configsPeerKeyResolvedMap: List<Pair<TunnelConf, Map<String, String?>>>
) { // Simplified param: no failureCount
val settings = appDataRepository.get().settings.get()
val pingIntervalMillis = settings.tunnelPingIntervalSeconds.toMillis()
configsPeerKeyResolvedMap.forEach { (config, peerMap) ->
val bounceCount = bounceCounts.value.getOrDefault(config.id, 0)
val exponent = bounceCount.toDouble()
val backoffDelay =
(pingIntervalMillis * 2.0.pow(exponent)).toLong().coerceAtMost(MAX_BACKOFF_MS)
val currentTime = System.currentTimeMillis()
val lastTime = lastBounceTimes.getOrDefault(config.id, 0L)
if (currentTime - lastTime >= backoffDelay) {
Timber.d(
"Bouncing tunnel ${config.name} after detecting failure, with bounce count $bounceCount and calculated backoff delay $backoffDelay ms"
)
tunnelManager.bounceTunnel(config, Ping(peerMap))
lastBounceTimes[config.id] = currentTime
bounceCounts.update { current ->
current.toMutableMap().apply { this[config.id] = (this[config.id] ?: 0) + 1 }
}
} else {
Timber.d(
"Backoff in progress for tunnel ${config.name}, skipping bounce (required delay: $backoffDelay ms)"
)
} }
} }
} }
@@ -432,6 +368,5 @@ class AutoTunnelService : LifecycleService() {
companion object { companion object {
// try to keep this window short as it will interrupt manual overrides // try to keep this window short as it will interrupt manual overrides
const val REEVALUATE_CHECK_DELAY = 2_000L const val REEVALUATE_CHECK_DELAY = 2_000L
const val MAX_BACKOFF_MS = 300_000L // 5 minutes
} }
} }
@@ -1,20 +1,14 @@
package com.zaneschepke.wireguardautotunnel.core.service.autotunnel package com.zaneschepke.wireguardautotunnel.core.service.autotunnel
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState import com.zaneschepke.wireguardautotunnel.domain.state.NetworkState
import com.zaneschepke.wireguardautotunnel.domain.state.PingState
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels import com.zaneschepke.wireguardautotunnel.util.extensions.Tunnels
import org.amnezia.awg.crypto.Key
sealed class StateChange { sealed class StateChange {
data class NetworkChange(val networkState: NetworkState) : StateChange() data class NetworkChange(val networkState: NetworkState) : StateChange()
data class SettingsChange(val settings: AppSettings, val tunnels: Tunnels) : StateChange() data class SettingsChange(val settings: GeneralSettings, val tunnels: Tunnels) : StateChange()
data class ActiveTunnelsChange(val activeTunnels: Map<TunnelConf, TunnelState>) : StateChange() data class ActiveTunnelsChange(val activeTunnels: Map<Int, TunnelState>) : StateChange()
data class MonitoringChange(val pingStates: Map<TunnelConf, Map<Key, PingState>?>) :
StateChange()
} }
@@ -13,9 +13,7 @@ import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel import com.zaneschepke.wireguardautotunnel.WireGuardAutoTunnel
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager import com.zaneschepke.wireguardautotunnel.core.tunnel.TunnelManager
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelState
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@@ -65,12 +63,15 @@ class TunnelControlTile : TileService(), LifecycleOwner {
when { when {
activeTunnels.isNotEmpty() -> { activeTunnels.isNotEmpty() -> {
val activeIds = activeTunnels.map { it.key.id } val activeIds = activeTunnels.map { it.key }
// TODO improvements would be needed to make this work well with toggling // TODO improvements would be needed to make this work well with toggling
// multiple tunnels // multiple tunnels
// this would be better managed elsewhere // this would be better managed elsewhere
WireGuardAutoTunnel.setLastActiveTunnels(activeIds) WireGuardAutoTunnel.setLastActiveTunnels(activeIds)
updateTileForActiveTunnels(activeTunnels) val tunnels = appDataRepository.tunnels.getAll()
val activeTunNames =
tunnels.filter { activeTunnels.keys.contains(it.id) }.map { it.tunName }
updateTileForActiveTunnels(activeTunNames)
} }
else -> updateTileForLastActiveTunnels() else -> updateTileForLastActiveTunnels()
} }
@@ -79,10 +80,10 @@ class TunnelControlTile : TileService(), LifecycleOwner {
} }
} }
private fun updateTileForActiveTunnels(activeTunnels: Map<TunnelConf, TunnelState>) { private fun updateTileForActiveTunnels(activeTunnelNames: List<String>) {
val tileName = val tileName =
when (activeTunnels.size) { when (activeTunnelNames.size) {
1 -> activeTunnels.keys.first().tunName 1 -> activeTunnelNames[0]
else -> getString(R.string.multiple) else -> getString(R.string.multiple)
} }
updateTile(tileName, true) updateTile(tileName, true)
@@ -111,7 +112,7 @@ class TunnelControlTile : TileService(), LifecycleOwner {
unlockAndRun { unlockAndRun {
lifecycleScope.launch { lifecycleScope.launch {
if (tunnelManager.activeTunnels.value.isNotEmpty()) if (tunnelManager.activeTunnels.value.isNotEmpty())
return@launch tunnelManager.stopTunnel() return@launch tunnelManager.stopActiveTunnels()
val lastActive = WireGuardAutoTunnel.getLastActiveTunnels() val lastActive = WireGuardAutoTunnel.getLastActiveTunnels()
if (lastActive.isEmpty()) { if (lastActive.isEmpty()) {
appDataRepository.getStartTunnelConfig()?.let { tunnelManager.startTunnel(it) } appDataRepository.getStartTunnelConfig()?.let { tunnelManager.startTunnel(it) }
@@ -44,7 +44,7 @@ class ShortcutsActivity : ComponentActivity() {
tunnelConfig?.let { tunnelConfig?.let {
when (intent.action) { when (intent.action) {
Action.START.name -> tunnelManager.startTunnel(it) Action.START.name -> tunnelManager.startTunnel(it)
Action.STOP.name -> tunnelManager.stopTunnel() Action.STOP.name -> tunnelManager.stopActiveTunnels()
else -> Unit else -> Unit
} }
} }
@@ -1,22 +1,21 @@
package com.zaneschepke.wireguardautotunnel.core.tunnel package com.zaneschepke.wireguardautotunnel.core.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.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
import com.zaneschepke.wireguardautotunnel.domain.state.PingState import com.zaneschepke.wireguardautotunnel.domain.state.PingState
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.ui.state.ConfigProxy
import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
@@ -25,58 +24,60 @@ import org.amnezia.awg.crypto.Key
import timber.log.Timber import timber.log.Timber
abstract class BaseTunnel( abstract class BaseTunnel(
private val applicationScope: CoroutineScope, @ApplicationScope protected val applicationScope: CoroutineScope,
private val appDataRepository: AppDataRepository, protected val appDataRepository: AppDataRepository,
private val serviceManager: ServiceManager, protected val serviceManager: ServiceManager,
) : TunnelProvider { ) : TunnelProvider {
private val _errorEvents = MutableSharedFlow<Pair<TunnelConf, BackendError>>() protected val errors = MutableSharedFlow<Pair<String, BackendCoreException>>()
override val errorEvents = _errorEvents.asSharedFlow() override val errorEvents = errors.asSharedFlow()
private val _messageEvents = MutableSharedFlow<Pair<TunnelConf, BackendMessage>>() private val _messageEvents = MutableSharedFlow<Pair<String, BackendMessage>>()
override val messageEvents = _messageEvents.asSharedFlow() override val messageEvents = _messageEvents.asSharedFlow()
private val activeTuns = MutableStateFlow<Map<TunnelConf, TunnelState>>(emptyMap()) protected val activeTuns = MutableStateFlow<Map<Int, TunnelState>>(emptyMap())
private val tunJobs = ConcurrentHashMap<Int, Job>()
override val activeTunnels = activeTuns.asStateFlow() override val activeTunnels = activeTuns.asStateFlow()
private val tunJobs = ConcurrentHashMap<Int, Job>()
private val tunMutex = Mutex() private val tunMutex = Mutex()
private val tunStatusMutex = Mutex() private val tunStatusMutex = Mutex()
private val bounceTunnelMutex = Mutex()
override val bouncingTunnelIds = ConcurrentHashMap<Int, TunnelStatus.StopReason>() abstract fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus>
abstract suspend fun startBackend(tunnel: TunnelConf) abstract override fun setBackendMode(backendMode: BackendMode)
abstract fun stopBackend(tunnel: TunnelConf) abstract override fun getBackendMode(): BackendMode
abstract override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
abstract override fun getStatistics(tunnelId: Int): TunnelStatistics?
override fun hasVpnPermission(): Boolean { override fun hasVpnPermission(): Boolean {
return serviceManager.hasVpnPermission() return serviceManager.hasVpnPermission()
} }
override suspend fun updateTunnelStatus( override suspend fun updateTunnelStatus(
tunnelConf: TunnelConf, tunnelId: Int,
status: TunnelStatus?, status: TunnelStatus?,
stats: TunnelStatistics?, stats: TunnelStatistics?,
pingStates: Map<Key, PingState>?, pingStates: Map<Key, PingState>?,
handshakeSuccessLogs: Boolean?, logHealthState: LogHealthState?,
) { ) {
tunStatusMutex.withLock { tunStatusMutex.withLock {
activeTuns.update { currentTuns -> activeTuns.update { currentTuns ->
val originalConf = currentTuns.getKeyById(tunnelConf.id) ?: tunnelConf val existingState = currentTuns[tunnelId] ?: TunnelState()
val existingState = currentTuns.getValueById(tunnelConf.id) ?: TunnelState()
val newStatus = status ?: existingState.status val newStatus = status ?: existingState.status
if (newStatus == TunnelStatus.Down) { if (newStatus == TunnelStatus.Down) {
Timber.d("Removing tunnel ${tunnelConf.id} from activeTunnels as state is DOWN") Timber.d("Removing tunnel $tunnelId from activeTunnels as state is DOWN")
cleanUpTunJob(tunnelConf) cleanUpTunJob(tunnelId)
currentTuns - originalConf currentTuns - tunnelId
} else if ( } else if (
existingState.status == newStatus && existingState.status == newStatus &&
stats == null && stats == null &&
pingStates == null && pingStates == null &&
handshakeSuccessLogs == null logHealthState == null
) { ) {
Timber.d("Skipping redundant state update for ${tunnelConf.id}: $newStatus") Timber.d("Skipping redundant state update for ${tunnelId}: $newStatus")
currentTuns currentTuns
} else { } else {
val updated = val updated =
@@ -84,17 +85,15 @@ abstract class BaseTunnel(
status = newStatus, status = newStatus,
statistics = stats ?: existingState.statistics, statistics = stats ?: existingState.statistics,
pingStates = pingStates ?: existingState.pingStates, pingStates = pingStates ?: existingState.pingStates,
handshakeSuccessLogs = logHealthState = logHealthState ?: existingState.logHealthState,
handshakeSuccessLogs ?: existingState.handshakeSuccessLogs,
) )
currentTuns + (originalConf to updated) currentTuns + (tunnelId to updated)
} }
} }
handleServiceStateOnChange()
} }
} }
private suspend fun stopActiveTunnels() { override 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)
@@ -102,191 +101,43 @@ abstract class BaseTunnel(
} }
} }
private fun configureTunnelCallbacks(tunnelConf: TunnelConf) {
Timber.d("Configuring TunnelConf instance: ${tunnelConf.hashCode()}")
tunnelConf.setStateChangeCallback { state ->
applicationScope.launch {
Timber.d(
"State change callback triggered for tunnel ${tunnelConf.id}: ${tunnelConf.tunName} with state $state at ${System.currentTimeMillis()}"
)
when (state) {
is Tunnel.State -> updateTunnelStatus(tunnelConf, state.asTunnelState())
is org.amnezia.awg.backend.Tunnel.State ->
updateTunnelStatus(tunnelConf, state.asTunnelState())
}
handleServiceStateOnChange()
}
serviceManager.updateTunnelTile()
}
}
override suspend fun startTunnel(tunnelConf: TunnelConf) { override suspend fun startTunnel(tunnelConf: TunnelConf) {
if (activeTuns.exists(tunnelConf.id) || tunJobs.containsKey(tunnelConf.id))
return Timber.w("Tunnel is already running ${tunnelConf.name}")
// For userspace, we need to make sure all previous tunnels are down
if (this@BaseTunnel is UserspaceTunnel) stopActiveTunnels()
tunMutex.withLock { tunMutex.withLock {
if (activeTuns.value.containsKey(tunnelConf.id) || tunJobs.containsKey(tunnelConf.id)) {
return Timber.w("Tunnel is already running: ${tunnelConf.tunName}")
}
updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
val job = val job =
applicationScope.launch { applicationScope.launch {
try { try {
Timber.d("Starting tunnel ${tunnelConf.id}...") tunnelStateFlow(tunnelConf).collect { status ->
startTunnelInner(tunnelConf) updateTunnelStatus(tunnelConf.id, status)
Timber.d("Started complete for tunnel ${tunnelConf.name}...") serviceManager.updateTunnelTile()
// catch cancellation that could occur before and during startTunnelInner }
// and trigger at that suspend point } catch (e: BackendCoreException) {
} catch (e: CancellationException) { errors.emit(tunnelConf.tunName to e)
Timber.w( updateTunnelStatus(tunnelConf.id, TunnelStatus.Down)
"Tunnel start has been cancelled as ${tunnelConf.name} failed to start" } catch (_: CancellationException) {}
)
}
} }
tunJobs[tunnelConf.id] = job tunJobs[tunnelConf.id] = job
job.invokeOnCompletion { job.invokeOnCompletion {
tunJobs.remove(tunnelConf.id) tunJobs.remove(tunnelConf.id)
Timber.d("Start job completed for tunnel ${tunnelConf.id}") activeTuns.update { it - tunnelConf.id }
} }
} }
} }
private suspend fun startTunnelInner(tunnelConf: TunnelConf) { override suspend fun stopTunnel(tunnelId: Int) {
configureTunnelCallbacks(tunnelConf)
Timber.d("Starting backend for tunnel ${tunnelConf.id}...")
var currentConf = tunnelConf
var restoreAttempted = false
var originalError: BackendError? = null
while (true) {
try {
startBackend(currentConf)
updateTunnelStatus(currentConf, TunnelStatus.Up)
Timber.d("Started for tun ${currentConf.id}...")
saveTunnelActiveState(currentConf, true)
serviceManager.startTunnelForegroundService()
if (restoreAttempted)
_messageEvents.emit(tunnelConf to BackendMessage.BounceRecovery)
if (bouncingTunnelIds[currentConf.id] is TunnelStatus.StopReason.Ping) {
_messageEvents.emit(tunnelConf to BackendMessage.BounceSuccess)
}
return // Success, return
} catch (e: BackendError) {
originalError = originalError ?: e
val bounceReason = bouncingTunnelIds[currentConf.id]
if (!restoreAttempted && bounceReason is TunnelStatus.StopReason.Ping) {
Timber.i(
"Attempting to recover bounce failure with previously resolved endpoints for ${currentConf.name}"
)
try {
val previouslyResolved = bounceReason.previouslyResolvedEndpoints
val configProxy = ConfigProxy.from(currentConf.toAmConfig())
val updatedConfigProxy =
configProxy.copy(
peers =
configProxy.peers.map {
it.copy(
endpoint =
previouslyResolved[it.publicKey] ?: it.endpoint
)
}
)
val (wg, amnezia) = updatedConfigProxy.buildConfigs()
currentConf =
currentConf.copyWithCallback(
amQuick = amnezia.toAwgQuickString(true, false),
wgQuick = wg.toWgQuickString(true),
)
bouncingTunnelIds.remove(currentConf.id)
restoreAttempted = true
continue // Retry
} catch (e: Exception) {
Timber.e(
e,
"Failed to update config with resolved endpoints for ${currentConf.name}",
)
// Fall through to failure (will emit BounceFailed since
// retryAttempted=true)
}
}
Timber.e(e, "Failed to start backend for ${currentConf.name}")
val emitError =
if (restoreAttempted) BackendError.BounceFailed(originalError) else e
_errorEvents.emit(currentConf to emitError)
updateTunnelStatus(currentConf, TunnelStatus.Down)
return
}
}
}
private suspend fun saveTunnelActiveState(tunnelConf: TunnelConf, active: Boolean) {
val tunnelCopy = tunnelConf.copyWithCallback(isActive = active)
appDataRepository.tunnels.save(tunnelCopy)
}
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) {
if (tunnelConf == null) return stopActiveTunnels()
tunMutex.withLock { tunMutex.withLock {
if (activeTuns.isStarting(tunnelConf.id)) updateTunnelStatus(tunnelId, TunnelStatus.Stopping)
return handleStuckStartingTunnelShutdown(tunnelConf) tunJobs[tunnelId]?.cancel() // Triggers awaitClose to stop backend
updateTunnelStatus(tunnelConf, TunnelStatus.Stopping(reason))
stopTunnelInner(tunnelConf)
} }
} }
private suspend fun stopTunnelInner(tunnelConf: TunnelConf) { private fun cleanUpTunJob(tunnelId: Int) {
try { Timber.d("Removing job for $tunnelId")
val tunnel = activeTuns.findTunnel(tunnelConf.id) ?: return tunJobs -= tunnelId
stopBackend(tunnel)
saveTunnelActiveState(tunnelConf, false)
removeActiveTunnel(tunnel)
} catch (e: BackendError) {
Timber.e(e, "Failed to stop tunnel ${tunnelConf.id}")
_errorEvents.emit(tunnelConf to e)
updateTunnelStatus(tunnelConf, TunnelStatus.Down)
}
}
private fun handleServiceStateOnChange() {
if (activeTuns.value.isEmpty()) serviceManager.stopTunnelForegroundService()
}
private suspend fun handleStuckStartingTunnelShutdown(tunnel: TunnelConf) {
Timber.d("Stuck in starting state so cancelling job for tunnel ${tunnel.name}")
try {
tunJobs[tunnel.id]?.cancel() ?: Timber.d("No job found for ${tunnel.name}")
} catch (e: Exception) {
Timber.e(e, "Failed to cancel job for ${tunnel.name}")
} finally {
updateTunnelStatus(tunnel, TunnelStatus.Down)
}
}
private fun cleanUpTunJob(tunnel: TunnelConf) {
Timber.d("Removing job for ${tunnel.name}")
tunJobs -= tunnel.id
}
private fun removeActiveTunnel(tunnelConf: TunnelConf) {
activeTuns.update { current -> current.toMutableMap().apply { remove(tunnelConf) } }
}
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) {
bounceTunnelMutex.withLock {
Timber.i(
"Bounce tunnel ${tunnelConf.name} for reason: $reason, current bouncing: ${bouncingTunnelIds.size}"
)
bouncingTunnelIds[tunnelConf.id] = reason
runCatching {
stopTunnel(tunnelConf, reason)
delay(BOUNCE_DELAY)
startTunnel(tunnelConf)
}
}
}
override suspend fun runningTunnelNames(): Set<String> =
activeTuns.value.keys.map { it.tunName }.toSet()
companion object {
const val BOUNCE_DELAY = 300L
} }
} }
@@ -2,61 +2,89 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.wireguard.android.backend.Backend import com.wireguard.android.backend.Backend
import com.wireguard.android.backend.BackendException import com.wireguard.android.backend.BackendException
import com.wireguard.android.backend.Tunnel import com.wireguard.android.backend.Tunnel as WgTunnel
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
import com.zaneschepke.wireguardautotunnel.di.Kernel import com.zaneschepke.wireguardautotunnel.di.Kernel
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
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 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.asTunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendCoreException
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
import timber.log.Timber import timber.log.Timber
class KernelTunnel class KernelTunnel
@Inject @Inject
constructor( constructor(
@ApplicationScope private val applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
serviceManager: ServiceManager, serviceManager: ServiceManager,
appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
@Kernel private val backend: Backend, @Kernel private val backend: Backend,
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) { ) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? { private val runtimeTunnels = ConcurrentHashMap<Int, WgTunnel>()
return try {
WireGuardStatistics(backend.getStatistics(tunnelConf)) // TODO Add DNS settings
} catch (e: Exception) { override fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus> = callbackFlow {
Timber.e(e) if (!tunnelConf.isNameKernelCompatible) close(BackendCoreException.TunnelNameTooLong)
null
} val stateChannel = Channel<WgTunnel.State>()
}
val runtimeTunnel = RuntimeWgTunnel(tunnelConf, stateChannel)
runtimeTunnels[tunnelConf.id] = runtimeTunnel
val consumerJob = launch {
stateChannel.consumeAsFlow().collect { state -> trySend(state.asTunnelState()) }
}
override suspend fun startBackend(tunnel: TunnelConf) {
// name too long for kernel mode
if (!tunnel.isNameKernelCompatible) throw BackendError.TunnelNameTooLong
try { try {
updateTunnelStatus(tunnel, TunnelStatus.Starting) updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
backend.setState(tunnel, Tunnel.State.UP, tunnel.toWgConfig()) backend.setState(runtimeTunnel, WgTunnel.State.UP, tunnelConf.toWgConfig())
} catch (e: BackendException) { } catch (e: BackendException) {
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}") close(e.toBackendCoreException())
throw e.toBackendError()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}") Timber.e(e, "Invalid backend arguments")
throw BackendError.Config close(BackendCoreException.Config)
} catch (e: Exception) {
Timber.e(e, "Error while setting tunnel state")
close(BackendCoreException.Unknown)
}
awaitClose {
try {
backend.setState(runtimeTunnel, WgTunnel.State.DOWN, null)
} catch (e: BackendException) {
errors.tryEmit(tunnelConf.tunName to e.toBackendCoreException())
} finally {
consumerJob.cancel()
stateChannel.close()
runtimeTunnels.remove(tunnelConf.id)
trySend(TunnelStatus.Down)
close()
}
} }
} }
override fun stopBackend(tunnel: TunnelConf) { override fun getStatistics(tunnelId: Int): TunnelStatistics? {
Timber.i("Stopping tunnel ${tunnel.id} kernel") return try {
try { val runtimeTunnel = runtimeTunnels[tunnelId] ?: return null
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toWgConfig()) WireGuardStatistics(backend.getStatistics(runtimeTunnel))
} catch (e: BackendException) { } catch (e: Exception) {
throw e.toBackendError() Timber.e(e, "Failed to get stats for $tunnelId")
null
} }
} }
@@ -68,6 +96,10 @@ constructor(
return BackendMode.Inactive return BackendMode.Inactive
} }
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
throw NotImplementedError()
}
override suspend fun runningTunnelNames(): Set<String> { override suspend fun runningTunnelNames(): Set<String> {
return backend.runningTunnelNames return backend.runningTunnelNames
} }
@@ -0,0 +1,19 @@
package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import kotlinx.coroutines.channels.Channel
import org.amnezia.awg.backend.Tunnel
class RuntimeAwgTunnel(
private val tunnelConf: TunnelConf,
private val stateChannel: Channel<Tunnel.State>,
) : Tunnel {
override fun getName() = tunnelConf.tunName
override fun onStateChange(newState: Tunnel.State) {
stateChannel.trySend(newState)
}
override fun isIpv4ResolutionPreferred() = tunnelConf.isIpv4Preferred
}
@@ -0,0 +1,19 @@
package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.wireguard.android.backend.Tunnel
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import kotlinx.coroutines.channels.Channel
class RuntimeWgTunnel(
private val config: TunnelConf,
private val stateChannel: Channel<Tunnel.State>,
) : Tunnel {
override fun getName() = config.tunName
override fun onStateChange(newState: Tunnel.State) {
stateChannel.trySend(newState)
}
override fun isIpv4ResolutionPreferred() = config.isIpv4Preferred
}
@@ -5,28 +5,26 @@ import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.di.* import com.zaneschepke.wireguardautotunnel.di.*
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
import com.zaneschepke.wireguardautotunnel.domain.state.PingState import com.zaneschepke.wireguardautotunnel.domain.state.PingState
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 javax.inject.Inject import javax.inject.Inject
import kotlin.concurrent.atomics.AtomicBoolean import kotlin.concurrent.atomics.AtomicBoolean
import kotlin.concurrent.atomics.AtomicReference import kotlin.concurrent.atomics.AtomicReference
import kotlin.concurrent.atomics.ExperimentalAtomicApi import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.* import kotlinx.coroutines.flow.*
import kotlinx.coroutines.plus
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
import timber.log.Timber import timber.log.Timber
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class, ExperimentalAtomicApi::class)
class TunnelManager class TunnelManager
@Inject @Inject
constructor( constructor(
@@ -39,16 +37,30 @@ constructor(
@IoDispatcher ioDispatcher: CoroutineDispatcher, @IoDispatcher ioDispatcher: CoroutineDispatcher,
) : TunnelProvider { ) : TunnelProvider {
@OptIn(ExperimentalAtomicApi::class) private data class SideEffectState(
val activeTuns: Map<Int, TunnelState>,
val tuns: List<TunnelConf>,
val settings: GeneralSettings,
val previouslyActive: Map<Int, TunnelState>,
)
private data class SideEffectWithCondition(
val effect: suspend (SideEffectState) -> Unit,
val condition: (SideEffectState) -> Boolean,
)
private val sideEffectChannelFlow =
MutableStateFlow<Channel<SideEffectState>>(Channel(Channel.CONFLATED))
private val tunnelProviderFlow: StateFlow<TunnelProvider> = run { private val tunnelProviderFlow: StateFlow<TunnelProvider> = run {
val currentBackend = AtomicReference(userspaceTunnel) val currentBackend = AtomicReference(userspaceTunnel)
val currentSettings = AtomicReference(AppSettings()) val currentSettings = AtomicReference(GeneralSettings())
val initialEmit = AtomicBoolean(true) val initialEmit = AtomicBoolean(true)
appDataRepository.settings.flow appDataRepository.settings.flow
.filterNotNull() .filterNotNull()
// ignore default state // ignore default state
.filterNot { it == AppSettings() } .filterNot { it == GeneralSettings() }
.distinctUntilChanged { old, new -> .distinctUntilChanged { old, new ->
old.appMode == new.appMode && old.appMode == new.appMode &&
old.isLanOnKillSwitchEnabled == new.isLanOnKillSwitchEnabled old.isLanOnKillSwitchEnabled == new.isLanOnKillSwitchEnabled
@@ -66,37 +78,14 @@ constructor(
} }
.onEach { (settings, newBackend) -> .onEach { (settings, newBackend) ->
val isInitialEmit = initialEmit.exchange(false) val isInitialEmit = initialEmit.exchange(false)
val oldBackend = currentBackend.exchange(newBackend) val previousBackend = currentBackend.exchange(newBackend)
val oldSettings = currentSettings.exchange(settings) val previousSettings = currentSettings.exchange(settings)
if ((oldSettings.appMode != settings.appMode) && !isInitialEmit) { if ((previousSettings.appMode != settings.appMode) && !isInitialEmit) {
oldBackend.stopTunnel() handleModeChangeCleanup(previousBackend, previousSettings.appMode)
if (oldSettings.appMode == AppMode.LOCK_DOWN)
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
} }
if (settings.appMode == AppMode.LOCK_DOWN) { if (settings.appMode == AppMode.LOCK_DOWN) {
// kill switch will always catch all ipv6, just add ipv4 networks for allowsIps handleLockDownModeInit(settings.isLanOnKillSwitchEnabled)
val allowedIps =
if (settings.isLanOnKillSwitchEnabled) TunnelConf.IPV4_PUBLIC_NETWORKS
else emptySet()
proxyUserspaceTunnel.setBackendMode(BackendMode.KillSwitch(allowedIps))
}
// restore state if configured
if (isInitialEmit && settings.isRestoreOnBootEnabled) {
Timber.d("Restoring previous state")
if (
settings.isAutoTunnelEnabled &&
serviceManager.autoTunnelService.value == null
) {
serviceManager.startAutoTunnel()
} else {
val previouslyActiveTuns = appDataRepository.tunnels.getActive()
val tunsToStart =
previouslyActiveTuns.filterNot { tun ->
activeTunnels.value.any { tun.id == it.key.id }
}
tunsToStart.forEach { startTunnel(it) }
}
} }
} }
.map { (_, backend) -> backend } .map { (_, backend) -> backend }
@@ -107,17 +96,83 @@ constructor(
) )
} }
override val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>> = override val activeTunnels: StateFlow<Map<Int, TunnelState>> = run {
val activeTunsReference: AtomicReference<Map<Int, TunnelState>> =
AtomicReference(emptyMap())
tunnelProviderFlow tunnelProviderFlow
.flatMapLatest { it.activeTunnels } .flatMapLatest { backend ->
// Create a new channel for each backend to reset side-effect processing
val newChannel = Channel<SideEffectState>(Channel.CONFLATED)
sideEffectChannelFlow.value = newChannel
val sideEffects =
listOf(
SideEffectWithCondition(
effect = { s ->
handleTunnelServiceChange(s.settings.appMode, s.activeTuns)
},
condition = { s -> s.activeTuns.size != s.previouslyActive.size },
),
SideEffectWithCondition(
effect = { s ->
handleActiveTunnelsChange(s.previouslyActive, s.activeTuns, s.tuns)
},
condition = { s -> s.activeTuns.size != s.previouslyActive.size },
),
// TODO Not for kernel mode for now
SideEffectWithCondition(
effect = { s -> handleTunnelMonitoringChanges(s.activeTuns, s.tuns) },
condition = { s ->
s.tuns.any {
it.restartOnPingFailure && s.activeTuns.keys.contains(it.id)
} && s.settings.appMode != AppMode.KERNEL
},
),
)
applicationScope.launch(ioDispatcher) {
for (state in newChannel) {
supervisorScope {
sideEffects
.filter { it.condition(state) }
.forEach { sideEffect ->
launch {
try {
sideEffect.effect(state)
} catch (e: Exception) {
Timber.e(e, "Side effect failed")
}
}
}
}
}
}
combine(
backend.activeTunnels,
appDataRepository.tunnels.flow,
appDataRepository.settings.flow.filterNotNull(),
) { activeTuns, tuns, settings ->
Triple(activeTuns, tuns, settings)
}
}
.onStart { handleStateRestore() }
.onEach { (activeTuns, tuns, settings) ->
val previouslyActive = activeTunsReference.exchange(activeTuns)
sideEffectChannelFlow.value.trySend(
SideEffectState(activeTuns, tuns, settings, previouslyActive)
)
}
.map { (activeTuns, _, _) -> activeTuns }
.stateIn( .stateIn(
scope = applicationScope.plus(ioDispatcher), scope = applicationScope,
started = SharingStarted.Eagerly, started = SharingStarted.Eagerly,
initialValue = emptyMap(), initialValue = emptyMap(),
) )
}
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
override val errorEvents: SharedFlow<Pair<TunnelConf, BackendError>> = override val errorEvents: SharedFlow<Pair<String, BackendCoreException>> =
tunnelProviderFlow tunnelProviderFlow
.flatMapLatest { it.errorEvents } .flatMapLatest { it.errorEvents }
.shareIn( .shareIn(
@@ -127,37 +182,36 @@ constructor(
) )
@OptIn(ExperimentalCoroutinesApi::class) @OptIn(ExperimentalCoroutinesApi::class)
override val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>> = override val messageEvents: SharedFlow<Pair<String, BackendMessage>> =
tunnelProviderFlow tunnelProviderFlow
.flatMapLatest { it.messageEvents } .flatMapLatest { it.messageEvents }
.filterNotNull()
.shareIn( .shareIn(
scope = applicationScope.plus(ioDispatcher), scope = applicationScope.plus(ioDispatcher),
started = SharingStarted.Eagerly, started = SharingStarted.Eagerly,
replay = 0, replay = 0,
) )
override val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason> =
tunnelProviderFlow.value.bouncingTunnelIds
override fun hasVpnPermission(): Boolean { override fun hasVpnPermission(): Boolean {
return userspaceTunnel.hasVpnPermission() return userspaceTunnel.hasVpnPermission()
} }
override fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? { override fun getStatistics(tunnelId: Int): TunnelStatistics? {
return tunnelProviderFlow.value.getStatistics(tunnelConf) return tunnelProviderFlow.value.getStatistics(tunnelId)
} }
override suspend fun startTunnel(tunnelConf: TunnelConf) { override suspend fun startTunnel(tunnelConf: TunnelConf) {
// for VPN Mode, we need to stop active tunnels as we can only have one active at a time
if (activeTunnels.value.isNotEmpty() && tunnelProviderFlow.value == userspaceTunnel)
stopActiveTunnels()
tunnelProviderFlow.value.startTunnel(tunnelConf) tunnelProviderFlow.value.startTunnel(tunnelConf)
} }
override suspend fun stopTunnel(tunnelConf: TunnelConf?, reason: TunnelStatus.StopReason) { override suspend fun stopTunnel(tunnelId: Int) {
tunnelProviderFlow.value.stopTunnel(tunnelConf, reason) tunnelProviderFlow.value.stopTunnel(tunnelId)
} }
override suspend fun bounceTunnel(tunnelConf: TunnelConf, reason: TunnelStatus.StopReason) { override suspend fun stopActiveTunnels() {
tunnelProviderFlow.value.bounceTunnel(tunnelConf, reason) tunnelProviderFlow.value.stopActiveTunnels()
} }
override fun setBackendMode(backendMode: BackendMode) { override fun setBackendMode(backendMode: BackendMode) {
@@ -172,19 +226,141 @@ constructor(
return tunnelProviderFlow.value.runningTunnelNames() return tunnelProviderFlow.value.runningTunnelNames()
} }
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
return tunnelProviderFlow.value.handleDnsReresolve(tunnelConf)
}
override suspend fun updateTunnelStatus( override suspend fun updateTunnelStatus(
tunnelConf: TunnelConf, tunnelId: Int,
status: TunnelStatus?, status: TunnelStatus?,
stats: TunnelStatistics?, stats: TunnelStatistics?,
pingStates: Map<Key, PingState>?, pingStates: Map<Key, PingState>?,
handshakeSuccessLogs: Boolean?, logHealthState: LogHealthState?,
) { ) {
tunnelProviderFlow.value.updateTunnelStatus( tunnelProviderFlow.value.updateTunnelStatus(
tunnelConf, tunnelId,
status, status,
stats, stats,
pingStates, pingStates,
handshakeSuccessLogs, logHealthState,
) )
} }
private suspend fun handleTunnelServiceChange(
appMode: AppMode,
activeTuns: Map<Int, TunnelState>,
) {
if (activeTuns.isEmpty()) serviceManager.stopTunnelService()
if (activeTuns.isNotEmpty() && serviceManager.tunnelService.value == null)
serviceManager.startTunnelService(appMode)
}
private fun handleLockDownModeInit(withLanBypass: Boolean) {
val allowedIps = if (withLanBypass) TunnelConf.IPV4_PUBLIC_NETWORKS else emptySet()
try {
// TODO handle situation where they don't have vpn permission, request it
if (hasVpnPermission()) {
proxyUserspaceTunnel.setBackendMode(BackendMode.KillSwitch(allowedIps))
}
} catch (e: BackendCoreException) {
// TODO expose this error to user
Timber.e(e)
}
}
private suspend fun handleModeChangeCleanup(
previousBackend: TunnelProvider,
previousAppMode: AppMode,
) {
previousBackend.stopActiveTunnels()
// stop lockdown if we switch from that mode
if (previousAppMode == AppMode.LOCK_DOWN)
proxyUserspaceTunnel.setBackendMode(BackendMode.Inactive)
}
private suspend fun handleStateRestore() {
val settings = appDataRepository.settings.flow.first()
if (settings.isRestoreOnBootEnabled) {
// if auto tun enabled, reset active and restore auto tun, letting it start appropriate
// tuns
if (settings.isAutoTunnelEnabled) {
appDataRepository.tunnels.resetActiveTunnels()
return serviceManager.startAutoTunnel()
}
val tunnels = appDataRepository.tunnels.flow.first()
when (settings.appMode) {
// TODO eventually, lockdown/proxy can support multi
AppMode.VPN,
AppMode.LOCK_DOWN,
AppMode.PROXY ->
tunnels
.firstOrNull { it.isActive }
?.let {
// clear any duplicates
appDataRepository.tunnels.resetActiveTunnels()
startTunnel(it)
}
// kernel supports multi
AppMode.KERNEL ->
tunnels.filter { it.isActive }.forEach { conf -> startTunnel(conf) }
}
}
}
private suspend fun handleTunnelMonitoringChanges(
activeTuns: Map<Int, TunnelState>,
configs: List<TunnelConf>,
) {
configs
.filter { it.restartOnPingFailure && activeTuns.keys.contains(it.id) }
.forEach { conf ->
val tunState = activeTuns[conf.id] ?: return@forEach
if (tunState.health() == TunnelState.Health.UNHEALTHY) {
runCatching {
val updated = handleDnsReresolve(conf)
// TODO user messages
if (updated) {
Timber.i("Successfully update the peer endpoint to new address.")
} else {
Timber.i("Current endpoint address is already up to date.")
}
}
.onFailure {
Timber.e(it, "Failed to handle dns re-resolution for ${conf.tunName}")
}
// TODO backoff
delay(30_000L)
}
}
}
private suspend fun handleActiveTunnelsChange(
previousActiveTuns: Map<Int, TunnelState>,
activeTuns: Map<Int, TunnelState>,
tuns: List<TunnelConf>,
) {
val relevantTunnels = previousActiveTuns.keys + activeTuns.keys
relevantTunnels.forEach { tunnelId ->
val wasActive = previousActiveTuns.containsKey(tunnelId)
val isActiveNow = activeTuns.containsKey(tunnelId)
when {
!wasActive && isActiveNow -> {
tuns
.find { it.id == tunnelId }
?.let { dbTunnelConf ->
appDataRepository.tunnels.save(dbTunnelConf.copy(isActive = true))
}
}
wasActive && !isActiveNow -> {
tuns
.find { it.id == tunnelId }
?.let { dbTunnelConf ->
appDataRepository.tunnels.save(dbTunnelConf.copy(isActive = false))
}
}
}
}
}
} }
@@ -2,10 +2,12 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.zaneschepke.logcatter.LogReader import com.zaneschepke.logcatter.LogReader
import com.zaneschepke.networkmonitor.NetworkMonitor import com.zaneschepke.networkmonitor.NetworkMonitor
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.state.FailureReason import com.zaneschepke.wireguardautotunnel.domain.state.FailureReason
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
import com.zaneschepke.wireguardautotunnel.domain.state.PingState import com.zaneschepke.wireguardautotunnel.domain.state.PingState
import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis import com.zaneschepke.wireguardautotunnel.util.extensions.toMillis
import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils
@@ -29,56 +31,43 @@ constructor(
) { ) {
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
suspend fun startMonitoring(tunnelConf: TunnelConf, withLogs: Boolean): Job = coroutineScope { suspend fun startMonitoring(tunnelId: Int, withLogs: Boolean): Job = coroutineScope {
launch { launch {
launch { startTunnelConfChangesJob(tunnelConf) } val config = appDataRepository.tunnels.getById(tunnelId) ?: return@launch
launch { startPingMonitor(tunnelConf) } launch { startPingMonitor(config) }
launch { startWgStatsPoll(tunnelConf) } launch { startWgStatsPoll(config.id) }
if (withLogs) launch { startLogsMonitor(tunnelConf) } if (withLogs) launch { startLogsMonitor(config) }
} }
} }
private suspend fun startTunnelConfChangesJob(tunnelConf: TunnelConf) {
appDataRepository.tunnels.flow
.map { storedTunnels -> storedTunnels.firstOrNull { it.id == tunnelConf.id } }
.filterNotNull()
.distinctUntilChanged { old, new -> old == new }
.collect { storedTunnel ->
if (tunnelConf != storedTunnel) {
Timber.d("Config changed for ${storedTunnel.tunName}, bouncing")
withContext(NonCancellable) {
tunnelManager.bounceTunnel(
storedTunnel,
TunnelStatus.StopReason.ConfigChanged,
)
}
}
}
}
private suspend fun startLogsMonitor(tunnelConf: TunnelConf) { private suspend fun startLogsMonitor(tunnelConf: TunnelConf) {
logReader.liveLogs.collect { log -> logReader.liveLogs
val healthLogs = .filter { log -> log.tag.contains(tunnelConf.tunName) }
.mapNotNull { log ->
val now = System.currentTimeMillis()
when { when {
log.message.contains(HANDSHAKE_RESPONSE_TEXT, true) || successLogRegex.containsMatchIn(log.message) ->
log.message.contains(KEEPALIVE_RESPONSE_TEXT, true) -> true LogHealthState(isHealthy = true, timestamp = now)
log.message.contains(HANDSHAKE_INIT_FAILED_TEXT, true) ||
log.message.contains(HANDSHAKE_NOT_COMPLETED_TEXT) || failureLogRegex.containsMatchIn(log.message) ->
log.message.contains(DATA_PACKET_FAILED_TEXT) -> false LogHealthState(isHealthy = false, timestamp = now)
else -> null else -> null
} }
healthLogs?.let { healthy ->
tunnelManager.updateTunnelStatus(tunnelConf, null, null, null, healthy)
} }
} .distinctUntilChangedBy { it.isHealthy } // Only emit when health changes
.collect { logHealthState ->
Timber.d("Tunnel log health updated for ${tunnelConf.tunName}: $logHealthState")
tunnelManager.updateTunnelStatus(tunnelConf.id, logHealthState = logHealthState)
}
} }
private suspend fun startPingMonitor(tunnelConf: TunnelConf) = coroutineScope { private suspend fun startPingMonitor(tunnelConf: TunnelConf) = coroutineScope {
val pingStatsFlow = MutableStateFlow<Map<Key, PingState>>(emptyMap()) val pingStatsFlow = MutableStateFlow<Map<Key, PingState>>(emptyMap())
val tunStateFlow = val tunStateFlow =
tunnelManager.activeTunnels.mapNotNull { it.getValueById(tunnelConf.id) }.stateIn(this) tunnelManager.activeTunnels.mapNotNull { it[tunnelConf.id] }.stateIn(this)
val connectivityStateFlow = networkMonitor.connectivityStateFlow.stateIn(this) val connectivityStateFlow = networkMonitor.connectivityStateFlow.stateIn(this)
@@ -109,9 +98,13 @@ constructor(
old.tunnelPingIntervalSeconds == new.tunnelPingIntervalSeconds && old.tunnelPingIntervalSeconds == new.tunnelPingIntervalSeconds &&
old.tunnelPingAttempts == new.tunnelPingAttempts && old.tunnelPingAttempts == new.tunnelPingAttempts &&
old.tunnelPingTimeoutSeconds == new.tunnelPingTimeoutSeconds old.tunnelPingTimeoutSeconds == new.tunnelPingTimeoutSeconds
old.appMode == new.appMode
} }
.collectLatest { settings -> .collectLatest { settings ->
if (!settings.isPingEnabled) return@collectLatest if (!settings.isPingEnabled) return@collectLatest
// TODO for now until we get monitoring for these modes
if (settings.appMode == AppMode.LOCK_DOWN || settings.appMode == AppMode.PROXY)
return@collectLatest
Timber.d("Starting pinger for ${tunnelConf.tunName} with settings") Timber.d("Starting pinger for ${tunnelConf.tunName} with settings")
@@ -210,7 +203,7 @@ constructor(
if (updates.isNotEmpty()) { if (updates.isNotEmpty()) {
pingStatsFlow.update { updates } pingStatsFlow.update { updates }
tunnelManager.updateTunnelStatus(tunnelConf, null, null, updates) tunnelManager.updateTunnelStatus(tunnelConf.id, null, null, updates)
} }
} }
@@ -234,7 +227,7 @@ constructor(
} }
} }
tunnelManager.updateTunnelStatus( tunnelManager.updateTunnelStatus(
tunnelConf, tunnelConf.id,
null, null,
null, null,
pingStatsFlow.value, pingStatsFlow.value,
@@ -245,15 +238,27 @@ constructor(
} }
} }
private suspend fun startWgStatsPoll(tunnelConf: TunnelConf) = coroutineScope { private suspend fun startWgStatsPoll(tunnelId: Int) = coroutineScope {
while (isActive) { while (isActive) {
val stats = tunnelManager.getStatistics(tunnelConf) val stats = tunnelManager.getStatistics(tunnelId)
tunnelManager.updateTunnelStatus(tunnelConf, null, stats, null) tunnelManager.updateTunnelStatus(tunnelId, null, stats, null)
delay(STATS_DELAY) delay(STATS_DELAY)
} }
} }
companion object { companion object {
private val successLogRegex =
Regex("Received handshake response|Receiving keepalive packet", RegexOption.IGNORE_CASE)
private val failureLogRegex =
Regex(
"Failed to send handshake initiation: write udp|" +
"Handshake did not complete after 5 seconds, retrying|" +
"Failed to send data packets",
RegexOption.IGNORE_CASE,
)
const val CLOUDFLARE_IPV6_IP = "2606:4700:4700::1111" const val CLOUDFLARE_IPV6_IP = "2606:4700:4700::1111"
const val CLOUDFLARE_IPV4_IP = "1.1.1.1" const val CLOUDFLARE_IPV4_IP = "1.1.1.1"
@@ -2,13 +2,13 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage import com.zaneschepke.wireguardautotunnel.domain.events.BackendMessage
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.state.LogHealthState
import com.zaneschepke.wireguardautotunnel.domain.state.PingState import com.zaneschepke.wireguardautotunnel.domain.state.PingState
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.SharedFlow import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import org.amnezia.awg.crypto.Key import org.amnezia.awg.crypto.Key
@@ -18,28 +18,14 @@ interface TunnelProvider {
suspend fun startTunnel(tunnelConf: TunnelConf) suspend fun startTunnel(tunnelConf: TunnelConf)
/** /**
* Stops the specified tunnel, or all tunnels if none is provided. * Stops the specified tunnel.
* *
* @param tunnelConf The tunnel to stop, or null to stop all active tunnels. * @param tunnelId The tunnelConf to stop.
* @param reason The reason for stopping, defaults to USER for manual stops. Callers should
* override with specific reasons (e.g., PING, CONFIG_CHANGED) when applicable.
*/ */
suspend fun stopTunnel( suspend fun stopTunnel(tunnelId: Int)
tunnelConf: TunnelConf? = null,
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
)
/** /** Stops all active tunnels. */
* Bounces (stops and restarts) the specified tunnel. suspend fun stopActiveTunnels()
*
* @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, ConfigChanged) when applicable.
*/
suspend fun bounceTunnel(
tunnelConf: TunnelConf,
reason: TunnelStatus.StopReason = TunnelStatus.StopReason.User,
)
fun setBackendMode(backendMode: BackendMode) fun setBackendMode(backendMode: BackendMode)
@@ -47,23 +33,23 @@ interface TunnelProvider {
suspend fun runningTunnelNames(): Set<String> suspend fun runningTunnelNames(): Set<String>
fun getStatistics(tunnelConf: TunnelConf): TunnelStatistics? fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean
val activeTunnels: StateFlow<Map<TunnelConf, TunnelState>> fun getStatistics(tunnelId: Int): TunnelStatistics?
val errorEvents: SharedFlow<Pair<TunnelConf, BackendError>> val activeTunnels: StateFlow<Map<Int, TunnelState>>
val messageEvents: SharedFlow<Pair<TunnelConf, BackendMessage>> val errorEvents: SharedFlow<Pair<String, BackendCoreException>>
val bouncingTunnelIds: ConcurrentHashMap<Int, TunnelStatus.StopReason> val messageEvents: SharedFlow<Pair<String, BackendMessage>>
fun hasVpnPermission(): Boolean fun hasVpnPermission(): Boolean
suspend fun updateTunnelStatus( suspend fun updateTunnelStatus(
tunnelConf: TunnelConf, tunnelId: Int,
status: TunnelStatus? = null, status: TunnelStatus? = null,
stats: TunnelStatistics? = null, stats: TunnelStatistics? = null,
pingStates: Map<Key, PingState>? = null, pingStates: Map<Key, PingState>? = null,
handshakeSuccessLogs: Boolean? = null, logHealthState: LogHealthState? = null,
) )
} }
@@ -2,9 +2,10 @@ package com.zaneschepke.wireguardautotunnel.core.tunnel
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode import com.zaneschepke.wireguardautotunnel.domain.enums.BackendMode
import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus import com.zaneschepke.wireguardautotunnel.domain.enums.TunnelStatus
import com.zaneschepke.wireguardautotunnel.domain.events.BackendError import com.zaneschepke.wireguardautotunnel.domain.events.BackendCoreException
import com.zaneschepke.wireguardautotunnel.domain.model.AppProxySettings import com.zaneschepke.wireguardautotunnel.domain.model.AppProxySettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
@@ -12,14 +13,23 @@ import com.zaneschepke.wireguardautotunnel.domain.state.AmneziaStatistics
import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics import com.zaneschepke.wireguardautotunnel.domain.state.TunnelStatistics
import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendMode import com.zaneschepke.wireguardautotunnel.util.extensions.asAmBackendMode
import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendMode import com.zaneschepke.wireguardautotunnel.util.extensions.asBackendMode
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendError import com.zaneschepke.wireguardautotunnel.util.extensions.asTunnelState
import com.zaneschepke.wireguardautotunnel.util.extensions.toBackendCoreException
import java.io.IOException
import java.util.* import java.util.*
import java.util.concurrent.ConcurrentHashMap
import javax.inject.Inject import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.launch
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.ProxyGoBackend import org.amnezia.awg.backend.ProxyGoBackend
import org.amnezia.awg.backend.Tunnel import org.amnezia.awg.backend.Tunnel as AwgTunnel
import org.amnezia.awg.config.Config import org.amnezia.awg.config.Config
import org.amnezia.awg.config.DnsSettings import org.amnezia.awg.config.DnsSettings
import org.amnezia.awg.config.proxy.HttpProxy import org.amnezia.awg.config.proxy.HttpProxy
@@ -30,15 +40,26 @@ import timber.log.Timber
class UserspaceTunnel class UserspaceTunnel
@Inject @Inject
constructor( constructor(
applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
val serviceManager: ServiceManager, serviceManager: ServiceManager,
val appDataRepository: AppDataRepository, appDataRepository: AppDataRepository,
private val backend: Backend, private val backend: Backend,
) : BaseTunnel(applicationScope, appDataRepository, serviceManager) { ) : BaseTunnel(applicationScope, appDataRepository, serviceManager) {
override suspend fun startBackend(tunnel: TunnelConf) { private val runtimeTunnels = ConcurrentHashMap<Int, AwgTunnel>()
override fun tunnelStateFlow(tunnelConf: TunnelConf): Flow<TunnelStatus> = callbackFlow {
val stateChannel = Channel<AwgTunnel.State>()
val runtimeTunnel = RuntimeAwgTunnel(tunnelConf, stateChannel)
runtimeTunnels[tunnelConf.id] = runtimeTunnel
val consumerJob = launch {
stateChannel.consumeAsFlow().collect { awgState -> trySend(awgState.asTunnelState()) }
}
try { try {
updateTunnelStatus(tunnel, TunnelStatus.Starting) updateTunnelStatus(tunnelConf.id, TunnelStatus.Starting)
val proxies: List<Proxy> = val proxies: List<Proxy> =
when (backend) { when (backend) {
@@ -71,7 +92,7 @@ constructor(
else -> emptyList() else -> emptyList()
} }
val setting = appDataRepository.settings.get() val setting = appDataRepository.settings.get()
val config = tunnel.toAmConfig() val config = tunnelConf.toAmConfig()
val updatedConfig = val updatedConfig =
Config.Builder() Config.Builder()
.apply { .apply {
@@ -86,24 +107,28 @@ constructor(
) )
} }
.build() .build()
backend.setState(runtimeTunnel, AwgTunnel.State.UP, updatedConfig)
backend.setState(tunnel, Tunnel.State.UP, updatedConfig)
} catch (e: BackendException) { } catch (e: BackendException) {
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}") close(e.toBackendCoreException())
throw e.toBackendError()
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
Timber.e(e, "Failed to start up backend for tunnel ${tunnel.name}") close(BackendCoreException.Config)
throw BackendError.Config } catch (e: Exception) {
Timber.e(e, "Error while setting tunnel state")
close(BackendCoreException.Unknown)
} }
}
override fun stopBackend(tunnel: TunnelConf) { awaitClose {
Timber.i("Stopping tunnel ${tunnel.name} userspace") try {
try { backend.setState(runtimeTunnel, AwgTunnel.State.DOWN, null)
backend.setState(tunnel, Tunnel.State.DOWN, tunnel.toAmConfig()) } catch (e: BackendException) {
} catch (e: BackendException) { errors.tryEmit(tunnelConf.tunName to e.toBackendCoreException())
Timber.e(e, "Failed to stop tunnel ${tunnel.id}") } finally {
throw e.toBackendError() consumerJob.cancel()
stateChannel.close()
runtimeTunnels.remove(tunnelConf.id)
trySend(TunnelStatus.Down)
close()
}
} }
} }
@@ -112,7 +137,10 @@ constructor(
try { try {
backend.backendMode = backendMode.asAmBackendMode() backend.backendMode = backendMode.asAmBackendMode()
} catch (e: BackendException) { } catch (e: BackendException) {
throw e.toBackendError() throw e.toBackendCoreException()
// TODO this should be mapped to BackendException in the lib
} catch (e: IOException) {
throw BackendCoreException.NotAuthorized
} }
} }
@@ -120,15 +148,22 @@ constructor(
return backend.backendMode.asBackendMode() return backend.backendMode.asBackendMode()
} }
override fun handleDnsReresolve(tunnelConf: TunnelConf): Boolean {
val tunnel =
runtimeTunnels.get(tunnelConf.id) ?: throw BackendCoreException.ServiceNotRunning
return backend.resolveDDNS(tunnelConf.toAmConfig(), tunnel.isIpv4ResolutionPreferred)
}
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(tunnelId: Int): TunnelStatistics? {
return try { return try {
AmneziaStatistics(backend.getStatistics(tunnelConf)) val runtimeTunnel = runtimeTunnels[tunnelId] ?: return null
AmneziaStatistics(backend.getStatistics(runtimeTunnel))
} catch (e: Exception) { } catch (e: Exception) {
Timber.e(e, "Failed to get stats for ${tunnelConf.tunName}") Timber.e(e, "Failed to get stats for $tunnelId")
null null
} }
} }
@@ -2,6 +2,7 @@ package com.zaneschepke.wireguardautotunnel.data
import androidx.room.* import androidx.room.*
import androidx.room.migration.AutoMigrationSpec import androidx.room.migration.AutoMigrationSpec
import androidx.sqlite.db.SupportSQLiteDatabase
import com.zaneschepke.wireguardautotunnel.data.dao.ProxySettingsDao import com.zaneschepke.wireguardautotunnel.data.dao.ProxySettingsDao
import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao import com.zaneschepke.wireguardautotunnel.data.dao.SettingsDao
import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao import com.zaneschepke.wireguardautotunnel.data.dao.TunnelConfigDao
@@ -11,7 +12,7 @@ import com.zaneschepke.wireguardautotunnel.data.entity.TunnelConfig
@Database( @Database(
entities = [Settings::class, TunnelConfig::class, ProxySettings::class], entities = [Settings::class, TunnelConfig::class, ProxySettings::class],
version = 20, version = 22,
autoMigrations = autoMigrations =
[ [
AutoMigration(from = 1, to = 2), AutoMigration(from = 1, to = 2),
@@ -33,6 +34,8 @@ import com.zaneschepke.wireguardautotunnel.data.entity.TunnelConfig
AutoMigration(from = 17, to = 18), AutoMigration(from = 17, to = 18),
AutoMigration(from = 18, to = 19, spec = PingMigration::class), AutoMigration(from = 18, to = 19, spec = PingMigration::class),
AutoMigration(from = 19, to = 20, spec = ProxyMigration::class), AutoMigration(from = 19, to = 20, spec = ProxyMigration::class),
AutoMigration(from = 20, to = 21, spec = FixProxySettingsMigration::class),
AutoMigration(from = 21, to = 22),
], ],
exportSchema = true, exportSchema = true,
) )
@@ -80,4 +83,20 @@ class PingMigration : AutoMigrationSpec
DeleteColumn(tableName = "Settings", columnName = "is_kernel_kill_switch_enabled"), DeleteColumn(tableName = "Settings", columnName = "is_kernel_kill_switch_enabled"),
DeleteColumn(tableName = "Settings", columnName = "is_kernel_enabled"), DeleteColumn(tableName = "Settings", columnName = "is_kernel_enabled"),
) )
class ProxyMigration : AutoMigrationSpec class ProxyMigration : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
db.execSQL("INSERT INTO proxy_settings DEFAULT VALUES")
}
}
class FixProxySettingsMigration : AutoMigrationSpec {
override fun onPostMigrate(db: SupportSQLiteDatabase) {
val cursor = db.query("SELECT COUNT(*) FROM proxy_settings")
val count = if (cursor.moveToFirst()) cursor.getInt(0) else 0
cursor.close()
if (count == 0) {
db.execSQL("INSERT INTO proxy_settings DEFAULT VALUES")
}
}
}
@@ -13,7 +13,6 @@ import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import timber.log.Timber import timber.log.Timber
@@ -85,9 +84,5 @@ class DataStoreManager(
} }
} }
fun <T> getFromStoreBlocking(key: Preferences.Key<T>) = runBlocking {
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)
} }
@@ -2,25 +2,15 @@ package com.zaneschepke.wireguardautotunnel.data
import androidx.room.RoomDatabase import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase import androidx.sqlite.db.SupportSQLiteDatabase
import com.zaneschepke.wireguardautotunnel.data.entity.ProxySettings
import com.zaneschepke.wireguardautotunnel.data.entity.Settings
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Provider import javax.inject.Provider
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class DatabaseCallback @Inject constructor(private val databaseProvider: Provider<AppDatabase>) : class DatabaseCallback @Inject constructor(private val databaseProvider: Provider<AppDatabase>) :
RoomDatabase.Callback() { RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) { override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db) super.onCreate(db)
db.execSQL("INSERT INTO proxy_settings DEFAULT VALUES")
// Launch coroutine to insert default entry db.execSQL("INSERT INTO Settings DEFAULT VALUES")
CoroutineScope(Dispatchers.IO).launch {
val db = databaseProvider.get()
db.settingDao().save(Settings())
db.proxySettingsDoa().save(ProxySettings())
}
} }
} }
@@ -24,6 +24,16 @@ class DatabaseConverters {
} }
} }
@TypeConverter
fun setToString(value: Set<String>): String {
return listToString(value.toList())
}
@TypeConverter
fun stringToSet(value: String): Set<String> {
return stringToList(value).toSet()
}
@TypeConverter fun fromStatus(status: WifiDetectionMethod): Int = status.value @TypeConverter fun fromStatus(status: WifiDetectionMethod): Int = status.value
@TypeConverter @TypeConverter
@@ -13,6 +13,9 @@ interface TunnelConfigDao {
@Query("SELECT * FROM TunnelConfig WHERE id=:id") suspend fun getById(id: Long): TunnelConfig? @Query("SELECT * FROM TunnelConfig WHERE id=:id") suspend fun getById(id: Long): TunnelConfig?
@Query("UPDATE TunnelConfig SET is_Active = 0 WHERE is_Active = 1")
suspend fun resetActiveTunnels()
@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?
@@ -22,6 +25,8 @@ interface TunnelConfigDao {
@Delete suspend fun delete(t: TunnelConfig) @Delete suspend fun delete(t: TunnelConfig)
@Delete suspend fun delete(t: TunnelConfigs)
@Query("SELECT COUNT('id') FROM TunnelConfig") suspend fun count(): Long @Query("SELECT COUNT('id') FROM TunnelConfig") suspend fun count(): Long
@Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'") @Query("SELECT * FROM TunnelConfig WHERE tunnel_networks LIKE '%' || :name || '%'")
@@ -7,10 +7,10 @@ import androidx.room.PrimaryKey
@Entity(tableName = "proxy_settings") @Entity(tableName = "proxy_settings")
data class ProxySettings( data class ProxySettings(
@PrimaryKey(autoGenerate = true) val id: Long = 0, @PrimaryKey(autoGenerate = true) val id: Long = 0,
@ColumnInfo(name = "socks5_proxy_enabled", defaultValue = "false") @ColumnInfo(name = "socks5_proxy_enabled", defaultValue = "0")
val socks5ProxyEnabled: Boolean = false, val socks5ProxyEnabled: Boolean = false,
@ColumnInfo(name = "socks5_proxy_bind_address") val socks5ProxyBindAddress: String? = null, @ColumnInfo(name = "socks5_proxy_bind_address") val socks5ProxyBindAddress: String? = null,
@ColumnInfo(name = "http_proxy_enable", defaultValue = "false") @ColumnInfo(name = "http_proxy_enable", defaultValue = "0")
val httpProxyEnabled: Boolean = false, val httpProxyEnabled: Boolean = false,
@ColumnInfo(name = "http_proxy_bind_address") val httpProxyBindAddress: String? = null, @ColumnInfo(name = "http_proxy_bind_address") val httpProxyBindAddress: String? = null,
@ColumnInfo(name = "proxy_username") val proxyUsername: String? = null, @ColumnInfo(name = "proxy_username") val proxyUsername: String? = null,
@@ -10,38 +10,40 @@ import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod
@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", defaultValue = "0")
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled") val isAutoTunnelEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_mobile_data_enabled", defaultValue = "0")
val isTunnelOnMobileDataEnabled: Boolean = false, val isTunnelOnMobileDataEnabled: Boolean = false,
@ColumnInfo(name = "trusted_network_ssids") val trustedNetworkSSIDs: List<String> = emptyList(), @ColumnInfo(name = "trusted_network_ssids", defaultValue = "")
@ColumnInfo(name = "is_always_on_vpn_enabled") val isAlwaysOnVpnEnabled: Boolean = false, val trustedNetworkSSIDs: Set<String> = emptySet(),
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled") @ColumnInfo(name = "is_always_on_vpn_enabled", defaultValue = "0")
val isAlwaysOnVpnEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_ethernet_enabled", defaultValue = "0")
val isTunnelOnEthernetEnabled: Boolean = false, val isTunnelOnEthernetEnabled: Boolean = false,
@ColumnInfo(name = "is_shortcuts_enabled", defaultValue = "false") @ColumnInfo(name = "is_shortcuts_enabled", defaultValue = "0")
val isShortcutsEnabled: Boolean = false, val isShortcutsEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_wifi_enabled", defaultValue = "false") @ColumnInfo(name = "is_tunnel_on_wifi_enabled", defaultValue = "0")
val isTunnelOnWifiEnabled: Boolean = false, val isTunnelOnWifiEnabled: Boolean = false,
@ColumnInfo(name = "is_restore_on_boot_enabled", defaultValue = "false") @ColumnInfo(name = "is_restore_on_boot_enabled", defaultValue = "0")
val isRestoreOnBootEnabled: Boolean = false, val isRestoreOnBootEnabled: Boolean = false,
@ColumnInfo(name = "is_multi_tunnel_enabled", defaultValue = "false") @ColumnInfo(name = "is_multi_tunnel_enabled", defaultValue = "0")
val isMultiTunnelEnabled: Boolean = false, val isMultiTunnelEnabled: Boolean = false,
@ColumnInfo(name = "is_ping_enabled", defaultValue = "false") @ColumnInfo(name = "is_ping_enabled", defaultValue = "0") val isPingEnabled: Boolean = false,
val isPingEnabled: Boolean = false, @ColumnInfo(name = "is_wildcards_enabled", defaultValue = "0")
@ColumnInfo(name = "is_wildcards_enabled", defaultValue = "false")
val isWildcardsEnabled: Boolean = false, val isWildcardsEnabled: Boolean = false,
@ColumnInfo(name = "is_stop_on_no_internet_enabled", defaultValue = "false") @ColumnInfo(name = "is_stop_on_no_internet_enabled", defaultValue = "0")
val isStopOnNoInternetEnabled: Boolean = false, val isStopOnNoInternetEnabled: Boolean = false,
@ColumnInfo(name = "is_lan_on_kill_switch_enabled", defaultValue = "false") @ColumnInfo(name = "is_lan_on_kill_switch_enabled", defaultValue = "0")
val isLanOnKillSwitchEnabled: Boolean = false, val isLanOnKillSwitchEnabled: Boolean = false,
@ColumnInfo(name = "debounce_delay_seconds", defaultValue = "3") @ColumnInfo(name = "debounce_delay_seconds", defaultValue = "3")
val debounceDelaySeconds: Int = 3, val debounceDelaySeconds: Int = 3,
@ColumnInfo(name = "is_disable_kill_switch_on_trusted_enabled", defaultValue = "false") @ColumnInfo(name = "is_disable_kill_switch_on_trusted_enabled", defaultValue = "0")
val isDisableKillSwitchOnTrustedEnabled: Boolean = false, val isDisableKillSwitchOnTrustedEnabled: Boolean = false,
@ColumnInfo(name = "is_tunnel_on_unsecure_enabled", defaultValue = "false") @ColumnInfo(name = "is_tunnel_on_unsecure_enabled", defaultValue = "0")
val isTunnelOnUnsecureEnabled: Boolean = false, val isTunnelOnUnsecureEnabled: Boolean = false,
@ColumnInfo(name = "wifi_detection_method", defaultValue = "0") @ColumnInfo(name = "wifi_detection_method", defaultValue = "0")
val wifiDetectionMethod: WifiDetectionMethod = WifiDetectionMethod.fromValue(0), val wifiDetectionMethod: WifiDetectionMethod = WifiDetectionMethod.fromValue(0),
@ColumnInfo(name = "is_ping_monitoring_enabled", defaultValue = "true") @ColumnInfo(name = "is_ping_monitoring_enabled", defaultValue = "1")
val isPingMonitoringEnabled: Boolean = true, val isPingMonitoringEnabled: Boolean = true,
@ColumnInfo(name = "tunnel_ping_interval_sec", defaultValue = "30") @ColumnInfo(name = "tunnel_ping_interval_sec", defaultValue = "30")
val tunnelPingIntervalSeconds: Int = 30, val tunnelPingIntervalSeconds: Int = 30,
@@ -11,7 +11,7 @@ data class TunnelConfig(
@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(name = "tunnel_networks", defaultValue = "") @ColumnInfo(name = "tunnel_networks", defaultValue = "")
val tunnelNetworks: List<String> = listOf(), val tunnelNetworks: Set<String> = setOf(),
@ColumnInfo(name = "is_mobile_data_tunnel", defaultValue = "false") @ColumnInfo(name = "is_mobile_data_tunnel", defaultValue = "false")
val isMobileDataTunnel: Boolean = false, val isMobileDataTunnel: Boolean = false,
@ColumnInfo(name = "is_primary_tunnel", defaultValue = "false") @ColumnInfo(name = "is_primary_tunnel", defaultValue = "false")
@@ -27,7 +27,7 @@ data class TunnelConfig(
val isIpv4Preferred: Boolean = true, val isIpv4Preferred: Boolean = true,
@ColumnInfo(name = "position", defaultValue = "0") val position: Int = 0, @ColumnInfo(name = "position", defaultValue = "0") val position: Int = 0,
@ColumnInfo(name = "auto_tunnel_apps", defaultValue = "[]") @ColumnInfo(name = "auto_tunnel_apps", defaultValue = "[]")
val autoTunnelApps: List<String> = listOf(), val autoTunnelApps: Set<String> = setOf(),
) { ) {
companion object { companion object {
@@ -1,14 +1,13 @@
package com.zaneschepke.wireguardautotunnel.data.mapper package com.zaneschepke.wireguardautotunnel.data.mapper
import com.zaneschepke.networkmonitor.AndroidNetworkMonitor
import com.zaneschepke.wireguardautotunnel.data.entity.Settings import com.zaneschepke.wireguardautotunnel.data.entity.Settings
import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol
import com.zaneschepke.wireguardautotunnel.data.model.DnsSettings import com.zaneschepke.wireguardautotunnel.data.model.DnsSettings
import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
fun Settings.toAppSettings(): AppSettings { fun Settings.toAppSettings(): GeneralSettings {
return AppSettings( return GeneralSettings(
id = id, id = id,
isAutoTunnelEnabled = isAutoTunnelEnabled, isAutoTunnelEnabled = isAutoTunnelEnabled,
isTunnelOnMobileDataEnabled = isTunnelOnMobileDataEnabled, isTunnelOnMobileDataEnabled = isTunnelOnMobileDataEnabled,
@@ -26,8 +25,7 @@ fun Settings.toAppSettings(): AppSettings {
debounceDelaySeconds = debounceDelaySeconds, debounceDelaySeconds = debounceDelaySeconds,
isDisableKillSwitchOnTrustedEnabled = isDisableKillSwitchOnTrustedEnabled, isDisableKillSwitchOnTrustedEnabled = isDisableKillSwitchOnTrustedEnabled,
isTunnelOnUnsecureEnabled = isTunnelOnUnsecureEnabled, isTunnelOnUnsecureEnabled = isTunnelOnUnsecureEnabled,
wifiDetectionMethod = wifiDetectionMethod = WifiDetectionMethod.fromValue(wifiDetectionMethod.value),
AndroidNetworkMonitor.WifiDetectionMethod.fromValue(wifiDetectionMethod.value),
tunnelPingIntervalSeconds = tunnelPingIntervalSeconds, tunnelPingIntervalSeconds = tunnelPingIntervalSeconds,
tunnelPingAttempts = tunnelPingAttempts, tunnelPingAttempts = tunnelPingAttempts,
tunnelPingTimeoutSeconds = tunnelPingTimeoutSeconds, tunnelPingTimeoutSeconds = tunnelPingTimeoutSeconds,
@@ -37,7 +35,7 @@ fun Settings.toAppSettings(): AppSettings {
) )
} }
fun AppSettings.toSettings(): Settings { fun GeneralSettings.toSettings(): Settings {
return Settings( return Settings(
id = id, id = id,
isAutoTunnelEnabled = isAutoTunnelEnabled, isAutoTunnelEnabled = isAutoTunnelEnabled,
@@ -66,7 +64,7 @@ fun AppSettings.toSettings(): Settings {
) )
} }
fun AppSettings.toDomain(): DnsSettings { fun GeneralSettings.toDomain(): DnsSettings {
return DnsSettings( return DnsSettings(
protocol = protocol =
DnsProtocol.entries.toTypedArray().getOrElse(dnsProtocol.value) { DnsProtocol.SYSTEM }, DnsProtocol.entries.toTypedArray().getOrElse(dnsProtocol.value) { DnsProtocol.SYSTEM },
@@ -74,6 +72,6 @@ fun AppSettings.toDomain(): DnsSettings {
) )
} }
fun DnsSettings.toAppSettings(existing: AppSettings): AppSettings { fun DnsSettings.toAppSettings(existing: GeneralSettings): GeneralSettings {
return existing.copy(dnsProtocol = protocol, dnsEndpoint = endpoint) return existing.copy(dnsProtocol = protocol, dnsEndpoint = endpoint)
} }
@@ -6,6 +6,10 @@ enum class WifiDetectionMethod(val value: Int) {
ROOT(2), ROOT(2),
SHIZUKU(3); SHIZUKU(3);
fun needsLocationPermissions(): Boolean {
return this == LEGACY || this == DEFAULT
}
companion object { companion object {
fun fromValue(value: Int): WifiDetectionMethod = fun fromValue(value: Int): WifiDetectionMethod =
entries.find { it.value == value } ?: DEFAULT entries.find { it.value == value } ?: DEFAULT
@@ -7,7 +7,7 @@ import javax.inject.Inject
class AppDataRoomRepository class AppDataRoomRepository
@Inject @Inject
constructor( constructor(
override val settings: AppSettingRepository, override val settings: GeneralSettingRepository,
override val tunnels: TunnelRepository, override val tunnels: TunnelRepository,
override val appState: AppStateRepository, override val appState: AppStateRepository,
override val proxySettings: ProxySettingsRepository, override val proxySettings: ProxySettingsRepository,
@@ -3,15 +3,25 @@ package com.zaneschepke.wireguardautotunnel.data.repository
import com.zaneschepke.wireguardautotunnel.data.DataStoreManager import com.zaneschepke.wireguardautotunnel.data.DataStoreManager
import com.zaneschepke.wireguardautotunnel.data.entity.GeneralState import com.zaneschepke.wireguardautotunnel.data.entity.GeneralState
import com.zaneschepke.wireguardautotunnel.data.mapper.GeneralStateMapper import com.zaneschepke.wireguardautotunnel.data.mapper.GeneralStateMapper
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.model.AppState import com.zaneschepke.wireguardautotunnel.domain.model.AppState
import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppStateRepository
import com.zaneschepke.wireguardautotunnel.ui.theme.Theme import com.zaneschepke.wireguardautotunnel.ui.theme.Theme
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus
import timber.log.Timber import timber.log.Timber
class DataStoreAppStateRepository(private val dataStoreManager: DataStoreManager) : class DataStoreAppStateRepository(
AppStateRepository { private val dataStoreManager: DataStoreManager,
@ApplicationScope private val applicationScope: CoroutineScope,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : AppStateRepository {
override suspend fun isLocationDisclosureShown(): Boolean { override suspend fun isLocationDisclosureShown(): Boolean {
return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown) return dataStoreManager.getFromStore(DataStoreManager.locationDisclosureShown)
?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT ?: GeneralState.LOCATION_DISCLOSURE_SHOWN_DEFAULT
@@ -167,4 +177,9 @@ class DataStoreAppStateRepository(private val dataStoreManager: DataStoreManager
} ?: GeneralState() } ?: GeneralState()
} }
.map(GeneralStateMapper::toAppState) .map(GeneralStateMapper::toAppState)
.stateIn(
scope = applicationScope + ioDispatcher,
started = SharingStarted.Eagerly,
initialValue = AppState(),
)
} }
@@ -0,0 +1,92 @@
package com.zaneschepke.wireguardautotunnel.data.repository
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import com.zaneschepke.wireguardautotunnel.di.ApplicationScope
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.model.InstalledPackage
import com.zaneschepke.wireguardautotunnel.domain.repository.InstalledPackageRepository
import com.zaneschepke.wireguardautotunnel.util.extensions.getAllInternetCapablePackages
import com.zaneschepke.wireguardautotunnel.util.extensions.getFriendlyAppName
import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import timber.log.Timber
@Singleton
class InstalledAndroidPackageRepository(
private val context: Context,
@ApplicationScope val applicationScope: CoroutineScope,
@IoDispatcher private val ioDispatcher: CoroutineDispatcher,
) : InstalledPackageRepository {
private var cachedPackages: List<InstalledPackage>? = null
init {
val receiver =
object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
when (intent.action) {
Intent.ACTION_PACKAGE_ADDED,
Intent.ACTION_PACKAGE_REMOVED,
Intent.ACTION_PACKAGE_CHANGED -> {
// don't update if we have nothing cached
if (cachedPackages == null) return
Timber.d("Updating installed packages cache")
applicationScope.launch { refreshInstalledPackages() }
}
}
}
}
val filter =
IntentFilter().apply {
addAction(Intent.ACTION_PACKAGE_ADDED)
addAction(Intent.ACTION_PACKAGE_REMOVED)
addAction(Intent.ACTION_PACKAGE_CHANGED)
addDataScheme("package")
}
context.registerReceiver(receiver, filter)
}
override suspend fun getInstalledPackages(): List<InstalledPackage> =
withContext(ioDispatcher) {
cachedPackages?.let {
return@withContext it
}
refreshInstalledPackages()
}
override suspend fun refreshInstalledPackages(): List<InstalledPackage> =
withContext(ioDispatcher) {
val packages = context.getAllInternetCapablePackages()
val installedPackages =
packages.mapNotNull { packageInfo ->
try {
val appInfo =
context.packageManager.getApplicationInfo(packageInfo.packageName, 0)
InstalledPackage(
name =
context.packageManager.getFriendlyAppName(
packageInfo.packageName,
appInfo,
),
packageName = packageInfo.packageName,
uId = appInfo.uid,
)
} catch (e: PackageManager.NameNotFoundException) {
Timber.e(e)
null
}
}
cachedPackages = installedPackages
installedPackages
}
}
@@ -5,8 +5,8 @@ import com.zaneschepke.wireguardautotunnel.data.entity.Settings
import com.zaneschepke.wireguardautotunnel.data.mapper.toAppSettings import com.zaneschepke.wireguardautotunnel.data.mapper.toAppSettings
import com.zaneschepke.wireguardautotunnel.data.mapper.toSettings import com.zaneschepke.wireguardautotunnel.data.mapper.toSettings
import com.zaneschepke.wireguardautotunnel.di.IoDispatcher import com.zaneschepke.wireguardautotunnel.di.IoDispatcher
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@@ -15,16 +15,16 @@ 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 { ) : GeneralSettingRepository {
override suspend fun save(appSettings: AppSettings) { override suspend fun save(generalSettings: GeneralSettings) {
withContext(ioDispatcher) { settingsDoa.save(appSettings.toSettings()) } withContext(ioDispatcher) { settingsDoa.save(generalSettings.toSettings()) }
} }
override val flow = override val flow =
settingsDoa.getSettingsFlow().flowOn(ioDispatcher).map { it.toAppSettings() } settingsDoa.getSettingsFlow().flowOn(ioDispatcher).map { it.toAppSettings() }
override suspend fun get(): AppSettings { override suspend fun get(): GeneralSettings {
return withContext(ioDispatcher) { return withContext(ioDispatcher) {
(settingsDoa.getAll().firstOrNull() ?: Settings()).toAppSettings() (settingsDoa.getAll().firstOrNull() ?: Settings()).toAppSettings()
} }
@@ -46,6 +46,10 @@ class RoomTunnelRepository(
} }
} }
override suspend fun resetActiveTunnels() {
withContext(ioDispatcher) { tunnelConfigDao.resetActiveTunnels() }
}
override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) { override suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) {
withContext(ioDispatcher) { withContext(ioDispatcher) {
tunnelConfigDao.resetMobileDataTunnel() tunnelConfigDao.resetMobileDataTunnel()
@@ -105,4 +109,10 @@ class RoomTunnelRepository(
tunnelConfigDao.findByPrimary().map(TunnelConfigMapper::toTunnelConf) tunnelConfigDao.findByPrimary().map(TunnelConfigMapper::toTunnelConf)
} }
} }
override suspend fun delete(tunnels: List<TunnelConf>) {
withContext(ioDispatcher) {
tunnelConfigDao.delete(tunnels.map { TunnelConfigMapper.toTunnelConfig(it) })
}
}
} }
@@ -23,6 +23,7 @@ import kotlinx.coroutines.SupervisorJob
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class AppModule { class AppModule {
@Singleton @Singleton
@ApplicationScope @ApplicationScope
@Provides @Provides
@@ -2,14 +2,14 @@ package com.zaneschepke.wireguardautotunnel.di
import javax.inject.Qualifier import javax.inject.Qualifier
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class DefaultDispatcher @Retention(AnnotationRetention.BINARY) @Qualifier annotation class DefaultDispatcher
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class IoDispatcher @Retention(AnnotationRetention.BINARY) @Qualifier annotation class IoDispatcher
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class MainDispatcher @Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainDispatcher
@Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainImmediateDispatcher @Retention(AnnotationRetention.BINARY) @Qualifier annotation class MainImmediateDispatcher
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ApplicationScope @Retention(AnnotationRetention.BINARY) @Qualifier annotation class ApplicationScope
@Retention(AnnotationRetention.RUNTIME) @Qualifier annotation class ServiceScope @Retention(AnnotationRetention.BINARY) @Qualifier annotation class ServiceScope
@@ -22,10 +22,28 @@ import dagger.hilt.components.SingletonComponent
import io.ktor.client.* import io.ktor.client.*
import javax.inject.Singleton import javax.inject.Singleton
import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
@Module @Module
@InstallIn(SingletonComponent::class) @InstallIn(SingletonComponent::class)
class RepositoryModule { class RepositoryModule {
@Provides
@Singleton
fun provideGlobalEffectRepository(): GlobalEffectRepository {
return GlobalEffectRepository()
}
@Provides
@Singleton
fun provideInstalledPackageRepository(
@ApplicationContext context: Context,
@ApplicationScope applicationScope: CoroutineScope,
@IoDispatcher ioDispatcher: CoroutineDispatcher,
): InstalledPackageRepository {
return InstalledAndroidPackageRepository(context, applicationScope, ioDispatcher)
}
@Provides @Provides
@Singleton @Singleton
fun provideDatabase( fun provideDatabase(
@@ -74,7 +92,7 @@ class RepositoryModule {
fun provideSettingsRepository( fun provideSettingsRepository(
settingsDao: SettingsDao, settingsDao: SettingsDao,
@IoDispatcher ioDispatcher: CoroutineDispatcher, @IoDispatcher ioDispatcher: CoroutineDispatcher,
): AppSettingRepository { ): GeneralSettingRepository {
return RoomSettingsRepository(settingsDao, ioDispatcher) return RoomSettingsRepository(settingsDao, ioDispatcher)
} }
@@ -98,14 +116,18 @@ class RepositoryModule {
@Provides @Provides
@Singleton @Singleton
fun provideGeneralStateRepository(dataStoreManager: DataStoreManager): AppStateRepository { fun provideGeneralStateRepository(
return DataStoreAppStateRepository(dataStoreManager) dataStoreManager: DataStoreManager,
@ApplicationScope applicationScope: CoroutineScope,
@IoDispatcher ioDispatcher: CoroutineDispatcher,
): AppStateRepository {
return DataStoreAppStateRepository(dataStoreManager, applicationScope, ioDispatcher)
} }
@Provides @Provides
@Singleton @Singleton
fun provideAppDataRepository( fun provideAppDataRepository(
settingsRepository: AppSettingRepository, settingsRepository: GeneralSettingRepository,
tunnelRepository: TunnelRepository, tunnelRepository: TunnelRepository,
appStateRepository: AppStateRepository, appStateRepository: AppStateRepository,
proxySettingsRepository: ProxySettingsRepository, proxySettingsRepository: ProxySettingsRepository,
@@ -10,7 +10,8 @@ import com.zaneschepke.networkmonitor.NetworkMonitor
import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager import com.zaneschepke.wireguardautotunnel.core.service.ServiceManager
import com.zaneschepke.wireguardautotunnel.core.tunnel.* import com.zaneschepke.wireguardautotunnel.core.tunnel.*
import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository import com.zaneschepke.wireguardautotunnel.domain.repository.AppDataRepository
import com.zaneschepke.wireguardautotunnel.domain.repository.AppSettingRepository import com.zaneschepke.wireguardautotunnel.domain.repository.GeneralSettingRepository
import com.zaneschepke.wireguardautotunnel.util.extensions.to
import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils import com.zaneschepke.wireguardautotunnel.util.network.NetworkUtils
import dagger.Module import dagger.Module
import dagger.Provides import dagger.Provides
@@ -140,7 +141,7 @@ class TunnelModule {
@Singleton @Singleton
fun provideNetworkMonitor( fun provideNetworkMonitor(
@ApplicationContext context: Context, @ApplicationContext context: Context,
settingsRepository: AppSettingRepository, settingsRepository: GeneralSettingRepository,
@ApplicationScope applicationScope: CoroutineScope, @ApplicationScope applicationScope: CoroutineScope,
@AppShell appShell: RootShell, @AppShell appShell: RootShell,
): NetworkMonitor { ): NetworkMonitor {
@@ -151,7 +152,7 @@ class TunnelModule {
get() = get() =
settingsRepository.flow settingsRepository.flow
.distinctUntilChangedBy { it.wifiDetectionMethod } .distinctUntilChangedBy { it.wifiDetectionMethod }
.map { it.wifiDetectionMethod } .map { it.wifiDetectionMethod.to() }
override val rootShell: RootShell override val rootShell: RootShell
get() = appShell get() = appShell
@@ -6,18 +6,10 @@ sealed class TunnelStatus {
data object Down : TunnelStatus() data object Down : TunnelStatus()
data class Stopping(val reason: StopReason) : TunnelStatus() data object Stopping : TunnelStatus()
data object Starting : TunnelStatus() data object Starting : TunnelStatus()
sealed class StopReason {
data object User : StopReason()
data class Ping(val previouslyResolvedEndpoints: Map<String, String?>) : StopReason()
data object ConfigChanged : StopReason()
}
fun isDown(): Boolean { fun isDown(): Boolean {
return this == Down return this == Down
} }
@@ -5,9 +5,6 @@ import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
sealed class AutoTunnelEvent { sealed class AutoTunnelEvent {
data class Start(val tunnelConf: TunnelConf? = null) : AutoTunnelEvent() data class Start(val tunnelConf: TunnelConf? = null) : AutoTunnelEvent()
data class Bounce(val configsPeerKeyResolvedMap: List<Pair<TunnelConf, Map<String, String?>>>) :
AutoTunnelEvent()
data object Stop : AutoTunnelEvent() data object Stop : AutoTunnelEvent()
data object DoNothing : AutoTunnelEvent() data object DoNothing : AutoTunnelEvent()
@@ -3,24 +3,26 @@ package com.zaneschepke.wireguardautotunnel.domain.events
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.util.StringValue import com.zaneschepke.wireguardautotunnel.util.StringValue
sealed class BackendError : Exception() { sealed class BackendCoreException : Exception() {
data object DNS : BackendError() data object DNS : BackendCoreException()
data object Unauthorized : BackendError() data object Unauthorized : BackendCoreException()
data object Config : BackendError() data object Config : BackendCoreException()
data object KernelModuleName : BackendError() data object KernelModuleName : BackendCoreException()
data object NotAuthorized : BackendError() data object NotAuthorized : BackendCoreException()
data object ServiceNotRunning : BackendError() data object ServiceNotRunning : BackendCoreException()
data object Unknown : BackendError() data object Unknown : BackendCoreException()
data object TunnelNameTooLong : BackendError() data object TunnelNameTooLong : BackendCoreException()
data class BounceFailed(val error: BackendError) : BackendError() data object UapiUpdateFailed : BackendCoreException()
data class BounceFailed(val error: BackendCoreException) : BackendCoreException()
fun toStringRes() = fun toStringRes() =
when (this) { when (this) {
@@ -33,6 +35,7 @@ sealed class BackendError : Exception() {
Unknown -> R.string.unknown_error Unknown -> R.string.unknown_error
TunnelNameTooLong -> R.string.error_tunnel_name TunnelNameTooLong -> R.string.error_tunnel_name
is BounceFailed -> R.string.bounce_failed_template is BounceFailed -> R.string.bounce_failed_template
UapiUpdateFailed -> R.string.active_tunnel_update_failed
} }
fun toStringValue(): StringValue { fun toStringValue(): StringValue {
@@ -3,14 +3,14 @@ package com.zaneschepke.wireguardautotunnel.domain.model
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 = false,
val isBatteryOptimizationDisableShown: Boolean, val isBatteryOptimizationDisableShown: Boolean = false,
val isPinLockEnabled: Boolean, val isPinLockEnabled: Boolean = false,
val expandedTunnelIds: List<Int>, val expandedTunnelIds: List<Int> = emptyList(),
val isLocalLogsEnabled: Boolean, val isLocalLogsEnabled: Boolean = false,
val isRemoteControlEnabled: Boolean, val isRemoteControlEnabled: Boolean = false,
val showDetailedPingStats: Boolean, val showDetailedPingStats: Boolean = false,
val remoteKey: String?, val remoteKey: String? = null,
val locale: String?, val locale: String? = null,
val theme: Theme, val theme: Theme = Theme.AUTOMATIC,
) )
@@ -1,14 +1,14 @@
package com.zaneschepke.wireguardautotunnel.domain.model package com.zaneschepke.wireguardautotunnel.domain.model
import com.zaneschepke.networkmonitor.AndroidNetworkMonitor
import com.zaneschepke.wireguardautotunnel.data.model.AppMode import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol import com.zaneschepke.wireguardautotunnel.data.model.DnsProtocol
import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod
data class AppSettings( data class GeneralSettings(
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: Set<String> = emptySet(),
val isAlwaysOnVpnEnabled: Boolean = false, val isAlwaysOnVpnEnabled: Boolean = false,
val isTunnelOnEthernetEnabled: Boolean = false, val isTunnelOnEthernetEnabled: Boolean = false,
val isShortcutsEnabled: Boolean = false, val isShortcutsEnabled: Boolean = false,
@@ -24,10 +24,9 @@ data class AppSettings(
val debounceDelaySeconds: Int = 3, val debounceDelaySeconds: Int = 3,
val isDisableKillSwitchOnTrustedEnabled: Boolean = false, val isDisableKillSwitchOnTrustedEnabled: Boolean = false,
val isTunnelOnUnsecureEnabled: Boolean = false, val isTunnelOnUnsecureEnabled: Boolean = false,
val wifiDetectionMethod: AndroidNetworkMonitor.WifiDetectionMethod = val wifiDetectionMethod: WifiDetectionMethod = WifiDetectionMethod.DEFAULT,
AndroidNetworkMonitor.WifiDetectionMethod.DEFAULT, val tunnelPingIntervalSeconds: Int = PING_INTERVAL_DEFAULT,
val tunnelPingIntervalSeconds: Int = 30, val tunnelPingAttempts: Int = PING_ATTEMPTS_DEFAULT,
val tunnelPingAttempts: Int = 3,
val tunnelPingTimeoutSeconds: Int? = null, val tunnelPingTimeoutSeconds: Int? = null,
val appMode: AppMode = AppMode.VPN, val appMode: AppMode = AppMode.VPN,
val dnsProtocol: DnsProtocol = DnsProtocol.SYSTEM, val dnsProtocol: DnsProtocol = DnsProtocol.SYSTEM,
@@ -44,4 +43,9 @@ data class AppSettings(
""" """
.trimIndent() .trimIndent()
} }
companion object {
const val PING_INTERVAL_DEFAULT = 30
const val PING_ATTEMPTS_DEFAULT = 3
}
} }
@@ -0,0 +1,3 @@
package com.zaneschepke.wireguardautotunnel.domain.model
data class InstalledPackage(val name: String, val packageName: String, val uId: Int)
@@ -1,8 +1,9 @@
package com.zaneschepke.wireguardautotunnel.domain.model package com.zaneschepke.wireguardautotunnel.domain.model
import com.wireguard.android.backend.Tunnel
import com.wireguard.config.Config import com.wireguard.config.Config
import com.zaneschepke.wireguardautotunnel.util.extensions.* import com.zaneschepke.wireguardautotunnel.util.extensions.defaultName
import com.zaneschepke.wireguardautotunnel.util.extensions.isValidIpv4orIpv6Address
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
import java.io.InputStream import java.io.InputStream
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@@ -10,7 +11,7 @@ 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: Set<String> = emptySet(),
val isMobileDataTunnel: Boolean = false, val isMobileDataTunnel: Boolean = false,
val isPrimaryTunnel: Boolean = false, val isPrimaryTunnel: Boolean = false,
val amQuick: String, val amQuick: String,
@@ -20,14 +21,9 @@ data class TunnelConf(
val isEthernetTunnel: Boolean = false, val isEthernetTunnel: Boolean = false,
val isIpv4Preferred: Boolean = true, val isIpv4Preferred: Boolean = true,
val position: Int = 0, val position: Int = 0,
@Transient private var stateChangeCallback: ((Any) -> Unit)? = null, ) {
) : Tunnel, org.amnezia.awg.backend.Tunnel {
val isNameKernelCompatible: Boolean = (name.length <= 15) val isNameKernelCompatible: Boolean = (tunName.length <= 15)
fun setStateChangeCallback(callback: (Any) -> Unit) {
stateChangeCallback = callback
}
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (this === other) return true if (this === other) return true
@@ -57,38 +53,6 @@ data class TunnelConf(
return toAmConfig().peers.all { it.endpoint.get().host.isValidIpv4orIpv6Address() } return toAmConfig().peers.all { it.endpoint.get().host.isValidIpv4orIpv6Address() }
} }
fun copyWithCallback(
id: Int = this.id,
tunName: String = this.tunName,
wgQuick: String = this.wgQuick,
tunnelNetworks: List<String> = this.tunnelNetworks,
isMobileDataTunnel: Boolean = this.isMobileDataTunnel,
isPrimaryTunnel: Boolean = this.isPrimaryTunnel,
amQuick: String = this.amQuick,
isActive: Boolean = this.isActive,
restartOnPingFailure: Boolean = this.restartOnPingFailure,
pingIp: String? = this.pingTarget,
isEthernetTunnel: Boolean = this.isEthernetTunnel,
isIpv4Preferred: Boolean = this.isIpv4Preferred,
): TunnelConf {
return TunnelConf(
id,
tunName,
wgQuick,
tunnelNetworks,
isMobileDataTunnel,
isPrimaryTunnel,
amQuick,
isActive,
pingIp,
restartOnPingFailure,
isEthernetTunnel,
isIpv4Preferred,
position,
)
.apply { stateChangeCallback = this@TunnelConf.stateChangeCallback }
}
fun toAmConfig(): org.amnezia.awg.config.Config { fun toAmConfig(): org.amnezia.awg.config.Config {
return configFromAmQuick(amQuick.ifBlank { wgQuick }) return configFromAmQuick(amQuick.ifBlank { wgQuick })
} }
@@ -97,36 +61,6 @@ data class TunnelConf(
return configFromWgQuick(wgQuick) return configFromWgQuick(wgQuick)
} }
override fun getName(): String = tunName
override fun onStateChange(newState: org.amnezia.awg.backend.Tunnel.State) {
stateChangeCallback?.invoke(newState)
}
override fun onStateChange(newState: Tunnel.State) {
stateChangeCallback?.invoke(newState)
}
override fun isIpv4ResolutionPreferred(): Boolean {
return true
}
fun generateUniqueName(tunnelNames: List<String>): String {
var tunnelName = this.tunName
var num = 1
while (tunnelNames.any { it == tunnelName }) {
tunnelName =
if (!tunnelName.hasNumberInParentheses()) {
"$name($num)"
} else {
val pair = tunnelName.extractNameAndNumber()
"${pair?.first}($num)"
}
num++
}
return tunnelName
}
companion object { companion object {
fun configFromWgQuick(wgQuick: String): Config { fun configFromWgQuick(wgQuick: String): Config {
val inputStream: InputStream = wgQuick.byteInputStream() val inputStream: InputStream = wgQuick.byteInputStream()
@@ -140,7 +74,17 @@ data class TunnelConf(
} }
} }
fun tunnelConfigFromAmConfig( fun tunnelConfFromQuick(amQuick: String, name: String? = null): TunnelConf {
val config = configFromAmQuick(amQuick)
val wgQuick = config.toWgQuickString()
return TunnelConf(
tunName = name ?: config.defaultName(),
wgQuick = wgQuick,
amQuick = amQuick,
)
}
private fun tunnelConfFromAmConfig(
config: org.amnezia.awg.config.Config, config: org.amnezia.awg.config.Config,
name: String? = null, name: String? = null,
): TunnelConf { ): TunnelConf {
@@ -7,7 +7,7 @@ interface AppDataRepository {
suspend fun getStartTunnelConfig(): TunnelConf? suspend fun getStartTunnelConfig(): TunnelConf?
val settings: AppSettingRepository val settings: GeneralSettingRepository
val tunnels: TunnelRepository val tunnels: TunnelRepository
val appState: AppStateRepository val appState: AppStateRepository
@@ -1,12 +0,0 @@
package com.zaneschepke.wireguardautotunnel.domain.repository
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings
import kotlinx.coroutines.flow.Flow
interface AppSettingRepository {
suspend fun save(appSettings: AppSettings)
val flow: Flow<AppSettings>
suspend fun get(): AppSettings
}
@@ -0,0 +1,18 @@
package com.zaneschepke.wireguardautotunnel.domain.repository
import com.zaneschepke.wireguardautotunnel.domain.sideeffect.GlobalSideEffect
import javax.inject.Singleton
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
@Singleton
class GlobalEffectRepository {
private val _globalEffectFlow =
MutableSharedFlow<GlobalSideEffect>(replay = 0, extraBufferCapacity = 1)
val flow = _globalEffectFlow.asSharedFlow()
suspend fun post(effect: GlobalSideEffect) {
_globalEffectFlow.emit(effect)
}
}
@@ -0,0 +1,12 @@
package com.zaneschepke.wireguardautotunnel.domain.repository
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import kotlinx.coroutines.flow.Flow
interface GeneralSettingRepository {
suspend fun save(generalSettings: GeneralSettings)
val flow: Flow<GeneralSettings>
suspend fun get(): GeneralSettings
}
@@ -0,0 +1,12 @@
package com.zaneschepke.wireguardautotunnel.domain.repository
import com.zaneschepke.wireguardautotunnel.domain.model.InstalledPackage
interface InstalledPackageRepository {
// gets packages from cache or queries and updates cache if empty
suspend fun getInstalledPackages(): List<InstalledPackage>
// updates the cache and returns the results
suspend fun refreshInstalledPackages(): List<InstalledPackage>
}
@@ -15,6 +15,8 @@ interface TunnelRepository {
suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?) suspend fun updatePrimaryTunnel(tunnelConf: TunnelConf?)
suspend fun resetActiveTunnels()
suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?) suspend fun updateMobileDataTunnel(tunnelConf: TunnelConf?)
suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?) suspend fun updateEthernetTunnel(tunnelConf: TunnelConf?)
@@ -34,4 +36,6 @@ interface TunnelRepository {
suspend fun findByMobileDataTunnel(): Tunnels suspend fun findByMobileDataTunnel(): Tunnels
suspend fun findPrimary(): Tunnels suspend fun findPrimary(): Tunnels
suspend fun delete(tunnels: List<TunnelConf>)
} }
@@ -0,0 +1,27 @@
package com.zaneschepke.wireguardautotunnel.domain.sideeffect
import com.zaneschepke.wireguardautotunnel.data.model.AppMode
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.util.StringValue
import java.io.File
sealed class GlobalSideEffect {
data object RequestBatteryOptimizationDisabled : GlobalSideEffect()
data class Snackbar(val message: StringValue) : GlobalSideEffect()
data class Toast(val message: StringValue) : GlobalSideEffect()
data object PopBackStack : GlobalSideEffect()
data class ShareFile(val file: File) : GlobalSideEffect()
data class LaunchUrl(val url: String) : GlobalSideEffect()
data object ConfigChanged : GlobalSideEffect()
data class RequestVpnPermission(val requestingMode: AppMode, val config: TunnelConf?) :
GlobalSideEffect()
data class InstallApk(val apk: File) : GlobalSideEffect()
}
@@ -2,20 +2,21 @@ package com.zaneschepke.wireguardautotunnel.domain.state
import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.StateChange import com.zaneschepke.wireguardautotunnel.core.service.autotunnel.StateChange
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent
import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.* import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.DoNothing
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings import com.zaneschepke.wireguardautotunnel.domain.events.AutoTunnelEvent.Start
import com.zaneschepke.wireguardautotunnel.domain.model.GeneralSettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
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<Int, TunnelState> = emptyMap(),
val networkState: NetworkState = NetworkState(), val networkState: NetworkState = NetworkState(),
val settings: AppSettings = AppSettings(), val settings: GeneralSettings = GeneralSettings(),
val tunnels: List<TunnelConf> = emptyList(), val tunnels: List<TunnelConf> = emptyList(),
) { ) {
fun determineAutoTunnelEvent(stateChange: StateChange): AutoTunnelEvent { fun determineAutoTunnelEvent(stateChange: StateChange): AutoTunnelEvent {
when (val change = stateChange) { when (stateChange) {
is StateChange.NetworkChange, is StateChange.NetworkChange,
is StateChange.SettingsChange -> { is StateChange.SettingsChange -> {
// Compute desired tunnel based on network conditions // Compute desired tunnel based on network conditions
@@ -40,7 +41,7 @@ data class AutoTunnelState(
// Handle tunnel start/stop/change // Handle tunnel start/stop/change
if (desiredTunnel != null) { if (desiredTunnel != null) {
if (currentTunnel != desiredTunnel) { if (currentTunnel != desiredTunnel.id) {
// Start or switch to the desired tunnel (overrides any kill switch) // Start or switch to the desired tunnel (overrides any kill switch)
return Start(desiredTunnel) return Start(desiredTunnel)
} }
@@ -54,12 +55,6 @@ data class AutoTunnelState(
} }
} }
} }
is StateChange.MonitoringChange -> {
val bounceTunnels = bounceOnPingFailed()
if (bounceTunnels.isNotEmpty()) {
return Bounce(bounceTunnels)
}
}
is StateChange.ActiveTunnelsChange -> Unit is StateChange.ActiveTunnelsChange -> Unit
} }
@@ -96,48 +91,19 @@ data class AutoTunnelState(
return !networkState.isEthernetConnected && networkState.isWifiConnected return !networkState.isEthernetConnected && networkState.isWifiConnected
} }
private fun stopKillSwitchOnTrusted(): Boolean {
return networkState.isWifiConnected &&
settings.isVpnKillSwitchEnabled &&
settings.isDisableKillSwitchOnTrustedEnabled &&
isCurrentSSIDTrusted()
}
private fun startKillSwitch(): Boolean {
return settings.isVpnKillSwitchEnabled &&
(!settings.isDisableKillSwitchOnTrustedEnabled || !isCurrentSSIDTrusted())
}
private fun isNoConnectivity(): Boolean { private fun isNoConnectivity(): Boolean {
return !networkState.isEthernetConnected && return !networkState.isEthernetConnected &&
!networkState.isWifiConnected && !networkState.isWifiConnected &&
!networkState.isMobileDataConnected !networkState.isMobileDataConnected
} }
private fun bounceOnPingFailed(): List<Pair<TunnelConf, Map<String, String?>>> {
return activeTunnels.entries
.filter { (tunnel, state) ->
tunnel.restartOnPingFailure &&
(state.pingStates?.any { (key, pingState) ->
pingState.failureReason == FailureReason.PingFailed
} ?: false)
}
.map { (tunnel, state) ->
val peerMap =
(state.statistics?.getPeers()?.associate { peerKey ->
peerKey.toBase64() to state.statistics.peerStats(peerKey)?.resolvedEndpoint
} ?: emptyMap())
Pair(tunnel, peerMap)
}
}
private fun isCurrentSSIDTrusted(): Boolean { private fun isCurrentSSIDTrusted(): Boolean {
return networkState.wifiName?.let { hasTrustedWifiName(it) } == true return networkState.wifiName?.let { hasTrustedWifiName(it) } == true
} }
private fun hasTrustedWifiName( private fun hasTrustedWifiName(
wifiName: String, wifiName: String,
wifiNames: List<String> = settings.trustedNetworkSSIDs, wifiNames: Set<String> = settings.trustedNetworkSSIDs,
): Boolean { ): Boolean {
return if (settings.isWildcardsEnabled) { return if (settings.isWildcardsEnabled) {
wifiNames.isMatchingToWildcardList(wifiName) wifiNames.isMatchingToWildcardList(wifiName)
@@ -0,0 +1,3 @@
package com.zaneschepke.wireguardautotunnel.domain.state
data class LogHealthState(val isHealthy: Boolean, val timestamp: Long = System.currentTimeMillis())
@@ -9,5 +9,51 @@ data class TunnelState(
val backendState: BackendMode = BackendMode.Inactive, val backendState: BackendMode = BackendMode.Inactive,
val statistics: TunnelStatistics? = null, val statistics: TunnelStatistics? = null,
val pingStates: Map<Key, PingState>? = null, val pingStates: Map<Key, PingState>? = null,
val handshakeSuccessLogs: Boolean? = null, val logHealthState: LogHealthState? = null,
) ) {
fun health(): Health {
val now = System.currentTimeMillis()
if (pingStates == null && logHealthState == null && statistics == null)
return Health.UNKNOWN
if (logHealthState?.isHealthy == false) return Health.UNHEALTHY
val healthLogs =
logHealthState?.isHealthy == true &&
(now - logHealthState.timestamp) <= LOG_HEALTH_SUCCESS_TIMEOUT_MS
if (pingStates?.any { !it.value.isReachable } == true) return Health.UNHEALTHY
if (statistics != null) {
if (statistics.isTunnelStale()) {
return Health.STALE
}
if ((logHealthState == null || !healthLogs) && pingStates == null) {
return Health.HEALTHY
}
} else {
if (!healthLogs) {
return Health.UNKNOWN
}
}
if (healthLogs) {
return Health.HEALTHY
}
return Health.UNKNOWN
}
enum class Health {
UNKNOWN,
UNHEALTHY,
HEALTHY,
STALE,
}
companion object {
const val LOG_HEALTH_SUCCESS_TIMEOUT_MS = 2 * 60 * 1000L // 2 minutes
}
}
@@ -0,0 +1,13 @@
package com.zaneschepke.wireguardautotunnel.ui
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.navigation.NavHostController
import com.zaneschepke.wireguardautotunnel.viewmodel.SharedAppViewModel
val LocalNavController =
compositionLocalOf<NavHostController> { error("NavController was not provided") }
val LocalIsAndroidTV = staticCompositionLocalOf { false }
val LocalSharedVm = staticCompositionLocalOf<SharedAppViewModel> { error("No SharedVm") }
@@ -1,54 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui
import kotlinx.serialization.Serializable
sealed class Route {
@Serializable data object Support : Route()
@Serializable data object Settings : Route()
@Serializable data object AutoTunnel : Route()
@Serializable data object AutoTunnelAdvanced : Route()
@Serializable data object WifiDetectionMethod : Route()
@Serializable data object LocationDisclosure : Route()
@Serializable data object Appearance : Route()
@Serializable data object Display : Route()
@Serializable data object Language : Route()
@Serializable data object Main : Route()
@Serializable data class TunnelOptions(val id: Int) : Route()
@Serializable data object Lock : Route()
@Serializable data object License : Route()
@Serializable data class Config(val id: Int) : Route()
@Serializable
data class SplitTunnel(val id: Int) : Route() {
companion object {
const val KEY_ID = "id"
}
}
@Serializable data class TunnelAutoTunnel(val id: Int) : Route()
@Serializable data object Logs : Route()
@Serializable data object Sort : Route()
@Serializable data object TunnelMonitoring : Route()
@Serializable data object SystemFeatures : Route()
@Serializable data object ProxySettings : Route()
@Serializable data object Dns : Route()
}
@@ -22,7 +22,7 @@ fun ExpandingRowListItem(
text: String, text: String,
trailing: @Composable () -> Unit, trailing: @Composable () -> Unit,
isSelected: Boolean, isSelected: Boolean,
expanded: @Composable () -> Unit, expanded: (@Composable () -> Unit),
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
Box( Box(
@@ -0,0 +1,21 @@
package com.zaneschepke.wireguardautotunnel.ui.common.button
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
@Composable
fun ActionIconButton(icon: ImageVector, labelRes: Int, onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
icon,
contentDescription = stringResource(labelRes),
modifier = Modifier.size(iconSize),
)
}
}
@@ -12,6 +12,8 @@ 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.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
@Composable @Composable
fun IconSurfaceButton( fun IconSurfaceButton(
@@ -55,7 +57,7 @@ fun IconSurfaceButton(
) { ) {
leading?.invoke() leading?.invoke()
Column(verticalArrangement = Arrangement.spacedBy(4.dp)) { Column(verticalArrangement = Arrangement.spacedBy(4.dp)) {
Text(title, style = MaterialTheme.typography.titleMedium) SelectionItemLabel(title, SelectionLabelType.TITLE)
description?.let { description?.let {
Text( Text(
description, description,
@@ -0,0 +1,19 @@
package com.zaneschepke.wireguardautotunnel.ui.common.button
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.Launch
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
@Composable
fun LaunchButton(modifier: Modifier = Modifier.focusable(), onClick: () -> Unit) {
IconButton(modifier = modifier, onClick = onClick) {
val icon = Icons.AutoMirrored.Outlined.Launch
Icon(icon, icon.name, Modifier.size(iconSize))
}
}
@@ -4,24 +4,28 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.* import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ripple
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
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.draw.clip
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
@Composable @Composable
fun SelectionItemButton( fun SelectionItemButton(
leading: (@Composable () -> Unit)? = null,
buttonText: String, buttonText: String,
trailing: (@Composable () -> Unit)? = null, description: String? = null,
onClick: () -> Unit, onClick: () -> Unit,
ripple: Boolean = true,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
leading: (@Composable () -> Unit)? = null,
trailing: (@Composable () -> Unit)? = null,
ripple: Boolean = true,
) { ) {
Card( Card(
modifier = modifier =
@@ -32,24 +36,38 @@ fun SelectionItemButton(
interactionSource = remember { MutableInteractionSource() }, interactionSource = remember { MutableInteractionSource() },
onClick = { onClick() }, onClick = { onClick() },
) )
.height(56.dp), .height(IntrinsicSize.Min)
.padding(horizontal = 12.dp)
.padding(end = 12.dp),
colors = CardDefaults.cardColors(containerColor = Color.Transparent), colors = CardDefaults.cardColors(containerColor = Color.Transparent),
) { ) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxSize().padding(horizontal = 12.dp), modifier = Modifier.fillMaxSize().padding(horizontal = 12.dp),
) { ) {
Row { Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(24.dp),
) {
leading?.let { it() } leading?.let { it() }
Text( Column(
buttonText, horizontalAlignment = Alignment.Start,
style = MaterialTheme.typography.labelMedium, modifier = Modifier.weight(1f).padding(end = 12.dp),
color = MaterialTheme.colorScheme.onSurface, verticalArrangement = Arrangement.Center,
modifier = Modifier.fillMaxWidth(3 / 4f), ) {
maxLines = 2, SelectionItemLabel(
overflow = TextOverflow.Ellipsis, buttonText,
) SelectionLabelType.TITLE,
modifier = Modifier.weight(1f).padding(end = 24.dp),
)
description?.let {
SelectionItemLabel(
it,
SelectionLabelType.DESCRIPTION,
modifier = Modifier.weight(1f).padding(end = 24.dp),
)
}
}
} }
trailing?.let { it() } trailing?.let { it() }
} }
@@ -3,6 +3,7 @@ package com.zaneschepke.wireguardautotunnel.ui.common.button.surface
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
data class SelectionItem( data class SelectionItem(
@@ -11,5 +12,9 @@ data class SelectionItem(
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 modifier: Modifier = Modifier.height(64.dp), val isEnabled: Boolean = true,
val disabledReason: String? = null,
val modifier: Modifier = Modifier.height(48.dp),
val padding: Dp = if (description == null && disabledReason == null) 16.dp else 6.dp,
val onLongPress: (() -> Unit)? = null,
) )
@@ -22,6 +22,7 @@ fun SelectionItemLabel(text: String, labelType: SelectionLabelType, modifier: Mo
} }
enum class SelectionLabelType { enum class SelectionLabelType {
DESCRIPTION, DESCRIPTION,
TITLE, TITLE,
} }
@@ -1,65 +1,113 @@
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.combinedClickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
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.MaterialTheme import androidx.compose.material3.MaterialTheme
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.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.semantics.stateDescription
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.R
@Composable @Composable
fun SurfaceSelectionGroupButton(items: List<SelectionItem>, modifier: Modifier = Modifier) { fun SurfaceSelectionGroupButton(items: List<SelectionItem>, modifier: Modifier = Modifier) {
if (items.isEmpty()) return
val context = LocalContext.current
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.map { item -> Column(
Box( verticalArrangement = Arrangement.spacedBy(0.dp),
contentAlignment = Alignment.Center, modifier = Modifier.fillMaxWidth(),
modifier = ) {
modifier items.forEach { item ->
.fillMaxWidth() Box(
.clip(RoundedCornerShape(8.dp)) contentAlignment = Alignment.Center,
.then(item.onClick?.let { modifier.clickable { it() } } ?: modifier), modifier =
) { Modifier.fillMaxWidth()
Row( .clip(RoundedCornerShape(8.dp))
verticalAlignment = Alignment.CenterVertically, .alpha(if (item.isEnabled) 1f else 0.6f)
modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp), .semantics {
if (!item.isEnabled) {
stateDescription =
item.disabledReason ?: context.getString(R.string.disabled)
}
}
.combinedClickable(
onClick = { item.onClick?.invoke() },
onLongClick = { item.onLongPress?.invoke() },
enabled = true,
),
) { ) {
Row( Row(
verticalAlignment = Alignment.CenterVertically, verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.weight(4f, false).fillMaxWidth(), modifier = Modifier.fillMaxWidth().padding(horizontal = 12.dp),
) { ) {
item.leading?.invoke() Row(
Column( verticalAlignment = Alignment.CenterVertically,
horizontalAlignment = Alignment.Start,
verticalArrangement =
Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
modifier = modifier =
Modifier.fillMaxWidth() Modifier.weight(4f, false)
.padding(start = if (item.leading != null) 16.dp else 0.dp) .fillMaxWidth()
.weight(1f) .padding(vertical = item.padding),
.padding(
vertical = if (item.description == null) 16.dp else 6.dp
),
) { ) {
item.title() item.leading?.let {
item.description?.let { it() } Box(modifier = Modifier.alpha(if (item.isEnabled) 1f else 0.6f)) {
it()
}
}
Column(
horizontalAlignment = Alignment.Start,
verticalArrangement =
Arrangement.spacedBy(2.dp, Alignment.CenterVertically),
modifier =
Modifier.fillMaxWidth()
.padding(start = if (item.leading != null) 16.dp else 0.dp)
.weight(1f),
) {
Box(modifier = Modifier.alpha(if (item.isEnabled) 1f else 0.6f)) {
item.title()
}
item.description?.invoke()
if (!item.isEnabled && item.disabledReason != null) {
Text(
text = item.disabledReason,
style = MaterialTheme.typography.bodySmall,
color =
MaterialTheme.colorScheme.onSurface.copy(alpha = 0.6f),
)
}
}
} }
} item.trailing?.let {
item.trailing?.let { Box(
Box( contentAlignment = Alignment.CenterEnd,
contentAlignment = Alignment.CenterEnd, modifier =
modifier = Modifier.padding(start = 16.dp), Modifier.padding(start = 16.dp)
) { .alpha(if (item.isEnabled) 1f else 0.6f)
it() .run {
if (!item.isEnabled) {
semantics {
stateDescription =
context.getString(R.string.disabled)
}
} else {
this
}
},
) {
it()
}
} }
} }
} }
@@ -9,8 +9,8 @@ import androidx.activity.compose.ManagedActivityResultLauncher
import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalIsAndroidTV import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
import com.zaneschepke.wireguardautotunnel.util.Constants import com.zaneschepke.wireguardautotunnel.util.FileUtils
import timber.log.Timber import timber.log.Timber
@Composable @Composable
@@ -26,9 +26,9 @@ fun rememberFileImportLauncherForResult(
super.createIntent(context, input).apply { super.createIntent(context, input).apply {
type = type =
if (isTv) { if (isTv) {
Constants.ALLOWED_TV_FILE_TYPES FileUtils.ALLOWED_TV_FILE_TYPES
} else { } else {
Constants.ALL_FILE_TYPES FileUtils.ALL_FILE_TYPES
} }
} }
@@ -51,8 +51,8 @@ fun rememberFileImportLauncherForResult(
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(FileUtils.GOOGLE_TV_EXPLORER_STUB) ||
name.startsWith(Constants.ANDROID_TV_EXPLORER_STUB) name.startsWith(FileUtils.ANDROID_TV_EXPLORER_STUB)
} }
) { ) {
onNoFileExplorer() onNoFileExplorer()
@@ -68,7 +68,7 @@ fun rememberFileImportLauncherForResult(
@Composable @Composable
fun rememberFileExportLauncherForResult( fun rememberFileExportLauncherForResult(
mimeType: String = Constants.ZIP_FILE_MIME_TYPE, mimeType: String = FileUtils.ZIP_FILE_MIME_TYPE,
onResult: (Uri?) -> Unit, onResult: (Uri?) -> Unit,
): ManagedActivityResultLauncher<String, Uri?> { ): ManagedActivityResultLauncher<String, Uri?> {
val isTv = LocalIsAndroidTV.current val isTv = LocalIsAndroidTV.current
@@ -82,7 +82,7 @@ fun rememberFileExportLauncherForResult(
addCategory(Intent.CATEGORY_OPENABLE) addCategory(Intent.CATEGORY_OPENABLE)
type = type =
if (isTv) { if (isTv) {
Constants.ALLOWED_TV_FILE_TYPES FileUtils.ALLOWED_TV_FILE_TYPES
} else { } else {
mimeType mimeType
} }
@@ -1,78 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.common.prompt
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_WEAK
import androidx.biometric.BiometricManager.Authenticators.DEVICE_CREDENTIAL
import androidx.biometric.BiometricPrompt
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.zaneschepke.wireguardautotunnel.R
@Composable
fun AuthorizationPrompt(onSuccess: () -> Unit, onFailure: () -> Unit, onError: (String) -> Unit) {
val context = LocalContext.current
val biometricManager = BiometricManager.from(context)
val bio = biometricManager.canAuthenticate(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
val isBiometricAvailable = remember {
when (bio) {
BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
onError(context.getString(R.string.bio_not_created))
false
}
BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
onError(context.getString(R.string.bio_update_required))
false
}
BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED,
BiometricManager.BIOMETRIC_STATUS_UNKNOWN,
BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE,
BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
onError(context.getString(R.string.bio_not_supported))
false
}
BiometricManager.BIOMETRIC_SUCCESS -> true
else -> false
}
}
if (isBiometricAvailable) {
val executor = remember { ContextCompat.getMainExecutor(context) }
val promptInfo =
BiometricPrompt.PromptInfo.Builder()
.setAllowedAuthenticators(BIOMETRIC_WEAK or DEVICE_CREDENTIAL)
.setTitle(context.getString(R.string.bio_auth_title))
.setSubtitle(context.getString(R.string.bio_subtitle))
.build()
val biometricPrompt =
BiometricPrompt(
context as FragmentActivity,
executor,
object : BiometricPrompt.AuthenticationCallback() {
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
onFailure()
}
override fun onAuthenticationSucceeded(
result: BiometricPrompt.AuthenticationResult
) {
super.onAuthenticationSucceeded(result)
onSuccess()
}
override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
onFailure()
}
},
)
biometricPrompt.authenticate(promptInfo)
}
}
@@ -16,7 +16,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalLayoutDirection import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.unit.LayoutDirection import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalIsAndroidTV import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
@Composable @Composable
fun CustomSnackBar( fun CustomSnackBar(
@@ -1,12 +1,11 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation package com.zaneschepke.wireguardautotunnel.ui.navigation
import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.ImageVector
import com.zaneschepke.wireguardautotunnel.ui.Route
data class BottomNavItem( data class BottomNavItem(
val name: String, val name: String,
val route: Route,
val icon: ImageVector, val icon: ImageVector,
val onClick: () -> Unit, val onClick: () -> Unit,
val active: Boolean = false, val active: Boolean = false,
val route: Route,
) )
@@ -1,20 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation
import android.annotation.SuppressLint
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavDestination.Companion.hasRoute
import androidx.navigation.NavDestination.Companion.hierarchy
import androidx.navigation.NavHostController
import com.zaneschepke.wireguardautotunnel.ui.Route
import kotlin.reflect.KClass
@SuppressLint("RestrictedApi")
fun <T : Route> NavBackStackEntry?.isCurrentRoute(cls: KClass<T>): Boolean {
return this?.destination?.hierarchy?.any { it.hasRoute(route = cls) } == true
}
val LocalNavController =
compositionLocalOf<NavHostController> { error("NavController was not provided") }
val LocalIsAndroidTV = staticCompositionLocalOf { false }
@@ -0,0 +1,65 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation
import androidx.annotation.Keep
import kotlinx.serialization.Serializable
@Keep
@Serializable
sealed class Route {
@Keep @Serializable data object TunnelsGraph : Route()
@Keep @Serializable data object AutoTunnelGraph : Route()
@Keep @Serializable data object SettingsGraph : Route()
@Keep @Serializable data object SupportGraph : Route()
@Keep @Serializable data object Support : Route()
@Keep @Serializable data object Lock : Route()
@Keep @Serializable data object License : Route()
@Keep @Serializable data object Logs : Route()
@Keep @Serializable data object Appearance : Route()
@Keep @Serializable data object Language : Route()
@Keep @Serializable data object Display : Route()
@Keep @Serializable data object Tunnels : Route()
@Keep @Serializable data class TunnelOptions(val id: Int) : Route()
@Keep @Serializable data class Config(val id: Int?) : Route()
@Keep @Serializable data class SplitTunnel(val id: Int) : Route()
@Keep @Serializable data class TunnelAutoTunnel(val id: Int) : Route()
@Keep @Serializable data object Sort : Route()
@Keep @Serializable data object Settings : Route()
@Keep @Serializable data object TunnelMonitoring : Route()
@Keep @Serializable data object SystemFeatures : Route()
@Keep @Serializable data object Dns : Route()
@Keep @Serializable data object ProxySettings : Route()
@Keep @Serializable data object AutoTunnel : Route()
@Keep @Serializable data object AdvancedAutoTunnel : Route()
@Keep @Serializable data object WifiDetectionMethod : Route()
@Keep @Serializable data object LocationDisclosure : Route()
@Keep @Serializable data object Donate : Route()
@Keep @Serializable data object Addresses : Route()
}
@@ -1,107 +1,139 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation.components package com.zaneschepke.wireguardautotunnel.ui.navigation.components
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.offset import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Bolt import androidx.compose.material.icons.rounded.Bolt
import androidx.compose.material.icons.rounded.Home import androidx.compose.material.icons.rounded.Home
import androidx.compose.material.icons.rounded.QuestionMark import androidx.compose.material.icons.rounded.QuestionMark
import androidx.compose.material.icons.rounded.Settings import androidx.compose.material.icons.rounded.Settings
import androidx.compose.material3.* import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.getValue import androidx.compose.ui.Alignment
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState import androidx.navigation.compose.currentBackStackEntryAsState
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route
import com.zaneschepke.wireguardautotunnel.ui.navigation.BottomNavItem import com.zaneschepke.wireguardautotunnel.ui.navigation.BottomNavItem
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
import com.zaneschepke.wireguardautotunnel.ui.navigation.isCurrentRoute import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree import com.zaneschepke.wireguardautotunnel.ui.theme.SilverTree
import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot
@Composable
fun NavHostController.getCurrentGraph(): State<Route?> {
val navBackStackEntry by currentBackStackEntryAsState()
return remember(navBackStackEntry) {
derivedStateOf {
val parentRouteString = navBackStackEntry?.destination?.parent?.route
when (parentRouteString) {
Route.TunnelsGraph::class.qualifiedName -> Route.TunnelsGraph
Route.AutoTunnelGraph::class.qualifiedName -> Route.AutoTunnelGraph
Route.SettingsGraph::class.qualifiedName -> Route.SettingsGraph
Route.SupportGraph::class.qualifiedName -> Route.SupportGraph
else -> null
}
}
}
}
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun BottomNavbar(appUiState: AppUiState) { fun BottomNavbar(
val navController = LocalNavController.current isAutoTunnelActive: Boolean,
val navBackStackEntry by navController.currentBackStackEntryAsState() navbarState: NavbarState,
navController: NavHostController,
) {
val currentGraph by navController.getCurrentGraph()
val items = val items =
listOf( listOf(
BottomNavItem( BottomNavItem(
name = stringResource(R.string.tunnels), name = stringResource(R.string.tunnels),
route = Route.Main,
icon = Icons.Rounded.Home, icon = Icons.Rounded.Home,
onClick = { navController.goFromRoot(Route.Main) }, onClick = { navController.navigate(Route.TunnelsGraph) },
route = Route.TunnelsGraph,
), ),
BottomNavItem( BottomNavItem(
name = stringResource(R.string.auto_tunnel), name = stringResource(R.string.auto_tunnel),
route = Route.AutoTunnel,
icon = Icons.Rounded.Bolt, icon = Icons.Rounded.Bolt,
onClick = { onClick = { navController.navigate(Route.AutoTunnelGraph) },
val route = route = Route.AutoTunnelGraph,
if (appUiState.appState.isLocationDisclosureShown) { active = isAutoTunnelActive,
Route.AutoTunnel
} else Route.LocationDisclosure
navController.goFromRoot(route)
},
active = appUiState.isAutoTunnelActive,
), ),
BottomNavItem( BottomNavItem(
name = stringResource(R.string.settings), name = stringResource(R.string.settings),
route = Route.Settings,
icon = Icons.Rounded.Settings, icon = Icons.Rounded.Settings,
onClick = { navController.goFromRoot(Route.Settings) }, onClick = { navController.navigate(Route.SettingsGraph) },
route = Route.SettingsGraph,
), ),
BottomNavItem( BottomNavItem(
name = stringResource(R.string.support), name = stringResource(R.string.support),
route = Route.Support,
icon = Icons.Rounded.QuestionMark, icon = Icons.Rounded.QuestionMark,
onClick = { navController.goFromRoot(Route.Support) }, onClick = { navController.navigate(Route.SupportGraph) },
route = Route.SupportGraph,
), ),
) )
NavigationBar(containerColor = MaterialTheme.colorScheme.surface) { if (!navbarState.removeBottom) {
items.forEach { item -> NavigationBar(containerColor = MaterialTheme.colorScheme.surface) {
val isSelected = navBackStackEntry.isCurrentRoute(item.route::class) AnimatedVisibility(
val interactionSource = remember { MutableInteractionSource() } visible = navbarState.showBottomItems,
enter = slideInVertically(initialOffsetY = { it }),
NavigationBarItem( exit = slideOutVertically(targetOffsetY = { it }),
icon = { ) {
if (item.active) { Row(
BadgedBox( modifier = Modifier.fillMaxWidth(),
badge = { horizontalArrangement = Arrangement.spacedBy(0.dp),
Badge( verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.offset(x = 8.dp, y = (-8).dp).size(6.dp), ) {
containerColor = SilverTree, items.forEach { item ->
) val interactionSource = remember { MutableInteractionSource() }
} NavigationBarItem(
) { icon = {
Icon(imageVector = item.icon, contentDescription = item.name) if (item.active) {
} BadgedBox(
} else { badge = {
Icon(imageVector = item.icon, contentDescription = item.name) Badge(
modifier =
Modifier.offset(x = 8.dp, y = (-8).dp)
.size(6.dp),
containerColor = SilverTree,
)
}
) {
Icon(
imageVector = item.icon,
contentDescription = item.name,
)
}
} else {
Icon(imageVector = item.icon, contentDescription = item.name)
}
},
onClick = item.onClick,
selected = currentGraph == item.route,
enabled = true,
label = null,
alwaysShowLabel = false,
colors =
NavigationBarItemDefaults.colors(
selectedIconColor = MaterialTheme.colorScheme.primary,
unselectedIconColor = MaterialTheme.colorScheme.onBackground,
indicatorColor = Color.Transparent,
),
interactionSource = interactionSource,
)
} }
}, }
onClick = item.onClick, }
selected = isSelected,
enabled = true,
label = null,
alwaysShowLabel = false,
colors =
NavigationBarItemDefaults.colors(
selectedIconColor = MaterialTheme.colorScheme.primary,
unselectedIconColor = MaterialTheme.colorScheme.onBackground,
indicatorColor = Color.Transparent,
),
interactionSource = interactionSource,
)
} }
} }
} }
@@ -1,6 +1,5 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation.components package com.zaneschepke.wireguardautotunnel.ui.navigation.components
import androidx.compose.animation.*
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
@@ -10,32 +9,20 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.state.NavBarState import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
import com.zaneschepke.wireguardautotunnel.ui.theme.LockedDownBannerHeight import com.zaneschepke.wireguardautotunnel.ui.theme.LockedDownBannerHeight
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun DynamicTopAppBar(navBarState: NavBarState, modifier: Modifier = Modifier) { fun DynamicTopAppBar(navBarState: NavbarState, modifier: Modifier = Modifier) {
TopAppBar( TopAppBar(
modifier = modifier.padding(top = LockedDownBannerHeight), modifier = modifier.padding(top = LockedDownBannerHeight),
colors = TopAppBarDefaults.topAppBarColors().copy(Color.Transparent), colors = TopAppBarDefaults.topAppBarColors().copy(Color.Transparent),
title = { title = {
AnimatedVisibility( Box(modifier = Modifier.padding(start = 10.dp)) { navBarState.topTitle?.invoke() }
visible = navBarState.showTop,
enter = slideInVertically() + fadeIn(),
exit = slideOutVertically() + fadeOut(),
) {
Box(modifier = Modifier.padding(start = 10.dp)) { navBarState.topTitle?.invoke() }
}
}, },
actions = { actions = {
AnimatedVisibility( Box(modifier = Modifier.padding(end = 10.dp)) { navBarState.topTrailing?.invoke() }
visible = navBarState.showTop,
enter = slideInVertically() + fadeIn(),
exit = slideOutVertically() + fadeOut(),
) {
Box(modifier = Modifier.padding(end = 10.dp)) { navBarState.topTrailing?.invoke() }
}
}, },
) )
} }
@@ -0,0 +1,334 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation.components
import android.os.Build
import androidx.compose.foundation.layout.Row
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.Sort
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavHostController
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.toRoute
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.button.ActionIconButton
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route.Config
import com.zaneschepke.wireguardautotunnel.ui.sideeffect.LocalSideEffect
import com.zaneschepke.wireguardautotunnel.ui.state.NavbarState
import com.zaneschepke.wireguardautotunnel.viewmodel.SharedAppViewModel
@Composable
fun NavHostController.currentBackStackEntryAsNavbarState(
sharedViewModel: SharedAppViewModel,
navController: NavHostController,
): State<NavbarState> {
val sharedState by sharedViewModel.container.stateFlow.collectAsStateWithLifecycle()
val backStackEntry by currentBackStackEntryAsState()
val keyboardController = LocalSoftwareKeyboardController.current
val route =
remember(backStackEntry) {
backStackEntry?.destination?.route?.let {
when (it.substringBefore("?").substringBefore("/").substringAfterLast(".")) {
Route.Support::class.simpleName -> backStackEntry?.toRoute<Route.Support>()
Route.Lock::class.simpleName -> backStackEntry?.toRoute<Route.Lock>()
Route.License::class.simpleName -> backStackEntry?.toRoute<Route.License>()
Route.Logs::class.simpleName -> backStackEntry?.toRoute<Route.Logs>()
Route.Appearance::class.simpleName ->
backStackEntry?.toRoute<Route.Appearance>()
Route.Language::class.simpleName -> backStackEntry?.toRoute<Route.Language>()
Route.Display::class.simpleName -> backStackEntry?.toRoute<Route.Display>()
Route.Tunnels::class.simpleName -> backStackEntry?.toRoute<Route.Tunnels>()
Route.TunnelOptions::class.simpleName ->
backStackEntry?.toRoute<Route.TunnelOptions>()
Route.Config::class.simpleName -> backStackEntry?.toRoute<Route.Config>()
Route.SplitTunnel::class.simpleName ->
backStackEntry?.toRoute<Route.SplitTunnel>()
Route.TunnelAutoTunnel::class.simpleName ->
backStackEntry?.toRoute<Route.TunnelAutoTunnel>()
Route.Sort::class.simpleName -> backStackEntry?.toRoute<Route.Sort>()
Route.Settings::class.simpleName -> backStackEntry?.toRoute<Route.Settings>()
Route.TunnelMonitoring::class.simpleName ->
backStackEntry?.toRoute<Route.TunnelMonitoring>()
Route.SystemFeatures::class.simpleName ->
backStackEntry?.toRoute<Route.SystemFeatures>()
Route.Dns::class.simpleName -> backStackEntry?.toRoute<Route.Dns>()
Route.ProxySettings::class.simpleName ->
backStackEntry?.toRoute<Route.ProxySettings>()
Route.AutoTunnel::class.simpleName ->
backStackEntry?.toRoute<Route.AutoTunnel>()
Route.AdvancedAutoTunnel::class.simpleName ->
backStackEntry?.toRoute<Route.AdvancedAutoTunnel>()
Route.WifiDetectionMethod::class.simpleName ->
backStackEntry?.toRoute<Route.WifiDetectionMethod>()
Route.LocationDisclosure::class.simpleName ->
backStackEntry?.toRoute<Route.LocationDisclosure>()
Route.Donate::class.simpleName -> backStackEntry?.toRoute<Route.Donate>()
Route.Addresses::class.simpleName -> backStackEntry?.toRoute<Route.Addresses>()
else -> null
}
}
}
val disableDelete by
rememberSaveable(sharedState.selectedTunnels, sharedState.tunnels) {
mutableStateOf(
sharedState.tunnels.any { tunnel ->
tunnel.isActive &&
sharedState.tunnels.any { selected -> selected.id == tunnel.id }
}
)
}
val selectedCount by
rememberSaveable(sharedState.selectedTunnels) {
mutableStateOf(sharedState.selectedTunnels.size)
}
return produceState(initialValue = NavbarState(), route, selectedCount, disableDelete) {
value =
when (route) {
Route.AdvancedAutoTunnel ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.advanced_settings)) },
)
Route.Appearance ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.appearance)) },
)
Route.AutoTunnel ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.auto_tunnel)) },
)
is Route.Config -> {
val tunnel = sharedState.tunnels.find { it.id == route.id }
NavbarState(
showBottomItems = true,
topTitle = {
val title = tunnel?.tunName ?: stringResource(R.string.new_tunnel)
Text(title)
},
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
keyboardController?.hide()
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
}
},
)
}
Route.Display ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.display_theme)) },
)
Route.Dns ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.dns_settings)) },
)
Route.Language ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.language)) },
)
Route.License ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.licenses)) },
)
Route.LocationDisclosure -> NavbarState(showBottomItems = true)
Route.Lock -> NavbarState(showBottomItems = false)
Route.Logs ->
NavbarState(
showBottomItems = false,
removeBottom = true,
topTitle = { Text(stringResource(R.string.logs)) },
topTrailing = {
ActionIconButton(Icons.Rounded.Menu, R.string.quick_actions) {
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.LoggerActions)
}
},
)
Route.ProxySettings ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.proxy_settings)) },
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
keyboardController?.hide()
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
}
},
)
Route.Settings ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.settings)) },
topTrailing = {
ActionIconButton(
Icons.Rounded.SettingsBackupRestore,
R.string.quick_actions,
) {
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.BackupApp)
}
},
)
Route.Sort ->
NavbarState(
showBottomItems = true,
topTitle = { Text(stringResource(R.string.sort)) },
topTrailing = {
Row {
ActionIconButton(Icons.Rounded.SortByAlpha, R.string.sort) {
sharedViewModel.postSideEffect(LocalSideEffect.Sort)
}
ActionIconButton(Icons.Rounded.Save, R.string.save) {
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
}
}
},
)
is Route.SplitTunnel -> {
val tunnel = sharedState.tunnels.find { it.id == route.id }
NavbarState(
topTitle = { Text(tunnel?.tunName ?: "") },
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
sharedViewModel.postSideEffect(LocalSideEffect.SaveChanges)
}
},
showBottomItems = true,
)
}
Route.Support ->
NavbarState(
topTitle = { Text(stringResource(R.string.support)) },
showBottomItems = true,
)
Route.SystemFeatures ->
NavbarState(
topTitle = { Text(stringResource(R.string.android_integrations)) },
showBottomItems = true,
)
is Route.TunnelAutoTunnel -> {
val tunnel = sharedState.tunnels.find { it.id == route.id }
NavbarState(showBottomItems = true, topTitle = { Text(tunnel?.tunName ?: "") })
}
Route.TunnelMonitoring ->
NavbarState(
topTitle = { Text(stringResource(R.string.tunnel_monitoring)) },
showBottomItems = true,
)
is Route.TunnelOptions -> {
val tunnel = sharedState.tunnels.find { it.id == route.id }
NavbarState(
showBottomItems = true,
topTitle = { Text(tunnel?.tunName ?: "") },
topTrailing = {
Row {
ActionIconButton(Icons.Rounded.QrCode2, R.string.show_qr) {
sharedViewModel.postSideEffect(LocalSideEffect.Modal.QR)
}
ActionIconButton(Icons.Rounded.Edit, R.string.edit_tunnel) {
navigate(Config(route.id))
}
}
},
)
}
Route.Tunnels -> {
NavbarState(
topTitle = { Text(stringResource(R.string.tunnels)) },
topTrailing = {
when (selectedCount) {
0 -> DefaultTunnelsActions(navController, sharedViewModel)
else ->
Row {
ActionIconButton(
Icons.Rounded.SelectAll,
R.string.select_all,
) {
sharedViewModel.toggleSelectAllTunnels()
}
// due to permissions, and SAF issues on TV, not support
// less than Android
// 10
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActionIconButton(
Icons.Rounded.Download,
R.string.download,
) {
sharedViewModel.postSideEffect(
LocalSideEffect.Sheet.ExportTunnels
)
}
}
if (selectedCount == 1) {
ActionIconButton(Icons.Rounded.CopyAll, R.string.copy) {
sharedViewModel.copySelectedTunnel()
}
}
if (!disableDelete) {
ActionIconButton(
Icons.Rounded.Delete,
R.string.delete_tunnel,
) {
sharedViewModel.postSideEffect(
LocalSideEffect.Modal.DeleteTunnels
)
}
}
}
}
},
showBottomItems = true,
)
}
Route.WifiDetectionMethod ->
NavbarState(
topTitle = { Text(stringResource(R.string.wifi_detection_method)) },
showBottomItems = true,
)
Route.Donate -> {
NavbarState(
topTitle = { Text(stringResource(R.string.donate_title)) },
showBottomItems = true,
)
}
Route.Addresses -> {
NavbarState(
topTitle = { Text(stringResource(R.string.addresses)) },
showBottomItems = true,
)
}
Route.TunnelsGraph,
Route.SettingsGraph,
Route.AutoTunnelGraph,
Route.SupportGraph,
null -> NavbarState()
}
}
}
@Composable
private fun DefaultTunnelsActions(
navController: NavHostController,
sharedViewModel: SharedAppViewModel,
) {
Row {
ActionIconButton(Icons.AutoMirrored.Rounded.Sort, R.string.sort) {
navController.navigate(Route.Sort)
}
ActionIconButton(Icons.Rounded.Add, R.string.add_tunnel) {
sharedViewModel.postSideEffect(LocalSideEffect.Sheet.ImportTunnels)
}
}
}
@@ -1,335 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.navigation.components
import android.os.Build
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.rounded.Sort
import androidx.compose.material.icons.rounded.*
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.produceState
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavController
import androidx.navigation.toRoute
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route
import com.zaneschepke.wireguardautotunnel.ui.navigation.isCurrentRoute
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.ui.state.AppViewState
import com.zaneschepke.wireguardautotunnel.ui.state.NavBarState
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
import com.zaneschepke.wireguardautotunnel.viewmodel.event.UiEvent
@Composable
fun currentNavBackStackEntryAsNavBarState(
navController: NavController,
backStackEntry: NavBackStackEntry?,
viewModel: AppViewModel,
uiState: AppUiState,
appViewState: AppViewState,
): State<NavBarState> {
val context = LocalContext.current
fun isActiveSelected() =
uiState.activeTunnels.any { active ->
appViewState.selectedTunnels.any { it.id == active.key.id }
}
@Composable
fun ActionIconButton(icon: ImageVector, labelRes: Int, onClick: () -> Unit) {
IconButton(onClick = onClick) {
Icon(
icon,
contentDescription = stringResource(labelRes),
modifier = Modifier.size(iconSize),
)
}
}
@Composable
fun TunnelActionBar() {
val selectedCount = appViewState.selectedTunnels.size
val showDelete = !isActiveSelected()
Row {
if (selectedCount == 0) {
val showSort = remember(uiState.tunnels) { uiState.tunnels.size > 1 }
if (showSort)
ActionIconButton(Icons.AutoMirrored.Rounded.Sort, R.string.sort) {
navController.navigate(Route.Sort)
}
ActionIconButton(Icons.Rounded.Add, R.string.add_tunnel) {
viewModel.handleEvent(
AppEvent.SetBottomSheet(AppViewState.BottomSheet.IMPORT_TUNNELS)
)
}
return@Row
}
ActionIconButton(Icons.Rounded.SelectAll, R.string.select_all) {
viewModel.handleEvent(AppEvent.ToggleSelectAllTunnels)
}
// due to permissions, and SAF issues on TV, not support less than Android 10 on
// Android TV for file exports
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ActionIconButton(Icons.Rounded.Download, R.string.download) {
viewModel.handleEvent(
AppEvent.SetBottomSheet(AppViewState.BottomSheet.EXPORT_TUNNELS)
)
}
}
if (selectedCount == 1) {
ActionIconButton(Icons.Rounded.CopyAll, R.string.copy) {
viewModel.handleEvent(AppEvent.CopySelectedTunnel)
}
}
if (showDelete) {
ActionIconButton(Icons.Rounded.Delete, R.string.delete_tunnel) {
viewModel.handleEvent(AppEvent.SetShowModal(AppViewState.ModalType.DELETE))
}
}
}
}
return produceState(
initialValue = NavBarState(),
key1 = backStackEntry,
key2 = uiState,
key3 = appViewState,
) {
value =
when {
backStackEntry.isCurrentRoute(Route.Main::class) -> {
NavBarState(
topTitle = { Text(stringResource(R.string.tunnels)) },
topTrailing = { TunnelActionBar() },
route = Route.Main,
)
}
backStackEntry.isCurrentRoute(Route.AutoTunnel::class) -> {
NavBarState(
topTitle = { Text(stringResource(R.string.auto_tunnel)) },
route = Route.AutoTunnel,
)
}
backStackEntry.isCurrentRoute(Route.Logs::class) -> {
NavBarState(
showBottom = false,
topTitle = { Text(stringResource(R.string.logs)) },
topTrailing = {
ActionIconButton(Icons.Rounded.Menu, R.string.quick_actions) {
viewModel.handleEvent(
AppEvent.SetBottomSheet(AppViewState.BottomSheet.LOGS)
)
}
},
route = Route.Logs,
)
}
backStackEntry.isCurrentRoute(Route.Settings::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.settings)) },
route = Route.Settings,
topTrailing = {
ActionIconButton(
Icons.Rounded.SettingsBackupRestore,
R.string.quick_actions,
) {
viewModel.handleEvent(
AppEvent.SetBottomSheet(
AppViewState.BottomSheet.BACKUP_AND_RESTORE
)
)
}
},
)
backStackEntry.isCurrentRoute(Route.Appearance::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.appearance)) },
route = Route.Appearance,
)
backStackEntry.isCurrentRoute(Route.Language::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.language)) },
route = Route.Language,
)
backStackEntry.isCurrentRoute(Route.Display::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.display_theme)) },
route = Route.Display,
)
backStackEntry.isCurrentRoute(Route.TunnelMonitoring::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.tunnel_monitoring)) },
route = Route.TunnelMonitoring,
)
backStackEntry.isCurrentRoute(Route.ProxySettings::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.proxy_settings)) },
route = Route.ProxySettings,
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
viewModel.handleEvent(AppEvent.InvokeScreenAction)
}
},
)
backStackEntry.isCurrentRoute(Route.WifiDetectionMethod::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.wifi_detection_method)) },
route = Route.WifiDetectionMethod,
)
backStackEntry.isCurrentRoute(Route.SystemFeatures::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.android_integrations)) },
route = Route.SystemFeatures,
)
backStackEntry.isCurrentRoute(Route.Dns::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.dns_settings)) },
route = Route.Dns,
)
backStackEntry.isCurrentRoute(Route.Support::class) ->
NavBarState(
topTitle = { Text(stringResource(R.string.support)) },
route = Route.Support,
)
backStackEntry.isCurrentRoute(Route.Sort::class) -> {
NavBarState(
showTop = true,
showBottom = true,
topTitle = { Text(stringResource(R.string.sort)) },
topTrailing = {
Row {
ActionIconButton(Icons.Rounded.SortByAlpha, R.string.sort) {
viewModel.handleUiEvent(UiEvent.SortTunnels)
}
ActionIconButton(Icons.Rounded.Save, R.string.save) {
viewModel.handleEvent(AppEvent.InvokeScreenAction)
}
}
},
route = Route.Sort,
)
}
backStackEntry.isCurrentRoute(Route.License::class) -> {
NavBarState(
showTop = true,
showBottom = true,
topTitle = { Text(stringResource(R.string.licenses)) },
route = Route.License,
)
}
backStackEntry.isCurrentRoute(Route.AutoTunnelAdvanced::class) ->
NavBarState(
showTop = true,
showBottom = true,
topTitle = { Text(stringResource(R.string.advanced_settings)) },
route = Route.AutoTunnelAdvanced,
)
backStackEntry.isCurrentRoute(Route.TunnelOptions::class) -> {
val args = backStackEntry?.toRoute<Route.TunnelOptions>()
val tunnel = uiState.tunnels.find { it.id == args?.id }
NavBarState(
showTop = true,
showBottom = true,
topTitle = { tunnel?.name?.let { Text(it) } },
topTrailing = {
Row {
ActionIconButton(Icons.Rounded.QrCode2, R.string.show_qr) {
tunnel?.id?.let {
viewModel.handleEvent(
AppEvent.SetShowModal(AppViewState.ModalType.QR)
)
}
}
ActionIconButton(Icons.Rounded.Edit, R.string.edit_tunnel) {
tunnel?.id?.let { navController.navigate(Route.Config(it)) }
}
}
},
route = args?.let { Route.TunnelOptions(it.id) },
)
}
backStackEntry.isCurrentRoute(Route.SplitTunnel::class) -> {
val args = backStackEntry?.toRoute<Route.SplitTunnel>()
val name = uiState.tunnels.find { it.id == args?.id }?.name
NavBarState(
showTop = true,
showBottom = true,
topTitle = { name?.let { Text(it) } },
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
viewModel.handleEvent(AppEvent.InvokeScreenAction)
}
},
route = args?.let { Route.SplitTunnel(it.id) },
)
}
backStackEntry.isCurrentRoute(Route.Config::class) -> {
val args = backStackEntry?.toRoute<Route.Config>()
val name =
uiState.tunnels.find { it.id == args?.id }?.name
?: context.getString(R.string.new_tunnel)
NavBarState(
showTop = true,
showBottom = true,
topTitle = { Text(name) },
topTrailing = {
ActionIconButton(Icons.Rounded.Save, R.string.save) {
viewModel.handleEvent(AppEvent.InvokeScreenAction)
}
},
route = args?.let { Route.Config(it.id) },
)
}
backStackEntry.isCurrentRoute(Route.TunnelAutoTunnel::class) -> {
val args = backStackEntry?.toRoute<Route.TunnelAutoTunnel>()
val name = uiState.tunnels.find { it.id == args?.id }?.name
NavBarState(
showTop = true,
showBottom = true,
topTitle = { name?.let { Text(it) } },
route = args?.let { Route.TunnelAutoTunnel(it.id) },
)
}
else -> NavBarState(showTop = false, showBottom = false)
}
}
}
@@ -17,61 +17,62 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.google.accompanist.permissions.ExperimentalPermissionsApi import com.google.accompanist.permissions.ExperimentalPermissionsApi
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider import com.zaneschepke.wireguardautotunnel.ui.common.SectionDivider
import com.zaneschepke.wireguardautotunnel.ui.common.banner.WarningBanner import com.zaneschepke.wireguardautotunnel.ui.common.banner.WarningBanner
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.AdvancedSettingsItem import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.AdvancedSettingsItem
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.NetworkTunnelingItems import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.networkTunnelingItems
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.WifiTunnelingItems import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components.wifiTunnelingItems
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings
import com.zaneschepke.wireguardautotunnel.util.extensions.launchLocationServicesSettings import com.zaneschepke.wireguardautotunnel.util.extensions.launchLocationServicesSettings
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@OptIn(ExperimentalPermissionsApi::class) @OptIn(ExperimentalPermissionsApi::class)
@Composable @Composable
fun AutoTunnelScreen(uiState: AppUiState, viewModel: AppViewModel) { fun AutoTunnelScreen(viewModel: AutoTunnelViewModel = hiltViewModel()) {
val navController = LocalNavController.current
val context = LocalContext.current val context = LocalContext.current
val navController = LocalNavController.current
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
var currentText by remember { mutableStateOf("") } if (!autoTunnelState.stateInitialized) return
var showLocationDialog by remember { mutableStateOf(false) } var showLocationDialog by remember { mutableStateOf(false) }
val showLocationServicesWarning by val showLocationServicesWarning by
remember( remember(
uiState.connectivityState?.wifiState, autoTunnelState.connectivityState?.wifiState,
uiState.appSettings.trustedNetworkSSIDs, autoTunnelState.generalSettings.trustedNetworkSSIDs,
uiState.appSettings.wifiDetectionMethod, autoTunnelState.generalSettings.wifiDetectionMethod,
) { ) {
derivedStateOf { derivedStateOf {
uiState.connectivityState?.wifiState?.locationServicesEnabled == false && autoTunnelState.connectivityState?.wifiState?.locationServicesEnabled == false &&
uiState.appSettings.wifiDetectionMethod.needsLocationPermissions() && autoTunnelState.generalSettings.wifiDetectionMethod
uiState.appSettings.trustedNetworkSSIDs.isNotEmpty() .needsLocationPermissions() &&
autoTunnelState.generalSettings.trustedNetworkSSIDs.isNotEmpty()
} }
} }
val showLocationPermissionsWarning by val showLocationPermissionsWarning by
remember( remember(
uiState.connectivityState?.wifiState, autoTunnelState.connectivityState?.wifiState,
uiState.appSettings.trustedNetworkSSIDs, autoTunnelState.generalSettings.trustedNetworkSSIDs,
uiState.appSettings.wifiDetectionMethod, autoTunnelState.generalSettings.wifiDetectionMethod,
) { ) {
derivedStateOf { derivedStateOf {
uiState.connectivityState?.wifiState?.locationPermissionsGranted == false && autoTunnelState.connectivityState?.wifiState?.locationPermissionsGranted == false &&
uiState.appSettings.wifiDetectionMethod.needsLocationPermissions() && autoTunnelState.generalSettings.wifiDetectionMethod
uiState.appSettings.trustedNetworkSSIDs.isNotEmpty() .needsLocationPermissions() &&
autoTunnelState.generalSettings.trustedNetworkSSIDs.isNotEmpty()
} }
} }
LaunchedEffect(uiState.appSettings.trustedNetworkSSIDs) { currentText = "" }
if (showLocationDialog) { if (showLocationDialog) {
InfoDialog( InfoDialog(
onAttest = { onAttest = {
@@ -121,8 +122,8 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AppViewModel) {
}, },
) )
val (title, buttonText, icon) = val (title, buttonText, icon) =
remember(uiState.isAutoTunnelActive) { remember(autoTunnelState.autoTunnelActive) {
when (uiState.isAutoTunnelActive) { when (autoTunnelState.autoTunnelActive) {
true -> true ->
Triple( Triple(
context.getString(R.string.auto_tunnel_running), context.getString(R.string.auto_tunnel_running),
@@ -144,7 +145,7 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AppViewModel) {
leading = { Icon(icon, null) }, leading = { Icon(icon, null) },
title = { Text(title) }, title = { Text(title) },
trailing = { trailing = {
Button({ viewModel.handleEvent(AppEvent.ToggleAutoTunnel) }) { Button({ viewModel.toggleAutoTunnel() }) {
Text( Text(
buttonText, buttonText,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
@@ -159,16 +160,16 @@ fun AutoTunnelScreen(uiState: AppUiState, viewModel: AppViewModel) {
) )
) )
SurfaceSelectionGroupButton( SurfaceSelectionGroupButton(
items = WifiTunnelingItems(uiState, viewModel, currentText) { currentText = it } items = wifiTunnelingItems(autoTunnelState, viewModel, navController)
) )
SectionDivider() SectionDivider()
SurfaceSelectionGroupButton(items = NetworkTunnelingItems(uiState, viewModel)) SurfaceSelectionGroupButton(items = networkTunnelingItems(autoTunnelState, viewModel))
SectionDivider() SectionDivider()
SurfaceSelectionGroupButton( SurfaceSelectionGroupButton(
items = items =
listOf( listOf(
AdvancedSettingsItem( AdvancedSettingsItem(
onClick = { navController.navigate(Route.AutoTunnelAdvanced) } onClick = { navController.navigate(Route.AdvancedAutoTunnel) }
) )
) )
) )
@@ -12,18 +12,19 @@ import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme 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.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.dropdown.LabelledDropdown import com.zaneschepke.wireguardautotunnel.ui.common.dropdown.LabelledDropdown
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable @Composable
fun AutoTunnelAdvancedScreen(appUiState: AppUiState, viewModel: AppViewModel) { fun AutoTunnelAdvancedScreen(viewModel: AutoTunnelViewModel) {
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
Column( Column(
horizontalAlignment = Alignment.Start, horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top), verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
@@ -44,11 +45,9 @@ fun AutoTunnelAdvancedScreen(appUiState: AppUiState, viewModel: AppViewModel) {
) )
}, },
leading = { Icon(Icons.Outlined.PauseCircle, null) }, leading = { Icon(Icons.Outlined.PauseCircle, null) },
onSelected = { selected -> onSelected = { selected -> viewModel.setDebounceDelay(selected!!) },
viewModel.handleEvent(AppEvent.SetDebounceDelay(selected!!))
},
options = (0..10).toList(), options = (0..10).toList(),
currentValue = appUiState.appSettings.debounceDelaySeconds, currentValue = autoTunnelState.generalSettings.debounceDelaySeconds,
optionToString = { it?.toString() ?: stringResource(R.string._default) }, optionToString = { it?.toString() ?: stringResource(R.string._default) },
) )
} }
@@ -14,12 +14,14 @@ import androidx.compose.ui.text.style.TextOverflow
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState import com.zaneschepke.wireguardautotunnel.ui.state.AutoTunnelUiState
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable @Composable
fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<SelectionItem> { fun networkTunnelingItems(
autoTunnelState: AutoTunnelUiState,
viewModel: AutoTunnelViewModel,
): List<SelectionItem> {
return listOf( return listOf(
SelectionItem( SelectionItem(
leading = { Icon(Icons.Outlined.SignalCellular4Bar, contentDescription = null) }, leading = { Icon(Icons.Outlined.SignalCellular4Bar, contentDescription = null) },
@@ -34,15 +36,14 @@ fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<Se
}, },
trailing = { trailing = {
ScaledSwitch( ScaledSwitch(
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled, checked = autoTunnelState.generalSettings.isTunnelOnMobileDataEnabled,
checked = uiState.appSettings.isTunnelOnMobileDataEnabled, onClick = { viewModel.setTunnelOnCellular(it) },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnCellular) },
) )
}, },
description = { description = {
val cellularActive = val cellularActive =
remember(uiState.connectivityState) { remember(autoTunnelState.connectivityState) {
uiState.connectivityState?.cellularConnected ?: false autoTunnelState.connectivityState?.cellularConnected ?: false
} }
Text( Text(
text = text =
@@ -56,7 +57,11 @@ fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<Se
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
) )
}, },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnCellular) }, onClick = {
viewModel.setTunnelOnCellular(
!autoTunnelState.generalSettings.isTunnelOnMobileDataEnabled
)
},
), ),
SelectionItem( SelectionItem(
leading = { Icon(Icons.Outlined.SettingsEthernet, contentDescription = null) }, leading = { Icon(Icons.Outlined.SettingsEthernet, contentDescription = null) },
@@ -71,15 +76,14 @@ fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<Se
}, },
trailing = { trailing = {
ScaledSwitch( ScaledSwitch(
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled, checked = autoTunnelState.generalSettings.isTunnelOnEthernetEnabled,
checked = uiState.appSettings.isTunnelOnEthernetEnabled, onClick = { viewModel.setTunnelOnEthernet(it) },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnEthernet) },
) )
}, },
description = { description = {
val ethernetActive = val ethernetActive =
remember(uiState.connectivityState) { remember(autoTunnelState.connectivityState) {
uiState.connectivityState?.ethernetConnected ?: false autoTunnelState.connectivityState?.ethernetConnected ?: false
} }
Text( Text(
text = text =
@@ -93,7 +97,11 @@ fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<Se
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
) )
}, },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnEthernet) }, onClick = {
viewModel.setTunnelOnEthernet(
!autoTunnelState.generalSettings.isTunnelOnEthernetEnabled
)
},
), ),
SelectionItem( SelectionItem(
leading = { Icon(Icons.Outlined.PublicOff, contentDescription = null) }, leading = { Icon(Icons.Outlined.PublicOff, contentDescription = null) },
@@ -115,11 +123,15 @@ fun NetworkTunnelingItems(uiState: AppUiState, viewModel: AppViewModel): List<Se
}, },
trailing = { trailing = {
ScaledSwitch( ScaledSwitch(
checked = uiState.appSettings.isStopOnNoInternetEnabled, checked = autoTunnelState.generalSettings.isStopOnNoInternetEnabled,
onClick = { viewModel.handleEvent(AppEvent.ToggleStopTunnelOnNoInternet) }, onClick = { viewModel.setStopOnNoInternetEnabled(it) },
)
},
onClick = {
viewModel.setStopOnNoInternetEnabled(
!autoTunnelState.generalSettings.isStopOnNoInternetEnabled
) )
}, },
onClick = { viewModel.handleEvent(AppEvent.ToggleStopTunnelOnNoInternet) },
), ),
) )
} }
@@ -20,14 +20,14 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.LocalIsAndroidTV
import com.zaneschepke.wireguardautotunnel.ui.common.button.ClickableIconButton import com.zaneschepke.wireguardautotunnel.ui.common.button.ClickableIconButton
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField import com.zaneschepke.wireguardautotunnel.ui.common.textbox.CustomTextField
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalIsAndroidTV
@OptIn(ExperimentalLayoutApi::class) @OptIn(ExperimentalLayoutApi::class)
@Composable @Composable
fun TrustedNetworkTextBox( fun TrustedNetworkTextBox(
trustedNetworks: List<String>, trustedNetworks: Set<String>,
onDelete: (ssid: String) -> Unit, onDelete: (ssid: String) -> Unit,
currentText: String, currentText: String,
onSave: (ssid: String) -> Unit, onSave: (ssid: String) -> Unit,
@@ -3,46 +3,48 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.components
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.* import androidx.compose.material.icons.outlined.Filter1
import androidx.compose.material.icons.outlined.Security
import androidx.compose.material.icons.outlined.Wifi
import androidx.compose.material.icons.outlined.WifiFind
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch import com.zaneschepke.wireguardautotunnel.ui.common.button.ScaledSwitch
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LearnMoreLinkLabel import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.LearnMoreLinkLabel
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState import com.zaneschepke.wireguardautotunnel.ui.state.AutoTunnelUiState
import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize import com.zaneschepke.wireguardautotunnel.ui.theme.iconSize
import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString
import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl import com.zaneschepke.wireguardautotunnel.util.extensions.openWebUrl
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable @Composable
fun WifiTunnelingItems( fun wifiTunnelingItems(
uiState: AppUiState, autoTunnelState: AutoTunnelUiState,
viewModel: AppViewModel, viewModel: AutoTunnelViewModel,
currentText: String, navController: NavController,
onTextChange: (String) -> Unit,
): List<SelectionItem> { ): List<SelectionItem> {
val context = LocalContext.current val context = LocalContext.current
val navController = LocalNavController.current
val clipboardHelper = rememberClipboardHelper() val clipboardHelper = rememberClipboardHelper()
var currentText by rememberSaveable { mutableStateOf("") }
LaunchedEffect(autoTunnelState.generalSettings.trustedNetworkSSIDs) { currentText = "" }
val baseItems = val baseItems =
listOf( listOf(
SelectionItem( SelectionItem(
@@ -58,16 +60,15 @@ fun WifiTunnelingItems(
}, },
trailing = { trailing = {
ScaledSwitch( ScaledSwitch(
enabled = !uiState.appSettings.isAlwaysOnVpnEnabled, checked = autoTunnelState.generalSettings.isTunnelOnWifiEnabled,
checked = uiState.appSettings.isTunnelOnWifiEnabled, onClick = { viewModel.setAutoTunnelOnWifiEnabled(it) },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnWifi) },
) )
}, },
description = { description = {
val wifiInfo by val wifiInfo by
remember(uiState.connectivityState) { remember(autoTunnelState.connectivityState) {
derivedStateOf { derivedStateOf {
uiState.connectivityState autoTunnelState.connectivityState
?.wifiState ?.wifiState
?.takeIf { it.connected } ?.takeIf { it.connected }
.let { Pair(it?.ssid, it?.securityType) } .let { Pair(it?.ssid, it?.securityType) }
@@ -101,11 +102,15 @@ fun WifiTunnelingItems(
} }
} }
}, },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelOnWifi) }, onClick = {
viewModel.setAutoTunnelOnWifiEnabled(
!autoTunnelState.generalSettings.isTunnelOnWifiEnabled
)
},
) )
) )
return if (uiState.appSettings.isTunnelOnWifiEnabled) { return if (autoTunnelState.generalSettings.isTunnelOnWifiEnabled) {
baseItems + baseItems +
listOf( listOf(
SelectionItem( SelectionItem(
@@ -123,7 +128,9 @@ fun WifiTunnelingItems(
Text( Text(
stringResource( stringResource(
R.string.current_template, R.string.current_template,
uiState.appSettings.wifiDetectionMethod.asTitleString(context), autoTunnelState.generalSettings.wifiDetectionMethod.asTitleString(
context
),
), ),
style = style =
MaterialTheme.typography.bodySmall.copy( MaterialTheme.typography.bodySmall.copy(
@@ -155,11 +162,15 @@ fun WifiTunnelingItems(
}, },
trailing = { trailing = {
ScaledSwitch( ScaledSwitch(
checked = uiState.appSettings.isWildcardsEnabled, checked = autoTunnelState.generalSettings.isWildcardsEnabled,
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelWildcards) }, onClick = { viewModel.setWildcardsEnabled(it) },
)
},
onClick = {
viewModel.setWildcardsEnabled(
!autoTunnelState.generalSettings.isWildcardsEnabled
) )
}, },
onClick = { viewModel.handleEvent(AppEvent.ToggleAutoTunnelWildcards) },
), ),
SelectionItem( SelectionItem(
title = { title = {
@@ -195,41 +206,18 @@ fun WifiTunnelingItems(
}, },
description = { description = {
TrustedNetworkTextBox( TrustedNetworkTextBox(
uiState.appSettings.trustedNetworkSSIDs, autoTunnelState.generalSettings.trustedNetworkSSIDs,
onDelete = { viewModel.handleEvent(AppEvent.DeleteTrustedSSID(it)) }, onDelete = { viewModel.removeTrustedNetworkName(it) },
currentText = currentText, currentText = currentText,
onSave = { ssid -> onSave = { ssid -> viewModel.saveTrustedNetworkName(ssid) },
viewModel.handleEvent(AppEvent.SaveTrustedSSID(ssid)) onValueChange = { currentText = it },
},
onValueChange = onTextChange,
supporting = { supporting = {
if (uiState.appSettings.isWildcardsEnabled) WildcardsLabel() if (autoTunnelState.generalSettings.isWildcardsEnabled)
WildcardsLabel()
}, },
) )
}, },
), ),
SelectionItem(
leading = { Icon(Icons.Outlined.VpnKeyOff, contentDescription = null) },
title = {
Text(
stringResource(R.string.kill_switch_off),
style =
MaterialTheme.typography.bodyMedium.copy(
MaterialTheme.colorScheme.onSurface
),
)
},
trailing = {
ScaledSwitch(
enabled = uiState.appSettings.isVpnKillSwitchEnabled,
checked = uiState.appSettings.isDisableKillSwitchOnTrustedEnabled,
onClick = {
viewModel.handleEvent(AppEvent.ToggleStopKillSwitchOnTrusted)
},
)
},
onClick = { viewModel.handleEvent(AppEvent.ToggleStopKillSwitchOnTrusted) },
),
) )
} else { } else {
baseItems baseItems
@@ -5,33 +5,38 @@ import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.networkmonitor.AndroidNetworkMonitor import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.zaneschepke.wireguardautotunnel.data.model.WifiDetectionMethod
import com.zaneschepke.wireguardautotunnel.ui.LocalSharedVm
import com.zaneschepke.wireguardautotunnel.ui.common.button.IconSurfaceButton import com.zaneschepke.wireguardautotunnel.ui.common.button.IconSurfaceButton
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.util.extensions.asDescriptionString import com.zaneschepke.wireguardautotunnel.util.extensions.asDescriptionString
import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString import com.zaneschepke.wireguardautotunnel.util.extensions.asTitleString
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable @Composable
fun WifiDetectionMethodScreen(uiState: AppUiState, viewModel: AppViewModel) { fun WifiDetectionMethodScreen(viewModel: AutoTunnelViewModel) {
val context = LocalContext.current val context = LocalContext.current
val sharedViewModel = LocalSharedVm.current
val autoTunnelState by viewModel.container.stateFlow.collectAsStateWithLifecycle()
Column( Column(
horizontalAlignment = Alignment.Start, horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top), verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
modifier = Modifier.fillMaxSize().padding(top = 24.dp).padding(horizontal = 24.dp), modifier = Modifier.fillMaxSize().padding(top = 24.dp).padding(horizontal = 24.dp),
) { ) {
enumValues<AndroidNetworkMonitor.WifiDetectionMethod>().forEach { enumValues<WifiDetectionMethod>().forEach {
val title = it.asTitleString(context) val title = it.asTitleString(context)
val description = it.asDescriptionString(context) val description = it.asDescriptionString(context)
IconSurfaceButton( IconSurfaceButton(
title = title, title = title,
onClick = { viewModel.handleEvent(AppEvent.SetDetectionMethod(it)) }, onClick = { sharedViewModel.setWifiDetectionMethod(it) },
selected = uiState.appSettings.wifiDetectionMethod == it, selected = autoTunnelState.generalSettings.wifiDetectionMethod == it,
description = description, description = description,
) )
} }
@@ -9,17 +9,18 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.ui.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.LocationDisclosureHeader import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.LocationDisclosureHeader
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.appSettingsItem import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.appSettingsItem
import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.skipItem import com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components.skipItem
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel import com.zaneschepke.wireguardautotunnel.viewmodel.AutoTunnelViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable @Composable
fun LocationDisclosureScreen(viewModel: AppViewModel) { fun LocationDisclosureScreen(viewModel: AutoTunnelViewModel) {
val navController = LocalNavController.current
LaunchedEffect(Unit) { viewModel.handleEvent(AppEvent.SetLocationDisclosureShown) } LaunchedEffect(Unit) { viewModel.setLocationDisclosureShown() }
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
@@ -28,6 +29,6 @@ fun LocationDisclosureScreen(viewModel: AppViewModel) {
) { ) {
LocationDisclosureHeader() LocationDisclosureHeader()
SurfaceSelectionGroupButton(items = listOf(appSettingsItem())) SurfaceSelectionGroupButton(items = listOf(appSettingsItem()))
SurfaceSelectionGroupButton(items = listOf(skipItem())) SurfaceSelectionGroupButton(items = listOf(skipItem(navController)))
} }
} }
@@ -3,14 +3,14 @@ package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.com
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.LocationOn import androidx.compose.material.icons.outlined.LocationOn
import androidx.compose.material3.Icon import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings import com.zaneschepke.wireguardautotunnel.util.extensions.launchAppSettings
@Composable @Composable
@@ -20,9 +20,9 @@ fun appSettingsItem(): SelectionItem {
return SelectionItem( return SelectionItem(
leading = { Icon(Icons.Outlined.LocationOn, contentDescription = null) }, leading = { Icon(Icons.Outlined.LocationOn, contentDescription = null) },
title = { title = {
Text( SelectionItemLabel(
text = stringResource(R.string.launch_app_settings), stringResource(R.string.launch_app_settings),
style = MaterialTheme.typography.bodyLarge.copy(MaterialTheme.colorScheme.onSurface), labelType = SelectionLabelType.TITLE,
) )
}, },
trailing = { ForwardButton { context.launchAppSettings() } }, trailing = { ForwardButton { context.launchAppSettings() } },
@@ -20,14 +20,14 @@ fun LocationDisclosureHeader() {
Icon( Icon(
imageVector = icon, imageVector = icon,
contentDescription = icon.name, contentDescription = icon.name,
modifier = Modifier.padding(30.dp).size(128.dp), modifier = Modifier.padding(24.dp).size(100.dp),
) )
Text( Text(
text = stringResource(R.string.prominent_background_location_title), text = stringResource(R.string.prominent_background_location_title),
style = MaterialTheme.typography.titleLarge.copy(fontWeight = FontWeight.Bold), style = MaterialTheme.typography.titleMedium.copy(fontWeight = FontWeight.Bold),
) )
Text( Text(
text = stringResource(R.string.prominent_background_location_message), text = stringResource(R.string.prominent_background_location_message),
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyMedium,
) )
} }
@@ -1,27 +1,22 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components package com.zaneschepke.wireguardautotunnel.ui.screens.autotunnel.disclosure.components
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.navigation.NavController
import com.zaneschepke.wireguardautotunnel.R import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route
import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton import com.zaneschepke.wireguardautotunnel.ui.common.button.ForwardButton
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItem
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionItemLabel
import com.zaneschepke.wireguardautotunnel.util.extensions.goFromRoot import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SelectionLabelType
import com.zaneschepke.wireguardautotunnel.ui.navigation.Route
@Composable @Composable
fun skipItem(): SelectionItem { fun skipItem(navController: NavController): SelectionItem {
val navController = LocalNavController.current
return SelectionItem( return SelectionItem(
title = { title = {
Text( SelectionItemLabel(stringResource(R.string.skip), labelType = SelectionLabelType.TITLE)
text = stringResource(R.string.skip),
style = MaterialTheme.typography.bodyLarge.copy(MaterialTheme.colorScheme.onSurface),
)
}, },
trailing = { ForwardButton { navController.goFromRoot(Route.AutoTunnel) } }, trailing = { ForwardButton { navController.navigate(Route.AutoTunnelGraph) } },
onClick = { navController.goFromRoot(Route.AutoTunnel) }, onClick = { navController.navigate(Route.AutoTunnelGraph) },
) )
} }
@@ -1,140 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.journeyapps.barcodescanner.ScanContract
import com.journeyapps.barcodescanner.ScanOptions
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.Route
import com.zaneschepke.wireguardautotunnel.ui.common.dialog.InfoDialog
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberClipboardHelper
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileImportLauncherForResult
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalNavController
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.ExportTunnelsBottomSheet
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelImportSheet
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.TunnelList
import com.zaneschepke.wireguardautotunnel.ui.screens.main.components.UrlImportDialog
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.ui.state.AppViewState
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.StringValue
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable
fun MainScreen(appUiState: AppUiState, appViewState: AppViewState, viewModel: AppViewModel) {
val navController = LocalNavController.current
val clipboard = rememberClipboardHelper()
var showUrlImportDialog by remember { mutableStateOf(false) }
val tunnelFileImportResultLauncher =
rememberFileImportLauncherForResult(
onNoFileExplorer = {
viewModel.handleEvent(
AppEvent.ShowMessage(
StringValue.StringResource(R.string.error_no_file_explorer)
)
)
},
onData = { data -> viewModel.handleEvent(AppEvent.ImportTunnelFromFile(data)) },
)
val scanLauncher =
rememberLauncherForActivityResult(
contract = ScanContract(),
onResult = { result ->
if (result != null && result.contents.isNotEmpty())
viewModel.handleEvent(AppEvent.ImportTunnelFromQrCode(result.contents))
},
)
val requestPermissionLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { isGranted
->
if (!isGranted) {
viewModel.handleEvent(
AppEvent.ShowMessage(
StringValue.StringResource(R.string.camera_permission_required)
)
)
return@rememberLauncherForActivityResult
}
scanLauncher.launch(
ScanOptions().setDesiredBarcodeFormats(ScanOptions.QR_CODE).setBeepEnabled(false)
)
}
if (appViewState.showModal == AppViewState.ModalType.DELETE) {
InfoDialog(
onDismiss = {
viewModel.handleEvent(AppEvent.SetShowModal(AppViewState.ModalType.NONE))
},
onAttest = {
viewModel.handleEvent(AppEvent.DeleteSelectedTunnels)
viewModel.handleEvent(AppEvent.SetShowModal(AppViewState.ModalType.NONE))
},
title = { Text(text = stringResource(R.string.delete_tunnel)) },
body = { Text(text = stringResource(R.string.delete_tunnel_message)) },
confirmText = { Text(text = stringResource(R.string.yes)) },
)
}
when (appViewState.bottomSheet) {
AppViewState.BottomSheet.EXPORT_TUNNELS -> {
ExportTunnelsBottomSheet(viewModel)
}
AppViewState.BottomSheet.IMPORT_TUNNELS -> {
TunnelImportSheet(
onDismiss = {
viewModel.handleEvent(AppEvent.SetBottomSheet(AppViewState.BottomSheet.NONE))
},
onFileClick = {
tunnelFileImportResultLauncher.launch(Constants.ALLOWED_TV_FILE_TYPES)
},
onQrClick = {
requestPermissionLauncher.launch(android.Manifest.permission.CAMERA)
},
onClipboardClick = {
clipboard.paste { result ->
if (result != null)
viewModel.handleEvent(AppEvent.ImportTunnelFromClipboard(result))
}
},
onManualImportClick = {
navController.navigate(Route.Config(Constants.MANUAL_TUNNEL_CONFIG_ID))
},
onUrlClick = { showUrlImportDialog = true },
)
}
else -> Unit
}
if (showUrlImportDialog) {
UrlImportDialog(
onDismiss = { showUrlImportDialog = false },
onConfirm = { url ->
viewModel.handleEvent(AppEvent.ImportTunnelFromUrl(url))
showUrlImportDialog = false
},
)
}
TunnelList(
appUiState = appUiState,
selectedTunnels = appViewState.selectedTunnels,
onToggleTunnel = { tunnel, checked ->
if (checked) viewModel.handleEvent(AppEvent.StartTunnel(tunnel))
else viewModel.handleEvent(AppEvent.StopTunnel(tunnel))
},
modifier = Modifier.fillMaxSize().padding(vertical = 24.dp).padding(horizontal = 12.dp),
viewModel = viewModel,
)
}
@@ -1,57 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.domain.model.AppSettings
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.ui.common.button.surface.SurfaceSelectionGroupButton
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.MobileDataTunnelItem
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.PingRestartItem
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.WifiTunnelItem
import com.zaneschepke.wireguardautotunnel.ui.screens.main.autotunnel.components.ethernetTunnelItem
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
@Composable
fun TunnelAutoTunnelScreen(
tunnelConf: TunnelConf,
appSettings: AppSettings,
viewModel: AppViewModel,
) {
var currentText by remember { mutableStateOf("") }
LaunchedEffect(tunnelConf.tunnelNetworks) { currentText = "" }
Column(
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(12.dp, Alignment.Top),
modifier =
Modifier.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(vertical = 24.dp)
.padding(horizontal = 12.dp),
) {
SurfaceSelectionGroupButton(
items =
buildList {
if (appSettings.isPingEnabled) {
add(PingRestartItem(tunnelConf, viewModel))
}
add(MobileDataTunnelItem(tunnelConf, viewModel))
add(ethernetTunnelItem(tunnelConf, viewModel))
add(
WifiTunnelItem(tunnelConf, appSettings, viewModel, currentText) {
currentText = it
}
)
}
)
}
}
@@ -1,103 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main.components
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.FolderZip
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.domain.enums.ConfigType
import com.zaneschepke.wireguardautotunnel.ui.common.functions.rememberFileExportLauncherForResult
import com.zaneschepke.wireguardautotunnel.ui.common.sheet.CustomBottomSheet
import com.zaneschepke.wireguardautotunnel.ui.common.sheet.SheetOption
import com.zaneschepke.wireguardautotunnel.ui.navigation.LocalIsAndroidTV
import com.zaneschepke.wireguardautotunnel.ui.screens.settings.components.AuthorizationPromptWrapper
import com.zaneschepke.wireguardautotunnel.ui.state.AppViewState
import com.zaneschepke.wireguardautotunnel.util.Constants
import com.zaneschepke.wireguardautotunnel.util.extensions.hasSAFSupport
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ExportTunnelsBottomSheet(viewModel: AppViewModel) {
val context = LocalContext.current
val isTv = LocalIsAndroidTV.current
var exportConfigType by remember { mutableStateOf(ConfigType.WG) }
var showAuthPrompt by remember { mutableStateOf(false) }
var isAuthorized by remember { mutableStateOf(false) }
var shouldExport by remember { mutableStateOf(false) }
val selectedTunnelsExportLauncher =
rememberFileExportLauncherForResult(
mimeType = Constants.ZIP_FILE_MIME_TYPE,
onResult = { file ->
if (file != null) {
viewModel.handleEvent(AppEvent.ExportSelectedTunnels(exportConfigType, file))
} else {
viewModel.handleEvent(AppEvent.ClearSelectedTunnels)
viewModel.handleEvent(AppEvent.SetBottomSheet(AppViewState.BottomSheet.NONE))
}
},
)
fun handleFileExport() {
if (context.hasSAFSupport(Constants.ZIP_FILE_MIME_TYPE)) {
selectedTunnelsExportLauncher.launch(Constants.DEFAULT_EXPORT_FILE_NAME)
} else {
viewModel.handleEvent(AppEvent.ExportSelectedTunnels(exportConfigType, null))
}
}
LaunchedEffect(shouldExport) {
if (shouldExport) {
handleFileExport()
shouldExport = false
}
}
if (showAuthPrompt) {
AuthorizationPromptWrapper(
onDismiss = { showAuthPrompt = false },
onSuccess = {
showAuthPrompt = false
isAuthorized = true
shouldExport = true
},
viewModel = viewModel,
)
}
CustomBottomSheet(
listOf(
SheetOption(
Icons.Outlined.FolderZip,
stringResource(R.string.export_tunnels_amnezia),
onClick = {
exportConfigType = ConfigType.AM
if (!isAuthorized && !isTv) {
showAuthPrompt = true
} else {
shouldExport = true
}
},
),
SheetOption(
Icons.Outlined.FolderZip,
stringResource(R.string.export_tunnels_wireguard),
onClick = {
exportConfigType = ConfigType.WG
if (!isAuthorized && !isTv) {
showAuthPrompt = true
} else {
shouldExport = true
}
},
),
)
) {
viewModel.handleEvent(AppEvent.SetBottomSheet(AppViewState.BottomSheet.NONE))
}
}
@@ -1,128 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.ui.common.SecureScreenFromRecording
import com.zaneschepke.wireguardautotunnel.ui.common.prompt.AuthorizationPrompt
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.AddPeerButton
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.InterfaceSection
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components.PeersSection
import com.zaneschepke.wireguardautotunnel.ui.state.AppUiState
import com.zaneschepke.wireguardautotunnel.util.StringValue
import com.zaneschepke.wireguardautotunnel.viewmodel.AppViewModel
import com.zaneschepke.wireguardautotunnel.viewmodel.event.AppEvent
@Composable
fun ConfigScreen(
tunnelConf: TunnelConf?,
appUiState: AppUiState,
appViewModel: AppViewModel,
viewModel: ConfigViewModel = hiltViewModel(),
) {
val context = LocalContext.current
val keyboardController = LocalSoftwareKeyboardController.current
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
var save by remember { mutableStateOf(false) }
val isTunnelNameTaken by
remember(uiState.tunnelName, appUiState.tunnels) {
derivedStateOf {
appUiState.tunnels
.filter { it.id != tunnelConf?.id }
.any { it.name == uiState.tunnelName }
}
}
SecureScreenFromRecording()
LaunchedEffect(Unit) {
// set callback for navbar to invoke save
appViewModel.handleEvent(
AppEvent.SetScreenAction {
keyboardController?.hide()
if (!isTunnelNameTaken) {
save = true
}
}
)
}
LaunchedEffect(tunnelConf) { viewModel.initFromTunnel(tunnelConf) }
// TODO improve error messages
LaunchedEffect(save) {
if (save) {
try {
appViewModel.handleEvent(
AppEvent.SaveTunnel(
uiState.configProxy.buildTunnelConfFromState(uiState.tunnelName, tunnelConf)
)
)
appViewModel.handleEvent(
AppEvent.ShowMessage(StringValue.StringResource(R.string.config_changes_saved))
)
appViewModel.handleEvent(AppEvent.PopBackStack(true))
} catch (e: Exception) {
val message = e.message ?: context.resources.getString(R.string.unknown_error)
appViewModel.handleEvent(AppEvent.ShowMessage(StringValue.DynamicString(message)))
} finally {
save = false
}
}
}
if (uiState.showAuthPrompt) {
AuthorizationPrompt(
onSuccess = {
viewModel.toggleShowAuthPrompt()
viewModel.onAuthenticated()
},
onError = {
viewModel.toggleShowAuthPrompt()
appViewModel.handleEvent(
AppEvent.ShowMessage(
StringValue.StringResource(R.string.error_authentication_failed)
)
)
},
onFailure = {
viewModel.toggleShowAuthPrompt()
appViewModel.handleEvent(
AppEvent.ShowMessage(
StringValue.StringResource(R.string.error_authorization_failed)
)
)
},
)
}
Column(
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.spacedBy(24.dp, Alignment.Top),
modifier =
Modifier.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(top = 12.dp, bottom = 24.dp)
.padding(horizontal = 12.dp),
) {
InterfaceSection(isTunnelNameTaken, uiState, viewModel)
PeersSection(uiState, viewModel)
AddPeerButton(viewModel)
}
}
@@ -1,119 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config
import androidx.lifecycle.ViewModel
import com.zaneschepke.wireguardautotunnel.domain.model.TunnelConf
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
import com.zaneschepke.wireguardautotunnel.ui.state.ConfigProxy
import com.zaneschepke.wireguardautotunnel.ui.state.InterfaceProxy
import com.zaneschepke.wireguardautotunnel.ui.state.PeerProxy
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
@HiltViewModel
class ConfigViewModel @Inject constructor() : ViewModel() {
private val _uiState = MutableStateFlow(ConfigUiState())
val uiState: StateFlow<ConfigUiState> = _uiState.asStateFlow()
fun initFromTunnel(tunnelConf: TunnelConf?) {
if (tunnelConf == null) return
_uiState.update {
val proxy = ConfigProxy.from(tunnelConf.toAmConfig())
it.copy(
tunnelName = tunnelConf.name,
configProxy = proxy,
showScripts = proxy.hasScripts(),
showAmneziaValues = proxy.`interface`.junkPacketCount.isNotBlank(),
isAuthenticated = false,
)
}
}
fun updateTunnelName(name: String) {
_uiState.update { it.copy(tunnelName = name) }
}
fun updateInterface(newInterface: InterfaceProxy) {
_uiState.update { it.copy(configProxy = it.configProxy.copy(`interface` = newInterface)) }
}
fun toggleAmneziaValues() {
_uiState.update { it.copy(showAmneziaValues = !it.showAmneziaValues) }
}
fun toggleScripts() {
_uiState.update { it.copy(showScripts = !it.showScripts) }
}
fun toggleAmneziaCompatibility() {
val (show, `interface`) =
with(_uiState.value.configProxy) {
if (`interface`.isAmneziaCompatibilityModeSet()) {
Pair(false, `interface`.resetAmneziaProperties())
} else {
Pair(true, `interface`.toAmneziaCompatibilityConfig())
}
}
_uiState.update {
it.copy(
showAmneziaValues = show,
configProxy = it.configProxy.copy(`interface` = `interface`),
)
}
}
fun addPeer() {
_uiState.update { currentState ->
currentState.copy(
configProxy =
currentState.configProxy.copy(
peers = currentState.configProxy.peers + PeerProxy()
)
)
}
}
fun removePeer(index: Int) {
_uiState.update { currentState ->
currentState.copy(
configProxy =
currentState.configProxy.copy(
peers =
currentState.configProxy.peers.toMutableList().apply { removeAt(index) }
)
)
}
}
fun updatePeer(index: Int, peer: PeerProxy) {
_uiState.update { currentState ->
currentState.copy(
configProxy =
currentState.configProxy.copy(
peers =
currentState.configProxy.peers.toMutableList().apply {
set(index, peer)
}
)
)
}
}
fun toggleLanExclusion(index: Int) {
val peer = _uiState.value.configProxy.peers[index]
val updated = if (peer.isLanExcluded()) peer.includeLan() else peer.excludeLan()
updatePeer(index, updated)
}
fun onAuthenticated() {
_uiState.update { it.copy(isAuthenticated = true) }
}
fun toggleShowAuthPrompt() {
_uiState.update { it.copy(showAuthPrompt = !it.showAuthPrompt) }
}
}
@@ -1,74 +0,0 @@
package com.zaneschepke.wireguardautotunnel.ui.screens.main.config.components
import androidx.compose.foundation.focusGroup
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.zaneschepke.wireguardautotunnel.R
import com.zaneschepke.wireguardautotunnel.ui.common.label.GroupLabel
import com.zaneschepke.wireguardautotunnel.ui.common.textbox.ConfigurationTextBox
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.ConfigViewModel
import com.zaneschepke.wireguardautotunnel.ui.screens.main.config.state.ConfigUiState
import java.util.*
@Composable
fun InterfaceSection(
isTunnelNameTaken: Boolean,
uiState: ConfigUiState,
viewModel: ConfigViewModel,
) {
var isDropDownExpanded by remember { mutableStateOf(false) }
val isAmneziaCompatibilitySet =
remember(uiState.configProxy.`interface`) {
uiState.configProxy.`interface`.isAmneziaCompatibilityModeSet()
}
Surface(shape = RoundedCornerShape(12.dp), color = MaterialTheme.colorScheme.surface) {
Column(
verticalArrangement = Arrangement.spacedBy(12.dp),
modifier = Modifier.padding(horizontal = 16.dp).focusGroup(),
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.fillMaxWidth(),
) {
GroupLabel(stringResource(R.string.interface_))
InterfaceDropdown(
expanded = isDropDownExpanded,
onExpandedChange = { isDropDownExpanded = it },
showScripts = uiState.showScripts,
showAmneziaValues = uiState.showAmneziaValues,
isAmneziaCompatibilitySet = isAmneziaCompatibilitySet,
onToggleScripts = viewModel::toggleScripts,
onToggleAmneziaValues = viewModel::toggleAmneziaValues,
onToggleAmneziaCompatibility = viewModel::toggleAmneziaCompatibility,
)
}
ConfigurationTextBox(
value = uiState.tunnelName,
onValueChange = viewModel::updateTunnelName,
label = stringResource(R.string.name),
isError = isTunnelNameTaken,
hint =
stringResource(R.string.hint_template, stringResource(R.string.tunnel_name))
.lowercase(Locale.getDefault()),
modifier = Modifier.fillMaxWidth(),
)
InterfaceFields(
interfaceState = uiState.configProxy.`interface`,
showAuthPrompt = { viewModel.toggleShowAuthPrompt() },
showScripts = uiState.showScripts,
showAmneziaValues = uiState.showAmneziaValues,
onInterfaceChange = viewModel::updateInterface,
isAuthenticated = uiState.isAuthenticated,
)
}
}
}

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