generated from ali/HarmonyTemplate
提交 Haven Android 仓库源码 v5.87.79
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
plugins {
|
||||
alias(libs.plugins.android.library)
|
||||
}
|
||||
|
||||
android {
|
||||
namespace = "sh.haven.core.prns"
|
||||
compileSdk = 37
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 26
|
||||
}
|
||||
|
||||
compileOptions {
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
}
|
||||
|
||||
testOptions {
|
||||
unitTests.isReturnDefaultValues = true
|
||||
// Forward the host-capsule path into the forked test JVM so the live
|
||||
// engine test can run: ./gradlew :core:prns:testDebugUnitTest
|
||||
// -Dpersonal.rns.library=/abs/path/libprns_host.so
|
||||
unitTests.all { test ->
|
||||
System.getProperty("personal.rns.library")?.let {
|
||||
test.systemProperty("personal.rns.library", it)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// libprns_host.so is built from the prns submodule's Rust source by
|
||||
// buildPrnsNative below — never committed (F-Droid builds it from source).
|
||||
sourceSets {
|
||||
getByName("main") {
|
||||
jniLibs.srcDirs("src/main/jniLibs")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Cross-build the Prns C-ABI capsule for every shipped ABI. The crate at
|
||||
// prns/prns-host/abi/c is its own detached cargo workspace with a committed
|
||||
// Cargo.lock, so this builds exactly the pinned graph. The task lives HERE
|
||||
// rather than in the included JVM build so the prns submodule stays pristine
|
||||
// upstream trunk (same division as build-proot: Haven owns the build wiring,
|
||||
// the submodule owns the source).
|
||||
val buildPrnsNative by tasks.registering(Exec::class) {
|
||||
val crateDir = File(rootProject.projectDir, "prns/prns-host/abi/c")
|
||||
val jniDir = File(projectDir, "src/main/jniLibs")
|
||||
|
||||
inputs.dir(File(crateDir, "src"))
|
||||
inputs.file(File(crateDir, "Cargo.toml"))
|
||||
inputs.file(File(crateDir, "Cargo.lock"))
|
||||
// The capsule's engine sources live outside the detached workspace.
|
||||
inputs.dir(File(rootProject.projectDir, "prns/prns-host/core/src"))
|
||||
inputs.dir(File(rootProject.projectDir, "prns/prns-host/impls/native/src"))
|
||||
outputs.dir(jniDir)
|
||||
|
||||
workingDir = crateDir
|
||||
|
||||
val ndkHome = System.getenv("ANDROID_NDK_HOME")
|
||||
?: (System.getenv("ANDROID_SDK_ROOT") ?: System.getenv("ANDROID_HOME"))?.let { sdk ->
|
||||
File(sdk, "ndk").listFiles()?.maxByOrNull { it.name }?.absolutePath
|
||||
}
|
||||
if (ndkHome != null) environment("ANDROID_NDK_HOME", ndkHome)
|
||||
|
||||
// prns/rust-toolchain.toml pins floating `stable` (with only an embedded
|
||||
// riscv target), which on CI resolves to a toolchain without the Android
|
||||
// std libs → E0463 "can't find crate for core". Override with the repo's
|
||||
// pinned Rust (the one setup-toolchain pre-installs Android targets for;
|
||||
// keep in sync with .github/actions/setup-toolchain and the F-Droid
|
||||
// recipe's rustup line). Also upgrades a ref-name "pin" to a real one.
|
||||
environment("RUSTUP_TOOLCHAIN", "1.94.1")
|
||||
|
||||
// Adding an ABI to the APK means adding it here in the same change —
|
||||
// a missing target ships a silently absent (or stale) library.
|
||||
commandLine(
|
||||
"cargo", "ndk", "-o", jniDir.absolutePath,
|
||||
"-t", "arm64-v8a", "-t", "armeabi-v7a", "-t", "x86_64",
|
||||
"build", "--release",
|
||||
)
|
||||
}
|
||||
|
||||
tasks.configureEach {
|
||||
if (name == "preBuild") {
|
||||
dependsOn(buildPrnsNative)
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// Upstream's JVM SDK, substituted from the included build inside the
|
||||
// prns submodule (settings.gradle.kts). Its declared JNA is the desktop
|
||||
// JAR; Android needs the AAR (same version, ships jnidispatch per ABI),
|
||||
// so the JAR is excluded here rather than patching the submodule.
|
||||
api("rs.reticulum:personal-rns:0.3.6") {
|
||||
exclude(group = "net.java.dev.jna", module = "jna")
|
||||
}
|
||||
implementation(libs.jna) { artifact { type = "aar" } }
|
||||
implementation(libs.coroutines.core)
|
||||
|
||||
testImplementation(libs.junit)
|
||||
testImplementation(libs.coroutines.test)
|
||||
// jar variant for host-JVM tests (raw libc dial in PrnsEngineTest).
|
||||
testImplementation(libs.jna)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest />
|
||||
@@ -0,0 +1,109 @@
|
||||
package sh.haven.core.prns
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import rs.reticulum.prns.Bitrate
|
||||
import rs.reticulum.prns.BitrateAuto
|
||||
import rs.reticulum.prns.Capability
|
||||
import rs.reticulum.prns.CommandFailed
|
||||
import rs.reticulum.prns.CommandSettlement
|
||||
import rs.reticulum.prns.CommandSucceeded
|
||||
import rs.reticulum.prns.DestinationConfig
|
||||
import rs.reticulum.prns.Host
|
||||
import rs.reticulum.prns.HostOptions
|
||||
import rs.reticulum.prns.HostRole
|
||||
import rs.reticulum.prns.IdentityConfig
|
||||
import rs.reticulum.prns.IdentityConfigGenerateEphemeral
|
||||
import rs.reticulum.prns.SuppliedPipe
|
||||
import rs.reticulum.prns.SuppliedPipeOpener
|
||||
|
||||
/**
|
||||
* Opens one CONNECTED file descriptor for the engine, or returns a negative
|
||||
* value / throws to decline this attempt (the engine retries after the
|
||||
* attachment's respawn delay). This is where Haven's tunnel-aware dialing —
|
||||
* and, if Haven ever hosts a VpnService, `protect()` — happens: the open runs
|
||||
* in the caller's own coroutine context, never in a native callback
|
||||
* (Prns#94's pull-driven contract).
|
||||
*/
|
||||
typealias PrnsFdOpener = suspend () -> Int
|
||||
|
||||
/**
|
||||
* Thin lifecycle owner for a Prns (Personal Reticulum, Rust) host with
|
||||
* app-supplied transports. Second Reticulum engine next to reticulum-kt
|
||||
* ([sh.haven.core.reticulum.ReticulumTransport]'s kotlin stack) — experimental,
|
||||
* not yet wired into the connection flow.
|
||||
*
|
||||
* The native capsule (libprns_host.so) is built from the `prns` submodule by
|
||||
* `:core:prns:buildPrnsNative`; JVM tests point at a host build via
|
||||
* `-Dpersonal.rns.library=`.
|
||||
*/
|
||||
class PrnsEngine(
|
||||
private val scope: CoroutineScope,
|
||||
) : AutoCloseable {
|
||||
|
||||
private var host: Host? = null
|
||||
private val pipes = mutableListOf<Pair<SuppliedPipe, Job>>()
|
||||
|
||||
val isOpen: Boolean get() = host != null
|
||||
|
||||
/** Whether the running host's backend supports app-supplied transports. */
|
||||
fun supportsSuppliedPipe(): Boolean =
|
||||
requireHost().backendInfo.capabilities.contains(Capability.SUPPLIED_PIPE)
|
||||
|
||||
fun open(
|
||||
role: HostRole = HostRole.ENDPOINT,
|
||||
identity: IdentityConfig = IdentityConfigGenerateEphemeral,
|
||||
destinations: List<DestinationConfig> = emptyList(),
|
||||
) {
|
||||
check(host == null) { "engine already open" }
|
||||
host = Host(
|
||||
HostOptions(
|
||||
role = role,
|
||||
identity = identity,
|
||||
destinations = destinations,
|
||||
requiredCapabilities = setOf(Capability.SUPPLIED_PIPE),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches a supplied-Pipe interface whose descriptors come from [opener]
|
||||
* and starts serving its open requests on the engine scope. Returns the
|
||||
* attachment settlement (the interface is live once it succeeds).
|
||||
*/
|
||||
suspend fun attachSupplied(
|
||||
name: String,
|
||||
respawnDelayMillis: Long,
|
||||
opener: PrnsFdOpener,
|
||||
bitrate: Bitrate = BitrateAuto,
|
||||
): CommandSettlement {
|
||||
val pipe = requireHost().beginSuppliedPipe(name, respawnDelayMillis, bitrate)
|
||||
val serveJob = scope.launch {
|
||||
pipe.serve(SuppliedPipeOpener { opener() })
|
||||
}
|
||||
pipes += pipe to serveJob
|
||||
val settlement = pipe.awaitAttachment()
|
||||
if (settlement is CommandFailed) {
|
||||
serveJob.cancel()
|
||||
pipe.close()
|
||||
pipes.removeAll { it.first === pipe }
|
||||
}
|
||||
return settlement
|
||||
}
|
||||
|
||||
fun requireHost(): Host = checkNotNull(host) { "engine not open" }
|
||||
|
||||
override fun close() {
|
||||
pipes.forEach { (pipe, job) ->
|
||||
job.cancel()
|
||||
pipe.close()
|
||||
}
|
||||
pipes.clear()
|
||||
host?.close()
|
||||
host = null
|
||||
}
|
||||
}
|
||||
|
||||
/** True when a settlement carries a successful outcome. */
|
||||
fun CommandSettlement.succeeded(): Boolean = this is CommandSucceeded
|
||||
@@ -0,0 +1,94 @@
|
||||
package sh.haven.core.prns
|
||||
|
||||
import com.sun.jna.Library
|
||||
import com.sun.jna.Memory
|
||||
import com.sun.jna.Native
|
||||
import java.io.File
|
||||
import java.net.ServerSocket
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
import rs.reticulum.prns.SUPPLIED_PIPE_DECLINED
|
||||
|
||||
class PrnsEngineTest {
|
||||
|
||||
/** Raw libc dial — the fd is born outside the JVM's Socket machinery, so
|
||||
* ownership can transfer to the engine cleanly (same shape as an Android
|
||||
* opener using android.system.Os.socket/connect). */
|
||||
private interface LibC : Library {
|
||||
fun socket(domain: Int, type: Int, protocol: Int): Int
|
||||
fun connect(fd: Int, address: Memory, length: Int): Int
|
||||
|
||||
companion object {
|
||||
val INSTANCE: LibC = Native.load("c", LibC::class.java)
|
||||
}
|
||||
}
|
||||
|
||||
private fun dialLoopback(port: Int): Int {
|
||||
val fd = LibC.INSTANCE.socket(2 /* AF_INET */, 1 /* SOCK_STREAM */, 0)
|
||||
if (fd < 0) return -1
|
||||
val address = Memory(16).apply {
|
||||
clear()
|
||||
setByte(0, 2 /* AF_INET */)
|
||||
setByte(2, ((port shr 8) and 0xff).toByte())
|
||||
setByte(3, (port and 0xff).toByte())
|
||||
setByte(4, 127); setByte(5, 0); setByte(6, 0); setByte(7, 1)
|
||||
}
|
||||
return if (LibC.INSTANCE.connect(fd, address, 16) == 0) fd else -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure-classpath sanity: proves the upstream JVM SDK compiled through the
|
||||
* included build and substituted into this module. Runs everywhere — no
|
||||
* native library needed.
|
||||
*/
|
||||
@Test
|
||||
fun `sdk classes are on the classpath`() {
|
||||
assertEquals(-1, SUPPLIED_PIPE_DECLINED)
|
||||
}
|
||||
|
||||
/**
|
||||
* Live engine round-trip: open a host, attach a supplied pipe whose
|
||||
* opener dials a local listener, and see the attachment succeed and the
|
||||
* opener actually pulled. Requires a host-built capsule:
|
||||
*
|
||||
* cargo build --release (in prns/prns-host/abi/c)
|
||||
* ./gradlew :core:prns:testDebugUnitTest \
|
||||
* -Dpersonal.rns.library=$PWD/prns/prns-host/abi/c/target/release/libprns_host.so
|
||||
*
|
||||
* Skipped (Assume) when the property is absent so CI without a host Rust
|
||||
* build stays green — the registered upstream suite covers this path too.
|
||||
*/
|
||||
@Test
|
||||
fun `supplied pipe attaches and pulls the opener`() {
|
||||
val library = System.getProperty("personal.rns.library")
|
||||
assumeTrue(
|
||||
"set -Dpersonal.rns.library=<libprns_host.so> to run the live engine test",
|
||||
library != null && File(library).isFile,
|
||||
)
|
||||
|
||||
runBlocking {
|
||||
ServerSocket(0).use { listener ->
|
||||
var opened = 0
|
||||
PrnsEngine(this).use { engine ->
|
||||
engine.open()
|
||||
assertTrue(engine.supportsSuppliedPipe())
|
||||
val settlement = withTimeout(10_000) {
|
||||
engine.attachSupplied("test-pipe", respawnDelayMillis = 250, opener = {
|
||||
opened++
|
||||
dialLoopback(listener.localPort)
|
||||
})
|
||||
}
|
||||
assertTrue("attach failed: $settlement", settlement.succeeded())
|
||||
withTimeout(5_000) {
|
||||
while (opened == 0) delay(50)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user