Replace package names on UIDs in custom config (#5527)
* Replace package names on UIDs in custom config * Update V2rayNG/app/src/main/java/com/v2ray/ang/handler/V2rayConfigManager.kt Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix copilot review & detect tun optimization --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -98,36 +98,46 @@ object V2rayConfigManager {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if tun inbound exists
|
val json = JsonUtil.parseString(raw)?.takeIf { it.isJsonObject }?.asJsonObject ?: return result
|
||||||
val json = JsonUtil.parseString(raw) ?: return result
|
|
||||||
val inboundsJson = if (json.has("inbounds") && json.get("inbounds")?.isJsonNull == false) {
|
|
||||||
json.getAsJsonArray("inbounds")
|
|
||||||
} else {
|
|
||||||
JsonArray()
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i in 0 until inboundsJson.size()) {
|
// Check whether package names need to be replaced with UIDs
|
||||||
val elem = inboundsJson.get(i)
|
if (SettingsManager.canUseProcessRouting()) {
|
||||||
if (elem.isJsonObject) {
|
val rulesJson = json.get("routing")?.takeIf { it.isJsonObject }?.asJsonObject
|
||||||
val inb = elem.asJsonObject
|
?.get("rules")?.takeIf { it.isJsonArray }?.asJsonArray
|
||||||
val tag = if (inb.has("tag") && inb.get("tag")?.isJsonNull == false) inb.get("tag").asString else ""
|
?: JsonArray()
|
||||||
if (tag == "tun") return result
|
|
||||||
|
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) } })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add tun inbound from template
|
// check if tun inbound exists
|
||||||
val templateConfig = initV2rayConfig(context) ?: return result
|
val inboundsJson = json.get("inbounds")?.takeIf { it.isJsonArray }?.asJsonArray
|
||||||
val inboundTun = templateConfig.inbounds.firstOrNull { it.tag == "tun" } ?: return result
|
?: JsonArray().also { json.add("inbounds", it) }
|
||||||
inboundTun.settings?.mtu = SettingsManager.getVpnMtu()
|
val tunNotExists = inboundsJson.none { elem ->
|
||||||
|
elem.isJsonObject && elem.asJsonObject.get("protocol")
|
||||||
// add to json
|
?.takeIf { it.isJsonPrimitive && it.asJsonPrimitive.isString }
|
||||||
inboundsJson.add(JsonUtil.parseString(JsonUtil.toJson(inboundTun)))
|
?.asString == "tun"
|
||||||
if (inboundsJson.size() == 1) {
|
|
||||||
json.add("inbounds", inboundsJson)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val updatedRaw = JsonUtil.toJsonPretty(json) ?: return result
|
if (tunNotExists) {
|
||||||
return ConfigResult(true, guid, updatedRaw)
|
// add tun inbound from template
|
||||||
|
initV2rayConfig(context)?.let { templateConfig ->
|
||||||
|
templateConfig.inbounds.firstOrNull { it.tag == "tun" }?.let { inboundTun ->
|
||||||
|
inboundTun.settings?.mtu = SettingsManager.getVpnMtu()
|
||||||
|
inboundsJson.add(JsonUtil.parseString(JsonUtil.toJson(inboundTun)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return JsonUtil.toJsonPretty(json)?.let { ConfigResult(true, guid, it) } ?: result
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user