feat: 提交0.4加速版二维码直传与自动配网优化

This commit is contained in:
zhy
2026-08-10 20:29:01 +08:00
parent 241e22abea
commit 0b7ffd2dab
17 changed files with 1462 additions and 111 deletions
@@ -17,29 +17,45 @@ data class PairingPayload(
val port: Int,
val endpoints: List<String>,
val wifi: WifiCredentials?,
val direction: TransferDirection = TransferDirection.UPLOAD_TO_PC,
val fileName: String? = null,
val fileSize: Long? = null,
val sha256: String? = null,
)
enum class TransferDirection { UPLOAD_TO_PC, DOWNLOAD_TO_PHONE }
object PairingProtocol {
const val PREFIX_V2 = "QFTPAIR2:"
const val PREFIX_SEND_V2 = "QFTSEND2:"
const val PREFIX_V1 = "QFTPAIR1:"
fun isPairingText(text: String): Boolean = text.startsWith(PREFIX_V2) || text.startsWith(PREFIX_V1)
fun isPairingText(text: String): Boolean =
text.startsWith(PREFIX_V2) || text.startsWith(PREFIX_SEND_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))
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_V1) -> parseV1(text.substring(PREFIX_V1.length))
else -> null
}
}
private fun parseV2(encoded: String): PairingPayload? {
private fun parseV2(encoded: String, expectedDirection: TransferDirection): PairingPayload? {
return try {
val jsonBytes = decodeUrlBase64(encoded) ?: return null
if (jsonBytes.size > 2048) return null
val obj = JSONObject(jsonBytes.toString(Charsets.UTF_8))
if (obj.optInt("v", 0) != 2) return null
val encodedDirection = obj.optString("d", "u")
val direction = when (encodedDirection) {
"u" -> TransferDirection.UPLOAD_TO_PC
"d" -> TransferDirection.DOWNLOAD_TO_PHONE
else -> return null
}
if (direction != expectedDirection) return null
val session = obj.getString("s")
if (!validSession(session)) return null
@@ -65,6 +81,16 @@ object PairingProtocol {
// and the actual Wi-Fi gateway are probed after Android obtains an IP.
if (wifi == null && endpoints.isEmpty()) return null
val fileName = if (direction == TransferDirection.DOWNLOAD_TO_PHONE) {
obj.optString("f").trim().takeIf { validFileName(it) } ?: return null
} else null
val fileSize = if (direction == TransferDirection.DOWNLOAD_TO_PHONE) {
obj.optLong("z", -1L).takeIf { it >= 0L } ?: return null
} else null
val sha = if (direction == TransferDirection.DOWNLOAD_TO_PHONE) {
obj.optString("h").lowercase().takeIf { it.matches(Regex("^[0-9a-f]{64}$")) } ?: return null
} else null
PairingPayload(
version = 2,
session = session,
@@ -73,6 +99,10 @@ object PairingProtocol {
port = port,
endpoints = endpoints,
wifi = wifi,
direction = direction,
fileName = fileName,
fileSize = fileSize,
sha256 = sha,
)
} catch (_: Exception) {
null
@@ -118,6 +148,7 @@ object PairingProtocol {
port = port,
endpoints = endpoints,
wifi = wifi,
direction = TransferDirection.UPLOAD_TO_PC,
)
} catch (_: Exception) {
null
@@ -132,6 +163,10 @@ object PairingProtocol {
return ssid.isNotBlank() && ssid.toByteArray(Charsets.UTF_8).size <= 32 && passphrase.length in 8..63
}
private fun validFileName(value: String): Boolean =
value.isNotBlank() && value.length <= 180 &&
value.none { it.code <= 31 || it.code == 127 || it in "\\/:*?\"<>|" }
/**
* Pairing QR is treated as untrusted input. QFT only probes literal local
* IPv4 addresses on the exact advertised port; public hosts and DNS names