Android VpnService routes can select the primary VPN address for traffic to a second subnet even when both aliases are installed. Keep a bounded binding at the local TUN boundary so the peer receives the source it owns in that network and the original Android socket receives the reply. Preserve native primary traffic, separate per-subnet capacity, handle fragmented IPv4 and quoted ICMP errors, and retain eligible bindings when VpnService replaces its descriptor. Support TCP, UDP and ICMP echo. An ambiguous tuple retains its original binding. Explicitly bound traffic for other IPv4 protocols passes using the proper network address. Mesh authentication, address admission and wire formats stay unchanged. Reproduced the wrong source on a disposable Android API 37 ARM64 emulator. After the fix, system ping and an unbound connected UDP socket both received replies across each real VPN subnet. Built both debug APKs and confirmed that only the instrumentation APK includes its helper. Eleven focused Rust checks cover reverse mapping, checksums, fragments, limits and reconfiguration.
89 lines
3.8 KiB
Groovy
89 lines
3.8 KiB
Groovy
import groovy.json.JsonSlurper
|
|
|
|
plugins { id 'com.android.application' version '9.2.1' }
|
|
|
|
def workspace = rootProject.projectDir.parentFile
|
|
def abi = 'arm64-v8a'
|
|
android {
|
|
namespace 'cy.hexor.tsunagi'
|
|
compileSdk 35
|
|
def cargoNdk = providers.environmentVariable('ANDROID_NDK_HOME')
|
|
if (cargoNdk.isPresent()) {
|
|
ndkPath = cargoNdk.get()
|
|
def ndkProperties = new Properties()
|
|
file("$ndkPath/source.properties").withInputStream { ndkProperties.load(it) }
|
|
ndkVersion = ndkProperties.getProperty('Pkg.Revision')
|
|
}
|
|
|
|
// Cargo also builds standalone cdylibs for these dependencies. Their code
|
|
// is linked into tsunagi_android; the JVM loads only the application library.
|
|
packaging.jniLibs.excludes += ['**/libboringtun.so', '**/libiroh.so', '**/libiroh_relay.so']
|
|
defaultConfig {
|
|
applicationId 'cy.hexor.tsunagi'
|
|
minSdk 26
|
|
targetSdk 35
|
|
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
|
|
versionCode providers.gradleProperty('tsunagiVersionCode').getOrElse('1').toInteger()
|
|
versionName providers.gradleProperty('tsunagiVersionName').getOrElse('1.0.1')
|
|
ndk { abiFilters abi }
|
|
}
|
|
}
|
|
|
|
// rustls ships its Android trust manager as an AAR in the locked Cargo package.
|
|
def metadata = providers.exec {
|
|
workingDir workspace
|
|
commandLine 'cargo', 'metadata', '--locked', '--format-version', '1', '--filter-platform', 'aarch64-linux-android'
|
|
}.standardOutput.asText.get()
|
|
def cargoMetadata = new JsonSlurper().parseText(metadata)
|
|
def verifier = cargoMetadata.packages.find { it.name == 'rustls-platform-verifier-android' }
|
|
repositories {
|
|
maven {
|
|
url new File(new File(verifier.manifest_path).parent, 'maven')
|
|
metadataSources { mavenPom() }
|
|
content { includeModule 'rustls', 'rustls-platform-verifier' }
|
|
}
|
|
google()
|
|
mavenCentral()
|
|
}
|
|
dependencies {
|
|
implementation "rustls:rustls-platform-verifier:${verifier.version}"
|
|
androidTestImplementation 'androidx.test:runner:1.5.2'
|
|
androidTestImplementation 'junit:junit:4.13.2'
|
|
}
|
|
|
|
abstract class BuildRust extends Exec {
|
|
@OutputDirectory abstract DirectoryProperty getJniLibs()
|
|
}
|
|
|
|
androidComponents.onVariants(androidComponents.selector().all()) { variant ->
|
|
def mode = variant.buildType
|
|
def libs = layout.buildDirectory.dir("generated/rust/${mode}")
|
|
def nativeBuild = tasks.register("cargo${mode.capitalize()}", BuildRust) {
|
|
workingDir workspace
|
|
inputs.files(fileTree(workspace) {
|
|
include 'Cargo.toml', 'Cargo.lock', '.cargo/**', 'crates/**/Cargo.toml', 'crates/**/src/**', 'vendor/**'
|
|
})
|
|
jniLibs.set(libs)
|
|
commandLine 'cargo', 'ndk', '--target', abi, '--platform', '26', '-o', libs.get().asFile,
|
|
'build', '--locked', '--lib', '-p', 'tsunagi-android'
|
|
if (mode == 'release') args '--release'
|
|
}
|
|
variant.sources.jniLibs.addGeneratedSourceDirectory(nativeBuild) { it.jniLibs }
|
|
if (variant.androidTest != null) {
|
|
def probe = tasks.register('cargoVpnSourceProbe', BuildRust) {
|
|
workingDir workspace
|
|
jniLibs.set(layout.buildDirectory.dir('generated/rust/vpnSourceTest'))
|
|
inputs.files(nativeBuild.map { it.inputs.files }, fileTree("${workspace}/crates/tsunagi-android/examples"))
|
|
commandLine 'cargo', 'ndk', '--target', abi, '--platform', '26',
|
|
'build', '--locked', '--example', 'vpn_source_probe', '-p', 'tsunagi-android'
|
|
doLast {
|
|
project.copy {
|
|
from "${cargoMetadata.target_directory}/aarch64-linux-android/debug/examples/libvpn_source_probe.so"
|
|
into jniLibs.dir(abi)
|
|
}
|
|
}
|
|
}
|
|
variant.androidTest.sources.jniLibs.addGeneratedSourceDirectory(probe) { it.jniLibs }
|
|
}
|
|
}
|