improve selector traffic count

This commit is contained in:
arm64v8a 2023-03-21 23:01:05 +09:00
parent 958361eca1
commit 8a5a8c75ab
4 changed files with 29 additions and 15 deletions

View File

@ -153,8 +153,8 @@ class BaseService {
val tag = data.proxy!!.config.profileTagMap[ent?.id] ?: ""
if (tag.isNotBlank() && ent != null) {
val success = data.proxy!!.box.selectOutbound(tag)
Logs.d("selectOutbound $tag $success")
runOnDefaultDispatcher {
if (success) runOnDefaultDispatcher {
data.proxy!!.looper?.selectMain(ent.id)
data.binder.broadcast {
it.stateChanged(-1, ent.displayName(), null)
}

View File

@ -15,7 +15,7 @@ class ProxyInstance(profile: ProxyEntity, var service: BaseService.Interface? =
var notTmp = true
// for TrafficLooper
private var looper: TrafficLooper? = null
var looper: TrafficLooper? = null
override fun buildConfig() {
super.buildConfig()

View File

@ -5,6 +5,7 @@ import io.nekohasekai.sagernet.aidl.TrafficData
import io.nekohasekai.sagernet.bg.BaseService
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.ProfileManager
import io.nekohasekai.sagernet.fmt.TAG_BYPASS
import io.nekohasekai.sagernet.fmt.TAG_PROXY
import io.nekohasekai.sagernet.ktx.Logs
import kotlinx.coroutines.*
@ -49,6 +50,19 @@ class TrafficLooper
job = sc.launch { loop() }
}
var selectorNowId = -1L
var selectorNowFakeTag = ""
fun selectMain(id: Long) {
Logs.d("select traffic count $TAG_PROXY to $id, old id is $selectorNowId")
val oldData = items[selectorNowId]
val data = items[id] ?: return
oldData?.tag = selectorNowFakeTag
selectorNowFakeTag = data.tag
data.tag = TAG_PROXY
selectorNowId = id
}
private suspend fun loop() {
val delayMs = DataStore.speedInterval
val showDirectSpeed = DataStore.showDirectSpeed
@ -72,32 +86,32 @@ class TrafficLooper
itemBypass = TrafficUpdater.TrafficLooperData(tag = "bypass")
items[-1] = itemBypass
//
val tags = hashSetOf("bypass")
val tags = hashSetOf(TAG_PROXY, TAG_BYPASS)
proxy.config.trafficMap.forEach { (tag, ents) ->
tags.add(tag)
for (ent in ents) {
val item = TrafficUpdater.TrafficLooperData(
tag = tag,
rx = ent.rx,
tx = ent.tx,
)
if (proxy.config.selectorGroupId < 0L && ent.id == proxy.config.mainEntId) {
if (tag == TAG_PROXY && itemMain == null) {
itemMain = item
itemMainBase = TrafficUpdater.TrafficLooperData(
tag = tag,
rx = ent.rx,
tx = ent.tx,
)
Logs.d("traffic count $tag to main")
Logs.d("traffic count $tag to main to ${ent.id}")
}
items[ent.id] = item
tags.add(tag)
Logs.d("traffic count $tag to ${ent.id}")
}
}
if (proxy.config.selectorGroupId >= 0L) {
itemMain = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
itemMainBase = TrafficUpdater.TrafficLooperData(tag = TAG_PROXY)
tags.add(TAG_PROXY)
selectMain(proxy.config.mainEntId)
}
//
trafficUpdater = TrafficUpdater(

View File

@ -5,7 +5,6 @@ import io.nekohasekai.sagernet.Key
import io.nekohasekai.sagernet.bg.VpnService
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.ProxyEntity
import io.nekohasekai.sagernet.database.ProxyEntity.Companion.TYPE_CHAIN
import io.nekohasekai.sagernet.database.ProxyEntity.Companion.TYPE_CONFIG
import io.nekohasekai.sagernet.database.SagerDatabase
import io.nekohasekai.sagernet.fmt.ConfigBuildResult.IndexEntity
@ -96,7 +95,7 @@ fun buildConfig(
}
}
val trafficMap = HashMap<String, MutableList<ProxyEntity>>()
val trafficMap = HashMap<String, List<ProxyEntity>>()
val tagMap = HashMap<Long, String>()
val globalOutbounds = ArrayList<Long>()
val group = SagerDatabase.groupDao.getById(proxy.groupId)
@ -270,6 +269,11 @@ fun buildConfig(
chainId: Long, entity: ProxyEntity
): String {
val profileList = entity.resolveChain()
val chainTrafficSet = HashSet<ProxyEntity>().apply {
plusAssign(profileList)
add(entity)
}
var currentOutbound = mutableMapOf<String, Any>()
lateinit var pastOutbound: MutableMap<String, Any>
lateinit var pastInboundTag: String
@ -342,11 +346,6 @@ fun buildConfig(
globalOutbounds.add(proxyEntity.id)
}
// include g-xx & chain ent
val mapList = mutableListOf(proxyEntity)
if (index == 0 && entity.type == TYPE_CHAIN) mapList.add(proxy) // chain ent
trafficMap[tagOut] = mapList
// Chain outbound
if (proxyEntity.needExternal()) {
val localPort = mkPort()
@ -465,6 +464,7 @@ fun buildConfig(
pastEntity = proxyEntity
}
trafficMap[chainTagOut] = chainTrafficSet.toList()
return chainTagOut
}