feat: 控制通道权限分级(仅查看 / 查看+控制 / 全权限)
- 协议新增 msgType=10 access 应答,sessionConfig 增加 access 请求字段,三端同步 - rust_core:解码/编码 Access 事件,收到后入队给界面;send_control 支持 msgType=10 - android_receiver:新增 AccessLevel,被控端按本机上限裁剪请求并回送有效等级 - android_receiver:越权帧真正丢弃——输入/剪贴板需 control,文件子协议需 full 且回错误响应 - android_receiver:主界面增加权限单选(持久化),调整后当前连接立即生效并通知主控端 - flutter_sender:AppSettings 增加请求等级并在设置页可选,连接后自动下发 - flutter_sender:接收 msgType=10 更新有效等级,越权操作在界面禁用并提示 - docs:README/AGENTS 补充权限协议与各操作的最低等级
This commit is contained in:
1 parent
e2020ab595
commit
a1c3f79cd8
17 files changed
+504
-25
No files matched your search
@@ -13,12 +13,15 @@ import android.view.View
|
||||
import android.widget.Button
|
||||
import android.widget.ImageView
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.RadioButton
|
||||
import android.widget.RadioGroup
|
||||
import android.widget.ScrollView
|
||||
import android.widget.Switch
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import com.ttstd.lancast.receiver.discovery.HostBeacon
|
||||
import com.ttstd.lancast.receiver.file.StorageAccess
|
||||
import com.ttstd.lancast.receiver.protocol.AccessLevel
|
||||
import com.ttstd.lancast.receiver.session.PairingCode
|
||||
import com.ttstd.lancast.receiver.util.DeviceIdentity
|
||||
import com.ttstd.lancast.receiver.util.Fingerprint
|
||||
@@ -98,6 +101,40 @@ class MainActivity : Activity() {
|
||||
}
|
||||
}
|
||||
|
||||
val accessLabel = TextView(this).apply {
|
||||
text = "允许主控端:"
|
||||
textSize = 14f
|
||||
}
|
||||
val accessButtons = listOf(
|
||||
AccessLevel.VIEW to "仅查看",
|
||||
AccessLevel.CONTROL to "查看+控制",
|
||||
AccessLevel.FULL to "全权限(含文件)",
|
||||
).map { (level, label) ->
|
||||
RadioButton(this).apply {
|
||||
id = View.generateViewId()
|
||||
text = label
|
||||
textSize = 13f
|
||||
tag = level
|
||||
}
|
||||
}
|
||||
val accessGroup = RadioGroup(this).apply {
|
||||
orientation = RadioGroup.HORIZONTAL
|
||||
}.also { group ->
|
||||
accessButtons.forEach(group::addView)
|
||||
val current = Prefs.accessLevel(this)
|
||||
accessButtons.firstOrNull { (it.tag as Int) == current }?.isChecked = true
|
||||
group.setOnCheckedChangeListener { _, checkedId ->
|
||||
val level = accessButtons.firstOrNull { it.id == checkedId }?.tag as? Int
|
||||
?: return@setOnCheckedChangeListener
|
||||
MediaProjectionService.setAccessLevel(this, level)
|
||||
Toast.makeText(
|
||||
this,
|
||||
"已切换为${AccessLevel.name(level)},超出的操作会被丢弃",
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
val authorize = Button(this).apply {
|
||||
text = if (privileged) "系统签名:免授权投屏" else "授权屏幕录制(仅待命,不投屏)"
|
||||
isEnabled = !privileged
|
||||
@@ -128,6 +165,8 @@ class MainActivity : Activity() {
|
||||
addView(pairingQr)
|
||||
addView(autoSwitch)
|
||||
addView(confirmSwitch)
|
||||
addView(accessLabel)
|
||||
addView(accessGroup)
|
||||
addView(authorize)
|
||||
addView(startButton)
|
||||
addView(storageInfo)
|
||||
|
||||
+22
@@ -20,6 +20,7 @@ import android.view.Display
|
||||
import android.widget.Toast
|
||||
import com.ttstd.lancast.receiver.discovery.HostBeacon
|
||||
import com.ttstd.lancast.receiver.file.FileHttpServer
|
||||
import com.ttstd.lancast.receiver.protocol.AccessLevel
|
||||
import com.ttstd.lancast.receiver.protocol.MirrorAction
|
||||
import com.ttstd.lancast.receiver.util.DeviceIdentity
|
||||
import com.ttstd.lancast.receiver.util.Fingerprint
|
||||
@@ -75,6 +76,25 @@ class MediaProjectionService : Service() {
|
||||
var filePort = 0
|
||||
private set
|
||||
|
||||
/** 当前活跃会话,仅供本机界面调整权限使用。 */
|
||||
@Volatile
|
||||
private var sessionRef: ReceiverSession? = null
|
||||
|
||||
/**
|
||||
* 调整本机允许主控端使用的最高权限。
|
||||
*
|
||||
* 服务未运行时只写偏好设置,下次会话沿用;运行中则立即作用于当前连接。
|
||||
*/
|
||||
fun setAccessLevel(context: Context, level: Int) {
|
||||
val next = AccessLevel.clamp(level)
|
||||
val active = sessionRef
|
||||
if (active != null) {
|
||||
active.setMaxAccess(next)
|
||||
} else {
|
||||
Prefs.setAccessLevel(context, next)
|
||||
}
|
||||
}
|
||||
|
||||
/** 是否有待确认的投屏请求。 */
|
||||
@Volatile
|
||||
var pendingRequest = false
|
||||
@@ -261,6 +281,7 @@ class MediaProjectionService : Service() {
|
||||
}
|
||||
}
|
||||
session = created
|
||||
sessionRef = created
|
||||
created.start()
|
||||
startBeacon()
|
||||
startFileServer()
|
||||
@@ -483,6 +504,7 @@ class MediaProjectionService : Service() {
|
||||
filePort = 0
|
||||
session?.stop()
|
||||
session = null
|
||||
sessionRef = null
|
||||
worker.shutdownNow()
|
||||
fileServer?.stop()
|
||||
fileServer = null
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ttstd.lancast.receiver
|
||||
|
||||
import android.content.Context
|
||||
import com.ttstd.lancast.receiver.protocol.AccessLevel
|
||||
import java.security.SecureRandom
|
||||
|
||||
/**
|
||||
@@ -17,6 +18,7 @@ object Prefs {
|
||||
private const val KEY_FILE_SERVICE = "file_service_enabled"
|
||||
private const val KEY_FILE_TOKEN = "file_token"
|
||||
private const val KEY_MIRROR_CONFIRM = "mirror_confirm"
|
||||
private const val KEY_ACCESS_LEVEL = "access_level"
|
||||
|
||||
private fun prefs(context: Context) = context.applicationContext
|
||||
.getSharedPreferences(NAME, Context.MODE_PRIVATE)
|
||||
@@ -34,6 +36,16 @@ object Prefs {
|
||||
prefs(context).edit().putBoolean(KEY_MIRROR_CONFIRM, value).apply()
|
||||
}
|
||||
|
||||
/** 本机允许主控端使用的最高权限(view / control / full),默认 full 保持旧行为。 */
|
||||
fun accessLevel(context: Context): Int = AccessLevel.parse(
|
||||
prefs(context).getString(KEY_ACCESS_LEVEL, null),
|
||||
AccessLevel.FULL,
|
||||
)
|
||||
|
||||
fun setAccessLevel(context: Context, level: Int) {
|
||||
prefs(context).edit().putString(KEY_ACCESS_LEVEL, AccessLevel.name(level)).apply()
|
||||
}
|
||||
|
||||
fun fileServiceEnabled(context: Context): Boolean =
|
||||
prefs(context).getBoolean(KEY_FILE_SERVICE, true)
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ import com.ttstd.lancast.receiver.encode.VideoEncoder
|
||||
import com.ttstd.lancast.receiver.file.FileBrowser
|
||||
import com.ttstd.lancast.receiver.file.FileService
|
||||
import com.ttstd.lancast.receiver.input.InputInjector
|
||||
import com.ttstd.lancast.receiver.protocol.AccessLevel
|
||||
import com.ttstd.lancast.receiver.protocol.ControlEvent
|
||||
import com.ttstd.lancast.receiver.session.ReceiverTransport
|
||||
import com.ttstd.lancast.receiver.util.DeviceIdentity
|
||||
@@ -99,6 +100,10 @@ class ReceiverSession(
|
||||
@Volatile
|
||||
private var clipboardSyncEnabled: Boolean = true
|
||||
|
||||
/** 本机允许主控端使用的最高权限;改权限立即作用于当前连接并持久化。 */
|
||||
@Volatile
|
||||
private var maxAccessLevel: Int = Prefs.accessLevel(context)
|
||||
|
||||
/** 编码长边上限:随编码格式变化(软编降一档)。 */
|
||||
@Volatile
|
||||
private var maxLongSide: Int = MAX_LONG_SIDE
|
||||
@@ -199,6 +204,23 @@ class ReceiverSession(
|
||||
transport?.sendMirror(action, message)
|
||||
}
|
||||
|
||||
/** 当前允许的最高权限等级(`AccessLevel.VIEW/CONTROL/FULL`)。 */
|
||||
fun maxAccess(): Int = maxAccessLevel
|
||||
|
||||
/**
|
||||
* 调整允许主控端使用的最高权限。
|
||||
*
|
||||
* 立即作用于当前连接(超出部分的事件会被丢弃并收到 msgType=10 应答),并写入偏好设置,
|
||||
* 下次启动沿用。
|
||||
*/
|
||||
fun setMaxAccess(level: Int) {
|
||||
val next = AccessLevel.clamp(level)
|
||||
maxAccessLevel = next
|
||||
Prefs.setAccessLevel(context, next)
|
||||
transport?.maxAccess = next
|
||||
Log.i(TAG, "本机权限上限设为 ${AccessLevel.name(next)}")
|
||||
}
|
||||
|
||||
val connected: Boolean get() = transport?.keyEstablished == true
|
||||
|
||||
private fun startCapture() {
|
||||
@@ -325,6 +347,7 @@ class ReceiverSession(
|
||||
)
|
||||
created.onMirrorEvent = { action, message -> onMirrorRequest?.invoke(action, message) }
|
||||
created.onSessionConfig = { json -> applySessionConfig(json) }
|
||||
created.maxAccess = maxAccessLevel
|
||||
transport = created
|
||||
created.start()
|
||||
return
|
||||
|
||||
+2
-1
@@ -82,8 +82,9 @@ class InputInjector(private val context: Context) {
|
||||
is ControlEvent.Ping, is ControlEvent.Pong -> Unit
|
||||
// 投屏请求由 Service 处理,不参与输入注入。
|
||||
is ControlEvent.Mirror -> Unit
|
||||
// 会话配置由 ReceiverSession 处理。
|
||||
// 会话配置由 ReceiverSession 处理,权限应答由 ReceiverTransport 处理。
|
||||
is ControlEvent.SessionConfig -> Unit
|
||||
is ControlEvent.Access -> Unit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.ttstd.lancast.receiver.protocol
|
||||
|
||||
/**
|
||||
* 会话权限等级。
|
||||
*
|
||||
* 由被控端本机设定上限(持久化在 `Prefs`),主控端在
|
||||
* msgType=9 会话配置帧里用 `access` 字段请求,被控端取两者较小值作为有效等级,
|
||||
* 再用 msgType=10 把结果回给主控端。
|
||||
*
|
||||
* - [VIEW]:只能看画面(投屏),不能注入输入、不能同步剪贴板、不能访问文件。
|
||||
* - [CONTROL]:看画面 + 注入触摸/鼠标/键盘/滚轮 + 剪贴板同步。
|
||||
* - [FULL]:在上面基础上开放文件子协议(浏览/上传/下载)。
|
||||
*/
|
||||
object AccessLevel {
|
||||
const val VIEW = 0
|
||||
const val CONTROL = 1
|
||||
const val FULL = 2
|
||||
|
||||
const val VIEW_NAME = "view"
|
||||
const val CONTROL_NAME = "control"
|
||||
const val FULL_NAME = "full"
|
||||
|
||||
/** 解析名称或数字,非法值回落到 [fallback]。 */
|
||||
fun parse(value: String?, fallback: Int = CONTROL): Int {
|
||||
val text = value?.trim()?.lowercase().orEmpty()
|
||||
if (text.isEmpty()) return fallback
|
||||
return when (text) {
|
||||
VIEW_NAME, "0", "read", "readonly", "read-only" -> VIEW
|
||||
CONTROL_NAME, "1" -> CONTROL
|
||||
FULL_NAME, "2" -> FULL
|
||||
else -> text.toIntOrNull()?.coerceIn(VIEW, FULL) ?: fallback
|
||||
}
|
||||
}
|
||||
|
||||
fun name(level: Int): String = when (level.coerceIn(VIEW, FULL)) {
|
||||
VIEW -> VIEW_NAME
|
||||
CONTROL -> CONTROL_NAME
|
||||
else -> FULL_NAME
|
||||
}
|
||||
|
||||
fun clamp(level: Int): Int = level.coerceIn(VIEW, FULL)
|
||||
}
|
||||
+12
-1
@@ -22,10 +22,18 @@ sealed class ControlEvent {
|
||||
* 会话配置(msgType = 9)。
|
||||
*
|
||||
* payload 是 UTF-8 JSON,字段全部可选:
|
||||
* `preferH265` / `bitrateMbps` / `audioEnabled` / `clipboardSync`。
|
||||
* `preferH265` / `bitrateMbps` / `audioEnabled` / `clipboardSync` / `access`。
|
||||
* 主控端在请求投屏之前下发,被控端据此决定编码格式、码率与音频/剪贴板开关。
|
||||
*/
|
||||
data class SessionConfig(val json: String) : ControlEvent()
|
||||
|
||||
/**
|
||||
* 权限等级应答(msgType = 10)。
|
||||
*
|
||||
* payload 是 UTF-8 JSON:`{"level":0|1|2,"name":"view|control|full","max":0|1|2,"requested":0|1|2}`。
|
||||
* 被控端在收到 `access` 请求或本机上限变化后回送,主控端据此禁用超出权限的操作。
|
||||
*/
|
||||
data class Access(val json: String) : ControlEvent()
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,6 +133,7 @@ object ControlFrames {
|
||||
ControlEvent.Mirror(action, message)
|
||||
}
|
||||
9 -> ControlEvent.SessionConfig(String(payload, StandardCharsets.UTF_8))
|
||||
10 -> ControlEvent.Access(String(payload, StandardCharsets.UTF_8))
|
||||
else -> return null
|
||||
}
|
||||
return ControlFrame(version, sequence, event)
|
||||
@@ -153,6 +162,7 @@ object ControlFrames {
|
||||
byteArrayOf(event.action.toByte()) + event.message.toByteArray(StandardCharsets.UTF_8)
|
||||
}
|
||||
is ControlEvent.SessionConfig -> event.json.toByteArray(StandardCharsets.UTF_8)
|
||||
is ControlEvent.Access -> event.json.toByteArray(StandardCharsets.UTF_8)
|
||||
}
|
||||
return byteArrayOf(VERSION.toByte(), msgType(event).toByte()) +
|
||||
u16Bytes(sequence) + u32Bytes(payload.size) + payload
|
||||
@@ -168,6 +178,7 @@ object ControlFrames {
|
||||
is ControlEvent.Pong -> 6
|
||||
is ControlEvent.Mirror -> 8
|
||||
is ControlEvent.SessionConfig -> 9
|
||||
is ControlEvent.Access -> 10
|
||||
}
|
||||
|
||||
private fun u16(bytes: ByteArray, offset: Int) =
|
||||
|
||||
+103
-5
@@ -6,6 +6,7 @@ import android.util.Log
|
||||
import com.ttstd.lancast.receiver.clipboard.ClipboardBridge
|
||||
import com.ttstd.lancast.receiver.file.FileService
|
||||
import com.ttstd.lancast.receiver.input.InputInjector
|
||||
import com.ttstd.lancast.receiver.protocol.AccessLevel
|
||||
import com.ttstd.lancast.receiver.protocol.ControlEvent
|
||||
import com.ttstd.lancast.receiver.protocol.ControlFrames
|
||||
import com.ttstd.lancast.receiver.protocol.MirrorAction
|
||||
@@ -31,7 +32,11 @@ import java.util.concurrent.Executors
|
||||
* 跨网段时(TCP 直连不通)可用信令服务器中继控制通道:协议与直连完全一致,
|
||||
* 只是把加密帧整帧塞进 `/relay` 消息。中继不承载视频/音频,投屏请求会被直接拒绝。
|
||||
*
|
||||
* 握手后控制通道的机密性由控制帧 AES-GCM 保证,demo 阶段不再叠加 TLS。
|
||||
* 控制通道可叠加 TLS(被控端作为 TLS 服务端出示自签证书,主控端按 `tls` 指纹 pinning),
|
||||
* 无论是否 TLS,控制帧都要再套一层 AES-GCM。
|
||||
*
|
||||
* 权限分级:[maxAccess] 是本机上限,主控端通过 msgType=9 的 `access` 字段请求,
|
||||
* 有效等级取两者较小值,并用 msgType=10 回送。输入/剪贴板需要 control,文件需要 full。
|
||||
*/
|
||||
class ReceiverTransport(
|
||||
private val host: String,
|
||||
@@ -122,6 +127,33 @@ class ReceiverTransport(
|
||||
@Volatile
|
||||
var clipboardSyncEnabled: Boolean = true
|
||||
|
||||
/** 本机允许的最高权限等级,由被控端界面/偏好设置决定。 */
|
||||
@Volatile
|
||||
var maxAccess: Int = AccessLevel.FULL
|
||||
set(value) {
|
||||
val next = AccessLevel.clamp(value)
|
||||
field = next
|
||||
// 上限调低时立刻把有效等级压下去并通知主控端,不等下一帧。
|
||||
if (access > next) {
|
||||
access = next
|
||||
sendAccessReply(access)
|
||||
Log.i(TAG, "权限上限调整为 ${AccessLevel.name(next)},已通知主控端")
|
||||
}
|
||||
}
|
||||
|
||||
/** 当前生效的权限等级:`min(主控端请求, 本机上限)`。 */
|
||||
@Volatile
|
||||
var access: Int = AccessLevel.FULL
|
||||
private set
|
||||
|
||||
/** 已回送过的最新等级,避免每帧回一次应答。 */
|
||||
@Volatile
|
||||
private var reportedAccess: Int = -1
|
||||
|
||||
/** 因权限不足丢弃的事件计数,只用于日志去抖。 */
|
||||
@Volatile
|
||||
private var accessDrops: Int = 0
|
||||
|
||||
/** 回送投屏应答(GRANTED / DENIED / STOPPED / ERROR)。 */
|
||||
fun sendMirror(action: Int, message: String = "") {
|
||||
sendControl(ControlEvent.Mirror(action, message))
|
||||
@@ -237,6 +269,8 @@ class ReceiverTransport(
|
||||
running = true
|
||||
consecutiveFailures = 0
|
||||
Log.i(TAG, "已连接主控端 $host:$controlPort")
|
||||
// 先报一次当前等级,主控端在收到会话配置前也知道能不能操作。
|
||||
sendAccessReply()
|
||||
readLoop(input)
|
||||
return true
|
||||
}
|
||||
@@ -397,11 +431,16 @@ class ReceiverTransport(
|
||||
val plaintext = crypto.decodeControlFrame(secureFrame, controlKey) ?: return
|
||||
if (plaintext.size < 8) return
|
||||
when (plaintext[1].toInt() and 0xff) {
|
||||
7 -> handleFileFrame(plaintext.copyOfRange(8, plaintext.size))
|
||||
7 -> {
|
||||
val payload = plaintext.copyOfRange(8, plaintext.size)
|
||||
if (access < AccessLevel.FULL) rejectFileFrame(payload) else handleFileFrame(payload)
|
||||
}
|
||||
else -> {
|
||||
val frame = ControlFrames.decode(plaintext) ?: return
|
||||
when (val event = frame.event) {
|
||||
is ControlEvent.Clipboard -> if (clipboardSyncEnabled) {
|
||||
is ControlEvent.Clipboard -> if (access < AccessLevel.CONTROL) {
|
||||
dropForAccess("剪贴板")
|
||||
} else if (clipboardSyncEnabled) {
|
||||
clipboard.publish(event.text)
|
||||
}
|
||||
is ControlEvent.Ping -> sendControl(ControlEvent.Pong(event.timestamp))
|
||||
@@ -414,13 +453,71 @@ class ReceiverTransport(
|
||||
onMirrorEvent?.invoke(event.action, event.message)
|
||||
}
|
||||
}
|
||||
is ControlEvent.SessionConfig -> onSessionConfig?.invoke(event.json)
|
||||
else -> injector.inject(frame)
|
||||
is ControlEvent.SessionConfig -> {
|
||||
applyAccessRequest(event.json)
|
||||
onSessionConfig?.invoke(event.json)
|
||||
}
|
||||
// 权限应答是被控端发出去的,收到只可能来自旧版对端,忽略即可。
|
||||
is ControlEvent.Access -> Unit
|
||||
else -> if (access < AccessLevel.CONTROL) {
|
||||
dropForAccess("输入")
|
||||
} else {
|
||||
injector.inject(frame)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析主控端请求的权限等级:`{"access":"view|control|full"}`。
|
||||
*
|
||||
* 有效等级永远不超过 [maxAccess],结果变化时回送一次 msgType=10 应答,
|
||||
* 主控端据此把超出权限的操作禁掉。
|
||||
*/
|
||||
private fun applyAccessRequest(json: String) {
|
||||
val requested = try {
|
||||
AccessLevel.parse(JSONObject(json).optString("access"), access)
|
||||
} catch (_: Exception) {
|
||||
return
|
||||
}
|
||||
val next = minOf(requested, maxAccess)
|
||||
if (next != access) {
|
||||
access = next
|
||||
Log.i(TAG, "权限等级 ${AccessLevel.name(next)}(请求 ${AccessLevel.name(requested)},上限 ${AccessLevel.name(maxAccess)})")
|
||||
}
|
||||
if (next != reportedAccess) sendAccessReply(next)
|
||||
}
|
||||
|
||||
/** 主动回送当前有效等级(首次连接、上限调整、主控端重新请求时)。 */
|
||||
fun sendAccessReply(level: Int = access) {
|
||||
reportedAccess = level
|
||||
val payload = JSONObject()
|
||||
.put("level", level)
|
||||
.put("name", AccessLevel.name(level))
|
||||
.put("max", maxAccess)
|
||||
.put("requested", level)
|
||||
sendControl(ControlEvent.Access(payload.toString()))
|
||||
}
|
||||
|
||||
private fun dropForAccess(what: String) {
|
||||
if (accessDrops++ % 50 == 0) {
|
||||
Log.i(TAG, "当前权限为 ${AccessLevel.name(access)},丢弃${what}事件")
|
||||
}
|
||||
}
|
||||
|
||||
/** 文件子协议被权限拦截时也要回错误响应,否则主控端只能等到超时。 */
|
||||
private fun rejectFileFrame(payload: ByteArray) {
|
||||
val reason = "被控端当前权限为 ${AccessLevel.name(access)},文件访问需要 full"
|
||||
sendRaw(
|
||||
7,
|
||||
JSONObject()
|
||||
.put("op", "error")
|
||||
.put("error", reason)
|
||||
.toString(),
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* 空闲时轮询信令服务器,等待主控端发来的 `hs`。
|
||||
*
|
||||
@@ -499,6 +596,7 @@ class ReceiverTransport(
|
||||
running = true
|
||||
consecutiveFailures = 0
|
||||
Log.i(TAG, "已通过中继连接 $peerId")
|
||||
sendAccessReply()
|
||||
try {
|
||||
relayReadLoop(client, sessionKeys.control, peerId)
|
||||
} catch (error: Exception) {
|
||||
|
||||
Reference in new issue
Block a user