generated from ali/HarmonyTemplate
提交 Haven Android 仓库源码 v5.87.79
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package sh.haven.mail.bridge
|
||||
|
||||
import sh.haven.rclone.binding.mailbridge.Mailbridge
|
||||
|
||||
/**
|
||||
* Thin Kotlin wrapper around the gomobile-generated bindings for the Go
|
||||
* `mailbridge` module (Proton Mail via rclone's go-proton-api). Mirrors
|
||||
* [sh.haven.rclone.bridge.RcloneBridge]: a single [rpc] entry point with
|
||||
* JSON in/out, plus typed convenience helpers.
|
||||
*
|
||||
* State (logged-in, unlocked Proton sessions) lives in the Go process,
|
||||
* keyed by an opaque [sessionId] chosen by the caller. Outgoing HTTPS is
|
||||
* routed through a per-profile SOCKS5 tunnel when [socks] is supplied at
|
||||
* login — see TunnelResolver.socksEndpoint.
|
||||
*/
|
||||
object MailBridge {
|
||||
|
||||
data class RpcResult(
|
||||
val status: Int,
|
||||
val output: String,
|
||||
) {
|
||||
val isOk: Boolean get() = status == 200
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a mailbridge method.
|
||||
*
|
||||
* @param method one of: login, listFolders, listMessages, getMessage, send, logout
|
||||
* @param input JSON parameters (see the Go handlers for each shape)
|
||||
*/
|
||||
fun rpc(method: String, input: String = "{}"): RpcResult {
|
||||
val result = Mailbridge.mbRPC(method, input)
|
||||
return RpcResult(status = result.status.toInt(), output = result.output)
|
||||
}
|
||||
|
||||
/**
|
||||
* SRP login + optional TOTP 2FA + keyring unlock. On success the result
|
||||
* JSON carries { uid, accessToken, refreshToken, saltedKeyPass } for the
|
||||
* caller to persist (encrypted) and resume later.
|
||||
*
|
||||
* Status 412 with output `{"error":"2fa_required"}` /
|
||||
* `{"error":"mailbox_password_required"}` signals the caller to re-prompt
|
||||
* and retry with [twoFA] / [mailboxPassword] filled in.
|
||||
*/
|
||||
fun login(
|
||||
sessionId: String,
|
||||
username: String,
|
||||
password: String,
|
||||
mailboxPassword: String? = null,
|
||||
twoFA: String? = null,
|
||||
appVersion: String? = null,
|
||||
hostUrl: String? = null,
|
||||
socks: String? = null,
|
||||
): RpcResult = rpc("login", jsonObject(
|
||||
"sessionId" to sessionId,
|
||||
"username" to username,
|
||||
"password" to password,
|
||||
"mailboxPassword" to mailboxPassword,
|
||||
"twoFA" to twoFA,
|
||||
"appVersion" to appVersion,
|
||||
"hostUrl" to hostUrl,
|
||||
"socks" to socks,
|
||||
))
|
||||
|
||||
/** List folders/labels for the session as a JSON array. */
|
||||
fun listFolders(sessionId: String): RpcResult =
|
||||
rpc("listFolders", jsonObject("sessionId" to sessionId))
|
||||
|
||||
/** List message metadata for a label as a JSON array. */
|
||||
fun listMessages(sessionId: String, labelId: String, desc: Boolean = true): RpcResult =
|
||||
rpc("listMessages", jsonObject(
|
||||
"sessionId" to sessionId,
|
||||
"labelID" to labelId,
|
||||
"desc" to desc,
|
||||
))
|
||||
|
||||
/** Fetch and decrypt one message; output `{ "rfc822": "<base64>" }`. */
|
||||
fun getMessage(sessionId: String, messageId: String): RpcResult =
|
||||
rpc("getMessage", jsonObject(
|
||||
"sessionId" to sessionId,
|
||||
"messageID" to messageId,
|
||||
))
|
||||
|
||||
/** Revoke and drop the Proton session. */
|
||||
fun logout(sessionId: String): RpcResult =
|
||||
rpc("logout", jsonObject("sessionId" to sessionId))
|
||||
|
||||
// Minimal JSON object builder — avoids pulling a JSON lib into this thin
|
||||
// wrapper. Null values are omitted; strings/booleans are escaped.
|
||||
private fun jsonObject(vararg pairs: Pair<String, Any?>): String =
|
||||
pairs.filter { it.second != null }.joinToString(
|
||||
prefix = "{", postfix = "}", separator = ","
|
||||
) { (k, v) ->
|
||||
val value = when (v) {
|
||||
is Boolean -> v.toString()
|
||||
else -> "\"${escape(v.toString())}\""
|
||||
}
|
||||
"\"${escape(k)}\":$value"
|
||||
}
|
||||
|
||||
private fun escape(s: String): String = buildString {
|
||||
for (c in s) when (c) {
|
||||
'\\' -> append("\\\\")
|
||||
'"' -> append("\\\"")
|
||||
'\n' -> append("\\n")
|
||||
'\r' -> append("\\r")
|
||||
'\t' -> append("\\t")
|
||||
else -> append(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package sh.haven.rclone.bridge
|
||||
|
||||
import sh.haven.rclone.binding.rcbridge.Rcbridge
|
||||
|
||||
/**
|
||||
* Thin Kotlin wrapper around the gomobile-generated Java bindings for the
|
||||
* Go rcbridge module. All rclone functionality is accessed via the [rpc]
|
||||
* method which calls rclone's RC (Remote Control) API.
|
||||
*/
|
||||
object RcloneBridge {
|
||||
|
||||
data class RpcResult(
|
||||
val status: Int,
|
||||
val output: String,
|
||||
) {
|
||||
val isOk: Boolean get() = status == 200
|
||||
}
|
||||
|
||||
private var initialized = false
|
||||
|
||||
/**
|
||||
* Whether this build actually carries rclone's native code.
|
||||
*
|
||||
* Haven's terminal flavour (#510) links a `libgojni.so` built without the
|
||||
* rcbridge package — 8 MB against 28 MB, keeping tailscale, WireGuard and
|
||||
* the mail bridge. The library still loads, so everything else in it works;
|
||||
* only rclone's JNI methods are absent.
|
||||
*
|
||||
* The generated [Rcbridge] class registers those methods from a static
|
||||
* initialiser, so merely *touching* the class throws where they are
|
||||
* missing — an ExceptionInInitializerError the first time and
|
||||
* NoClassDefFoundError after that, neither of which is an Exception.
|
||||
* Hence [Throwable], and hence `touch()`, which gomobile emits as an empty
|
||||
* method for exactly this purpose: it forces class initialisation without
|
||||
* calling into rclone.
|
||||
*
|
||||
* Probed once and cached. Every entry point below checks it, so a caller
|
||||
* that forgets gets a no-op rather than taking the process down.
|
||||
*/
|
||||
val available: Boolean by lazy {
|
||||
try {
|
||||
Rcbridge.touch()
|
||||
true
|
||||
} catch (t: Throwable) {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialise the rclone library. Must be called once before any [rpc]
|
||||
* calls. Safe to call multiple times (subsequent calls are no-ops).
|
||||
*
|
||||
* @param configPath absolute path to the rclone config file
|
||||
* (e.g. `/data/data/sh.haven.app/files/rclone/rclone.conf`).
|
||||
* Pass empty string to use rclone's default.
|
||||
*/
|
||||
fun initialize(configPath: String) {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return
|
||||
if (initialized) return
|
||||
Rcbridge.rbInitialize(configPath)
|
||||
initialized = true
|
||||
}
|
||||
|
||||
/** Shut down the rclone library. Call once at app shutdown. */
|
||||
fun shutdown() {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return
|
||||
if (!initialized) return
|
||||
Rcbridge.rbFinalize()
|
||||
initialized = false
|
||||
}
|
||||
|
||||
/**
|
||||
* Set HTTPS_PROXY / HTTP_PROXY for outgoing rclone HTTP traffic.
|
||||
* Routes the embedded rclone process through the given proxy URL —
|
||||
* typically `socks5://127.0.0.1:<port>` for one of Haven's per-tunnel
|
||||
* SOCKS5 listeners (#149).
|
||||
*
|
||||
* Pass null or an empty string to clear both vars and route direct.
|
||||
*
|
||||
* Caveat: rclone caches HTTP clients per-fs, so changing the proxy
|
||||
* mid-session may not take effect until a fresh fs is created. For
|
||||
* the per-profile use case the caller sets this before the first
|
||||
* RPC for the profile, which is fresh.
|
||||
*/
|
||||
fun setProxy(proxyUrl: String?) {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return
|
||||
Rcbridge.rbSetProxy(proxyUrl.orEmpty())
|
||||
}
|
||||
|
||||
/**
|
||||
* Call an rclone RC method.
|
||||
*
|
||||
* @param method RC method name, e.g. "operations/list", "config/create"
|
||||
* @param input JSON string of method parameters, e.g. `{"fs":"remote:","remote":"/"}`
|
||||
* @return [RpcResult] with HTTP-style status code and JSON output
|
||||
*/
|
||||
fun rpc(method: String, input: String = "{}"): RpcResult {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return RpcResult(status = 501, output = """{"error":"rclone is not included in this build"}""")
|
||||
check(initialized) { "RcloneBridge.initialize() must be called first" }
|
||||
val result = Rcbridge.rbRPC(method, input)
|
||||
return RpcResult(
|
||||
status = result.status.toInt(),
|
||||
output = result.output,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Start a local HTTP server that streams files from the given rclone
|
||||
* remote via VFS. Binds to 127.0.0.1 with an auto-assigned port.
|
||||
*
|
||||
* @param remoteName rclone remote name without trailing colon, e.g. "gdrive"
|
||||
* @return [RpcResult] with JSON `{"port": N}` on success
|
||||
*/
|
||||
fun startMediaServer(remoteName: String, preferredPort: Long = 0): RpcResult {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return RpcResult(status = 501, output = """{"error":"rclone is not included in this build"}""")
|
||||
check(initialized) { "RcloneBridge.initialize() must be called first" }
|
||||
val result = Rcbridge.rbStartMediaServer(remoteName, preferredPort)
|
||||
return RpcResult(
|
||||
status = result.status.toInt(),
|
||||
output = result.output,
|
||||
)
|
||||
}
|
||||
|
||||
/** Query the current media server state. Returns JSON with "port" and optional "remote". */
|
||||
fun mediaServerStatus(): RpcResult {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return RpcResult(status = 501, output = """{"error":"rclone is not included in this build"}""")
|
||||
check(initialized) { "RcloneBridge.initialize() must be called first" }
|
||||
val result = Rcbridge.rbMediaServerStatus()
|
||||
return RpcResult(
|
||||
status = result.status.toInt(),
|
||||
output = result.output,
|
||||
)
|
||||
}
|
||||
|
||||
/** Stop the media streaming HTTP server if running. */
|
||||
fun stopMediaServer(): RpcResult {
|
||||
// #510: no-op rather than crash when this build has no rclone.
|
||||
if (!available) return RpcResult(status = 501, output = """{"error":"rclone is not included in this build"}""")
|
||||
check(initialized) { "RcloneBridge.initialize() must be called first" }
|
||||
val result = Rcbridge.rbStopMediaServer()
|
||||
return RpcResult(
|
||||
status = result.status.toInt(),
|
||||
output = result.output,
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user