diff options
author | Mikael Magnusson <mikma@users.sourceforge.net> | 2020-05-15 22:54:48 +0200 |
---|---|---|
committer | Mikael Magnusson <mikma@users.sourceforge.net> | 2022-03-18 23:38:55 +0100 |
commit | c5c714716e58aee4aa2922eb30d1459a51da4200 (patch) | |
tree | 2a284ad368d7cf04bd8e364c759d0c9f23099111 | |
parent | ca65f3ceba613421aa6704e0fab9aa865a35c030 (diff) |
WIP tunnel manager
-rw-r--r-- | ui/src/main/java/com/wireguard/android/model/TunnelManager.kt | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt index ec796164..ab0e2e79 100644 --- a/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt +++ b/ui/src/main/java/com/wireguard/android/model/TunnelManager.kt @@ -33,11 +33,17 @@ import kotlinx.coroutines.awaitAll import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import java9.util.concurrent.CompletableFuture +import java9.util.concurrent.CompletionStage +import java.io.BufferedReader +import java.io.StringReader +import java.util.ArrayList /** * Maintains and mediates changes to the set of available WireGuard tunnels, */ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { + private val TAG: String = "WireGuard/TunnelManager" private val tunnels = CompletableDeferred<ObservableSortedKeyedArrayList<String, ObservableTunnel>>() private val context: Context = get() private val tunnelMap: ObservableSortedKeyedArrayList<String, ObservableTunnel> = ObservableSortedKeyedArrayList(TunnelComparator) @@ -158,7 +164,17 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { })!! } +<<<<<<< HEAD suspend fun setTunnelName(tunnel: ObservableTunnel, name: String): String = withContext(Dispatchers.Main.immediate) { +======= + fun setTunnelUserspaceConfig(tunnel: ObservableTunnel, config: Config): CompletionStage<Config> = getAsyncWorker().supplyAsync { + Log.println(Log.INFO, TAG, "Async setTunnelUserspaceConfig " + config); + getBackend().setState(tunnel, tunnel.state, config) + configStore.save(tunnel.name, config) + }.thenApply { getBackend().applyUserspaceConfig(tunnel, it) } + + fun setTunnelName(tunnel: ObservableTunnel, name: String): CompletionStage<String> { +>>>>>>> WIP tunnel manager if (Tunnel.isNameInvalid(name)) throw IllegalArgumentException(context.getString(R.string.tunnel_error_invalid_name)) if (tunnelMap.containsKey(name)) { @@ -211,7 +227,9 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { } class IntentReceiver : BroadcastReceiver() { + private val TAG: String = "WireGuard/IntentReceiver" override fun onReceive(context: Context, intent: Intent?) { +<<<<<<< HEAD applicationScope.launch { val manager = getTunnelManager() if (intent == null) return@launch @@ -236,6 +254,71 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { } catch (e: Throwable) { Toast.makeText(context, ErrorMessages[e], Toast.LENGTH_LONG).show() } +======= + Log.println(Log.INFO, TAG, "Broadcast received " + intent) + val manager = getTunnelManager() + if (intent == null) return + val action = intent.action ?: return + if ("com.wireguard.android.action.REFRESH_TUNNEL_STATES" == action) { + manager.refreshTunnelStates() + return + } + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || !getSharedPreferences().getBoolean("allow_remote_control_intents", false)) + return + if ("com.wireguard.android.action.SET_TUNNEL_UP" == action || + "com.wireguard.android.action.SET_TUNNEL_DOWN" == action) + changeState(manager, intent, action) + else if ("com.wireguard.android.action.GET_TUNNEL_CONFIG" == action || + "com.wireguard.android.action.SET_TUNNEL_CONFIG" == action || + "com.wireguard.android.action.SET_TUNNEL_USERSPACE_CONFIG" == action) + configAction(manager, intent, action) + } + + private fun changeState(manager: TunnelManager, intent: Intent, action: String) { + val state: Tunnel.State + state = when (action) { + "com.wireguard.android.action.SET_TUNNEL_UP" -> Tunnel.State.UP + "com.wireguard.android.action.SET_TUNNEL_DOWN" -> Tunnel.State.DOWN + else -> return + } + val tunnelName = intent.getStringExtra("tunnel") ?: return + manager.tunnels.thenAccept { + val tunnel = it[tunnelName] ?: return@thenAccept + manager.setTunnelState(tunnel, state) +>>>>>>> WIP tunnel manager + } + } + + private fun configAction(manager: TunnelManager, intent: Intent, action: String) { + val code: ActionCode + code = when (action) { + "com.wireguard.android.action.SET_TUNNEL_CONFIG" -> ActionCode.SET_CONFIG + "com.wireguard.android.action.SET_USERSPACE_CONFIG" -> ActionCode.SET_USERSPACE_CONFIG + "com.wireguard.android.action.GET_TUNNEL_CONFIG" -> ActionCode.GET_CONFIG + else -> return + } + val tunnelName = intent.getStringExtra("tunnel") ?: return + manager.tunnels.thenAccept { + val tunnel = it[tunnelName] ?: return@thenAccept + if (code == ActionCode.GET_CONFIG) { + val config = manager.getTunnelConfig(tunnel) + Log.println(Log.INFO, TAG, "Call getTunnelConfig " + config) + // TODO return config + } else { + val strConfig = intent.getStringExtra("config") ?: return@thenAccept + val config = Config.parse(BufferedReader(StringReader(strConfig))) + when (code) { + ActionCode.SET_CONFIG -> { + Log.println(Log.INFO, TAG, "Call setTunnelConfig " + config) + manager.setTunnelConfig(tunnel, config) + } + ActionCode.SET_USERSPACE_CONFIG -> { + Log.println(Log.INFO, TAG, "Call setTunnelUserspaceConfig " + config) + manager.setTunnelUserspaceConfig(tunnel, config) + } + else -> return@thenAccept + } + } } } } @@ -251,4 +334,10 @@ class TunnelManager(private val configStore: ConfigStore) : BaseObservable() { companion object { private const val TAG = "WireGuard/TunnelManager" } + + enum class ActionCode { + SET_CONFIG, + GET_CONFIG, + SET_USERSPACE_CONFIG + } } |