network reset settings

This commit is contained in:
armv9 2025-03-01 19:36:38 +09:00
parent a1b5813e55
commit da539372bc
10 changed files with 46 additions and 62 deletions

View File

@ -43,7 +43,8 @@ object Key {
const val CONNECTION_TEST_URL = "connectionTestURL"
const val TCP_KEEP_ALIVE_INTERVAL = "tcpKeepAliveInterval"
const val NETWORK_CHANGE_RESET_CONNECTIONS = "networkChangeResetConnections"
const val WAKE_RESET_CONNECTIONS = "wakeResetConnections"
const val RULES_PROVIDER = "rulesProvider"
const val LOG_LEVEL = "logLevel"
const val LOG_BUF_SIZE = "logBufSize"

View File

@ -23,7 +23,6 @@ import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import libcore.Libcore
import moe.matsuri.nb4a.Protocols
import moe.matsuri.nb4a.utils.LibcoreUtil
import moe.matsuri.nb4a.utils.Util
import java.net.UnknownHostException
@ -58,12 +57,15 @@ class BaseService {
proxy?.box?.sleep()
} else {
proxy?.box?.wake()
if (DataStore.wakeResetConnections) {
Libcore.resetAllConnections(true)
}
}
}
}
Action.RESET_UPSTREAM_CONNECTIONS -> runOnDefaultDispatcher {
LibcoreUtil.resetAllConnections(true)
Libcore.resetAllConnections(true)
runOnMainDispatcher {
Util.collapseStatusBar(ctx)
Toast.makeText(ctx, "Reset upstream connections done", Toast.LENGTH_SHORT)
@ -259,7 +261,7 @@ class BaseService {
}
}
open fun persistStats() {
fun persistStats() {
// TODO NEW save app stats?
}
@ -278,7 +280,9 @@ class BaseService {
}
if (oldName != null && upstreamInterfaceName != null && oldName != upstreamInterfaceName) {
Logs.d("Network changed: $oldName -> $upstreamInterfaceName")
LibcoreUtil.resetAllConnections(true)
if (DataStore.networkChangeResetConnections) {
Libcore.resetAllConnections(true)
}
}
}
}
@ -286,16 +290,6 @@ class BaseService {
var wakeLock: PowerManager.WakeLock?
fun acquireWakeLock()
suspend fun switchWakeLock() {
wakeLock?.apply {
release()
wakeLock = null
data.notification?.postNotificationWakeLockStatus(false)
} ?: apply {
acquireWakeLock()
data.notification?.postNotificationWakeLockStatus(true)
}
}
suspend fun lateInit() {
wakeLock?.apply {

View File

@ -87,6 +87,8 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var showBottomBar by configurationStore.boolean(Key.SHOW_BOTTOM_BAR)
var allowInsecureOnRequest by configurationStore.boolean(Key.ALLOW_INSECURE_ON_REQUEST)
var networkChangeResetConnections by configurationStore.boolean(Key.NETWORK_CHANGE_RESET_CONNECTIONS) { true }
var wakeResetConnections by configurationStore.boolean(Key.WAKE_RESET_CONNECTIONS)
//
@ -98,7 +100,6 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var trafficSniffing by configurationStore.stringToInt(Key.TRAFFIC_SNIFFING) { 1 }
var resolveDestination by configurationStore.boolean(Key.RESOLVE_DESTINATION)
// var tcpKeepAliveInterval by configurationStore.stringToInt(Key.TCP_KEEP_ALIVE_INTERVAL) { 15 }
var mtu by configurationStore.stringToInt(Key.MTU) { 9000 }
var bypassLan by configurationStore.boolean(Key.BYPASS_LAN)

View File

@ -79,9 +79,6 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
val ipv6Mode = findPreference<Preference>(Key.IPV6_MODE)!!
val trafficSniffing = findPreference<Preference>(Key.TRAFFIC_SNIFFING)!!
val tcpKeepAliveInterval = findPreference<EditTextPreference>(Key.TCP_KEEP_ALIVE_INTERVAL)!!
tcpKeepAliveInterval.isVisible = false
val bypassLan = findPreference<SwitchPreference>(Key.BYPASS_LAN)!!
val bypassLanInCore = findPreference<SwitchPreference>(Key.BYPASS_LAN_IN_CORE)!!
@ -162,7 +159,6 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
appendHttpProxy.onPreferenceChangeListener = reloadListener
showDirectSpeed.onPreferenceChangeListener = reloadListener
trafficSniffing.onPreferenceChangeListener = reloadListener
tcpKeepAliveInterval.onPreferenceChangeListener = reloadListener
bypassLan.onPreferenceChangeListener = reloadListener
bypassLanInCore.onPreferenceChangeListener = reloadListener
mtu.onPreferenceChangeListener = reloadListener

View File

@ -15,8 +15,8 @@ import io.nekohasekai.sagernet.ktx.app
import io.nekohasekai.sagernet.ktx.runOnDefaultDispatcher
import io.nekohasekai.sagernet.utils.PackageCache
import libcore.BoxPlatformInterface
import libcore.Libcore
import libcore.NB4AInterface
import moe.matsuri.nb4a.utils.LibcoreUtil
import java.net.InetSocketAddress
class NativeInterface : BoxPlatformInterface, NB4AInterface {
@ -86,7 +86,7 @@ class NativeInterface : BoxPlatformInterface, NB4AInterface {
Logs.d("other selector: $selectorTag")
return
}
LibcoreUtil.resetAllConnections(true)
Libcore.resetAllConnections(true)
DataStore.baseService?.apply {
runOnDefaultDispatcher {
val id = data.proxy!!.config.profileTagMap

View File

@ -1,18 +0,0 @@
package moe.matsuri.nb4a.utils
import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.database.ProxyEntity
import io.nekohasekai.sagernet.database.SagerDatabase
import libcore.Libcore
object LibcoreUtil {
fun resetAllConnections(system: Boolean) {
if (DataStore.serviceState.started) {
val proxy = SagerDatabase.proxyDao.getById(DataStore.currentProfile)
if (proxy?.type == ProxyEntity.TYPE_TUIC) {
return
}
}
Libcore.resetAllConnections(system)
}
}

View File

@ -489,4 +489,6 @@
<string name="group_not_subscription">组类型不是订阅</string>
<string name="allow_insecure_on_request_sum">更新订阅的时候允许不安全的连接</string>
<string name="global_allow_insecure">总是跳过 TLS 证书验证</string>
<string name="network_change_reset_connections">当网络发生变化时重置出站连接</string>
<string name="wake_reset_connections">当设备从睡眠状态唤醒时重置出站连接</string>
</resources>

View File

@ -570,4 +570,6 @@
<string name="global_allow_insecure">Always allow insecure</string>
<string name="mux_preference">Mulitplex</string>
<string name="padding">Padding</string>
<string name="network_change_reset_connections">Reset outbound connections when network changes</string>
<string name="wake_reset_connections">Reset outbound connections when device wake from sleep</string>
</resources>

View File

@ -56,6 +56,13 @@
app:key="profileTrafficStatistics"
app:summary="@string/profile_traffic_statistics_summary"
app:title="@string/profile_traffic_statistics" />
<SwitchPreference
app:defaultValue="true"
app:icon="@drawable/ic_baseline_speed_24"
app:key="showDirectSpeed"
app:summary="@string/show_direct_speed_sum"
app:title="@string/show_direct_speed"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:key="showGroupInNotification"
app:title="@string/show_group_in_notification" />
@ -70,12 +77,10 @@
app:summary="@string/metered_summary"
app:title="@string/metered" />
<SwitchPreference
app:defaultValue="true"
app:icon="@drawable/ic_baseline_speed_24"
app:key="showDirectSpeed"
app:summary="@string/show_direct_speed_sum"
app:title="@string/show_direct_speed"
app:useSimpleSummaryProvider="true" />
app:icon="@drawable/baseline_developer_board_24"
app:key="acquireWakeLock"
app:summary="@string/acquire_wake_lock_summary"
app:title="@string/acquire_wake_lock" />
<moe.matsuri.nb4a.ui.LongClickListPreference
app:defaultValue="0"
app:entries="@array/log_level"
@ -206,28 +211,20 @@
app:key="connectionTestURL"
app:title="@string/connection_test_url"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:icon="@drawable/baseline_developer_board_24"
app:key="acquireWakeLock"
app:summary="@string/acquire_wake_lock_summary"
app:title="@string/acquire_wake_lock" />
<SwitchPreference
app:icon="@drawable/baseline_construction_24"
app:key="enableClashAPI"
app:summary="@string/enable_clash_api_summary"
app:title="@string/enable_clash_api" />
<EditTextPreference
app:defaultValue="15"
<SwitchPreference
app:defaultValue="true"
app:icon="@drawable/ic_baseline_flip_camera_android_24"
app:key="tcpKeepAliveInterval"
app:title="@string/tcp_keep_alive_interval"
app:key="networkChangeResetConnections"
app:title="@string/network_change_reset_connections"
app:useSimpleSummaryProvider="true" />
<moe.matsuri.nb4a.ui.SimpleMenuPreference
app:defaultValue="1.2"
app:entries="@array/app_tls_version"
app:entryValues="@array/app_tls_version"
app:key="appTLSVersion"
app:title="@string/app_tls_version"
<SwitchPreference
app:key="wakeResetConnections"
app:title="@string/wake_reset_connections"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:icon="@drawable/ic_action_lock_open"
@ -236,6 +233,13 @@
<SwitchPreference
app:key="allowInsecureOnRequest"
app:title="@string/allow_insecure_on_request_sum" />
<moe.matsuri.nb4a.ui.SimpleMenuPreference
app:defaultValue="1.2"
app:entries="@array/app_tls_version"
app:entryValues="@array/app_tls_version"
app:key="appTLSVersion"
app:title="@string/app_tls_version"
app:useSimpleSummaryProvider="true" />
<SwitchPreference
app:key="showBottomBar"
app:title="@string/show_bottom_bar" />

View File

@ -60,7 +60,9 @@ func VersionBox() string {
func ResetAllConnections(system bool) {
if system {
conntrack.Close()
log.Println("[Debug] Reset system connections done")
log.Println("Reset system connections done")
} else {
log.Println("TODO: Reset user connections")
}
}