Refactor config options

This commit is contained in:
problematicconsumer
2024-03-07 20:21:25 +03:30
parent 55afbd55f6
commit fabd559b19
6 changed files with 77 additions and 151 deletions

View File

@@ -302,8 +302,8 @@
"enableWarp": "Enable WARP", "enableWarp": "Enable WARP",
"warpDetourMode": "Detour Mode", "warpDetourMode": "Detour Mode",
"warpDetourModes": { "warpDetourModes": {
"inbound": "Detour WARP through proxies", "proxyOverWarp": "Detour proxies through WARP",
"outbound": "Detour proxies through WARP" "warpOverProxy": "Detour WARP through proxies"
}, },
"warpLicenseKey": "License Key", "warpLicenseKey": "License Key",
"warpCleanIp": "Clean IP", "warpCleanIp": "Clean IP",

View File

@@ -11,7 +11,7 @@ ConfigOptionRepository configOptionRepository(
) { ) {
return ConfigOptionRepository( return ConfigOptionRepository(
preferences: ref.watch(sharedPreferencesProvider).requireValue, preferences: ref.watch(sharedPreferencesProvider).requireValue,
getConfigOptions: () => ConfigOptions.singboxOptions(ref), getConfigOptions: () => ref.read(ConfigOptions.singboxConfigOptions.future),
geoAssetRepository: ref.watch(geoAssetRepositoryProvider).requireValue, geoAssetRepository: ref.watch(geoAssetRepositoryProvider).requireValue,
geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider), geoAssetPathResolver: ref.watch(geoAssetPathResolverProvider),
); );

View File

