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",
"unknown": "Unknown",
"hidden": "Hidden",
"timeout": "timeout"
"timeout": "timeout",
"clipboardExportSuccessMsg": "Added to Clipboard"
},
"intro": {
"termsAndPolicyCaution(rich)": "by continuing you agree with ${tap(@:about.termsAndConditions)}",
@@ -166,6 +167,10 @@
"requiresRestartMsg": "For this to take effect restart the app",
"experimental": "Experimental",
"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": {
"sectionTitle": "General",
"locale": "Language",

View File

@@ -1,6 +1,9 @@
targets:
$default:
builders:
json_serializable:
options:
explicit_to_json: true
drift_dev:
options:
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 {
try {
await preferences.remove(key);
@@ -145,6 +156,11 @@ class PreferencesNotifier<T, P> extends StateNotifier<T> {
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 {
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:go_router/go_router.dart';
@@ -13,7 +12,7 @@ Future<bool> showConfirmationDialog(
builder: (context) {
final localizations = MaterialLocalizations.of(context);
return AlertDialog(
icon: const Icon(FluentIcons.delete_24_regular),
icon: icon != null ? Icon(icon) : null,
title: Text(title),
content: Text(message),
actions: [

View File

@@ -284,47 +284,58 @@ abstract class ConfigOptions {
},
);
/// list of all config option preferences
static final preferences = [
serviceMode,
logLevel,
resolveDestination,
ipv6Mode,
remoteDnsAddress,
remoteDnsDomainStrategy,
directDnsAddress,
directDnsDomainStrategy,
mixedPort,
localDnsPort,
tunImplementation,
mtu,
strictRoute,
connectionTestUrl,
urlTestInterval,
clashApiPort,
bypassLan,
allowConnectionFromLan,
enableDnsRouting,
enableTlsFragment,
tlsFragmentSize,
tlsFragmentSleep,
enableTlsMixedSniCase,
enableTlsPadding,
tlsPaddingSize,
enableMux,
muxPadding,
muxMaxStreams,
muxProtocol,
enableWarp,
warpDetourMode,
warpLicenseKey,
warpAccountId,
warpAccessToken,
warpCleanIp,
warpPort,
warpNoise,
warpWireguardConfig,
];
/// preferences to exclude from share and export
static final privatePreferencesKeys = {
"warp.license-key",
"warp.access-token",
"warp.account-id",
"warp.wireguard-config",
};
static final Map<String, StateNotifierProvider<PreferencesNotifier, dynamic>>
preferences = {
"service-mode": serviceMode,
"log-level": logLevel,
"resolve-destination": resolveDestination,
"ipv6-mode": ipv6Mode,
"remote-dns-address": remoteDnsAddress,
"remote-dns-domain-strategy": remoteDnsDomainStrategy,
"direct-dns-address": directDnsAddress,
"direct-dns-domain-strategy": directDnsDomainStrategy,
"mixed-port": mixedPort,
"local-dns-port": localDnsPort,
"tun-implementation": tunImplementation,
"mtu": mtu,
"strict-route": strictRoute,
"connection-test-url": connectionTestUrl,
"url-test-interval": urlTestInterval,
"clash-api-port": clashApiPort,
"bypass-lan": bypassLan,
"allow-connection-from-lan": allowConnectionFromLan,
"enable-dns-routing": enableDnsRouting,
"enable-tls-fragment": enableTlsFragment,
"tls-fragment-size": tlsFragmentSize,
"tls-fragment-sleep": tlsFragmentSleep,
"enable-tls-mixed-sni-case": enableTlsMixedSniCase,
"enable-tls-padding": enableTlsPadding,
"tls-padding-size": tlsPaddingSize,
"enable-mux": enableMux,
"mux-padding": muxPadding,
"mux-max-streams": muxMaxStreams,
"mux-protocol": muxProtocol,
// 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>(
(ref) async {
@@ -411,8 +422,8 @@ abstract class ConfigOptions {
accessToken: ref.watch(warpAccessToken),
cleanIp: ref.watch(warpCleanIp),
cleanPort: ref.watch(warpPort),
warpNoise: ref.watch(warpNoise),
warpNoiseDelay: ref.watch(warpNoiseDelay),
noise: ref.watch(warpNoise),
noiseDelay: ref.watch(warpNoiseDelay),
),
geoipPath: ref.watch(geoAssetPathResolverProvider).relativePath(
geoAssets.geoip.providerName,
@@ -477,8 +488,8 @@ abstract class ConfigOptions {
accessToken: ref.read(warpAccessToken),
cleanIp: ref.read(warpCleanIp),
cleanPort: ref.read(warpPort),
warpNoise: ref.read(warpNoise),
warpNoiseDelay: ref.read(warpNoiseDelay),
noise: ref.read(warpNoise),
noiseDelay: ref.read(warpNoiseDelay),
),
geoipPath: "",
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/notifier/connection_notifier.dart';
import 'package:hiddify/utils/custom_loggers.dart';
import 'package:json_path/json_path.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'config_option_notifier.g.dart';
@@ -36,18 +37,57 @@ class ConfigOptionNotifier extends _$ConfigOptionNotifier with AppLogger {
DateTime? _lastUpdate;
Future<void> exportJsonToClipboard() async {
final map = {
for (final option in ConfigOptions.preferences)
ref.read(option.notifier).entry.key: ref.read(option.notifier).raw(),
};
Future<bool> exportJsonToClipboard({bool excludePrivate = true}) async {
try {
final options = await ref.read(ConfigOptions.singboxConfigOptions.future);
Map map = options.toJson();
if (excludePrivate) {
for (final key in ConfigOptions.privatePreferencesKeys) {
final query = key.split('.').map((e) => '["$e"]').join();
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 {
for (final option in ConfigOptions.preferences) {
for (final option in ConfigOptions.preferences.values) {
await ref.read(option.notifier).reset();
}
ref.invalidateSelf();

View File

@@ -3,8 +3,11 @@ import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:hiddify/core/localization/translations.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/tip_card.dart';
import 'package:hiddify/features/common/confirmation_dialogs.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/notifier/config_option_notifier.dart';
@@ -40,10 +43,50 @@ class ConfigOptionsPage extends HookConsumerWidget {
itemBuilder: (context) {
return [
PopupMenuItem(
onTap: ref
onTap: () async => ref
.read(configOptionNotifierProvider.notifier)
.exportJsonToClipboard,
child: Text(t.general.addToClipboard),
.exportJsonToClipboard()
.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(
child: Text(t.settings.config.resetBtn),

View File

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

View File

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

View File

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

Submodule libcore updated: 3793b614db...f9e6f022c8

View File

@@ -813,6 +813,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.4"
iregexp:
dependency: transitive
description:
name: iregexp
sha256: "143859dcaeecf6f683102786762d70a47ef8441a0d2287a158172d32d38799cf"
url: "https://pub.dev"
source: hosted
version: "0.1.2"
js:
dependency: transitive
description:
@@ -837,6 +845,14 @@ packages:
url: "https://pub.dev"
source: hosted
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:
dependency: "direct dev"
description:
@@ -893,6 +909,14 @@ packages:
url: "https://pub.dev"
source: hosted
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:
dependency: transitive
description:
@@ -1229,6 +1253,14 @@ packages:
url: "https://pub.dev"
source: hosted
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:
dependency: transitive
description:

View File

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