From ae52eee86bc65a215b2fb03cbd4a56e7318411e2 Mon Sep 17 00:00:00 2001 From: allenyelger-hash <268644732+allenyelger-hash@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:33:09 +0800 Subject: [PATCH] feat(app): app-cloning (cross-user) awareness in access control and app list cache - Enumerate cloned-app (dual app / clone user) instances in the access control list with a (clone) label suffix, sharing the selection state of their main-profile counterpart - Report cloned instances' uids in the app list cache so connections from cloned apps resolve to package names and PROCESS-NAME rules can match them - Show a one-time dialog with ADB grant instructions when clone profiles are detected but cross-user enumeration is denied - All cross-user enumeration fails soft and is a no-op without the ADB-granted INTERACT_ACROSS_USERS permission (declared since #788) --- .../kr328/clash/AccessControlActivity.kt | 54 ++++++++++++- .../kr328/clash/common/compat/CrossUser.kt | 80 +++++++++++++++++++ .../kr328/clash/design/AccessControlDesign.kt | 19 +++++ .../com/github/kr328/clash/design/util/App.kt | 6 +- design/src/main/res/values-zh/strings.xml | 3 + design/src/main/res/values/strings.xml | 3 + .../clash/module/AppListCacheModule.kt | 22 ++++- 7 files changed, 180 insertions(+), 7 deletions(-) create mode 100644 common/src/main/java/com/github/kr328/clash/common/compat/CrossUser.kt diff --git a/app/src/main/java/com/github/kr328/clash/AccessControlActivity.kt b/app/src/main/java/com/github/kr328/clash/AccessControlActivity.kt index 81beea03bc..4989319710 100644 --- a/app/src/main/java/com/github/kr328/clash/AccessControlActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/AccessControlActivity.kt @@ -7,7 +7,10 @@ import android.content.pm.ApplicationInfo import android.content.pm.PackageInfo import android.content.pm.PackageManager import androidx.core.content.getSystemService +import com.github.kr328.clash.common.compat.crossUserProfiles +import com.github.kr328.clash.common.compat.getInstalledPackagesAsUserCompat import com.github.kr328.clash.design.AccessControlDesign +import com.github.kr328.clash.design.R import com.github.kr328.clash.design.model.AppInfo import com.github.kr328.clash.design.util.toAppInfo import com.github.kr328.clash.service.store.ServiceStore @@ -20,6 +23,8 @@ import kotlinx.coroutines.selects.select import kotlinx.coroutines.withContext class AccessControlActivity : BaseActivity() { + private var crossUserHintPending = false + override suspend fun main() { val service = ServiceStore(this) @@ -56,6 +61,17 @@ class AccessControlActivity : BaseActivity() { when (it) { AccessControlDesign.Request.ReloadApps -> { design.patchApps(loadApps(selected)) + + if (crossUserHintPending) { + crossUserHintPending = false + + design.showCrossUserHint( + getString( + R.string.access_control_clone_hint_message, + packageName, + ) + ) + } } AccessControlDesign.Request.SelectAll -> { @@ -129,7 +145,7 @@ class AccessControlActivity : BaseActivity() { val pm = packageManager val packages = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS) - packages.asSequence() + val mainApps = packages.asSequence() .filter { it.packageName != packageName } @@ -145,8 +161,42 @@ class AccessControlActivity : BaseActivity() { .map { it.toAppInfo(pm) } - .sortedWith(comparator) .toList() + + val cloneSuffix = getString(R.string.access_control_clone_suffix) + + val cloneApps = crossUserProfiles().flatMap { profile -> + val clonePackages = getInstalledPackagesAsUserCompat( + PackageManager.GET_PERMISSIONS, + profile, + ) + + if (clonePackages == null) { + crossUserHintPending = true + + emptyList() + } else { + clonePackages.asSequence() + .filter { + it.packageName != packageName + } + .filter { + it.applicationInfo != null + } + .filter { + it.requestedPermissions?.contains(INTERNET) == true || it.applicationInfo!!.uid < android.os.Process.FIRST_APPLICATION_UID + } + .filter { + systemApp || !it.isSystemApp + } + .map { + it.toAppInfo(pm, cloneSuffix) + } + .toList() + } + } + + (mainApps + cloneApps).sortedWith(comparator) } private val PackageInfo.isSystemApp: Boolean diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/CrossUser.kt b/common/src/main/java/com/github/kr328/clash/common/compat/CrossUser.kt new file mode 100644 index 0000000000..94e12eed1e --- /dev/null +++ b/common/src/main/java/com/github/kr328/clash/common/compat/CrossUser.kt @@ -0,0 +1,80 @@ +package com.github.kr328.clash.common.compat + +import android.content.Context +import android.content.pm.PackageInfo +import android.content.pm.PackageManager +import android.os.Process +import android.os.UserHandle +import android.os.UserManager +import java.lang.reflect.Method + +/** + * Cross-user (app cloning / "app cloning" / work profile) package enumeration. + * + * Android VPNs are strictly per-user: a VPN established in the main profile + * never covers apps running in other profiles (e.g. OEM app-cloning instances + * in the clone user). Enumerating those instances nevertheless makes them + * visible in access control and lets the core resolve their uids. + * + * Cross-user package queries require INTERACT_ACROSS_USERS. The app declares + * it, but it is only grantable manually via ADB, e.g.: + * + * adb shell pm grant --user 0 android.permission.INTERACT_ACROSS_USERS + * + * Every function here fails soft: it returns empty/null instead of throwing + * when the user id is unavailable or the permission is missing. + */ + +private val getInstalledPackagesAsUserMethod: Method? by lazy { + runCatching { + PackageManager::class.java.getDeclaredMethod( + "getInstalledPackagesAsUser", + Int::class.javaPrimitiveType, + Int::class.javaPrimitiveType, + ) + }.getOrNull() +} + +private val userHandleGetIdentifierMethod: Method? by lazy { + runCatching { + UserHandle::class.java.getDeclaredMethod("getIdentifier") + }.getOrNull() +} + +private fun UserHandle.identifierCompat(): Int? { + val method = userHandleGetIdentifierMethod ?: return null + + return runCatching { + method.invoke(this) as Int + }.getOrNull() +} + +/** + * Profiles of the current user other than the main profile, e.g. work + * profiles and app-cloning ("dual app") profiles. + */ +fun Context.crossUserProfiles(): List { + val userManager = getSystemService(UserManager::class.java) ?: return emptyList() + + return runCatching { + userManager.userProfiles + .filter { it != Process.myUserHandle() } + }.getOrDefault(emptyList()) +} + +/** + * Installed packages of the given profile, or null when the query is not + * possible (missing INTERACT_ACROSS_USERS, hidden api blocked, ...). + */ +fun Context.getInstalledPackagesAsUserCompat( + flags: Int, + profile: UserHandle, +): List? { + val method = getInstalledPackagesAsUserMethod ?: return null + val userId = profile.identifierCompat() ?: return null + + return runCatching { + @Suppress("UNCHECKED_CAST") + method.invoke(packageManager, flags, userId) as? List + }.getOrNull() +} diff --git a/design/src/main/java/com/github/kr328/clash/design/AccessControlDesign.kt b/design/src/main/java/com/github/kr328/clash/design/AccessControlDesign.kt index cc1d2c8000..3a7f198b96 100644 --- a/design/src/main/java/com/github/kr328/clash/design/AccessControlDesign.kt +++ b/design/src/main/java/com/github/kr328/clash/design/AccessControlDesign.kt @@ -11,7 +11,9 @@ import com.github.kr328.clash.design.dialog.FullScreenDialog import com.github.kr328.clash.design.model.AppInfo import com.github.kr328.clash.design.store.UiStore import com.github.kr328.clash.design.util.* +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.* +import kotlin.coroutines.resume import kotlinx.coroutines.channels.Channel class AccessControlDesign( @@ -53,6 +55,23 @@ class AccessControlDesign( } } + suspend fun showCrossUserHint(message: CharSequence) { + withContext(Dispatchers.Main) { + suspendCancellableCoroutine { continuation -> + val dialog = MaterialAlertDialogBuilder(context) + .setTitle(R.string.access_control_clone_hint_title) + .setMessage(message) + .setPositiveButton(R.string.close) { _, _ -> } + .setOnDismissListener { + if (continuation.isActive) continuation.resume(Unit) + } + .show() + + continuation.invokeOnCancellation { dialog.dismiss() } + } + } + } + init { binding.self = this diff --git a/design/src/main/java/com/github/kr328/clash/design/util/App.kt b/design/src/main/java/com/github/kr328/clash/design/util/App.kt index 8bd37ac1f6..1824735622 100644 --- a/design/src/main/java/com/github/kr328/clash/design/util/App.kt +++ b/design/src/main/java/com/github/kr328/clash/design/util/App.kt @@ -5,11 +5,13 @@ import android.content.pm.PackageManager import com.github.kr328.clash.common.compat.foreground import com.github.kr328.clash.design.model.AppInfo -fun PackageInfo.toAppInfo(pm: PackageManager): AppInfo { +fun PackageInfo.toAppInfo(pm: PackageManager, labelSuffix: String? = null): AppInfo { + val label = applicationInfo!!.loadLabel(pm).toString() + return AppInfo( packageName = packageName, icon = applicationInfo!!.loadIcon(pm).foreground(), - label = applicationInfo!!.loadLabel(pm).toString(), + label = if (labelSuffix != null) "$label ($labelSuffix)" else label, installTime = firstInstallTime, updateDate = lastUpdateTime, ) diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index ee7daf75e1..372010aa01 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -1,6 +1,9 @@ 为应用配置访问权限 + 分身 + 检测到应用分身 + 此设备存在应用分身(双开)用户。Android 的 VPN 按用户隔离,分身用户内的应用不受主空间 VPN 管理。\n\n若分身应用在 VPN 开启时无法联网,可尝试:\n- 在系统 VPN 设置中关闭「阻止不带 VPN 的连接」\n- 将本应用加入应用分身,在分身空间内单独运行\n\n若希望在此列表中显示分身应用实例,请通过 ADB 授予跨用户权限后重启本应用:\n\nadb shell pm grant --user 0 %1$s android.permission.INTERACT_ACROSS_USERS 关于 访问控制模式 访问控制应用包列表 diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index a2f951e679..66123b9419 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -148,6 +148,9 @@ Access Control Mode Access Control Packages Configure access permission for apps + clone + App cloning detected + This device has app-cloning (dual app) profiles. Android isolates VPNs per user, so cloned app instances are outside the VPN of the main profile.\n\nIf cloned apps lose network access while the VPN is on, try one of the following:\n- Turn off \"Block connections without VPN\" in the system VPN settings\n- Clone this app into the cloned profile and run it separately\n\nTo list cloned app instances here, grant the cross-user permission via ADB and restart the app:\n\nadb shell pm grant --user 0 %1$s android.permission.INTERACT_ACROSS_USERS Options Unavailable until Clash Stopped diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/AppListCacheModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/AppListCacheModule.kt index f5d5b41e01..c1655dba6c 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/AppListCacheModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/AppListCacheModule.kt @@ -3,6 +3,8 @@ package com.github.kr328.clash.service.clash.module import android.app.Service import android.content.Intent import android.content.pm.PackageInfo +import com.github.kr328.clash.common.compat.crossUserProfiles +import com.github.kr328.clash.common.compat.getInstalledPackagesAsUserCompat import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.core.Clash import kotlinx.coroutines.channels.Channel @@ -13,9 +15,8 @@ class AppListCacheModule(service: Service) : Module(service) { private fun PackageInfo.uniqueUidName(): String = if (sharedUserId?.isNotBlank() == true) sharedUserId!! else packageName - private fun reload() { - val packages = service.packageManager.getInstalledPackages(0) - .filter { it.applicationInfo != null } + private fun List.toUidNames(): List> = + filter { it.applicationInfo != null } .groupBy { it.uniqueUidName() } .map { (_, v) -> val info = v[0] @@ -30,6 +31,21 @@ class AppListCacheModule(service: Service) : Module(service) { } } + private fun reload() { + val packages = buildList { + addAll(service.packageManager.getInstalledPackages(0).toUidNames()) + + // Packages in other profiles (e.g. app-cloning instances) have their + // own uids; enumerate them separately so connections from cloned apps + // can be resolved by package name. Requires INTERACT_ACROSS_USERS, + // silently skipped otherwise. + service.crossUserProfiles().forEach { profile -> + service.getInstalledPackagesAsUserCompat(0, profile) + ?.toUidNames() + ?.let(::addAll) + } + } + Clash.notifyInstalledAppsChanged(packages) Log.d("Installed ${packages.size} packages cached")