Skip to content

Replace package names on UIDs in custom config#5527

Open
fanymagnet wants to merge 1 commit into2dust:masterfrom
fanymagnet:patch-1
Open

Replace package names on UIDs in custom config#5527
fanymagnet wants to merge 1 commit into2dust:masterfrom
fanymagnet:patch-1

Conversation

@fanymagnet
Copy link
Copy Markdown

No description provided.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates custom-config handling so that, when TUN/VPN mode is enabled, routing rules can convert app package names to UIDs and the config can be auto-augmented with a tun inbound when missing.

Changes:

  • Parse custom config JSON and rewrite routing.rules[*].process from package names to UID strings.
  • Detect whether a tun inbound exists (now by protocol == "tun") and inject the template tun inbound if absent.
  • Refactor JSON traversal to use safer takeIf { isJson* } checks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +110 to +115
val packages = process.mapNotNull {
it.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
}.takeIf { it.isNotEmpty() } ?: continue
val uids = PackageUidResolver.packageNamesToUids(context, packages).takeIf { it.isNotEmpty() } ?: continue

rule.add("process", JsonArray().apply { uids.forEach { add(it) } })
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The conversion overwrites process with only the successfully-resolved package UIDs. If the original array contains numeric UID strings, or package names that fail to resolve, those entries are silently dropped, changing routing behavior. Prefer converting element-by-element (e.g., keep numeric UID strings as-is; replace only resolvable package names; preserve unresolved values).

Suggested change
val packages = process.mapNotNull {
it.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
}.takeIf { it.isNotEmpty() } ?: continue
val uids = PackageUidResolver.packageNamesToUids(context, packages).takeIf { it.isNotEmpty() } ?: continue
rule.add("process", JsonArray().apply { uids.forEach { add(it) } })
val resolvedProcess = JsonArray()
for (processElem in process) {
val processName = processElem
.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }
?.asString
if (processName == null) {
resolvedProcess.add(processElem)
continue
}
if (processName.toIntOrNull() != null) {
resolvedProcess.add(processName)
continue
}
val uid = PackageUidResolver.packageNamesToUids(context, listOf(processName)).firstOrNull()
resolvedProcess.add(uid ?: processName)
}
rule.add("process", resolvedProcess)

Copilot uses AI. Check for mistakes.
json.getAsJsonArray("inbounds")
} else {
JsonArray()
// check if package names need to replaced on UIDs
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grammar: "need to replaced on UIDs" is incorrect; consider rephrasing to "need to be replaced with UIDs" (or similar).

Suggested change
// check if package names need to replaced on UIDs
// Check whether package names need to be replaced with UIDs

Copilot uses AI. Check for mistakes.
Comment on lines +107 to +115
for (elem in rulesJson) {
val rule = elem.takeIf { it.isJsonObject }?.asJsonObject ?: continue
val process = rule.get("process")?.takeIf { it.isJsonArray }?.asJsonArray ?: continue
val packages = process.mapNotNull {
it.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
}.takeIf { it.isNotEmpty() } ?: continue
val uids = PackageUidResolver.packageNamesToUids(context, packages).takeIf { it.isNotEmpty() } ?: continue

rule.add("process", JsonArray().apply { uids.forEach { add(it) } })
Copy link

Copilot AI Apr 23, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

process replacement runs for every custom config when needTun() is true, but UID-based process routing is only supported when SettingsManager.canUseProcessRouting() is true (see getRoutingUserRule). Consider guarding this conversion (or leaving process untouched) when process routing isn’t available to avoid changing custom configs’ semantics on unsupported devices/settings.

Suggested change
for (elem in rulesJson) {
val rule = elem.takeIf { it.isJsonObject }?.asJsonObject ?: continue
val process = rule.get("process")?.takeIf { it.isJsonArray }?.asJsonArray ?: continue
val packages = process.mapNotNull {
it.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
}.takeIf { it.isNotEmpty() } ?: continue
val uids = PackageUidResolver.packageNamesToUids(context, packages).takeIf { it.isNotEmpty() } ?: continue
rule.add("process", JsonArray().apply { uids.forEach { add(it) } })
if (SettingsManager.canUseProcessRouting()) {
for (elem in rulesJson) {
val rule = elem.takeIf { it.isJsonObject }?.asJsonObject ?: continue
val process = rule.get("process")?.takeIf { it.isJsonArray }?.asJsonArray ?: continue
val packages = process.mapNotNull {
it.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }?.asString
}.takeIf { it.isNotEmpty() } ?: continue
val uids = PackageUidResolver.packageNamesToUids(context, packages).takeIf { it.isNotEmpty() } ?: continue
rule.add("process", JsonArray().apply { uids.forEach { add(it) } })
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants