add AmneziaWG tunnel mode support and update core to 3.4.0
Automatically select tunnel.mode = "awg" when AmneziaWG parameters are present in [Interface], and update freeturnAar to 3.4.0.
This commit is contained in:
1 parent
c556a20e09
commit
e33fd3f982
4 files changed
+116
-3
No files matched your search
@@ -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,
|
||||
),
|
||||
|
||||
@@ -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",
|
||||
)
|
||||
@@ -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 {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -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
|
||||
Reference in new issue
Block a user