Skip to content

Commit

Permalink
feat: clash api selector callback
Browse files Browse the repository at this point in the history
  • Loading branch information
arm64v8a committed Apr 21, 2023
1 parent 4652656 commit dd64844
Show file tree
Hide file tree
Showing 20 changed files with 177 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ oneway interface ISagerNetServiceCallback {
void routeAlert(int type, String routeName);
void cbSpeedUpdate(in SpeedDisplayData stats);
void cbTrafficUpdate(in TrafficData stats);
void cbSelectorUpdate(long id);
}
20 changes: 20 additions & 0 deletions app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import go.Seq
import io.nekohasekai.sagernet.bg.SagerConnection
import io.nekohasekai.sagernet.bg.ServiceNotification
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.SagerDatabase
import io.nekohasekai.sagernet.ktx.Logs
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import io.nekohasekai.sagernet.ui.MainActivity
Expand Down Expand Up @@ -266,4 +268,22 @@ class SagerNet : Application(),
return DataStore.rulesProvider == 0
}

override fun selector_OnProxySelected(tag: String) {
DataStore.baseService?.apply {
runOnDefaultDispatcher {
val id = data.proxy!!.config.profileTagMap
.filterValues { it == tag }.keys.firstOrNull() ?: -1
val ent = SagerDatabase.proxyDao.getById(id) ?: return@runOnDefaultDispatcher
// traffic & title
data.proxy!!.looper?.selectMain(id)
val title = ServiceNotification.genTitle(ent)
data.notification?.postNotificationTitle(title)
// post MainActivity animation
data.binder.broadcast { b ->
b.cbSelectorUpdate(id)
}
}
}
}

}
11 changes: 5 additions & 6 deletions app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class BaseService {
.show()
}
}

else -> service.stopRunner()
}
}
Expand Down Expand Up @@ -165,12 +166,10 @@ class BaseService {
val ent = SagerDatabase.proxyDao.getById(DataStore.selectedProxy)
val tag = data.proxy!!.config.profileTagMap[ent?.id] ?: ""
if (tag.isNotBlank() && ent != null) {
val success = data.proxy!!.box.selectOutbound(tag)
if (success) runOnDefaultDispatcher {
data.proxy!!.looper?.selectMain(ent.id)
val title = ServiceNotification.genTitle(ent)
data.notification?.postNotificationTitle(title)
}
// select from GUI
data.proxy!!.box.selectOutbound(tag)
// or select from webui
// => selector_OnProxySelected
}
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class SagerConnection(

fun cbSpeedUpdate(stats: SpeedDisplayData) {}
fun cbTrafficUpdate(data: TrafficData) {}
fun cbSelectorUpdate(id: Long) {}

fun stateChanged(state: BaseService.State, profileName: String?, msg: String?)

Expand Down Expand Up @@ -83,6 +84,13 @@ class SagerConnection(
}
}

override fun cbSelectorUpdate(id: Long) {
val callback = callback ?: return
runOnMainDispatcher {
callback.cbSelectorUpdate(id)
}
}

override fun missingPlugin(profileName: String, pluginName: String) {
val callback = callback ?: return
runOnMainDispatcher {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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 io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import kotlinx.coroutines.*

class TrafficLooper
Expand Down Expand Up @@ -55,14 +56,22 @@ class TrafficLooper
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
val newData = items[id] ?: return
oldData?.apply {
tag = selectorNowFakeTag
ignore = true
// post traffic when switch
data.proxy?.config?.trafficMap?.get(tag)?.firstOrNull()?.let {
it.rx = rx
it.tx = tx
runOnDefaultDispatcher {
ProfileManager.updateProfile(it) // update DB
}
}
}
selectorNowFakeTag = data.tag
selectorNowFakeTag = newData.tag
selectorNowId = id
data.apply {
newData.apply {
tag = TAG_PROXY
ignore = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object ProfileManager {
interface Listener {
suspend fun onAdd(profile: ProxyEntity)
suspend fun onUpdated(data: TrafficData)
suspend fun onUpdated(profile: ProxyEntity)
suspend fun onUpdated(profile: ProxyEntity, noTraffic: Boolean)
suspend fun onRemoved(groupId: Long, profileId: Long)
}

Expand Down Expand Up @@ -87,13 +87,13 @@ object ProfileManager {

suspend fun updateProfile(profile: ProxyEntity) {
SagerDatabase.proxyDao.updateProxy(profile)
iterator { onUpdated(profile) }
iterator { onUpdated(profile, false) }
}

suspend fun updateProfile(profiles: List<ProxyEntity>) {
SagerDatabase.proxyDao.updateProxy(profiles)
profiles.forEach {
iterator { onUpdated(it) }
iterator { onUpdated(it, false) }
}
}

Expand Down Expand Up @@ -141,12 +141,12 @@ object ProfileManager {

// postUpdate: post to listeners, don't change the DB

suspend fun postUpdate(profileId: Long) {
postUpdate(getProfile(profileId) ?: return)
suspend fun postUpdate(profileId: Long, noTraffic: Boolean = false) {
postUpdate(getProfile(profileId) ?: return, noTraffic)
}

suspend fun postUpdate(profile: ProxyEntity) {
iterator { onUpdated(profile) }
suspend fun postUpdate(profile: ProxyEntity, noTraffic: Boolean = false) {
iterator { onUpdated(profile, noTraffic) }
}

suspend fun postUpdate(data: TrafficData) {
Expand Down
Loading

0 comments on commit dd64844

Please sign in to comment.