Fix service mode

This commit is contained in:
problematicconsumer
2023-12-14 15:46:15 +03:30
parent af64efec00
commit b14938ca62
5 changed files with 26 additions and 13 deletions

View File

@@ -12,7 +12,6 @@ import java.io.ByteArrayInputStream
import java.io.File import java.io.File
import java.io.ObjectInputStream import java.io.ObjectInputStream
object Settings { object Settings {
private val preferences by lazy { private val preferences by lazy {
@@ -67,9 +66,6 @@ object Settings {
get() = preferences.getBoolean(SettingsKey.DEBUG_MODE, false) get() = preferences.getBoolean(SettingsKey.DEBUG_MODE, false)
set(value) = preferences.edit().putBoolean(SettingsKey.DEBUG_MODE, value).apply() set(value) = preferences.edit().putBoolean(SettingsKey.DEBUG_MODE, value).apply()
val enableTun: Boolean
get() = preferences.getBoolean(SettingsKey.ENABLE_TUN, true)
var disableMemoryLimit: Boolean var disableMemoryLimit: Boolean
get() = preferences.getBoolean(SettingsKey.DISABLE_MEMORY_LIMIT, false) get() = preferences.getBoolean(SettingsKey.DISABLE_MEMORY_LIMIT, false)
set(value) = set(value) =
@@ -112,7 +108,7 @@ object Settings {
} }
private suspend fun needVPNService(): Boolean { private suspend fun needVPNService(): Boolean {
if (enableTun) return true if (serviceMode == ServiceMode.VPN) return true
val filePath = activeConfigPath val filePath = activeConfigPath
if (filePath.isBlank()) return false if (filePath.isBlank()) return false
val content = JSONObject(File(filePath).readText()) val content = JSONObject(File(filePath).readText())

View File

@@ -1,6 +1,6 @@
package com.hiddify.hiddify.constant package com.hiddify.hiddify.constant
object ServiceMode { object ServiceMode {
const val NORMAL = "normal" const val NORMAL = "proxy"
const val VPN = "vpn" const val VPN = "vpn"
} }

View File

@@ -3,18 +3,15 @@ package com.hiddify.hiddify.constant
object SettingsKey { object SettingsKey {
private const val KEY_PREFIX = "flutter." private const val KEY_PREFIX = "flutter."
const val SERVICE_MODE = "${KEY_PREFIX}service-mode"
const val ACTIVE_CONFIG_PATH = "${KEY_PREFIX}active_config_path" const val ACTIVE_CONFIG_PATH = "${KEY_PREFIX}active_config_path"
const val ACTIVE_PROFILE_NAME = "${KEY_PREFIX}active_profile_name" const val ACTIVE_PROFILE_NAME = "${KEY_PREFIX}active_profile_name"
const val SERVICE_MODE = "${KEY_PREFIX}service_mode"
const val CONFIG_OPTIONS = "config_options_json"
const val PER_APP_PROXY_MODE = "${KEY_PREFIX}per_app_proxy_mode" const val PER_APP_PROXY_MODE = "${KEY_PREFIX}per_app_proxy_mode"
const val PER_APP_PROXY_INCLUDE_LIST = "${KEY_PREFIX}per_app_proxy_include_list" const val PER_APP_PROXY_INCLUDE_LIST = "${KEY_PREFIX}per_app_proxy_include_list"
const val PER_APP_PROXY_EXCLUDE_LIST = "${KEY_PREFIX}per_app_proxy_exclude_list" const val PER_APP_PROXY_EXCLUDE_LIST = "${KEY_PREFIX}per_app_proxy_exclude_list"
const val DEBUG_MODE = "${KEY_PREFIX}debug_mode" const val DEBUG_MODE = "${KEY_PREFIX}debug_mode"
const val ENABLE_TUN = "${KEY_PREFIX}enable-tun"
const val DISABLE_MEMORY_LIMIT = "${KEY_PREFIX}disable_memory_limit" const val DISABLE_MEMORY_LIMIT = "${KEY_PREFIX}disable_memory_limit"
const val DYNAMIC_NOTIFICATION = "${KEY_PREFIX}dynamic_notification" const val DYNAMIC_NOTIFICATION = "${KEY_PREFIX}dynamic_notification"
const val SYSTEM_PROXY_ENABLED = "${KEY_PREFIX}system_proxy_enabled" const val SYSTEM_PROXY_ENABLED = "${KEY_PREFIX}system_proxy_enabled"
@@ -22,5 +19,6 @@ object SettingsKey {
// cache // cache
const val STARTED_BY_USER = "${KEY_PREFIX}started_by_user" const val STARTED_BY_USER = "${KEY_PREFIX}started_by_user"
const val CONFIG_OPTIONS = "config_options_json"
} }

View File

@@ -48,6 +48,20 @@ class PreferencesVersion1Migration extends PreferencesMigrationStep
@override @override
Future<void> migrate() async { Future<void> migrate() async {
if (sharedPreferences.getString("service-mode")
case final String serviceMode) {
final newMode = switch (serviceMode) {
"proxy" || "system-proxy" || "vpn" => serviceMode,
"systemProxy" => "system-proxy",
"tun" => "vpn",
_ => PlatformUtils.isDesktop ? "system-proxy" : "vpn",
};
loggy.debug(
"changing service-mode from [$serviceMode] to [$newMode]",
);
await sharedPreferences.setString("service-mode", newMode);
}
if (sharedPreferences.getString("ipv6-mode") case final String ipv6Mode) { if (sharedPreferences.getString("ipv6-mode") case final String ipv6Mode) {
loggy.debug( loggy.debug(
"changing ipv6-mode from [$ipv6Mode] to [${_ipv6Mapper(ipv6Mode)}]", "changing ipv6-mode from [$ipv6Mode] to [${_ipv6Mapper(ipv6Mode)}]",

View File

@@ -2,10 +2,15 @@ import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/utils/platform_utils.dart'; import 'package:hiddify/utils/platform_utils.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
@JsonEnum(valueField: 'key')
enum ServiceMode { enum ServiceMode {
proxy, proxy("proxy"),
systemProxy, systemProxy("system-proxy"),
tun; tun("vpn");
const ServiceMode(this.key);
final String key;
static ServiceMode get defaultMode => static ServiceMode get defaultMode =>
PlatformUtils.isDesktop ? systemProxy : tun; PlatformUtils.isDesktop ? systemProxy : tun;