fix: use testnet ip during bootstrap phase

This commit is contained in:
zaneschepke
2026-06-11 18:45:28 -04:00
parent 221b38a119
commit 588a2a18bd
7 changed files with 32 additions and 32 deletions
@@ -42,7 +42,9 @@ sealed class Route : NavKey {
@Keep @Serializable data object Display : Route()
@Keep @Serializable data object Tunnels : Route(), SecureRoute {
@Keep
@Serializable
data object Tunnels : Route(), SecureRoute {
override val requiresProtection: Boolean
get() = true
}
@@ -43,11 +43,7 @@ fun PeerStatisticsSection(peer: ActivePeer) {
color = color,
)
peer.endpoint?.let {
StatText(
stringResource(R.string.endpoint_template, it),
style = style,
color = color
)
StatText(stringResource(R.string.endpoint_template, it), style = style, color = color)
}
}
}
@@ -140,7 +140,7 @@ class TunnelBackend(
mode.config.`interface`.postUp?.let { runScripts(it, tunnel.id) }
tunnelJobs[result.tunnelId] =
startTunnelJobs(result.handle, tunnel, mode, result.removedPeerEndpoint)
startTunnelJobs(result.handle, tunnel, mode, result.replacedWithNonRoutable)
}
.onFailure { cleanup(tunnel.id) }
}
@@ -398,16 +398,16 @@ class TunnelBackend(
handle: Int,
tunnel: Tunnel,
mode: BackendMode,
removedPeerEndpoint: Boolean,
replacedWithNonRoutable: Boolean,
): Job {
return scope.launch {
supervisorScope {
if (removedPeerEndpoint) {
if (replacedWithNonRoutable) {
updateTunnelBootstrapState(tunnel.id, BootstrapState.ResolvingDns)
startDnsBootstrapJob(handle, tunnel, mode)
}
if (removedPeerEndpoint) {
if (replacedWithNonRoutable) {
when (val strategy = tunnel.ipStrategy) {
Tunnel.IpStrategy.Ipv4Only -> Unit
@@ -27,7 +27,7 @@ internal class WireGuardTunnelEngine(private val serviceHolder: ServiceHolder) :
val ifName = WGT_INTERFACE_PREFIX + tunnel.id
val (config, removedPeerEndpoint) = buildConfig(mode)
val (config, replacedWithNonRoutable) = buildConfig(mode)
// guard against static listenPort issues
val listenPort = config.`interface`.listenPort
@@ -77,7 +77,7 @@ internal class WireGuardTunnelEngine(private val serviceHolder: ServiceHolder) :
handle = handle,
interfaceName = ifName,
mode = mode,
removedPeerEndpoint = removedPeerEndpoint,
replacedWithNonRoutable = replacedWithNonRoutable,
)
}
@@ -91,16 +91,21 @@ internal class WireGuardTunnelEngine(private val serviceHolder: ServiceHolder) :
}
private fun buildConfig(mode: BackendMode): Pair<Config, Boolean> {
var removedPeerEndpoint = false
var replacedWithNonRoutable = false
return mode.config.copy(
peers =
mode.config.peers.map { peer ->
if (!peer.isStaticallyConfigured) {
removedPeerEndpoint = true
rewriteDynamicEndpoint(peer)
// keep support for valid configs with no endpoints
// replace domain configs with nonroutable and let the boostrap job update this
// with the real ip later
if (!peer.isStaticallyConfigured && peer.endpoint != null) {
replacedWithNonRoutable = true
val port = peer.endpoint!!.substringAfterLast(":")
peer.copy(endpoint = "$TEST_NET_IP:$port",
persistentKeepalive = 0)
} else peer
}
) to removedPeerEndpoint
) to replacedWithNonRoutable
}
private fun buildBridgeProxyConfig(): ProxyConfig {
@@ -149,11 +154,6 @@ internal class WireGuardTunnelEngine(private val serviceHolder: ServiceHolder) :
}
}
// omit peer endpoint while bootstrapping
private fun rewriteDynamicEndpoint(peer: PeerSection): PeerSection {
return peer.copy(endpoint = null)
}
override suspend fun stop(handle: Int, mode: BackendMode) {
when (mode) {
is BackendMode.Proxy.Standard -> stopProxyTunnel(handle)
@@ -270,6 +270,7 @@ internal class WireGuardTunnelEngine(private val serviceHolder: ServiceHolder) :
}
companion object {
const val TEST_NET_IP = "192.0.2.1"
const val WGT_INTERFACE_PREFIX = "wgtun"
}
}
@@ -6,13 +6,13 @@ import androidx.lifecycle.LifecycleService
import com.zaneschepke.tunnel.backend.Backend
import com.zaneschepke.tunnel.backend.ServiceHolder
import com.zaneschepke.tunnel.backend.ServiceHolder.Companion.alwaysOnCallback
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import org.koin.java.KoinJavaComponent.inject
import timber.log.Timber
import kotlin.concurrent.atomics.ExperimentalAtomicApi
class TunnelService : LifecycleService() {
@@ -56,10 +56,11 @@ class TunnelService : LifecycleService() {
override fun onDestroy() {
ServiceCompat.stopForeground(this, ServiceCompat.STOP_FOREGROUND_REMOVE)
serviceHolder.signalTunnelServiceDestroyed()
if(!userActivatedShutdown) {
if (!userActivatedShutdown) {
Timber.d("Service being killed by system, clean up tunnels")
shutdownScope.launch {
// TODO eventually, this should only shut down proxy mode tunnels with future multi tunnel
// TODO eventually, this should only shut down proxy mode tunnels with future multi
// tunnel
backend.stopAllActiveTunnels()
}
}
@@ -20,6 +20,9 @@ import com.zaneschepke.tunnel.model.KillSwitchConfig
import com.zaneschepke.tunnel.util.parseDns
import com.zaneschepke.tunnel.util.parseInetNetwork
import com.zaneschepke.wireguardautotunnel.parser.Config
import java.io.IOException
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
@@ -29,9 +32,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.koin.java.KoinJavaComponent.inject
import timber.log.Timber
import java.io.IOException
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.time.Duration.Companion.milliseconds
class VpnService : android.net.VpnService(), KillSwitch, SocketProtector {
@@ -72,10 +72,11 @@ class VpnService : android.net.VpnService(), KillSwitch, SocketProtector {
hevBridgeJob?.cancel()
serviceScope.cancel()
stopHevSocks5Bridge()
if(!userActivatedShutdown) {
if (!userActivatedShutdown) {
Timber.d("Service being killed by system, clean up tunnels")
shutdownScope.launch {
// TODO eventually, this should only shut down vpn mode tunnels with future multi tunnel
// TODO eventually, this should only shut down vpn mode tunnels with future
// multi tunnel
backend.stopAllActiveTunnels()
}
}
@@ -227,7 +228,6 @@ class VpnService : android.net.VpnService(), KillSwitch, SocketProtector {
?.map { it.trim() }
?.filter { it.isNotEmpty() }
?.forEach { entry ->
val (address, prefix) = entry.parseInetNetwork()
if (prefix == 0) {
@@ -7,5 +7,5 @@ data class EngineStartResult(
val handle: Int,
val interfaceName: String,
val mode: BackendMode,
val removedPeerEndpoint: Boolean,
val replacedWithNonRoutable: Boolean,
)