Skip to content

Commit

Permalink
Merge pull request #235 from openziti/tunnel-sdk-update
Browse files Browse the repository at this point in the history
Fix Enable/Disable of identities
  • Loading branch information
ekoby authored Oct 30, 2024
2 parents 9b933db + 167e6e5 commit 171314d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build/
/captures
.externalNativeBuild

.kotlin/
.idea/*
!.idea/runConfigurations/
!.idea/copyright/
Expand Down
70 changes: 48 additions & 22 deletions app/src/main/java/org/openziti/mobile/TunnelModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.Context
import android.util.Log
import androidx.datastore.core.DataStore
import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
Expand Down Expand Up @@ -66,8 +67,8 @@ class TunnelModel(

fun setDNS(server: String?, range: String?) = runBlocking {
context().prefs.edit { settings ->
settings[NAMESERVER] = server ?: defaultDNS
settings[RANGE] = range ?: defaultRange
settings[NAMESERVER] = server ?: DEFAULT_DNS
settings[RANGE] = range ?: DEFAULT_RANGE
}
}

Expand All @@ -85,7 +86,11 @@ class TunnelModel(
}
}

class TunnelIdentity(val id: String, val tunnelModel: TunnelModel): ViewModel() {
class TunnelIdentity(
val id: String,
private val tunnel: TunnelModel,
enable: Boolean = true
): ViewModel() {

val zitiID: String =
with(URI(id)){
Expand All @@ -97,37 +102,44 @@ class TunnelModel(
internal val name = MutableLiveData(id)

fun status(): LiveData<String> = status
internal val status = MutableLiveData<String>("Loading")
internal val status = MutableLiveData("Loading")

internal val controller = MutableLiveData<String>("<controller")
internal val controller = MutableLiveData("<controller")
fun controller() = controller

private val enabled = MutableLiveData(true)
private val enabled = MutableLiveData(enable)
fun enabled(): LiveData<Boolean> = enabled

private val serviceMap = mutableMapOf<String, Service>()
private val services = MutableLiveData<List<Service>>()
fun services(): LiveData<List<Service>> = services

fun refresh() {
tunnelModel.refreshIdentity(id).handleAsync { _, ex ->
Log.w(TAG, "failed refresh", ex)
tunnel.refreshIdentity(id).handleAsync { _, ex ->
ex?.let {
Log.w(TAG, "failed refresh", it)
}
}
}

fun setEnabled(on: Boolean) {
tunnelModel.enableIdentity(id, on).thenAccept {
if (on)
Log.i(TAG, "enabling[${name.value}]")
else
Log.i(TAG, "disabling[${name.value}]")

tunnel.enableIdentity(id, on).thenAccept {
enabled.postValue(on)
if (on)
status.postValue("Disabled")
else
status.postValue("Enabled")
else
status.postValue("Disabled")
}
}

fun delete() {
setEnabled(false)
tunnelModel.deleteIdentity(id)
tunnel.deleteIdentity(id)
}

internal fun processServiceUpdate(ev: ServiceEvent) {
Expand Down Expand Up @@ -187,16 +199,23 @@ class TunnelModel(
}
}

private fun loadConfig(ident: String, cfg: ZitiConfig) {
Log.i("model", "loading identity[$ident]")
val cmd = LoadIdentity(ident, cfg)
private fun disabledKey(id: String) = booleanPreferencesKey("$id.disabled")

private fun loadConfig(id: String, cfg: ZitiConfig) {
val disabled = runBlocking {
context().prefs.data.map {
it[disabledKey(id)] ?: false
}.first()
}
Log.i("model", "loading identity[$id] disabled[$disabled]")
val cmd = LoadIdentity(id, cfg, disabled)
tunnel.processCmd(cmd).handleAsync { json: JsonElement? , ex: Throwable? ->
if (ex != null) {
Log.w("model", "failed to execute", ex)
} else {
identities[ident] = TunnelIdentity(ident, this)
identities[id] = TunnelIdentity(id, this, !disabled)
identitiesData.postValue(identities.values.toList())
Log.i("model", "load result[$ident]: $json")
Log.i("model", "load result[$id]: $json")
}
}
}
Expand Down Expand Up @@ -262,8 +281,15 @@ class TunnelModel(
private fun refreshIdentity(id: String) =
tunnel.processCmd(RefreshIdentity(id)).thenAccept{}

private fun enableIdentity(id: String, on: Boolean) =
tunnel.processCmd(OnOffCommand(id, on)).thenAccept {}
private fun enableIdentity(id: String, on: Boolean): CompletableFuture<Unit> {
val disabledKey = disabledKey(id)
runBlocking {
context().prefs.edit {
it[disabledKey] = !on
}
}
return tunnel.processCmd(OnOffCommand(id, on)).thenApply {}
}

private fun deleteIdentity(identifier: String) {
identities.remove(identifier)
Expand All @@ -288,8 +314,8 @@ class TunnelModel(
}

companion object {
val TAG = TunnelModel::class.simpleName
val defaultDNS = "100.64.0.2"
val defaultRange = "100.64.0.0/10"
const val TAG = "tunnel-model"
const val DEFAULT_DNS = "100.64.0.2"
const val DEFAULT_RANGE = "100.64.0.0/10"
}
}
2 changes: 0 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
* Copyright (c) 2021 NetFoundry. All rights reserved.
*/

// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
alias(libs.plugins.android.app) apply(false)
alias(libs.plugins.android.lib) apply(false)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android = "8.7.1"
kotlin = "2.0.21"

# ziti projects
ziti-tunnel-sdk = "1.2.0"
ziti-tunnel-sdk = "1.2.5"

# 3rd part deps
kotlinx-serialization-json = "1.7.3"
Expand Down
3 changes: 2 additions & 1 deletion tunnel/src/main/java/org/openziti/tunnel/TunnelCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum class CMD {

@Serializable data class OnOffCommand(
@SerialName("Identifier") val identifier: String,
val on: Boolean
@SerialName("OnOff") val on: Boolean
) : TunnelCommand(CMD.IdentityOnOff)

@Serializable data class RefreshIdentity(
Expand All @@ -67,6 +67,7 @@ enum class CMD {
@Serializable data class LoadIdentity(
@SerialName("Identifier") val identifier: String,
@SerialName("Config") val config: ZitiConfig,
@SerialName("Disabled") val disabled: Boolean,
): TunnelCommand(CMD.LoadIdentity)

@Serializable data class Dump(
Expand Down

0 comments on commit 171314d

Please sign in to comment.