@@ -215,7 +215,7 @@ abstract class ConfigOptions {
static final warpDetourMode = static final warpDetourMode =
PreferencesNotifier.create<WarpDetourMode, String>( PreferencesNotifier.create<WarpDetourMode, String>(
"warp-detour-mode", "warp-detour-mode",
WarpDetourMode.outbound, WarpDetourMode.proxyOverWarp,
mapFrom: WarpDetourMode.values.byName, mapFrom: WarpDetourMode.values.byName,
mapTo: (value) => value.name, mapTo: (value) => value.name,
); );
@@ -313,16 +313,20 @@ abstract class ConfigOptions {
"bypass-lan": bypassLan, "bypass-lan": bypassLan,
"allow-connection-from-lan": allowConnectionFromLan, "allow-connection-from-lan": allowConnectionFromLan,
"enable-dns-routing": enableDnsRouting, "enable-dns-routing": enableDnsRouting,
"enable-tls-fragment": enableTlsFragment,
"tls-fragment-size": tlsFragmentSize, // mux
"tls-fragment-sleep": tlsFragmentSleep, "mux.enable": enableMux,
"enable-tls-mixed-sni-case": enableTlsMixedSniCase, "mux.padding": muxPadding,
"enable-tls-padding": enableTlsPadding, "mux.max-streams": muxMaxStreams,
"tls-padding-size": tlsPaddingSize, "mux.protocol": muxProtocol,
"enable-mux": enableMux,
"mux-padding": muxPadding, // tls-tricks
"mux-max-streams": muxMaxStreams, "tls-tricks.enable-fragment": enableTlsFragment,
"mux-protocol": muxProtocol, "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
"warp.enable": enableWarp, "warp.enable": enableWarp,
@@ -403,16 +407,20 @@ abstract class ConfigOptions {
enableFakeDns: ref.watch(enableFakeDns), enableFakeDns: ref.watch(enableFakeDns),
enableDnsRouting: ref.watch(enableDnsRouting), enableDnsRouting: ref.watch(enableDnsRouting),
independentDnsCache: ref.watch(independentDnsCache), independentDnsCache: ref.watch(independentDnsCache),
enableTlsFragment: ref.watch(enableTlsFragment), mux: SingboxMuxOption(
tlsFragmentSize: ref.watch(tlsFragmentSize), enable: ref.watch(enableMux),
tlsFragmentSleep: ref.watch(tlsFragmentSleep), padding: ref.watch(muxPadding),
enableTlsMixedSniCase: ref.watch(enableTlsMixedSniCase), maxStreams: ref.watch(muxMaxStreams),
enableTlsPadding: ref.watch(enableTlsPadding), protocol: ref.watch(muxProtocol),
tlsPaddingSize: ref.watch(tlsPaddingSize), ),
enableMux: ref.watch(enableMux), tlsTricks: SingboxTlsTricks(
muxPadding: ref.watch(muxPadding), enableFragment: ref.watch(enableTlsFragment),
muxMaxStreams: ref.watch(muxMaxStreams), fragmentSize: ref.watch(tlsFragmentSize),
muxProtocol: ref.watch(muxProtocol), fragmentSleep: ref.watch(tlsFragmentSleep),
mixedSniCase: ref.watch(enableTlsMixedSniCase),
enablePadding: ref.watch(enableTlsPadding),
paddingSize: ref.watch(tlsPaddingSize),
),
warp: SingboxWarpOption( warp: SingboxWarpOption(
enable: ref.watch(enableWarp), enable: ref.watch(enableWarp),
mode: ref.watch(warpDetourMode), 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 { class ConfigOptionRepository with ExceptionHandler, InfraLogger {
@@ -507,7 +456,7 @@ class ConfigOptionRepository with ExceptionHandler, InfraLogger {
}); });
final SharedPreferences preferences; final SharedPreferences preferences;
final SingboxConfigOption Function() getConfigOptions; final Future<SingboxConfigOption> Function() getConfigOptions;
final GeoAssetRepository geoAssetRepository; final GeoAssetRepository geoAssetRepository;
final GeoAssetPathResolver geoAssetPathResolver; final GeoAssetPathResolver geoAssetPathResolver;
@@ -515,57 +464,7 @@ class ConfigOptionRepository with ExceptionHandler, InfraLogger {
getFullSingboxConfigOption() { getFullSingboxConfigOption() {
return exceptionHandler( return exceptionHandler(
() async { () async {
final region = return right(await getConfigOptions());
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,
),
],
_ => <SingboxRule>[],
};
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);
}, },
ConfigOptionUnexpectedFailure.new, ConfigOptionUnexpectedFailure.new,
); );

View File

@@ -101,12 +101,17 @@ enum MuxProtocol {
yamux; yamux;
} }
@JsonEnum(valueField: 'key')
enum WarpDetourMode { enum WarpDetourMode {
outbound, proxyOverWarp("proxy_over_warp"),
inbound; warpOverProxy("warp_over_proxy");
const WarpDetourMode(this.key);
final String key;
String present(TranslationsEn t) => switch (this) { String present(TranslationsEn t) => switch (this) {
outbound => t.settings.config.warpDetourModes.outbound, proxyOverWarp => t.settings.config.warpDetourModes.proxyOverWarp,
inbound => t.settings.config.warpDetourModes.inbound, warpOverProxy => t.settings.config.warpDetourModes.warpOverProxy,
}; };
} }

View File

@@ -41,19 +41,11 @@ class SingboxConfigOption with _$SingboxConfigOption {
required bool enableFakeDns, required bool enableFakeDns,
required bool enableDnsRouting, required bool enableDnsRouting,
required bool independentDnsCache, 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 geoipPath,
required String geositePath, required String geositePath,
required List<SingboxRule> rules, required List<SingboxRule> rules,
required SingboxMuxOption mux,
required SingboxTlsTricks tlsTricks,
required SingboxWarpOption warp, required SingboxWarpOption warp,
}) = _SingboxConfigOption; }) = _SingboxConfigOption;
@@ -68,7 +60,7 @@ class SingboxConfigOption with _$SingboxConfigOption {
@freezed @freezed
class SingboxWarpOption with _$SingboxWarpOption { class SingboxWarpOption with _$SingboxWarpOption {
@JsonSerializable(fieldRename: FieldRename.kebab, createFieldMap: true) @JsonSerializable(fieldRename: FieldRename.kebab)
const factory SingboxWarpOption({ const factory SingboxWarpOption({
required bool enable, required bool enable,
required WarpDetourMode mode, required WarpDetourMode mode,
@@ -85,3 +77,33 @@ class SingboxWarpOption with _$SingboxWarpOption {
factory SingboxWarpOption.fromJson(Map<String, dynamic> json) => factory SingboxWarpOption.fromJson(Map<String, dynamic> json) =>
_$SingboxWarpOptionFromJson(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<String, dynamic> 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<String, dynamic> json) =>
_$SingboxTlsTricksFromJson(json);
}

Submodule libcore updated: f9e6f022c8...43fd1ceb17