Skip to content

Commit

Permalink
fix: remove unstable branch for geoip when sharing config &
Browse files Browse the repository at this point in the history
write pretty code
  • Loading branch information
xchacha20-poly1305 committed Aug 31, 2024
1 parent 70c27b0 commit 1f9352b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 45 deletions.
48 changes: 31 additions & 17 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ import moe.matsuri.nb4a.SingBoxOptions.Rule_Default
import moe.matsuri.nb4a.SingBoxOptions.V2RayAPIOptions
import moe.matsuri.nb4a.SingBoxOptions.V2RayStatsServiceOptions
import moe.matsuri.nb4a.checkEmpty
import moe.matsuri.nb4a.makeRuleSetRulesLocal
import moe.matsuri.nb4a.makeRuleSetRulesRemote
import moe.matsuri.nb4a.makeSingBoxRule
import moe.matsuri.nb4a.makeSingBoxRuleSet
import moe.matsuri.nb4a.proxy.config.ConfigBean
import moe.matsuri.nb4a.proxy.shadowtls.ShadowTLSBean
import moe.matsuri.nb4a.proxy.shadowtls.buildSingBoxOutboundShadowTLSBean
Expand Down Expand Up @@ -106,8 +107,6 @@ val FAKE_DNS_QUERY_TYPE: List<String> = listOf("A", "AAAA")
val ERR_NO_REMOTE_DNS by lazy { Exception("No remote DNS, check your settings!") }
val ERR_NO_DIRECT_DNS by lazy { Exception("No direct DNS, check your settings!") }

val externalAssets: String by lazy { SagerNet.application.externalAssets.absolutePath }

class ConfigBuildResult(
var config: String,
var externalIndex: List<IndexEntity>,
Expand Down Expand Up @@ -749,39 +748,54 @@ fun buildConfig(
}
}

// "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs"
var domainPath = "$externalAssets/geo"
var ipPath: String? = null
val ruleSetResource by lazy { SagerNet.application.externalAssets.absolutePath + "/geo" }
var geositeLink: String? = null
var geoipLink: String? = null
if (forExport) {
// "https://raw.githubusercontent.com/SagerNet/sing-geosite/rule-set/geosite-cn.srs"
val pathPrefix = "https://raw.githubusercontent.com"
val provider = DataStore.rulesProvider
val branch = if (RuleProvider.hasUnstableBranch(provider)) {

val normalBranch = "rule-set"
val geoipBranch = normalBranch
val geositeBranch = if (RuleProvider.hasUnstableBranch(provider)) {
"rule-set-unstable"
} else {
"rule-set"
normalBranch
}

when (provider) {
RuleProvider.OFFICIAL -> {
domainPath = "$pathPrefix/SagerNet/sing-geosite/$branch"
ipPath = "$pathPrefix/SagerNet/sing-geoip/$branch"
geositeLink = "$pathPrefix/SagerNet/sing-geosite/$geositeBranch"
geoipLink = "$pathPrefix/SagerNet/sing-geoip/$geoipBranch"
}

RuleProvider.LOYALSOLDIER -> {
domainPath = "$pathPrefix/xchacha20-poly1305/sing-geosite/$branch"
ipPath = "$pathPrefix/xchacha20-poly1305/sing-geoip/$branch"
geositeLink = "$pathPrefix/xchacha20-poly1305/sing-geosite/$geositeBranch"
geoipLink = "$pathPrefix/xchacha20-poly1305/sing-geoip/$geoipBranch"
}

RuleProvider.CHOCOLATE4U -> {
domainPath = "$pathPrefix/Chocolate4U/sing-geosite/$branch"
ipPath = "$pathPrefix/Chocolate4U/sing-geoip/$branch"
geositeLink = "$pathPrefix/Chocolate4U/sing-geosite/$geositeBranch"
geoipLink = "$pathPrefix/Chocolate4U/sing-geoip/$geoipBranch"
}

RuleProvider.CUSTOM -> {} // Can't generate.
}
}
for (rule in extraRules) {
if (rule.ruleSet.isNotBlank()) {
route.makeSingBoxRuleSet(rule.ruleSet.listByLineOrComma(), domainPath, ipPath)
val useRemote = geositeLink != null
for (rule in extraRules) if (rule.ruleSet.isNotBlank()) {
if (useRemote) {
route.makeRuleSetRulesRemote(
rule.ruleSet.listByLineOrComma(),
geositeLink!!,
geoipLink!!,
)
} else {
route.makeRuleSetRulesLocal(
rule.ruleSet.listByLineOrComma(),
ruleSetResource,
)
}
}
route.rule_set = route.rule_set.distinctBy { it.tag }
Expand Down
55 changes: 27 additions & 28 deletions app/src/main/java/moe/matsuri/nb4a/SingBoxOptionsUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,38 +187,37 @@ const val RULE_SET_FORMAT_BINARY = "binary"
const val RULE_SET_TYPE_REMOTE = "remote"
const val RULE_SET_TYPE_LOCAL = "local"

/**
* @param domainPath Domain rule set path. Also local rule-set path if ipPath == null.
* @param ipPath IP rule set path. Not null marks use remote.
*/
fun SingBoxOptions.RouteOptions.makeSingBoxRuleSet(
fun SingBoxOptions.RouteOptions.makeRuleSetRulesLocal(names: List<String>, resourcePath: String) {
if (rule_set == null) rule_set = mutableListOf<SingBoxOptions.RuleSet>()
for (name in names) {
val newName = name.removePrefix(PREFIX_IP_DNS)
rule_set.plusAssign(SingBoxOptions.RuleSet_Local().apply {
tag = newName
type = RULE_SET_TYPE_LOCAL
format = RULE_SET_FORMAT_BINARY
path = "$resourcePath/$newName.srs"
})
}
}

fun SingBoxOptions.RouteOptions.makeRuleSetRulesRemote(
names: List<String>,
domainPath: String,
ipPath: String? = null,
domainURL: String,
ipURL: String,
) {
val isRemote = ipPath != null

if (rule_set == null) rule_set = mutableListOf<SingBoxOptions.RuleSet>()
for (name in names) {
val newName = name.removePrefix(PREFIX_IP_DNS)
if (isRemote) {
rule_set.plusAssign(SingBoxOptions.RuleSet_Remote().apply {
tag = newName
type = RULE_SET_TYPE_REMOTE
format = RULE_SET_FORMAT_BINARY
url = if (name.startsWith("geosite-")) {
"$domainPath/$newName.srs"
} else {
"$ipPath/$newName.srs"
}
})
} else {
rule_set.plusAssign(SingBoxOptions.RuleSet_Local().apply {
tag = newName
type = RULE_SET_TYPE_LOCAL
format = RULE_SET_FORMAT_BINARY
path = "$domainPath/$newName.srs"
})
}
rule_set.plusAssign(SingBoxOptions.RuleSet_Remote().apply {
tag = newName
type = RULE_SET_TYPE_REMOTE
format = RULE_SET_FORMAT_BINARY
val isIP = newName.startsWith("geoip-")
url = if (isIP) {
"${ipURL}/${newName}.srs"
} else {
"${domainURL}/${newName}.srs"
}
})
}
}

0 comments on commit 1f9352b

Please sign in to comment.