Skip to content

Commit

Permalink
Fix service mode
Browse files Browse the repository at this point in the history
  • Loading branch information
problematicconsumer committed Dec 14, 2023
1 parent af64efe commit b14938c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
6 changes: 1 addition & 5 deletions android/app/src/main/kotlin/com/hiddify/hiddify/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import java.io.ByteArrayInputStream
import java.io.File
import java.io.ObjectInputStream


object Settings {

private val preferences by lazy {
Expand Down Expand Up @@ -67,9 +66,6 @@ object Settings {
get() = preferences.getBoolean(SettingsKey.DEBUG_MODE, false)
set(value) = preferences.edit().putBoolean(SettingsKey.DEBUG_MODE, value).apply()

val enableTun: Boolean
get() = preferences.getBoolean(SettingsKey.ENABLE_TUN, true)

var disableMemoryLimit: Boolean
get() = preferences.getBoolean(SettingsKey.DISABLE_MEMORY_LIMIT, false)
set(value) =
Expand Down Expand Up @@ -112,7 +108,7 @@ object Settings {
}

private suspend fun needVPNService(): Boolean {
if (enableTun) return true
if (serviceMode == ServiceMode.VPN) return true
val filePath = activeConfigPath
if (filePath.isBlank()) return false
val content = JSONObject(File(filePath).readText())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hiddify.hiddify.constant

object ServiceMode {
const val NORMAL = "normal"
const val NORMAL = "proxy"
const val VPN = "vpn"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package com.hiddify.hiddify.constant
object SettingsKey {
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_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_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 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 DYNAMIC_NOTIFICATION = "${KEY_PREFIX}dynamic_notification"
const val SYSTEM_PROXY_ENABLED = "${KEY_PREFIX}system_proxy_enabled"

// cache

const val STARTED_BY_USER = "${KEY_PREFIX}started_by_user"
const val CONFIG_OPTIONS = "config_options_json"

}
14 changes: 14 additions & 0 deletions lib/core/preferences/preferences_migration.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ class PreferencesVersion1Migration extends PreferencesMigrationStep

@override
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) {
loggy.debug(
"changing ipv6-mode from [$ipv6Mode] to [${_ipv6Mapper(ipv6Mode)}]",
Expand Down
11 changes: 8 additions & 3 deletions lib/singbox/model/singbox_config_enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/utils/platform_utils.dart';
import 'package:json_annotation/json_annotation.dart';

@JsonEnum(valueField: 'key')
enum ServiceMode {
proxy,
systemProxy,
tun;
proxy("proxy"),
systemProxy("system-proxy"),
tun("vpn");

const ServiceMode(this.key);

final String key;

static ServiceMode get defaultMode =>
PlatformUtils.isDesktop ? systemProxy : tun;
Expand Down

0 comments on commit b14938c

Please sign in to comment.