From fabd559b19036fdcc088c10e9781586d2bceb861 Mon Sep 17 00:00:00 2001 From: problematicconsumer Date: Thu, 7 Mar 2024 20:21:25 +0330 Subject: [PATCH] Refactor config options --- assets/translations/strings_en.i18n.json | 4 +- .../data/config_option_data_providers.dart | 2 +- .../data/config_option_repository.dart | 163 ++++-------------- lib/singbox/model/singbox_config_enum.dart | 13 +- lib/singbox/model/singbox_config_option.dart | 44 +++-- libcore | 2 +- 6 files changed, 77 insertions(+), 151 deletions(-) diff --git a/assets/translations/strings_en.i18n.json b/assets/translations/strings_en.i18n.json index 2bb34ce8..4d6665db 100644 --- a/assets/translations/strings_en.i18n.json +++ b/assets/translations/strings_en.i18n.json @@ -302,8 +302,8 @@ "enableWarp": "Enable WARP", "warpDetourMode": "Detour Mode", "warpDetourModes": { - "inbound": "Detour WARP through proxies", - "outbound": "Detour proxies through WARP" + "proxyOverWarp": "Detour proxies through WARP", + "warpOverProxy": "Detour WARP through proxies" }, "warpLicenseKey": "License Key", "warpCleanIp": "Clean IP", diff --git a/lib/features/config_option/data/config_option_data_providers.dart b/lib/features/config_option/data/config_option_data_providers.dart index 987a4c31..f28d127e 100644 --- a/lib/features/config_option/data/config_option_data_providers.dart +++ b/lib/features/config_option/data/config_option_data_providers.dart @@ -11,7 +11,7 @@ ConfigOptionRepository configOptionRepository( ) { return ConfigOptionRepository( preferences: ref.watch(sharedPreferencesProvider).requireValue, - getConfigOptions: () => ConfigOptions.singboxOptions(ref), + getConfigOptions: () => ref.read(ConfigOptions.singboxConfigOptions.future), geoAssetRepository: ref.watch(geoAssetRepositoryProvider).requireValue, geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider), ); diff --git a/lib/features/config_option/data/config_option_repository.dart b/lib/features/config_option/data/config_option_repository.dart index db77793d..e0ef080a 100644 --- a/lib/features/config_option/data/config_option_repository.dart +++ b/lib/features/config_option/data/config_option_repository.dart @@ -215,7 +215,7 @@ abstract class ConfigOptions { static final warpDetourMode = PreferencesNotifier.create( "warp-detour-mode", - WarpDetourMode.outbound, + WarpDetourMode.proxyOverWarp, mapFrom: WarpDetourMode.values.byName, mapTo: (value) => value.name, ); @@ -313,16 +313,20 @@ abstract class ConfigOptions { "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, + + // mux + "mux.enable": enableMux, + "mux.padding": muxPadding, + "mux.max-streams": muxMaxStreams, + "mux.protocol": muxProtocol, + + // tls-tricks + "tls-tricks.enable-fragment": enableTlsFragment, + "tls-tricks.fragment-size": tlsFragmentSize, + "tls-tricks.fragment-sleep": tlsFragmentSleep, + "tls-tricks.mixed-sni-case": enableTlsMixedSniCase, + "tls-tricks.enable-padding": enableTlsPadding, + "tls-tricks.padding-size": tlsPaddingSize, // warp "warp.enable": enableWarp, @@ -403,16 +407,20 @@ abstract class ConfigOptions { enableFakeDns: ref.watch(enableFakeDns), enableDnsRouting: ref.watch(enableDnsRouting), independentDnsCache: ref.watch(independentDnsCache), - enableTlsFragment: ref.watch(enableTlsFragment), - tlsFragmentSize: ref.watch(tlsFragmentSize), - tlsFragmentSleep: ref.watch(tlsFragmentSleep), - enableTlsMixedSniCase: ref.watch(enableTlsMixedSniCase), - enableTlsPadding: ref.watch(enableTlsPadding), - tlsPaddingSize: ref.watch(tlsPaddingSize), - enableMux: ref.watch(enableMux), - muxPadding: ref.watch(muxPadding), - muxMaxStreams: ref.watch(muxMaxStreams), - muxProtocol: ref.watch(muxProtocol), + mux: SingboxMuxOption( + enable: ref.watch(enableMux), + padding: ref.watch(muxPadding), + maxStreams: ref.watch(muxMaxStreams), + protocol: ref.watch(muxProtocol), + ), + tlsTricks: SingboxTlsTricks( + enableFragment: ref.watch(enableTlsFragment), + fragmentSize: ref.watch(tlsFragmentSize), + fragmentSleep: ref.watch(tlsFragmentSleep), + mixedSniCase: ref.watch(enableTlsMixedSniCase), + enablePadding: ref.watch(enableTlsPadding), + paddingSize: ref.watch(tlsPaddingSize), + ), warp: SingboxWarpOption( enable: ref.watch(enableWarp), mode: ref.watch(warpDetourMode), @@ -437,65 +445,6 @@ abstract class ConfigOptions { ); }, ); - - /// singbox options - /// - /// **this is partial, don't use it directly** - static SingboxConfigOption singboxOptions(ProviderRef ref) { - final mode = ref.read(serviceMode); - return SingboxConfigOption( - executeConfigAsIs: false, - logLevel: ref.read(logLevel), - resolveDestination: ref.read(resolveDestination), - ipv6Mode: ref.read(ipv6Mode), - remoteDnsAddress: ref.read(remoteDnsAddress), - remoteDnsDomainStrategy: ref.read(remoteDnsDomainStrategy), - directDnsAddress: ref.read(directDnsAddress), - directDnsDomainStrategy: ref.read(directDnsDomainStrategy), - mixedPort: ref.read(mixedPort), - localDnsPort: ref.read(localDnsPort), - tunImplementation: ref.read(tunImplementation), - mtu: ref.read(mtu), - strictRoute: ref.read(strictRoute), - connectionTestUrl: ref.read(connectionTestUrl), - urlTestInterval: ref.read(urlTestInterval), - enableClashApi: ref.read(enableClashApi), - clashApiPort: ref.read(clashApiPort), - enableTun: mode == ServiceMode.tun, - enableTunService: mode == ServiceMode.tunService, - setSystemProxy: mode == ServiceMode.systemProxy, - bypassLan: ref.read(bypassLan), - allowConnectionFromLan: ref.read(allowConnectionFromLan), - enableFakeDns: ref.read(enableFakeDns), - enableDnsRouting: ref.read(enableDnsRouting), - independentDnsCache: ref.read(independentDnsCache), - enableTlsFragment: ref.read(enableTlsFragment), - tlsFragmentSize: ref.read(tlsFragmentSize), - tlsFragmentSleep: ref.read(tlsFragmentSleep), - enableTlsMixedSniCase: ref.read(enableTlsMixedSniCase), - enableTlsPadding: ref.read(enableTlsPadding), - tlsPaddingSize: ref.read(tlsPaddingSize), - enableMux: ref.read(enableMux), - muxPadding: ref.read(muxPadding), - muxMaxStreams: ref.read(muxMaxStreams), - muxProtocol: ref.read(muxProtocol), - warp: SingboxWarpOption( - enable: ref.read(enableWarp), - mode: ref.read(warpDetourMode), - wireguardConfig: ref.read(warpWireguardConfig), - licenseKey: ref.read(warpLicenseKey), - accountId: ref.read(warpAccountId), - accessToken: ref.read(warpAccessToken), - cleanIp: ref.read(warpCleanIp), - cleanPort: ref.read(warpPort), - noise: ref.read(warpNoise), - noiseDelay: ref.read(warpNoiseDelay), - ), - geoipPath: "", - geositePath: "", - rules: [], - ); - } } class ConfigOptionRepository with ExceptionHandler, InfraLogger { @@ -507,7 +456,7 @@ class ConfigOptionRepository with ExceptionHandler, InfraLogger { }); final SharedPreferences preferences; - final SingboxConfigOption Function() getConfigOptions; + final Future Function() getConfigOptions; final GeoAssetRepository geoAssetRepository; final GeoAssetPathResolver geoAssetPathResolver; @@ -515,57 +464,7 @@ class ConfigOptionRepository with ExceptionHandler, InfraLogger { getFullSingboxConfigOption() { return exceptionHandler( () async { - final region = - Region.values.byName(preferences.getString("region") ?? "other"); - final rules = switch (region) { - Region.ir => [ - const SingboxRule( - domains: "domain:.ir,geosite:ir", - ip: "geoip:ir", - outbound: RuleOutbound.bypass, - ), - ], - Region.cn => [ - const SingboxRule( - domains: "domain:.cn,geosite:cn", - ip: "geoip:cn", - outbound: RuleOutbound.bypass, - ), - ], - Region.ru => [ - const SingboxRule( - domains: "domain:.ru", - ip: "geoip:ru", - outbound: RuleOutbound.bypass, - ), - ], - Region.af => [ - const SingboxRule( - domains: "domain:.af,geosite:af", - ip: "geoip:af", - outbound: RuleOutbound.bypass, - ), - ], - _ => [], - }; - - final geoAssets = await geoAssetRepository - .getActivePair() - .getOrElse((l) => throw l) - .run(); - - final singboxConfigOption = getConfigOptions().copyWith( - geoipPath: geoAssetPathResolver.relativePath( - geoAssets.geoip.providerName, - geoAssets.geoip.fileName, - ), - geositePath: geoAssetPathResolver.relativePath( - geoAssets.geosite.providerName, - geoAssets.geosite.fileName, - ), - rules: rules, - ); - return right(singboxConfigOption); + return right(await getConfigOptions()); }, ConfigOptionUnexpectedFailure.new, ); diff --git a/lib/singbox/model/singbox_config_enum.dart b/lib/singbox/model/singbox_config_enum.dart index 82990e8a..17b38917 100644 --- a/lib/singbox/model/singbox_config_enum.dart +++ b/lib/singbox/model/singbox_config_enum.dart @@ -101,12 +101,17 @@ enum MuxProtocol { yamux; } +@JsonEnum(valueField: 'key') enum WarpDetourMode { - outbound, - inbound; + proxyOverWarp("proxy_over_warp"), + warpOverProxy("warp_over_proxy"); + + const WarpDetourMode(this.key); + + final String key; String present(TranslationsEn t) => switch (this) { - outbound => t.settings.config.warpDetourModes.outbound, - inbound => t.settings.config.warpDetourModes.inbound, + proxyOverWarp => t.settings.config.warpDetourModes.proxyOverWarp, + warpOverProxy => t.settings.config.warpDetourModes.warpOverProxy, }; } diff --git a/lib/singbox/model/singbox_config_option.dart b/lib/singbox/model/singbox_config_option.dart index 10397446..7ecb6eae 100644 --- a/lib/singbox/model/singbox_config_option.dart +++ b/lib/singbox/model/singbox_config_option.dart @@ -41,19 +41,11 @@ class SingboxConfigOption with _$SingboxConfigOption { required bool enableFakeDns, required bool enableDnsRouting, required bool independentDnsCache, - required bool enableTlsFragment, - @OptionalRangeJsonConverter() required OptionalRange tlsFragmentSize, - @OptionalRangeJsonConverter() required OptionalRange tlsFragmentSleep, - required bool enableTlsMixedSniCase, - required bool enableTlsPadding, - @OptionalRangeJsonConverter() required OptionalRange tlsPaddingSize, - required bool enableMux, - required bool muxPadding, - required int muxMaxStreams, - required MuxProtocol muxProtocol, required String geoipPath, required String geositePath, required List rules, + required SingboxMuxOption mux, + required SingboxTlsTricks tlsTricks, required SingboxWarpOption warp, }) = _SingboxConfigOption; @@ -68,7 +60,7 @@ class SingboxConfigOption with _$SingboxConfigOption { @freezed class SingboxWarpOption with _$SingboxWarpOption { - @JsonSerializable(fieldRename: FieldRename.kebab, createFieldMap: true) + @JsonSerializable(fieldRename: FieldRename.kebab) const factory SingboxWarpOption({ required bool enable, required WarpDetourMode mode, @@ -85,3 +77,33 @@ class SingboxWarpOption with _$SingboxWarpOption { factory SingboxWarpOption.fromJson(Map json) => _$SingboxWarpOptionFromJson(json); } + +@freezed +class SingboxMuxOption with _$SingboxMuxOption { + @JsonSerializable(fieldRename: FieldRename.kebab) + const factory SingboxMuxOption({ + required bool enable, + required bool padding, + required int maxStreams, + required MuxProtocol protocol, + }) = _SingboxMuxOption; + + factory SingboxMuxOption.fromJson(Map json) => + _$SingboxMuxOptionFromJson(json); +} + +@freezed +class SingboxTlsTricks with _$SingboxTlsTricks { + @JsonSerializable(fieldRename: FieldRename.kebab) + const factory SingboxTlsTricks({ + required bool enableFragment, + @OptionalRangeJsonConverter() required OptionalRange fragmentSize, + @OptionalRangeJsonConverter() required OptionalRange fragmentSleep, + required bool mixedSniCase, + required bool enablePadding, + @OptionalRangeJsonConverter() required OptionalRange paddingSize, + }) = _SingboxTlsTricks; + + factory SingboxTlsTricks.fromJson(Map json) => + _$SingboxTlsTricksFromJson(json); +} diff --git a/libcore b/libcore index f9e6f022..43fd1ceb 160000 --- a/libcore +++ b/libcore @@ -1 +1 @@ -Subproject commit f9e6f022c89604c2dac87d0ddd0831f8337fcdc3 +Subproject commit 43fd1ceb1707854f736315f5e11c99d2d0dca7e5