From c3aafbfdc98a03b0a2755f1d7526b8be36385d71 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Sun, 30 Apr 2023 09:23:32 +0900 Subject: [PATCH] improve selector --- .../java/io/nekohasekai/sagernet/SagerNet.kt | 10 +++--- .../io/nekohasekai/sagernet/bg/BaseService.kt | 3 +- .../io/nekohasekai/sagernet/bg/TileService.kt | 34 ++++++------------- .../io/nekohasekai/sagernet/bg/VpnService.kt | 9 +++-- .../sagernet/bg/proto/BoxInstance.kt | 9 +++++ .../sagernet/bg/proto/ProxyInstance.kt | 5 ++- .../sagernet/bg/proto/TrafficLooper.kt | 12 ++++--- 7 files changed, 41 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt b/app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt index ea21558..8b3d3e7 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/SagerNet.kt @@ -280,10 +280,12 @@ class SagerNet : Application(), .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.proxy?.apply { + looper?.selectMain(id) + displayProfileName = ServiceNotification.genTitle(ent) + data.notification?.postNotificationTitle(displayProfileName) + } + // post binder data.binder.broadcast { b -> b.cbSelectorUpdate(id) } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt index 7d4be57..02dfa61 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/BaseService.kt @@ -88,7 +88,7 @@ class BaseService { override val coroutineContext = Dispatchers.Main.immediate + Job() override fun getState(): Int = (data?.state ?: State.Idle).ordinal - override fun getProfileName(): String = data?.proxy?.profile?.displayName() ?: "Idle" + override fun getProfileName(): String = data?.proxy?.displayProfileName ?: "Idle" override fun registerCallback(cb: ISagerNetServiceCallback, id: Int) { if (!callbackIdMap.contains(cb)) { @@ -189,7 +189,6 @@ class BaseService { tmpBox.buildConfigTmp() if (tmpBox.lastSelectorGroupId == data.proxy?.lastSelectorGroupId) { return true - // TODO if profile changed? } return false } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/TileService.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/TileService.kt index c1b0bb4..dd46ee5 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/TileService.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/TileService.kt @@ -1,23 +1,3 @@ -/******************************************************************************* - * * - * Copyright (C) 2017 by Max Lv * - * Copyright (C) 2017 by Mygod Studio * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - * * - *******************************************************************************/ - package io.nekohasekai.sagernet.bg import android.graphics.drawable.Icon @@ -26,6 +6,7 @@ import androidx.annotation.RequiresApi import io.nekohasekai.sagernet.R import io.nekohasekai.sagernet.SagerNet import io.nekohasekai.sagernet.aidl.ISagerNetService +import io.nekohasekai.sagernet.database.SagerDatabase import android.service.quicksettings.TileService as BaseTileService @RequiresApi(24) @@ -39,16 +20,21 @@ class TileService : BaseTileService(), SagerConnection.Callback { private val connection = SagerConnection(SagerConnection.CONNECTION_ID_TILE) override fun stateChanged(state: BaseService.State, profileName: String?, msg: String?) = - updateTile(state) { profileName } + updateTile(state, profileName) override fun onServiceConnected(service: ISagerNetService) { - updateTile(BaseService.State.values()[service.state]) { service.profileName } + updateTile(BaseService.State.values()[service.state], service.profileName) if (tapPending) { tapPending = false onClick() } } + override fun cbSelectorUpdate(id: Long) { + val profile = SagerDatabase.proxyDao.getById(id) ?: return + updateTile(BaseService.State.Connected, profile.displayName()) + } + override fun onStartListening() { super.onStartListening() connection.connect(this, this) @@ -63,7 +49,7 @@ class TileService : BaseTileService(), SagerConnection.Callback { if (isLocked) unlockAndRun(this::toggle) else toggle() } - private fun updateTile(serviceState: BaseService.State, profileName: () -> String?) { + private fun updateTile(serviceState: BaseService.State, profileName: String?) { qsTile?.apply { label = null when (serviceState) { @@ -75,7 +61,7 @@ class TileService : BaseTileService(), SagerConnection.Callback { BaseService.State.Connected -> { icon = iconConnected - label = profileName() + label = profileName state = Tile.STATE_ACTIVE } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/VpnService.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/VpnService.kt index b6f0b43..5e26c95 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/VpnService.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/VpnService.kt @@ -95,9 +95,8 @@ class VpnService : BaseVpnService(), // val tunOptions = JSONObject(tunOptionsJson) // address & route & MTU ...... use NB4A GUI config - val profile = data.proxy!!.profile val builder = Builder().setConfigureIntent(SagerNet.configureIntent(this)) - .setSession(profile.displayName()) + .setSession(getString(R.string.app_name)) .setMtu(DataStore.mtu) val ipv6Mode = DataStore.ipv6Mode @@ -132,10 +131,10 @@ class VpnService : BaseVpnService(), // app route val packageName = packageName - var proxyApps = DataStore.proxyApps + val proxyApps = DataStore.proxyApps var bypass = DataStore.bypass - var workaroundSYSTEM = false /* DataStore.tunImplementation == TunImplementation.SYSTEM */ - var needBypassRootUid = workaroundSYSTEM || data.proxy!!.config.trafficMap.values.any { + val workaroundSYSTEM = false /* DataStore.tunImplementation == TunImplementation.SYSTEM */ + val needBypassRootUid = workaroundSYSTEM || data.proxy!!.config.trafficMap.values.any { it[0].nekoBean?.needBypassRootUid() == true || it[0].hysteriaBean?.protocol == HysteriaBean.PROTOCOL_FAKETCP } diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/BoxInstance.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/BoxInstance.kt index 190a99e..74cd658 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/BoxInstance.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/BoxInstance.kt @@ -67,10 +67,12 @@ abstract class BoxInstance( initPlugin("trojan-go-plugin") pluginConfigs[port] = profile.type to bean.buildTrojanGoConfig(port) } + is NaiveBean -> { initPlugin("naive-plugin") pluginConfigs[port] = profile.type to bean.buildNaiveConfig(port) } + is HysteriaBean -> { initPlugin("hysteria-plugin") pluginConfigs[port] = profile.type to bean.buildHysteriaConfig(port) { @@ -82,6 +84,7 @@ abstract class BoxInstance( } } } + is TuicBean -> { initPlugin("tuic-plugin") pluginConfigs[port] = profile.type to bean.buildTuicConfig(port) { @@ -94,6 +97,7 @@ abstract class BoxInstance( } } } + is NekoBean -> { // check if plugin binary can be loaded initPlugin(bean.plgId) @@ -127,6 +131,7 @@ abstract class BoxInstance( externalInstances.containsKey(port) -> { externalInstances[port]!!.launch() } + bean is TrojanGoBean -> { val configFile = File( cache, "trojan_go_" + SystemClock.elapsedRealtime() + ".json" @@ -141,6 +146,7 @@ abstract class BoxInstance( processes.start(commands) } + bean is NaiveBean -> { val configFile = File( cache, "naive_" + SystemClock.elapsedRealtime() + ".json" @@ -170,6 +176,7 @@ abstract class BoxInstance( processes.start(commands, envMap) } + bean is HysteriaBean -> { val configFile = File( cache, "hysteria_" + SystemClock.elapsedRealtime() + ".json" @@ -195,6 +202,7 @@ abstract class BoxInstance( processes.start(commands) } + bean is NekoBean -> { // config built from JS val nekoRunConfigs = bean.allConfig.optJSONArray("nekoRunConfigs") @@ -232,6 +240,7 @@ abstract class BoxInstance( processes.start(commands) } + bean is TuicBean -> { val configFile = File( context.noBackupFilesDir, diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt index 81178fd..3865386 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/ProxyInstance.kt @@ -2,6 +2,7 @@ package io.nekohasekai.sagernet.bg.proto import io.nekohasekai.sagernet.BuildConfig import io.nekohasekai.sagernet.bg.BaseService +import io.nekohasekai.sagernet.bg.ServiceNotification import io.nekohasekai.sagernet.database.ProxyEntity import io.nekohasekai.sagernet.ktx.Logs import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher @@ -11,9 +12,11 @@ import moe.matsuri.nb4a.utils.JavaUtil class ProxyInstance(profile: ProxyEntity, var service: BaseService.Interface? = null) : BoxInstance(profile) { - var lastSelectorGroupId = -1L var notTmp = true + var lastSelectorGroupId = -1L + var displayProfileName = ServiceNotification.genTitle(profile) + // for TrafficLooper var looper: TrafficLooper? = null diff --git a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt index 152490b..f2ae4df 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/bg/proto/TrafficLooper.kt @@ -61,11 +61,13 @@ class TrafficLooper 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 + if (DataStore.profileTrafficStatistics) { + data.proxy?.config?.trafficMap?.get(tag)?.firstOrNull()?.let { + it.rx = rx + it.tx = tx + runOnDefaultDispatcher { + ProfileManager.updateProfile(it) // update DB + } } } }