mirror of
https://github.com/MatsuriDayo/NekoBoxForAndroid.git
synced 2025-12-20 07:00:05 +08:00
feat: Use ruleSet instead of geoip and geosite
This commit is contained in:
parent
be1edbd5e4
commit
9d2cdabd58
@ -256,6 +256,7 @@ fun buildConfig(
|
|||||||
route = RouteOptions().apply {
|
route = RouteOptions().apply {
|
||||||
auto_detect_interface = true
|
auto_detect_interface = true
|
||||||
rules = mutableListOf()
|
rules = mutableListOf()
|
||||||
|
rule_set = mutableListOf()
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns outbound tag
|
// returns outbound tag
|
||||||
@ -518,6 +519,7 @@ fun buildConfig(
|
|||||||
}
|
}
|
||||||
PackageCache[it]?.takeIf { uid -> uid >= 1000 }
|
PackageCache[it]?.takeIf { uid -> uid >= 1000 }
|
||||||
}.toHashSet().filterNotNull()
|
}.toHashSet().filterNotNull()
|
||||||
|
val ruleSets = mutableListOf<RuleSet>()
|
||||||
|
|
||||||
val ruleObj = Rule_DefaultOptions().apply {
|
val ruleObj = Rule_DefaultOptions().apply {
|
||||||
if (uidList.isNotEmpty()) {
|
if (uidList.isNotEmpty()) {
|
||||||
@ -533,7 +535,7 @@ fun buildConfig(
|
|||||||
makeSingBoxRule(rule.ip.listByLineOrComma(), true)
|
makeSingBoxRule(rule.ip.listByLineOrComma(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
generateRuleSet()
|
generateRuleSet(ruleSets)
|
||||||
|
|
||||||
if (rule.port.isNotBlank()) {
|
if (rule.port.isNotBlank()) {
|
||||||
port = mutableListOf<Int>()
|
port = mutableListOf<Int>()
|
||||||
@ -614,6 +616,7 @@ fun buildConfig(
|
|||||||
).show()
|
).show()
|
||||||
} else {
|
} else {
|
||||||
route.rules.add(ruleObj)
|
route.rules.add(ruleObj)
|
||||||
|
route.rule_set.addAll(ruleSets)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,12 +6,23 @@ import libcore.Libcore
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
object GeoipUtils {
|
object GeoipUtils {
|
||||||
fun generateRuleSet(context: Context = app.applicationContext, country: String) {
|
/**
|
||||||
|
* Generate a rule set for a specific country
|
||||||
|
* @param context the context to use
|
||||||
|
* @param country the country code to generate the rule set for
|
||||||
|
* @return the path to the generated rule set
|
||||||
|
*/
|
||||||
|
fun generateRuleSet(context: Context = app.applicationContext, country: String): String {
|
||||||
|
|
||||||
val filesDir = context.getExternalFilesDir(null) ?: context.filesDir
|
val filesDir = context.getExternalFilesDir(null) ?: context.filesDir
|
||||||
|
|
||||||
val ruleSetDir = filesDir.resolve("ruleSets")
|
val ruleSetDir = filesDir.resolve("ruleSets")
|
||||||
ruleSetDir.mkdirs()
|
ruleSetDir.mkdirs()
|
||||||
|
val output = ruleSetDir.resolve("geoip-$country.srs")
|
||||||
|
|
||||||
|
if (output.isFile) {
|
||||||
|
return output.absolutePath
|
||||||
|
}
|
||||||
|
|
||||||
val geositeFile = File(filesDir, "geoip.db")
|
val geositeFile = File(filesDir, "geoip.db")
|
||||||
|
|
||||||
@ -20,6 +31,8 @@ object GeoipUtils {
|
|||||||
error("open geoip failed")
|
error("open geoip failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
geoip.convertGeoip(country, ruleSetDir.resolve("geoip-$country.srs").absolutePath)
|
geoip.convertGeoip(country, output.absolutePath)
|
||||||
|
|
||||||
|
return output.absolutePath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,20 +6,28 @@ import libcore.Geosite
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
object GeositeUtils {
|
object GeositeUtils {
|
||||||
fun generateRuleSet(context: Context = app.applicationContext, code: String) {
|
/**
|
||||||
|
* Generate a rule set for a specific geosite code
|
||||||
|
* @param context the context to use
|
||||||
|
* @param code the geosite code to generate the rule set for
|
||||||
|
* @return the path to the generated rule set
|
||||||
|
*/
|
||||||
|
fun generateRuleSet(context: Context = app.applicationContext, code: String): String {
|
||||||
|
|
||||||
val filesDir = context.getExternalFilesDir(null) ?: context.filesDir
|
val filesDir = context.getExternalFilesDir(null) ?: context.filesDir
|
||||||
|
val geositeFile = File(filesDir, "geosite.db")
|
||||||
val ruleSetDir = filesDir.resolve("ruleSets")
|
val ruleSetDir = filesDir.resolve("ruleSets")
|
||||||
ruleSetDir.mkdirs()
|
ruleSetDir.mkdirs()
|
||||||
|
|
||||||
val geositeFile = File(filesDir, "geosite.db")
|
val output = ruleSetDir.resolve("geosite-$code.srs")
|
||||||
|
|
||||||
val geosite = Geosite()
|
val geosite = Geosite()
|
||||||
if (!geosite.checkGeositeCode(geositeFile.absolutePath, code)) {
|
if (!geosite.checkGeositeCode(geositeFile.absolutePath, code)) {
|
||||||
error("code $code not found in geosite")
|
error("code $code not found in geosite")
|
||||||
}
|
}
|
||||||
|
|
||||||
geosite.convertGeosite(code, ruleSetDir.resolve("geosite-$code.srs").absolutePath)
|
geosite.convertGeosite(code, output.absolutePath)
|
||||||
|
|
||||||
|
return output.absolutePath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -3,6 +3,7 @@ package moe.matsuri.nb4a
|
|||||||
import io.nekohasekai.sagernet.database.DataStore
|
import io.nekohasekai.sagernet.database.DataStore
|
||||||
import io.nekohasekai.sagernet.utils.GeoipUtils
|
import io.nekohasekai.sagernet.utils.GeoipUtils
|
||||||
import io.nekohasekai.sagernet.utils.GeositeUtils
|
import io.nekohasekai.sagernet.utils.GeositeUtils
|
||||||
|
import moe.matsuri.nb4a.SingBoxOptions.RuleSet
|
||||||
|
|
||||||
object SingBoxOptionsUtil {
|
object SingBoxOptionsUtil {
|
||||||
|
|
||||||
@ -72,15 +73,27 @@ fun SingBoxOptions.DNSRule_DefaultOptions.checkEmpty(): Boolean {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
fun SingBoxOptions.Rule_DefaultOptions.generateRuleSet() {
|
fun SingBoxOptions.Rule_DefaultOptions.generateRuleSet(ruleSet: MutableList<RuleSet>) {
|
||||||
rule_set.forEach {
|
rule_set.forEach {
|
||||||
when {
|
when {
|
||||||
it.startsWith("geoip") -> {
|
it.startsWith("geoip") -> {
|
||||||
GeoipUtils.generateRuleSet(country = it.removePrefix("geoip:"))
|
val geoipPath = GeoipUtils.generateRuleSet(country = it.removePrefix("geoip:"))
|
||||||
|
ruleSet.add(RuleSet().apply {
|
||||||
|
type = "local"
|
||||||
|
tag = it
|
||||||
|
format = "binary"
|
||||||
|
path = geoipPath
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
it.startsWith("geosite") -> {
|
it.startsWith("geosite") -> {
|
||||||
GeositeUtils.generateRuleSet(code = it.removePrefix("geosite:"))
|
val geositePath = GeositeUtils.generateRuleSet(code = it.removePrefix("geosite:"))
|
||||||
|
ruleSet.add(RuleSet().apply {
|
||||||
|
type = "local"
|
||||||
|
tag = it
|
||||||
|
format = "binary"
|
||||||
|
path = geositePath
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user