Release WDTT Plus v1
Prepare WDTT Plus for the public fork repository: update release links, refresh the displayed release date, and publish the current WDTT Plus source tree on top of the original project history.
This commit is contained in:
1 parent
d82483ae36
commit
9b69abe3d5
127 files changed
+17412
-6579
No files matched your search
+238
@@ -0,0 +1,238 @@
|
||||
package com.wdtt.plus
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import android.util.Log
|
||||
import com.wireguard.android.backend.GoBackend
|
||||
import com.wireguard.android.backend.Tunnel
|
||||
import com.wireguard.config.Config
|
||||
import com.wireguard.config.Interface
|
||||
import com.wireguard.config.Peer
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.ByteArrayInputStream
|
||||
|
||||
class WireGuardHelper(context: Context) {
|
||||
private val appContext = context.applicationContext
|
||||
private val backend = (appContext as WdttApplication).getBackend(context)
|
||||
|
||||
private companion object {
|
||||
val wgMutex = Mutex()
|
||||
var sharedTunnel: WgTunnel? = null
|
||||
}
|
||||
|
||||
class WgTunnel(
|
||||
private val onExternalDown: (() -> Unit)? = null
|
||||
) : Tunnel {
|
||||
@Volatile
|
||||
var suppressDownCallback: Boolean = false
|
||||
|
||||
override fun getName() = "WDTT Plus"
|
||||
override fun onStateChange(newState: Tunnel.State) {
|
||||
if (newState != Tunnel.State.DOWN) return
|
||||
val isCurrentTunnel = sharedTunnel === this
|
||||
if (isCurrentTunnel) {
|
||||
sharedTunnel = null
|
||||
}
|
||||
if (isCurrentTunnel && !suppressDownCallback) {
|
||||
onExternalDown?.invoke()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun startTunnel(configString: String) = wgMutex.withLock {
|
||||
startTunnelLocked(configString)
|
||||
}
|
||||
|
||||
private suspend fun startTunnelLocked(configString: String) = withContext(Dispatchers.IO) {
|
||||
try {
|
||||
if (VpnService.prepare(appContext) != null) {
|
||||
throw IllegalStateException("VPN-разрешение не выдано")
|
||||
}
|
||||
|
||||
ensureGoBackendServiceStarted()
|
||||
|
||||
sharedTunnel?.let { existingTunnel ->
|
||||
try {
|
||||
existingTunnel.suppressDownCallback = true
|
||||
backend.setState(existingTunnel, Tunnel.State.DOWN, null)
|
||||
} catch (e: Exception) {
|
||||
Log.w("WG", "Failed to stop previous tunnel before restart: ${e.readableMessage()}")
|
||||
}
|
||||
sharedTunnel = null
|
||||
delay(150)
|
||||
}
|
||||
|
||||
val parsedConfig = Config.parse(ByteArrayInputStream(configString.toByteArray(Charsets.UTF_8)))
|
||||
|
||||
val builder = Interface.Builder()
|
||||
.parseAddresses(parsedConfig.`interface`.addresses.joinToString(", ") { it.toString() })
|
||||
|
||||
if (parsedConfig.`interface`.dnsServers.isNotEmpty()) {
|
||||
builder.parseDnsServers(parsedConfig.`interface`.dnsServers.joinToString(", ") { it.hostAddress ?: "" })
|
||||
}
|
||||
if (parsedConfig.`interface`.listenPort.isPresent) {
|
||||
builder.parseListenPort(parsedConfig.`interface`.listenPort.get().toString())
|
||||
}
|
||||
if (parsedConfig.`interface`.mtu.isPresent) {
|
||||
val serverMtu = parsedConfig.`interface`.mtu.get()
|
||||
// Используем серверное значение, но не менее 1280 для мобильных сетей
|
||||
builder.parseMtu(serverMtu.coerceAtLeast(1280).toString())
|
||||
} else {
|
||||
builder.parseMtu("1280")
|
||||
}
|
||||
builder.parsePrivateKey(parsedConfig.`interface`.keyPair.privateKey.toBase64())
|
||||
|
||||
// 1. Пакеты, которые всегда исключаются (наше приложение, ВК)
|
||||
// 2. Получаю настройки пользователя
|
||||
val settingsStore = SettingsStore(appContext)
|
||||
val selectedPackages = settingsStore.vpnAppPackages.first()
|
||||
.split(",")
|
||||
.map { it.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.toSet()
|
||||
val isWhitelist = settingsStore.isWhitelist.first()
|
||||
val installedPackages = appContext.packageManager
|
||||
.getInstalledApplications(0)
|
||||
.map { it.packageName }
|
||||
.toSet()
|
||||
val routing = resolveVpnAppRouting(
|
||||
isWhitelist = isWhitelist,
|
||||
selectedPackages = selectedPackages,
|
||||
installedPackages = installedPackages,
|
||||
ownPackageName = appContext.packageName
|
||||
)
|
||||
if (routing.included.isNotEmpty()) {
|
||||
builder.includeApplications(routing.included)
|
||||
} else if (routing.excluded.isNotEmpty()) {
|
||||
builder.excludeApplications(routing.excluded)
|
||||
}
|
||||
|
||||
val newInterface = builder.build()
|
||||
|
||||
val peerBuilder = Peer.Builder()
|
||||
val firstPeer = parsedConfig.peers.firstOrNull()
|
||||
?: throw IllegalStateException("WireGuard config has no peer")
|
||||
firstPeer.let { peer ->
|
||||
peerBuilder.parsePublicKey(peer.publicKey.toBase64())
|
||||
if (peer.preSharedKey.isPresent) peerBuilder.parsePreSharedKey(peer.preSharedKey.get().toBase64())
|
||||
if (peer.endpoint.isPresent) peerBuilder.parseEndpoint(peer.endpoint.get().toString())
|
||||
if (peer.persistentKeepalive.isPresent) peerBuilder.parsePersistentKeepalive(peer.persistentKeepalive.get().toString())
|
||||
}
|
||||
// Override AllowedIPs
|
||||
peerBuilder.parseAllowedIPs("0.0.0.0/0")
|
||||
|
||||
val finalConfig = Config.Builder()
|
||||
.setInterface(newInterface)
|
||||
.addPeer(peerBuilder.build())
|
||||
.build()
|
||||
|
||||
val nextTunnel = WgTunnel {
|
||||
TunnelManager.onWireGuardStoppedExternally()
|
||||
}
|
||||
setTunnelUpWithRetry(nextTunnel, finalConfig)
|
||||
sharedTunnel = nextTunnel
|
||||
Log.d("WG", "WireGuard tunnel started successfully")
|
||||
} catch (e: Exception) {
|
||||
val detailed = "WireGuard start failed: ${e.readableMessage()}; ${configString.describeWireGuardConfig()}"
|
||||
Log.e("WG", detailed)
|
||||
e.printStackTrace()
|
||||
throw IllegalStateException(detailed, e)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun reloadTunnel() = wgMutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
val currentTunnel = sharedTunnel ?: return@withContext
|
||||
try {
|
||||
val configFlow = TunnelManager.config.first() ?: return@withContext
|
||||
currentTunnel.suppressDownCallback = true
|
||||
backend.setState(currentTunnel, Tunnel.State.DOWN, null)
|
||||
sharedTunnel = null
|
||||
delay(150)
|
||||
startTunnelLocked(configFlow)
|
||||
Log.d("WG", "WireGuard tunnel reloaded for new exceptions")
|
||||
} catch (e: Exception) {
|
||||
Log.e("WG", "Failed to reload WireGuard: ${e.readableMessage()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun isTunnelUp(): Boolean = wgMutex.withLock {
|
||||
val current = sharedTunnel ?: return false
|
||||
return try {
|
||||
backend.getState(current) == Tunnel.State.UP
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun stopTunnel() = wgMutex.withLock {
|
||||
withContext(Dispatchers.IO) {
|
||||
try {
|
||||
sharedTunnel?.let {
|
||||
it.suppressDownCallback = true
|
||||
backend.setState(it, Tunnel.State.DOWN, null)
|
||||
sharedTunnel = null
|
||||
Log.d("WG", "WireGuard tunnel stopped")
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
Log.e("WG", "Failed to stop WireGuard: ${e.readableMessage()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun ensureGoBackendServiceStarted() {
|
||||
withContext(Dispatchers.Main) {
|
||||
runCatching {
|
||||
val intent = Intent(appContext, GoBackend.VpnService::class.java)
|
||||
appContext.startService(intent)
|
||||
}.onFailure {
|
||||
Log.w("WG", "GoBackend service warmup failed: ${it.readableMessage()}")
|
||||
}
|
||||
}
|
||||
delay(300)
|
||||
}
|
||||
|
||||
private suspend fun setTunnelUpWithRetry(nextTunnel: WgTunnel, finalConfig: Config) {
|
||||
var lastError: Exception? = null
|
||||
repeat(3) { attempt ->
|
||||
try {
|
||||
backend.setState(nextTunnel, Tunnel.State.UP, finalConfig)
|
||||
return
|
||||
} catch (e: Exception) {
|
||||
lastError = e
|
||||
Log.w("WG", "WireGuard UP attempt ${attempt + 1}/3 failed: ${e.readableMessage()}")
|
||||
runCatching { backend.setState(nextTunnel, Tunnel.State.DOWN, null) }
|
||||
ensureGoBackendServiceStarted()
|
||||
delay(250L * (attempt + 1))
|
||||
}
|
||||
}
|
||||
throw lastError ?: IllegalStateException("WireGuard UP failed")
|
||||
}
|
||||
|
||||
private fun Throwable.readableMessage(): String {
|
||||
val text = message ?: localizedMessage
|
||||
return if (text.isNullOrBlank()) this::class.java.simpleName else "${this::class.java.simpleName}: $text"
|
||||
}
|
||||
|
||||
private fun String.describeWireGuardConfig(): String {
|
||||
val lines = lineSequence().map { it.trim() }.filter { it.isNotEmpty() }.toList()
|
||||
val hasInterface = lines.any { it.equals("[Interface]", ignoreCase = true) }
|
||||
val hasPeer = lines.any { it.equals("[Peer]", ignoreCase = true) }
|
||||
val hasPrivateKey = lines.any { it.startsWith("PrivateKey", ignoreCase = true) }
|
||||
val hasPublicKey = lines.any { it.startsWith("PublicKey", ignoreCase = true) }
|
||||
val hasAddress = lines.any { it.startsWith("Address", ignoreCase = true) }
|
||||
val endpoint = lines.firstOrNull { it.startsWith("Endpoint", ignoreCase = true) }
|
||||
?.substringAfter("=", "")
|
||||
?.trim()
|
||||
?.take(80)
|
||||
?: "none"
|
||||
return "config lines=${lines.size}, interface=$hasInterface, peer=$hasPeer, privateKey=$hasPrivateKey, publicKey=$hasPublicKey, address=$hasAddress, endpoint=$endpoint"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user