feat: add Android to Android phone sharing
This commit is contained in:
@@ -21,23 +21,29 @@ data class PairingPayload(
|
||||
val fileName: String? = null,
|
||||
val fileSize: Long? = null,
|
||||
val sha256: String? = null,
|
||||
/** Internal per-file route used by Android → Android manifests. QR input
|
||||
* never controls this value; parsed pairing codes always use the default. */
|
||||
val downloadPath: String = "/qft/download",
|
||||
)
|
||||
|
||||
enum class TransferDirection { UPLOAD_TO_PC, DOWNLOAD_TO_PHONE }
|
||||
enum class TransferDirection { UPLOAD_TO_PC, DOWNLOAD_TO_PHONE, PHONE_TO_PHONE }
|
||||
|
||||
object PairingProtocol {
|
||||
const val PREFIX_V2 = "QFTPAIR2:"
|
||||
const val PREFIX_SEND_V2 = "QFTSEND2:"
|
||||
const val PREFIX_PHONE_V2 = "QFTPHONE2:"
|
||||
const val PREFIX_V1 = "QFTPAIR1:"
|
||||
|
||||
fun isPairingText(text: String): Boolean =
|
||||
text.startsWith(PREFIX_V2) || text.startsWith(PREFIX_SEND_V2) || text.startsWith(PREFIX_V1)
|
||||
text.startsWith(PREFIX_V2) || text.startsWith(PREFIX_SEND_V2) ||
|
||||
text.startsWith(PREFIX_PHONE_V2) || text.startsWith(PREFIX_V1)
|
||||
|
||||
fun parse(text: String): PairingPayload? {
|
||||
if (text.length > 4096) return null
|
||||
return when {
|
||||
text.startsWith(PREFIX_V2) -> parseV2(text.substring(PREFIX_V2.length), TransferDirection.UPLOAD_TO_PC)
|
||||
text.startsWith(PREFIX_SEND_V2) -> parseV2(text.substring(PREFIX_SEND_V2.length), TransferDirection.DOWNLOAD_TO_PHONE)
|
||||
text.startsWith(PREFIX_PHONE_V2) -> parseV2(text.substring(PREFIX_PHONE_V2.length), TransferDirection.PHONE_TO_PHONE)
|
||||
text.startsWith(PREFIX_V1) -> parseV1(text.substring(PREFIX_V1.length))
|
||||
else -> null
|
||||
}
|
||||
@@ -53,6 +59,7 @@ object PairingProtocol {
|
||||
val direction = when (encodedDirection) {
|
||||
"u" -> TransferDirection.UPLOAD_TO_PC
|
||||
"d" -> TransferDirection.DOWNLOAD_TO_PHONE
|
||||
"m" -> TransferDirection.PHONE_TO_PHONE
|
||||
else -> return null
|
||||
}
|
||||
if (direction != expectedDirection) return null
|
||||
@@ -67,7 +74,9 @@ object PairingProtocol {
|
||||
val ssid = wifiArray.optString(0)
|
||||
val passphrase = wifiArray.optString(1)
|
||||
if (!validWifi(ssid, passphrase)) return null
|
||||
WifiCredentials(ssid, passphrase, "WPA2")
|
||||
val security = wifiArray.optString(2, "WPA2")
|
||||
if (security !in setOf("WPA2", "WPA3")) return null
|
||||
WifiCredentials(ssid, passphrase, security)
|
||||
} else null
|
||||
|
||||
val endpoints = mutableListOf<String>()
|
||||
@@ -157,6 +166,34 @@ object PairingProtocol {
|
||||
|
||||
private fun decodeUrlBase64(encoded: String): ByteArray? = Base64Url.decode(encoded)
|
||||
|
||||
/** Build a compact Android-sender QR. File names and hashes stay in the
|
||||
* authenticated local manifest so a large queue does not make the QR dense. */
|
||||
fun encodePhoneShare(
|
||||
session: String,
|
||||
senderName: String,
|
||||
port: Int,
|
||||
endpoints: List<String>,
|
||||
wifi: WifiCredentials?,
|
||||
): String {
|
||||
require(validSession(session))
|
||||
require(port in 1..65535)
|
||||
val safeEndpoints = endpoints
|
||||
.map { it.trim().removeSuffix("/") }
|
||||
.filter { isAllowedLocalHttpEndpoint(it, port) }
|
||||
.distinct()
|
||||
require(wifi != null || safeEndpoints.isNotEmpty())
|
||||
val obj = JSONObject().apply {
|
||||
put("v", 2)
|
||||
put("s", session)
|
||||
put("n", senderName.take(80))
|
||||
put("p", port)
|
||||
put("d", "m")
|
||||
if (safeEndpoints.isNotEmpty()) put("e", org.json.JSONArray(safeEndpoints))
|
||||
wifi?.let { put("w", org.json.JSONArray(listOf(it.ssid, it.passphrase, it.security))) }
|
||||
}
|
||||
return PREFIX_PHONE_V2 + Base64Url.encode(obj.toString().toByteArray(Charsets.UTF_8))
|
||||
}
|
||||
|
||||
private fun validSession(session: String): Boolean = session.matches(Regex("^[A-Za-z0-9_-]{20,64}$"))
|
||||
|
||||
private fun validWifi(ssid: String, passphrase: String): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user