LD-FDPI/mobile-app/docs/TUNNEL_BACKEND.md

3.2 KiB

AmneziaWG native backend

The app deliberately does not fake a VPN. LdFdpiVpnService only calls Builder.establish() when the native core is present. Without the core the UI stays usable but Connect is rejected with:

AmneziaWG çekirdeği (libawg.so) APK içinde yok.

Why not the stock WireGuard Android library?

com.wireguard.android:tunnel implements WireGuard but has no notion of AmneziaWG's Jc/Jmin/Jmax/S1-S4/H1-H4/HeaderProtectionKey fields. A standard WireGuard handshake will not be accepted by the AWG server. Shipping it and pretending it works would create a silent leak/blackhole.

JNI contract

Kotlin (NativeTunnel.kt):

external fun start(tunFd: Int, configJson: String): Boolean
external fun stop()

C/C++/Go exports:

JNIEXPORT jboolean JNICALL
Java_com_ldfdpi_mobile_NativeTunnel_start(JNIEnv *env, jobject thiz,
                                           jint tun_fd, jstring config_json);

JNIEXPORT void JNICALL
Java_com_ldfdpi_mobile_NativeTunnel_stop(JNIEnv *env, jobject thiz);

configJson has:

{
  "address": "10.8.1.20/32",
  "dns": ["1.1.1.1", "1.0.0.1"],
  "endpoint": "116.202.14.171:42771",
  "server_public_key": "...",
  "preshared_key": "...",
  "allowed_ips": ["0.0.0.0/0", "::/0"],
  "private_key": "...",
  "awg": {
    "jc": 5, "jmin": 10, "jmax": 50,
    "s1": 12, "s2": 12, "s3": 12, "s4": 12,
    "h1": 1, "h2": 2, "h3": 3, "h4": 4,
    "header_protection_key": "...",
    "rekey_after_time": "100-120",
    "rekey_timeout": "3-7",
    "reject_after_time": "150-180",
    "keepalive_timeout": "5-15"
  }
}

The native side must:

  1. Take ownership of the TUN fd (do not close it twice).
  2. Apply AWG obfuscation parameters before the first handshake.
  3. Pump packets until stop().
  4. Never log private_key or preshared_key.
  5. Return false on bad config/handshake failure so Kotlin closes the TUN and reports an error.

Building libawg.so from amneziawg-go

High-level, on Linux/WSL with Go + Android NDK:

git clone https://github.com/amnezia-vpn/amneziawg-go
cd amneziawg-go
# Add a small JNI export file that:
#  - parses configJson
#  - creates awg device with TUN fd
#  - applies Jc/Jmin/Jmax/S1-S4/H1-H4/HeaderProtectionKey
#  - runs device.Wait()
GOOS=android GOARCH=arm64 CGO_ENABLED=1 \
CC=$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin/aarch64-linux-android26-clang \
go build -buildmode=c-shared -o libawg.so ./jni

Place per ABI:

android/app/src/main/jniLibs/arm64-v8a/libawg.so
android/app/src/main/jniLibs/x86_64/libawg.so   # emulator

Then rebuild:

flutter build apk --release

Kill-switch reality on Android

Android does not allow an app to silently enable Always-on VPN or Block connections without VPN. Those are user-controlled system settings. The app deep-links to Settings.ACTION_VPN_SETTINGS; the UI explains that both must be enabled manually for a true fail-closed behavior.

VpnService itself captures all IPv4/IPv6 traffic while active and uses the tunnel DNS; that prevents normal DNS leaks. If the native core dies, the TUN remains and traffic is blackholed until Android tears the service down or the user disconnects. This is intentionally fail-closed, not a silent fallback.