diff --git a/app/src/main/java/com/freeturn/app/data/config/CoreConfigJson.kt b/app/src/main/java/com/freeturn/app/data/config/CoreConfigJson.kt index 8057318..089c799 100644 --- a/app/src/main/java/com/freeturn/app/data/config/CoreConfigJson.kt +++ b/app/src/main/java/com/freeturn/app/data/config/CoreConfigJson.kt @@ -76,6 +76,7 @@ data class CoreConfigJson( const val PROXY_MODE_UDP = "udp" const val TUNNEL_MODE_NONE = "none" const val TUNNEL_MODE_WG = "wg" + const val TUNNEL_MODE_AWG = "awg" const val PLATFORM_MOBILE = "mobile" fun encode(cfg: CoreConfigJson): String = json.encodeToString(cfg) @@ -143,8 +144,11 @@ fun ClientConfig.toCoreJson( log = CoreConfigJson.Log(debug = debugMode), kcp = if (tcpMode) srv.kcp.toCoreJson() else null, tunnel = CoreConfigJson.Tunnel( - mode = if (wireGuardActive) CoreConfigJson.TUNNEL_MODE_WG - else CoreConfigJson.TUNNEL_MODE_NONE, + mode = when { + !wireGuardActive -> CoreConfigJson.TUNNEL_MODE_NONE + wireGuardConfig.usesAmneziaWireGuard() -> CoreConfigJson.TUNNEL_MODE_AWG + else -> CoreConfigJson.TUNNEL_MODE_WG + }, config = if (wireGuardActive) wireGuardConfig else "", mtu = ClientConfig.WG_MTU, ), diff --git a/app/src/main/java/com/freeturn/app/data/config/WireGuardConfigType.kt b/app/src/main/java/com/freeturn/app/data/config/WireGuardConfigType.kt new file mode 100644 index 0000000..f21b03f --- /dev/null +++ b/app/src/main/java/com/freeturn/app/data/config/WireGuardConfigType.kt @@ -0,0 +1,53 @@ +package com.freeturn.app.data.config + +/** + * Determines whether a wg-quick configuration asks for AmneziaWG. + * + * The bundled core supports both `tunnel.mode = "wg"` and `"awg"`, but in + * `wg` mode it deliberately discards every Amnezia parameter. Keep this list + * in sync with `internal/tunnel/wgconf.applyInterface` in free-turn-proxy. + */ +internal fun String.usesAmneziaWireGuard(): Boolean { + var inInterfaceSection = false + + for (rawLine in lineSequence()) { + // Matches the bundled core parser: comments begin with '#' or ';'. + val line = rawLine + .substringBefore('#') + .substringBefore(';') + .trim() + + if (line.isEmpty()) continue + + if (line.startsWith('[') && line.endsWith(']')) { + inInterfaceSection = line.equals("[Interface]", ignoreCase = true) + continue + } + if (!inInterfaceSection) continue + + val separator = line.indexOf('=') + if (separator < 0) continue + val key = line.substring(0, separator).trim().lowercase() + if (key in amneziaInterfaceKeys) return true + } + + return false +} + +private val amneziaInterfaceKeys = setOf( + // AmneziaWG 1/2. + "jc", "jmin", "jmax", + "s1", "s2", "s3", "s4", + "h1", "h2", "h3", "h4", + "i1", "i2", "i3", "i4", "i5", + // AmneziaWG 3+. + "headerprotectionkey", + "contentpaddingaddition", + "rekeyaftertime", + "rekeytimeout", + "rejectaftertime", + "keepalivetimeout", + "maxhandshakeattempts", + "randomtrailers", + "disablecookies", +) diff --git a/app/src/test/java/com/freeturn/app/data/config/CoreConfigJsonTest.kt b/app/src/test/java/com/freeturn/app/data/config/CoreConfigJsonTest.kt index 6c1996d..460f54b 100644 --- a/app/src/test/java/com/freeturn/app/data/config/CoreConfigJsonTest.kt +++ b/app/src/test/java/com/freeturn/app/data/config/CoreConfigJsonTest.kt @@ -53,6 +53,61 @@ class CoreConfigJsonTest { assertEquals(ClientConfig.WG_MTU, tunnel["mtu"]!!.jsonPrimitive.content.toInt()) } + @Test + fun tunnelSelectsAwgForLegacyAmneziaParameters() { + val awg = base.copy( + tunnelTransport = TunnelTransport.WIREGUARD, + wireGuardConfig = """ + [Interface] + Address = 10.8.0.2/32 + Jc = 5 + Jmin = 10 + Jmax = 50 + S1 = 78 + + [Peer] + AllowedIPs = 0.0.0.0/0 + """.trimIndent(), + ) + val tunnel = parse(awg)["tunnel"]!!.jsonObject + assertEquals(CoreConfigJson.TUNNEL_MODE_AWG, tunnel["mode"]!!.jsonPrimitive.content) + } + + @Test + fun tunnelSelectsAwgForAwg3Parameters() { + val awg = base.copy( + tunnelTransport = TunnelTransport.WIREGUARD, + wireGuardConfig = """ + [interface] + Address = 10.8.0.2/32 + HeaderProtectionKey = AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA= + + [peer] + AllowedIPs = 0.0.0.0/0 + """.trimIndent(), + ) + val tunnel = parse(awg)["tunnel"]!!.jsonObject + assertEquals(CoreConfigJson.TUNNEL_MODE_AWG, tunnel["mode"]!!.jsonPrimitive.content) + } + + @Test + fun awgMarkersInCommentsOrPeerSectionAreIgnored() { + val wg = base.copy( + tunnelTransport = TunnelTransport.WIREGUARD, + wireGuardConfig = """ + [Interface] + Address = 10.8.0.2/32 + # Jc = 5 + + [Peer] + AllowedIPs = 0.0.0.0/0 + H1 = 123 + """.trimIndent(), + ) + val tunnel = parse(wg)["tunnel"]!!.jsonObject + assertEquals(CoreConfigJson.TUNNEL_MODE_WG, tunnel["mode"]!!.jsonPrimitive.content) + } + // Лишний ключ в proxy валит старт ядра (DisallowUnknownFields), а не тест схемы. @Test fun proxyHasModeAndListen() { @@ -154,3 +209,4 @@ class CoreConfigJsonTest { ) } } + diff --git a/gradle.properties b/gradle.properties index 565224b..afd9c86 100644 --- a/gradle.properties +++ b/gradle.properties @@ -22,4 +22,4 @@ android.uniquePackageNames=false android.dependency.useConstraints=true android.generateSyncIssueWhenLibraryConstraintsAreEnabled=false android.r8.strictFullModeForKeepRules=false -freeturnAar=3.2.0 +freeturnAar=3.4.0