Skip to content

Commit

Permalink
feat: Use ruleSet instead of geoip and geosite
Browse files Browse the repository at this point in the history
  • Loading branch information
purofle committed Feb 17, 2024
1 parent be1edbd commit 9d2cdab
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ fun buildConfig(
route = RouteOptions().apply {
auto_detect_interface = true
rules = mutableListOf()
rule_set = mutableListOf()
}

// returns outbound tag
Expand Down Expand Up @@ -518,6 +519,7 @@ fun buildConfig(
}
PackageCache[it]?.takeIf { uid -> uid >= 1000 }
}.toHashSet().filterNotNull()
val ruleSets = mutableListOf<RuleSet>()

val ruleObj = Rule_DefaultOptions().apply {
if (uidList.isNotEmpty()) {
Expand All @@ -533,7 +535,7 @@ fun buildConfig(
makeSingBoxRule(rule.ip.listByLineOrComma(), true)
}

generateRuleSet()
generateRuleSet(ruleSets)

if (rule.port.isNotBlank()) {
port = mutableListOf<Int>()
Expand Down Expand Up @@ -614,6 +616,7 @@ fun buildConfig(
).show()
} else {
route.rules.add(ruleObj)
route.rule_set.addAll(ruleSets)
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/java/io/nekohasekai/sagernet/utils/GeoipUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import libcore.Libcore
import java.io.File

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 ruleSetDir = filesDir.resolve("ruleSets")
ruleSetDir.mkdirs()
val output = ruleSetDir.resolve("geoip-$country.srs")

if (output.isFile) {
return output.absolutePath
}

val geositeFile = File(filesDir, "geoip.db")

Expand All @@ -20,6 +31,8 @@ object GeoipUtils {
error("open geoip failed")
}

geoip.convertGeoip(country, ruleSetDir.resolve("geoip-$country.srs").absolutePath)
geoip.convertGeoip(country, output.absolutePath)

return output.absolutePath
}
}
16 changes: 12 additions & 4 deletions app/src/main/java/io/nekohasekai/sagernet/utils/GeositeUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ import libcore.Geosite
import java.io.File

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 geositeFile = File(filesDir, "geosite.db")
val ruleSetDir = filesDir.resolve("ruleSets")
ruleSetDir.mkdirs()

val geositeFile = File(filesDir, "geosite.db")
val output = ruleSetDir.resolve("geosite-$code.srs")

val geosite = Geosite()
if (!geosite.checkGeositeCode(geositeFile.absolutePath, code)) {
error("code $code not found in geosite")
}

geosite.convertGeosite(code, ruleSetDir.resolve("geosite-$code.srs").absolutePath)
geosite.convertGeosite(code, output.absolutePath)

return output.absolutePath
}
}
19 changes: 16 additions & 3 deletions app/src/main/java/moe/matsuri/nb4a/SingBoxOptionsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package moe.matsuri.nb4a
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.utils.GeoipUtils
import io.nekohasekai.sagernet.utils.GeositeUtils
import moe.matsuri.nb4a.SingBoxOptions.RuleSet

object SingBoxOptionsUtil {

Expand Down Expand Up @@ -72,15 +73,27 @@ fun SingBoxOptions.DNSRule_DefaultOptions.checkEmpty(): Boolean {
return true
}

fun SingBoxOptions.Rule_DefaultOptions.generateRuleSet() {
fun SingBoxOptions.Rule_DefaultOptions.generateRuleSet(ruleSet: MutableList<RuleSet>) {
rule_set.forEach {
when {
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") -> {
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
})
}
}
}
Expand Down

0 comments on commit 9d2cdab

Please sign in to comment.