Add Config options import

This commit is contained in:
problematicconsumer
2024-03-04 15:58:56 +03:30
parent 9c2e9d8d85
commit d87e207771
13 changed files with 216 additions and 63 deletions

View File

@@ -17,7 +17,8 @@
"decline": "Decline", "decline": "Decline",
"unknown": "Unknown", "unknown": "Unknown",
"hidden": "Hidden", "hidden": "Hidden",
"timeout": "timeout" "timeout": "timeout",
"clipboardExportSuccessMsg": "Added to Clipboard"
}, },
"intro": { "intro": {
"termsAndPolicyCaution(rich)": "by continuing you agree with ${tap(@:about.termsAndConditions)}", "termsAndPolicyCaution(rich)": "by continuing you agree with ${tap(@:about.termsAndConditions)}",
@@ -166,6 +167,10 @@
"requiresRestartMsg": "For this to take effect restart the app", "requiresRestartMsg": "For this to take effect restart the app",
"experimental": "Experimental", "experimental": "Experimental",
"experimentalMsg": "Features with Experimental flag are still in development and might cause issues.", "experimentalMsg": "Features with Experimental flag are still in development and might cause issues.",
"exportOptions": "Export Options to Clipboard",
"exportAllOptions": "Export Options to Clipboard (debug)",
"importOptions": "Import Options from Clipboard",
"importOptionsMsg": "This will rewrite all config options with provided values. Are you sure?",
"general": { "general": {
"sectionTitle": "General", "sectionTitle": "General",
"locale": "Language", "locale": "Language",

View File

@@ -1,6 +1,9 @@
targets: targets:
$default: $default:
builders: builders:
json_serializable:
options:
explicit_to_json: true
drift_dev: drift_dev:
options: options:
store_date_time_values_as_text: true store_date_time_values_as_text: true

View File

@@ -71,6 +71,17 @@ class PreferencesEntry<T, P> with InfraLogger {
} }
} }
Future<T?> writeRaw(P input) async {
final T value;
if (mapFrom != null) {
value = mapFrom!(input);
} else {
value = input as T;
}
if (await write(value)) return value;
return null;
}
Future<void> remove() async { Future<void> remove() async {
try { try {
await preferences.remove(key); await preferences.remove(key);
@@ -145,6 +156,11 @@ class PreferencesNotifier<T, P> extends StateNotifier<T> {
return value as P; return value as P;
} }
Future<void> updateRaw(P input) async {
final value = await entry.writeRaw(input);
if (value != null) state = value;
}
Future<void> update(T value) async { Future<void> update(T value) async {
if (await entry.write(value)) state = value; if (await entry.write(value)) state = value;
} }

View File

@@ -1,4 +1,3 @@
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
@@ -13,7 +12,7 @@ Future<bool> showConfirmationDialog(
builder: (context) { builder: (context) {
final localizations = MaterialLocalizations.of(context); final localizations = MaterialLocalizations.of(context);
return AlertDialog( return AlertDialog(
icon: const Icon(FluentIcons.delete_24_regular), icon: icon != null ? Icon(icon) : null,
title: Text(title), title: Text(title),
content: Text(message), content: Text(message),
actions: [ actions: [

View File

@@ -284,47 +284,58 @@ abstract class ConfigOptions {
}, },
); );
/// list of all config option preferences /// preferences to exclude from share and export
static final preferences = [ static final privatePreferencesKeys = {
serviceMode, "warp.license-key",
logLevel, "warp.access-token",
resolveDestination, "warp.account-id",
ipv6Mode, "warp.wireguard-config",
remoteDnsAddress, };
remoteDnsDomainStrategy,
directDnsAddress, static final Map<String, StateNotifierProvider<PreferencesNotifier, dynamic>>
directDnsDomainStrategy, preferences = {
mixedPort, "service-mode": serviceMode,
localDnsPort, "log-level": logLevel,
tunImplementation, "resolve-destination": resolveDestination,
mtu, "ipv6-mode": ipv6Mode,
strictRoute, "remote-dns-address": remoteDnsAddress,
connectionTestUrl, "remote-dns-domain-strategy": remoteDnsDomainStrategy,
urlTestInterval, "direct-dns-address": directDnsAddress,
clashApiPort, "direct-dns-domain-strategy": directDnsDomainStrategy,
bypassLan, "mixed-port": mixedPort,
allowConnectionFromLan, "local-dns-port": localDnsPort,
enableDnsRouting, "tun-implementation": tunImplementation,
enableTlsFragment, "mtu": mtu,
tlsFragmentSize, "strict-route": strictRoute,
tlsFragmentSleep, "connection-test-url": connectionTestUrl,
enableTlsMixedSniCase, "url-test-interval": urlTestInterval,
enableTlsPadding, "clash-api-port": clashApiPort,
tlsPaddingSize, "bypass-lan": bypassLan,
enableMux, "allow-connection-from-lan": allowConnectionFromLan,
muxPadding, "enable-dns-routing": enableDnsRouting,
muxMaxStreams, "enable-tls-fragment": enableTlsFragment,
muxProtocol, "tls-fragment-size": tlsFragmentSize,
enableWarp, "tls-fragment-sleep": tlsFragmentSleep,
warpDetourMode, "enable-tls-mixed-sni-case": enableTlsMixedSniCase,
warpLicenseKey, "enable-tls-padding": enableTlsPadding,
warpAccountId, "tls-padding-size": tlsPaddingSize,
warpAccessToken, "enable-mux": enableMux,
warpCleanIp, "mux-padding": muxPadding,
warpPort, "mux-max-streams": muxMaxStreams,
warpNoise, "mux-protocol": muxProtocol,
warpWireguardConfig,
]; // warp
"warp.enable": enableWarp,
"warp.mode": warpDetourMode,
"warp.license-key": warpLicenseKey,
"warp.account-id": warpAccountId,
"warp.access-token": warpAccessToken,
"warp.clean-ip": warpCleanIp,
"warp.clean-port": warpPort,
"warp.noise": warpNoise,
"warp.noise-delay": warpNoiseDelay,
"warp.wireguard-config": warpWireguardConfig,
};
static final singboxConfigOptions = FutureProvider<SingboxConfigOption>( static final singboxConfigOptions = FutureProvider<SingboxConfigOption>(
(ref) async { (ref) async {
@@ -411,8 +422,8 @@ abstract class ConfigOptions {
accessToken: ref.watch(warpAccessToken), accessToken: ref.watch(warpAccessToken),
cleanIp: ref.watch(warpCleanIp), cleanIp: ref.watch(warpCleanIp),
cleanPort: ref.watch(warpPort), cleanPort: ref.watch(warpPort),
warpNoise: ref.watch(warpNoise), noise: ref.watch(warpNoise),
warpNoiseDelay: ref.watch(warpNoiseDelay), noiseDelay: ref.watch(warpNoiseDelay),
), ),
geoipPath: ref.watch(geoAssetPathResolverProvider).relativePath( geoipPath: ref.watch(geoAssetPathResolverProvider).relativePath(
geoAssets.geoip.providerName, geoAssets.geoip.providerName,
@@ -477,8 +488,8 @@ abstract class ConfigOptions {
accessToken: ref.read(warpAccessToken), accessToken: ref.read(warpAccessToken),
cleanIp: ref.read(warpCleanIp), cleanIp: ref.read(warpCleanIp),
cleanPort: ref.read(warpPort), cleanPort: ref.read(warpPort),
warpNoise: ref.read(warpNoise), noise: ref.read(warpNoise),
warpNoiseDelay: ref.read(warpNoiseDelay), noiseDelay: ref.read(warpNoiseDelay),
), ),
geoipPath: "", geoipPath: "",
geositePath: "", geositePath: "",

View File

@@ -5,6 +5,7 @@ import 'package:hiddify/features/config_option/data/config_option_repository.dar
import 'package:hiddify/features/connection/data/connection_data_providers.dart'; import 'package:hiddify/features/connection/data/connection_data_providers.dart';
import 'package:hiddify/features/connection/notifier/connection_notifier.dart'; import 'package:hiddify/features/connection/notifier/connection_notifier.dart';
import 'package:hiddify/utils/custom_loggers.dart'; import 'package:hiddify/utils/custom_loggers.dart';
import 'package:json_path/json_path.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'config_option_notifier.g.dart'; part 'config_option_notifier.g.dart';
@@ -36,18 +37,57 @@ class ConfigOptionNotifier extends _$ConfigOptionNotifier with AppLogger {
DateTime? _lastUpdate; DateTime? _lastUpdate;
Future<void> exportJsonToClipboard() async { Future<bool> exportJsonToClipboard({bool excludePrivate = true}) async {
final map = { try {
for (final option in ConfigOptions.preferences) final options = await ref.read(ConfigOptions.singboxConfigOptions.future);
ref.read(option.notifier).entry.key: ref.read(option.notifier).raw(), Map map = options.toJson();
}; if (excludePrivate) {
const encoder = JsonEncoder.withIndent(' '); for (final key in ConfigOptions.privatePreferencesKeys) {
final json = encoder.convert(map); final query = key.split('.').map((e) => '["$e"]').join();
await Clipboard.setData(ClipboardData(text: json)); final res = JsonPath('\$$query').read(map).firstOrNull;
if (res != null) {
map = res.pointer.remove(map)! as Map;
}
}
}
const encoder = JsonEncoder.withIndent(' ');
final json = encoder.convert(map);
await Clipboard.setData(ClipboardData(text: json));
return true;
} catch (e, st) {
loggy.warning("error exporting config options to clipboard", e, st);
return false;
}
}
Future<bool> importFromClipboard() async {
try {
final input =
await Clipboard.getData("text/plain").then((value) => value?.text);
if (input == null) return false;
if (jsonDecode(input) case final Map<String, dynamic> map) {
for (final option in ConfigOptions.preferences.entries) {
final query = option.key.split('.').map((e) => '["$e"]').join();
final res = JsonPath('\$$query').read(map).firstOrNull;
if (res?.value case final value?) {
try {
await ref.read(option.value.notifier).updateRaw(value);
} catch (e) {
loggy.debug("error updating [${option.key}]: $e", e);
}
}
}
}
return true;
} catch (e, st) {
loggy.warning("error importing config options to clipboard", e, st);
return false;
}
} }
Future<void> resetOption() async { Future<void> resetOption() async {
for (final option in ConfigOptions.preferences) { for (final option in ConfigOptions.preferences.values) {
await ref.read(option.notifier).reset(); await ref.read(option.notifier).reset();
} }
ref.invalidateSelf(); ref.invalidateSelf();

View File

@@ -3,8 +3,11 @@ import 'package:flutter/material.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:hiddify/core/localization/translations.dart'; import 'package:hiddify/core/localization/translations.dart';
import 'package:hiddify/core/model/optional_range.dart'; import 'package:hiddify/core/model/optional_range.dart';
import 'package:hiddify/core/notification/in_app_notification_controller.dart';
import 'package:hiddify/core/preferences/general_preferences.dart';
import 'package:hiddify/core/widget/adaptive_icon.dart'; import 'package:hiddify/core/widget/adaptive_icon.dart';
import 'package:hiddify/core/widget/tip_card.dart'; import 'package:hiddify/core/widget/tip_card.dart';
import 'package:hiddify/features/common/confirmation_dialogs.dart';
import 'package:hiddify/features/common/nested_app_bar.dart'; import 'package:hiddify/features/common/nested_app_bar.dart';
import 'package:hiddify/features/config_option/data/config_option_repository.dart'; import 'package:hiddify/features/config_option/data/config_option_repository.dart';
import 'package:hiddify/features/config_option/notifier/config_option_notifier.dart'; import 'package:hiddify/features/config_option/notifier/config_option_notifier.dart';
@@ -40,10 +43,50 @@ class ConfigOptionsPage extends HookConsumerWidget {
itemBuilder: (context) { itemBuilder: (context) {
return [ return [
PopupMenuItem( PopupMenuItem(
onTap: ref onTap: () async => ref
.read(configOptionNotifierProvider.notifier) .read(configOptionNotifierProvider.notifier)
.exportJsonToClipboard, .exportJsonToClipboard()
child: Text(t.general.addToClipboard), .then((success) {
if (success) {
ref
.read(inAppNotificationControllerProvider)
.showSuccessToast(
t.general.clipboardExportSuccessMsg,
);
}
}),
child: Text(t.settings.exportOptions),
),
if (ref.watch(debugModeNotifierProvider))
PopupMenuItem(
onTap: () async => ref
.read(configOptionNotifierProvider.notifier)
.exportJsonToClipboard(excludePrivate: false)
.then((success) {
if (success) {
ref
.read(inAppNotificationControllerProvider)
.showSuccessToast(
t.general.clipboardExportSuccessMsg,
);
}
}),
child: Text(t.settings.exportAllOptions),
),
PopupMenuItem(
onTap: () async {
final shouldImport = await showConfirmationDialog(
context,
title: t.settings.importOptions,
message: t.settings.importOptionsMsg,
);
if (shouldImport) {
await ref
.read(configOptionNotifierProvider.notifier)
.importFromClipboard();
}
},
child: Text(t.settings.importOptions),
), ),
PopupMenuItem( PopupMenuItem(
child: Text(t.settings.config.resetBtn), child: Text(t.settings.config.resetBtn),

View File

@@ -116,6 +116,7 @@ class ProfileDetailsPage extends HookConsumerWidget with PresLogger {
context, context,
title: t.profile.delete.buttonTxt, title: t.profile.delete.buttonTxt,
message: t.profile.delete.confirmationMsg, message: t.profile.delete.confirmationMsg,
icon: FluentIcons.delete_24_regular,
); );
if (deleteConfirmed) { if (deleteConfirmed) {
await notifier.delete(); await notifier.delete();

View File

@@ -327,6 +327,7 @@ class ProfileActionsMenu extends HookConsumerWidget {
context, context,
title: t.profile.delete.buttonTxt, title: t.profile.delete.buttonTxt,
message: t.profile.delete.confirmationMsg, message: t.profile.delete.confirmationMsg,
icon: FluentIcons.delete_24_regular,
); );
if (deleteConfirmed) { if (deleteConfirmed) {
deleteProfileMutation.setFuture( deleteProfileMutation.setFuture(

View File

@@ -68,6 +68,7 @@ class SingboxConfigOption with _$SingboxConfigOption {
@freezed @freezed
class SingboxWarpOption with _$SingboxWarpOption { class SingboxWarpOption with _$SingboxWarpOption {
@JsonSerializable(fieldRename: FieldRename.kebab, createFieldMap: true)
const factory SingboxWarpOption({ const factory SingboxWarpOption({
required bool enable, required bool enable,
required WarpDetourMode mode, required WarpDetourMode mode,
@@ -77,8 +78,8 @@ class SingboxWarpOption with _$SingboxWarpOption {
required String accessToken, required String accessToken,
required String cleanIp, required String cleanIp,
required int cleanPort, required int cleanPort,
@OptionalRangeJsonConverter() required OptionalRange warpNoise, @OptionalRangeJsonConverter() required OptionalRange noise,
@OptionalRangeJsonConverter() required OptionalRange warpNoiseDelay, @OptionalRangeJsonConverter() required OptionalRange noiseDelay,
}) = _SingboxWarpOption; }) = _SingboxWarpOption;
factory SingboxWarpOption.fromJson(Map<String, dynamic> json) => factory SingboxWarpOption.fromJson(Map<String, dynamic> json) =>

Submodule libcore updated: 3793b614db...f9e6f022c8

View File

@@ -813,6 +813,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "1.0.4" version: "1.0.4"
iregexp:
dependency: transitive
description:
name: iregexp
sha256: "143859dcaeecf6f683102786762d70a47ef8441a0d2287a158172d32d38799cf"
url: "https://pub.dev"
source: hosted
version: "0.1.2"
js: js:
dependency: transitive dependency: transitive
description: description:
@@ -837,6 +845,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "4.8.1" version: "4.8.1"
json_path:
dependency: "direct main"
description:
name: json_path
sha256: "149d32ceb7dc22422ea6d09e401fd688f54e1343bc9ff8c3cb1900ca3b1ad8b1"
url: "https://pub.dev"
source: hosted
version: "0.7.1"
json_serializable: json_serializable:
dependency: "direct dev" dependency: "direct dev"
description: description:
@@ -893,6 +909,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "0.5.0" version: "0.5.0"
maybe_just_nothing:
dependency: transitive
description:
name: maybe_just_nothing
sha256: "0c06326e26d08f6ed43247404376366dc4d756cef23a4f1db765f546224c35e0"
url: "https://pub.dev"
source: hosted
version: "0.5.3"
menu_base: menu_base:
dependency: transitive dependency: transitive
description: description:
@@ -1229,6 +1253,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "3.1.2" version: "3.1.2"
rfc_6901:
dependency: transitive
description:
name: rfc_6901
sha256: df1bbfa3d023009598f19636d6114c6ac1e0b7bb7bf6a260f0e6e6ce91416820
url: "https://pub.dev"
source: hosted
version: "0.2.0"
riverpod: riverpod:
dependency: transitive dependency: transitive
description: description:

View File

@@ -76,6 +76,7 @@ dependencies:
circle_flags: ^4.0.2 circle_flags: ^4.0.2
http: ^1.2.0 http: ^1.2.0
timezone_to_country: ^2.1.0 timezone_to_country: ^2.1.0
json_path: ^0.7.1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: