Skip to content

Commit

Permalink
refactor: use Listable to store list
Browse files Browse the repository at this point in the history
  • Loading branch information
xchacha20-poly1305 committed Aug 31, 2024
1 parent 1f9352b commit 46a182e
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 178 deletions.
74 changes: 38 additions & 36 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ConfigBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import io.nekohasekai.sagernet.ktx.mkPort
import io.nekohasekai.sagernet.utils.PackageCache
import libcore.Libcore
import moe.matsuri.nb4a.IP_PRIVATE
import moe.matsuri.nb4a.Listable
import moe.matsuri.nb4a.SingBoxOptions.BrutalOptions
import moe.matsuri.nb4a.SingBoxOptions.CacheFileOptions
import moe.matsuri.nb4a.SingBoxOptions.ClashAPIOptions
Expand Down Expand Up @@ -71,6 +72,7 @@ import moe.matsuri.nb4a.proxy.shadowtls.buildSingBoxOutboundShadowTLSBean
import moe.matsuri.nb4a.utils.JavaUtil.gson
import moe.matsuri.nb4a.utils.Util
import moe.matsuri.nb4a.utils.listByLineOrComma
import moe.matsuri.nb4a.utils.toListable

// Inbound
const val TAG_MIXED = "mixed-in"
Expand Down Expand Up @@ -226,7 +228,7 @@ fun buildConfig(
listen = "$LOCALHOST4:0" // Never really listen
stats = V2RayStatsServiceOptions().also {
it.enabled = true
it.outbounds = tagMap.values.toList() + TAG_PROXY + TAG_DIRECT
it.outbounds = (tagMap.values.toList() + TAG_PROXY + TAG_DIRECT).toListable()
}
}
clash_api = ClashAPIOptions().apply {
Expand Down Expand Up @@ -268,8 +270,8 @@ fun buildConfig(
}

dns = DNSOptions().apply {
servers = mutableListOf()
rules = mutableListOf()
servers = Listable()
rules = Listable()
independent_cache = true
}

Expand Down Expand Up @@ -304,15 +306,15 @@ fun buildConfig(
sniff_override_destination = needSniffOverride
when (ipv6Mode) {
IPv6Mode.DISABLE -> {
address = listOf(VpnService.PRIVATE_VLAN4_CLIENT + "/28")
address = Listable(VpnService.PRIVATE_VLAN4_CLIENT + "/28")
}

IPv6Mode.ONLY -> {
address = listOf(VpnService.PRIVATE_VLAN6_CLIENT + "/126")
address = Listable(VpnService.PRIVATE_VLAN6_CLIENT + "/126")
}

else -> {
address = listOf(
address = Listable(
VpnService.PRIVATE_VLAN4_CLIENT + "/28",
VpnService.PRIVATE_VLAN6_CLIENT + "/126",
)
Expand All @@ -328,7 +330,7 @@ fun buildConfig(
sniff = needSniff
sniff_override_destination = needSniffOverride
if (DataStore.inboundUsername.isNotBlank() || DataStore.inboundPassword.isNotBlank()) {
users = listOf(
users = Listable(
User().apply {
username = DataStore.inboundUsername
password = DataStore.inboundPassword
Expand All @@ -343,8 +345,8 @@ fun buildConfig(
// init routing object
route = RouteOptions().apply {
auto_detect_interface = true
rules = mutableListOf()
rule_set = mutableListOf()
rules = Listable()
rule_set = Listable()
}

// returns outbound tag
Expand Down Expand Up @@ -411,7 +413,7 @@ fun buildConfig(
// chain route/proxy rules
if (pastEntity!!.needExternal()) {
route.rules.add(Rule_Default().apply {
inbound = listOf(pastInboundTag)
inbound = Listable(pastInboundTag)
outbound = tagOut
})
} else {
Expand Down Expand Up @@ -565,7 +567,7 @@ fun buildConfig(
// no chain rule and not outbound, so need to set to direct
if (index == profileList.lastIndex) {
route.rules.add(Rule_Default().apply {
inbound = listOf(tag)
inbound = Listable(tag)
outbound = TAG_DIRECT
})
}
Expand All @@ -592,7 +594,7 @@ fun buildConfig(
type = "selector"
tag = TAG_PROXY
default_ = tagMap[proxy.id]
outbounds = tagMap.values.toList()
outbounds = tagMap.values.toListable()
interrupt_exist_connections = DataStore.interruptSelector
}.asMap())
} else {
Expand All @@ -617,7 +619,7 @@ fun buildConfig(
).show()
}
PackageCache[it]?.takeIf { uid -> uid >= 1000 }
}.toHashSet().filterNotNull()
}.toHashSet().filterNotNull().toListable()

val ruleObj = Rule_Default().apply {
if (uidList.isNotEmpty()) {
Expand All @@ -627,7 +629,7 @@ fun buildConfig(
if (rule.ruleSet.isNotBlank()) {
rule_set = rule.ruleSet.listByLineOrComma().filterNot {
it.startsWith(PREFIX_IP_DNS)
}
}.toListable()
}
var domainList: List<String> = listOf()
if (rule.domains.isNotBlank()) {
Expand All @@ -638,8 +640,8 @@ fun buildConfig(
makeSingBoxRule(rule.ip.listByLineOrComma(), true)
}
if (rule.port.isNotBlank()) {
port = mutableListOf<Int>()
port_range = mutableListOf<String>()
port = Listable()
port_range = Listable()
rule.port.listByLineOrComma().map {
if (it.contains(":")) {
port_range.add(it)
Expand All @@ -649,8 +651,8 @@ fun buildConfig(
}
}
if (rule.sourcePort.isNotBlank()) {
source_port = mutableListOf<Int>()
source_port_range = mutableListOf<String>()
source_port = Listable()
source_port_range = Listable()
rule.sourcePort.listByLineOrComma().map {
if (it.contains(":")) {
source_port_range.add(it)
Expand All @@ -660,10 +662,10 @@ fun buildConfig(
}
}
if (rule.network.isNotBlank()) {
network = listOf(rule.network)
network = Listable(rule.network)
}
if (rule.source.isNotBlank()) {
val sourceIPs = mutableListOf<String>()
val sourceIPs = Listable<String>()
for (source in rule.source.listByLineOrComma()) {
if (source.startsWith(IP_PRIVATE)) {
source_ip_is_private = true
Expand All @@ -674,16 +676,16 @@ fun buildConfig(
if (sourceIPs.isNotEmpty()) source_ip_cidr = sourceIPs
}
if (rule.protocol.isNotBlank()) {
protocol = rule.protocol.listByLineOrComma()
protocol = rule.protocol.listByLineOrComma().toListable()
}
if (rule.ssid.isNotBlank()) {
wifi_ssid = rule.ssid.listByLineOrComma()
wifi_ssid = rule.ssid.listByLineOrComma().toListable()
}
if (rule.bssid.isNotBlank()) {
wifi_bssid = rule.bssid.listByLineOrComma()
wifi_bssid = rule.bssid.listByLineOrComma().toListable()
}
if (rule.clientType.isNotBlank()) {
client = rule.clientType.listByLineOrComma()
client = rule.clientType.listByLineOrComma().toListable()
}
if (rule.clashMode.isNotBlank()) {
clash_mode = rule.clashMode
Expand Down Expand Up @@ -711,8 +713,8 @@ fun buildConfig(
0L -> {
if (useFakeDns) userDNSRuleList += makeDnsRuleObj().apply {
server = TAG_DNS_FAKE
inbound = listOf(TAG_TUN)
query_type = FAKE_DNS_QUERY_TYPE
inbound = Listable(TAG_TUN)
query_type = Listable(FAKE_DNS_QUERY_TYPE)
}
userDNSRuleList += makeDnsRuleObj().apply {
server = TAG_DNS_REMOTE
Expand Down Expand Up @@ -798,7 +800,7 @@ fun buildConfig(
)
}
}
route.rule_set = route.rule_set.distinctBy { it.tag }
route.rule_set = route.rule_set.distinctBy { it.tag }.toListable()

outbounds.add(Outbound().apply {
tag = TAG_DIRECT
Expand Down Expand Up @@ -914,13 +916,13 @@ fun buildConfig(

if (forTest) {
// Always use system DNS for urlTest
dns.servers = listOf(
dns.servers = Listable(
DNSServerOptions().apply {
address = LOCAL_DNS_SERVER
tag = TAG_DNS_LOCAL
}
)
dns.rules = listOf()
dns.rules = Listable()
} else {
// clash mode
route.rules.add(0, Rule_Default().apply {
Expand All @@ -934,11 +936,11 @@ fun buildConfig(

// built-in DNS rules
route.rules.add(0, Rule_Default().apply {
inbound = listOf(TAG_DNS_IN)
inbound = Listable(TAG_DNS_IN)
outbound = TAG_DNS_OUT
})
route.rules.add(0, Rule_Default().apply {
port = listOf(53)
port = Listable(53)
outbound = TAG_DNS_OUT
})

Expand All @@ -950,8 +952,8 @@ fun buildConfig(
}
// block mcast
route.rules.add(Rule_Default().apply {
ip_cidr = listOf("224.0.0.0/3", "ff00::/8")
source_ip_cidr = listOf("224.0.0.0/3", "ff00::/8")
ip_cidr = Listable("224.0.0.0/3", "ff00::/8")
source_ip_cidr = Listable("224.0.0.0/3", "ff00::/8")
outbound = TAG_BLOCK
})

Expand All @@ -967,10 +969,10 @@ fun buildConfig(
tag = "dns-fake"
})
dns.rules.add(DNSRule_Default().apply {
inbound = listOf(TAG_TUN)
inbound = Listable(TAG_TUN)
server = TAG_DNS_FAKE
disable_cache = true
query_type = FAKE_DNS_QUERY_TYPE
query_type = Listable(FAKE_DNS_QUERY_TYPE)
})
}

Expand All @@ -985,7 +987,7 @@ fun buildConfig(
})
// force bypass (always top DNS rule)
dns.rules.add(0, DNSRule_Default().apply {
outbound = listOf(TAG_ANY)
outbound = Listable(TAG_ANY)
server = TAG_DNS_DIRECT
})
if (domainListDNSDirectForce.isNotEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.fmt.LOCALHOST4
import io.nekohasekai.sagernet.ktx.*
import libcore.Libcore
import moe.matsuri.nb4a.Listable
import moe.matsuri.nb4a.SingBoxOptions
import moe.matsuri.nb4a.SingBoxOptions.OutboundECHOptions
import moe.matsuri.nb4a.utils.listByLineOrComma
import moe.matsuri.nb4a.utils.toListable
import org.json.JSONObject
import java.io.File

Expand Down Expand Up @@ -317,18 +319,18 @@ fun buildSingBoxOutboundHysteriaBean(bean: HysteriaBean): MutableMap<String, Any
server_name = bean.sni
}
if (bean.alpn.isNotBlank()) {
alpn = bean.alpn.listByLineOrComma()
alpn = bean.alpn.listByLineOrComma().toListable()
}
if (bean.caText.isNotBlank()) {
certificate = listOf(bean.caText)
certificate = Listable(bean.caText)
}
if (bean.ech) {
val echList = bean.echCfg.split("\n")
ech = OutboundECHOptions().apply {
enabled = true
pq_signature_schemes_enabled = echList.size > 5
dynamic_record_sizing_disabled = true
config = echList
config = Listable(echList)
}
}
insecure = bean.allowInsecure || DataStore.globalAllowInsecure
Expand Down Expand Up @@ -360,17 +362,17 @@ fun buildSingBoxOutboundHysteriaBean(bean: HysteriaBean): MutableMap<String, Any
if (bean.sni.isNotBlank()) {
server_name = bean.sni
}
alpn = listOf("h3")
alpn = Listable("h3")
if (bean.caText.isNotBlank()) {
certificate = listOf(bean.caText)
certificate = Listable(bean.caText)
}
if (bean.ech) {
val echList = bean.echCfg.split("\n")
ech = OutboundECHOptions().apply {
enabled = true
pq_signature_schemes_enabled = echList.size > 5
dynamic_record_sizing_disabled = true
config = echList
config = Listable(echList)
}
}
insecure = bean.allowInsecure || DataStore.globalAllowInsecure
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/ssh/SSHFmt.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.nekohasekai.sagernet.fmt.ssh

import moe.matsuri.nb4a.Listable
import moe.matsuri.nb4a.SingBoxOptions
import moe.matsuri.nb4a.utils.listByLineOrComma
import moe.matsuri.nb4a.utils.toListable

fun buildSingBoxOutboundSSHBean(bean: SSHBean): SingBoxOptions.Outbound_SSHOptions {
return SingBoxOptions.Outbound_SSHOptions().apply {
Expand All @@ -10,11 +12,11 @@ fun buildSingBoxOutboundSSHBean(bean: SSHBean): SingBoxOptions.Outbound_SSHOptio
server_port = bean.serverPort
user = bean.username
if (bean.publicKey.isNotBlank()) {
host_key = bean.publicKey.listByLineOrComma()
host_key = bean.publicKey.listByLineOrComma().toListable()
}
when (bean.authType) {
SSHBean.AUTH_TYPE_PRIVATE_KEY -> {
private_key = listOf(bean.privateKey)
private_key = Listable(bean.privateKey)
private_key_passphrase = bean.privateKeyPassphrase
}

Expand Down
8 changes: 5 additions & 3 deletions app/src/main/java/io/nekohasekai/sagernet/fmt/tuic/TuicFmt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package io.nekohasekai.sagernet.fmt.tuic

import io.nekohasekai.sagernet.database.DataStore
import libcore.Libcore
import moe.matsuri.nb4a.Listable
import moe.matsuri.nb4a.SingBoxOptions
import moe.matsuri.nb4a.SingBoxOptions.OutboundECHOptions
import moe.matsuri.nb4a.utils.listByLineOrComma
import moe.matsuri.nb4a.utils.toListable

// https://github.com/daeuniverse/dae/discussions/182
fun parseTuic(link: String): TuicBean {
Expand Down Expand Up @@ -82,18 +84,18 @@ fun buildSingBoxOutboundTuicBean(bean: TuicBean): SingBoxOptions.Outbound_TUICOp
server_name = bean.sni
}
if (bean.alpn.isNotBlank()) {
alpn = bean.alpn.listByLineOrComma()
alpn = bean.alpn.listByLineOrComma().toListable()
}
if (bean.caText.isNotBlank()) {
certificate = listOf(bean.caText)
certificate = Listable(bean.caText)
}
if (bean.ech) {
val echList = bean.echCfg.split("\n")
ech = OutboundECHOptions().apply {
enabled = true
pq_signature_schemes_enabled = echList.size > 5
dynamic_record_sizing_disabled = true
config = echList
config = Listable(echList)
}
}
disable_sni = bean.disableSNI
Expand Down
Loading

0 comments on commit 46a182e

Please sign in to comment.