Skip to content

基于自定义规则的番剧采集APP,支持流媒体在线观看,支持弹幕,支持实时超分辨率。

feat(proxy): follow Windows system proxy automatically

Prediditf13cd4d6c477
diff --git a/lib/main.dart b/lib/main.dart
index 011897f1..668f2cdd 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -8,6 +8,7 @@ import 'package:path_provider/path_provider.dart';
import 'package:kazumi/services/storage/storage.dart';
import 'package:hive_ce_flutter/hive_flutter.dart';
import 'package:kazumi/services/network/proxy_manager.dart';
+import 'package:kazumi/services/network/system_proxy_service.dart';
import 'package:flutter/services.dart';
import 'package:media_kit/media_kit.dart';
import 'package:window_manager/window_manager.dart';
@@ -86,6 +87,9 @@ void main() async {
await windowManager.focus();
});
}
+ if (Platform.isWindows) {
+ SystemProxyService.init();
+ }
ProxyManager.applyProxy();
runApp(
ModularApp(
diff --git a/lib/pages/player/controller/player_playback_controller.dart b/lib/pages/player/controller/player_playback_controller.dart
index 80e3a1e9..0db7c184 100644
--- a/lib/pages/player/controller/player_playback_controller.dart
+++ b/lib/pages/player/controller/player_playback_controller.dart
@@ -11,6 +11,7 @@ import 'package:kazumi/services/shaders/shader_asset_service.dart';
import 'package:kazumi/utils/constants.dart';
import 'package:kazumi/services/logging/logger.dart';
import 'package:kazumi/services/network/proxy_utils.dart';
+import 'package:kazumi/services/network/system_proxy_service.dart';
import 'package:kazumi/services/player/player_screenshot_service.dart';
import 'package:kazumi/services/storage/storage.dart';
import 'package:media_kit/media_kit.dart';
@@ -275,6 +276,15 @@ abstract class _PlayerPlaybackController with Store {
}
KazumiLogger().i('Player: HTTP 代理设置成功 $formattedProxy');
}
+ } else if (SystemProxyService.isActive) {
+ final proxy = SystemProxyService.proxyFor('https');
+ if (proxy != null) {
+ await pp.setProperty("http-proxy", 'http://${proxy.$1}:${proxy.$2}');
+ if (!isCurrentPlayer(player)) {
+ return await _discardIfNotCurrent(candidate);
+ }
+ KazumiLogger().i('Player: 跟随系统代理 http://${proxy.$1}:${proxy.$2}');
+ }
}
await player.setAudioTrack(
diff --git a/lib/request/core/network_config.dart b/lib/request/core/network_config.dart
index 6339577e..3e420067 100644
--- a/lib/request/core/network_config.dart
+++ b/lib/request/core/network_config.dart
@@ -3,6 +3,7 @@ import 'dart:io';
import 'package:dio/io.dart';
import 'package:kazumi/services/logging/logger.dart';
import 'package:kazumi/services/network/proxy_utils.dart';
+import 'package:kazumi/services/network/system_proxy_service.dart';
import 'package:kazumi/services/storage/storage.dart';
class NetworkConfig {
@@ -32,6 +33,8 @@ class NetworkConfig {
final client = HttpClient();
if (hasProxy) {
client.findProxy = (_) => 'PROXY $proxyHost:$proxyPort';
+ } else if (Platform.isWindows) {
+ client.findProxy = SystemProxyService.findProxy;
}
if (allowBadCertificates) {
client.badCertificateCallback = (cert, host, port) => true;
diff --git a/lib/services/network/proxy_aware_image_cache_manager.dart b/lib/services/network/proxy_aware_image_cache_manager.dart
index 73e0bfc3..65f86b46 100644
--- a/lib/services/network/proxy_aware_image_cache_manager.dart
+++ b/lib/services/network/proxy_aware_image_cache_manager.dart
@@ -4,6 +4,7 @@ import 'dart:io';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:kazumi/services/logging/logger.dart';
import 'package:kazumi/services/network/proxy_utils.dart';
+import 'package:kazumi/services/network/system_proxy_service.dart';
import 'package:kazumi/services/storage/storage.dart';
class ProxyAwareImageCacheManager extends CacheManager with ImageCacheManager {
@@ -64,7 +65,13 @@ class ProxyAwareImageFileService extends FileService {
HttpClient _createHttpClient() {
final client = HttpClient();
final proxy = _currentProxy();
- if (proxy == null) return client;
+ if (proxy == null) {
+ if (Platform.isWindows) {
+ // Unlike the manual proxy path, certificate checks stay strict here.
+ client.findProxy = SystemProxyService.findProxy;
+ }
+ return client;
+ }
client.findProxy = (_) => 'PROXY ${proxy.$1}:${proxy.$2}';
client.badCertificateCallback = (cert, host, port) => true;
diff --git a/lib/services/network/system_proxy_service.dart b/lib/services/network/system_proxy_service.dart
new file mode 100644
index 00000000..2a1a339e
--- /dev/null
+++ b/lib/services/network/system_proxy_service.dart
@@ -0,0 +1,326 @@
+import 'dart:async';
+import 'dart:ffi';
+import 'dart:io';
+import 'dart:isolate';
+
+import 'package:ffi/ffi.dart';
+import 'package:kazumi/services/logging/logger.dart';
+import 'package:kazumi/services/network/proxy_manager.dart';
+import 'package:kazumi/services/storage/storage.dart';
+import 'package:win32/win32.dart';
+
+/// Snapshot of the Windows system proxy configuration.
+class SystemProxyState {
+ const SystemProxyState({
+ this.httpProxy,
+ this.httpsProxy,
+ this.bypassPatterns = const [],
+ this.bypassLocal = false,
+ });
+
+ final (String, int)? httpProxy;
+ final (String, int)? httpsProxy;
+ final List<String> bypassPatterns;
+ final bool bypassLocal;
+
+ bool get hasProxy => httpProxy != null || httpsProxy != null;
+
+ bool sameAs(SystemProxyState other) {
+ if (httpProxy != other.httpProxy || httpsProxy != other.httpsProxy) {
+ return false;
+ }
+ if (bypassLocal != other.bypassLocal) return false;
+ if (bypassPatterns.length != other.bypassPatterns.length) return false;
+ for (var i = 0; i < bypassPatterns.length; i++) {
+ if (bypassPatterns[i] != other.bypassPatterns[i]) return false;
+ }
+ return true;
+ }
+}
+
+/// Follows the WinINET system proxy on Windows, Chrome-style.
+///
+/// The registry snapshot lives in memory only and is never persisted, so the
+/// app always starts from the actual system state. A background isolate
+/// watches the registry key and refreshes the snapshot on change. Like
+/// Chrome, an unreachable proxy endpoint surfaces as a network error rather
+/// than silently falling back to a direct connection.
+///
+/// The in-app manual proxy takes precedence structurally: call sites only
+/// install [findProxy] when no manual proxy is configured.
+class SystemProxyService {
+ SystemProxyService._();
+
+ static const _regPath =
+ r'Software\Microsoft\Windows\CurrentVersion\Internet Settings';
+
+ static SystemProxyState _state = const SystemProxyState();
+ static bool _watcherStarted = false;
+ static Timer? _debounce;
+
+ /// Reads the registry synchronously and starts the change watcher.
+ /// Windows only; call before `runApp` so the first requests see the
+ /// correct state.
+ static void init() {
+ if (!Platform.isWindows) return;
+ refresh();
+ _startWatcher();
+ }
+
+ /// Re-reads the registry. Returns true when the effective proxy changed.
+ static bool refresh() {
+ if (!Platform.isWindows) return false;
+ SystemProxyState newState;
+ try {
+ newState = _readFromRegistry();
+ } catch (e) {
+ KazumiLogger().w('Proxy: 读取系统代理配置失败 $e');
+ newState = const SystemProxyState();
+ }
+ if (newState.sameAs(_state)) return false;
+ _state = newState;
+ final proxy = newState.httpProxy ?? newState.httpsProxy;
+ KazumiLogger().i(proxy == null
+ ? 'Proxy: 系统代理已移除,回到直连'
+ : 'Proxy: 检测到系统代理 ${proxy.$1}:${proxy.$2}');
+ return true;
+ }
+
+ static bool get isActive => Platform.isWindows && _state.hasProxy;
+
+ /// Proxy for [scheme] ('http' or 'https'), for consumers that cannot use
+ /// [findProxy] (e.g. mpv).
+ static (String, int)? proxyFor(String scheme) {
+ if (!isActive) return null;
+ return scheme == 'https' ? _state.httpsProxy : _state.httpProxy;
+ }
+
+ /// PAC-style callback for `HttpClient.findProxy`. Evaluates the current
+ /// snapshot per request; never throws.
+ static String findProxy(Uri url) {
+ try {
+ if (!isActive) return 'DIRECT';
+ final state = _state;
+ if (shouldBypass(url, state)) return 'DIRECT';
+ final proxy =
+ url.scheme == 'https' ? state.httpsProxy : state.httpProxy;
+ if (proxy == null) return 'DIRECT';
+ return 'PROXY ${proxy.$1}:${proxy.$2}';
+ } catch (_) {
+ return 'DIRECT';
+ }
+ }
+
+ // Pure parsing helpers, public for unit tests.
+
+ /// Parses the registry `ProxyServer` value into (http, https) proxies.
+ ///
+ /// Accepts a single `host:port` (applies to both schemes) or a
+ /// per-protocol list like `http=h:p;https=h:p;socks=h:p`. `http` and
+ /// `https` fall back to each other; `ftp`/`socks` entries are ignored
+ /// because `HttpClient` cannot use them.
+ static ((String, int)?, (String, int)?) parseProxyServer(String raw) {
+ raw = raw.trim();
+ if (raw.isEmpty) return (null, null);
+
+ if (!raw.contains('=')) {
+ final parsed = parseHostPort(raw);
+ return (parsed, parsed);
+ }
+
+ (String, int)? http;
+ (String, int)? https;
+ for (final entry in raw.split(';')) {
+ final idx = entry.indexOf('=');
+ if (idx <= 0) continue;
+ final scheme = entry.substring(0, idx).trim().toLowerCase();
+ final parsed = parseHostPort(entry.substring(idx + 1));
+ if (parsed == null) continue;
+ if (scheme == 'http') http = parsed;
+ if (scheme == 'https') https = parsed;
+ }
+ return (http ?? https, https ?? http);
+ }
+
+ /// Parses `host:port`, tolerating a `scheme://` prefix and `[IPv6]:port`.
+ static (String, int)? parseHostPort(String value) {
+ value = value.trim();
+ if (value.isEmpty) return null;
+
+ final schemeIdx = value.indexOf('://');
+ if (schemeIdx > 0) {
+ value = value.substring(schemeIdx + 3);
+ }
+
+ // IPv6 literals contain colons; the port follows the last one.
+ final colon = value.lastIndexOf(':');
+ if (colon <= 0 || colon == value.length - 1) return null;
+
+ var host = value.substring(0, colon);
+ final port = int.tryParse(value.substring(colon + 1));
+ if (port == null || port < 1 || port > 65535) return null;
+
+ if (host.startsWith('[') && host.endsWith(']')) {
+ host = host.substring(1, host.length - 1);
+ }
+ if (host.isEmpty) return null;
+ return (host, port);
+ }
+
+ /// Parses the registry `ProxyOverride` (bypass) list into wildcard
+ /// patterns and whether `<local>` is present.
+ static (List<String>, bool) parseBypassList(String raw) {
+ final patterns = <String>[];
+ var bypassLocal = false;
+ for (final part in raw.split(RegExp(r'[;,]'))) {
+ var entry = part.trim().toLowerCase();
+ if (entry.isEmpty) continue;
+ if (entry == '<local>') {
+ bypassLocal = true;
+ continue;
+ }
+ final schemeIdx = entry.indexOf('://');
+ if (schemeIdx > 0) {
+ entry = entry.substring(schemeIdx + 3);
+ }
+ if (entry.isNotEmpty) patterns.add(entry);
+ }
+ return (patterns, bypassLocal);
+ }
+
+ /// Loopback hosts always bypass (as in Chrome); `<local>` matches dotless
+ /// hostnames; patterns containing a `:port` match against `host:port`.
+ static bool shouldBypass(Uri url, SystemProxyState state) {
+ final host = url.host.toLowerCase();
+ if (host.isEmpty) return true;
+ if (_isLoopback(host)) return true;
+ if (state.bypassLocal && !host.contains('.') && !host.contains(':')) {
+ return true;
+ }
+ for (final pattern in state.bypassPatterns) {
+ if (_matchesPattern(pattern, host, url.port)) return true;
+ }
+ return false;
+ }
+
+ static bool _isLoopback(String host) {
+ if (host == 'localhost' || host == '::1') return true;
+ return host.startsWith('127.');
+ }
+
+ static bool _matchesPattern(String pattern, String host, int port) {
+ var target = host;
+ final colon = pattern.lastIndexOf(':');
+ if (colon > 0 && int.tryParse(pattern.substring(colon + 1)) != null) {
+ target = '$host:$port';
+ }
+ final regex =
+ RegExp('^${RegExp.escape(pattern).replaceAll(r'\*', '.*')}\$');
+ return regex.hasMatch(target);
+ }
+
+ static SystemProxyState buildState(String proxyServer, String proxyOverride) {
+ final (http, https) = parseProxyServer(proxyServer);
+ final (patterns, bypassLocal) = parseBypassList(proxyOverride);
+ return SystemProxyState(
+ httpProxy: http,
+ httpsProxy: https,
+ bypassPatterns: patterns,
+ bypassLocal: bypassLocal,
+ );
+ }
+
+ static SystemProxyState _readFromRegistry() {
+ return using((arena) {
+ final subKey = _regPath.toNativeUtf16(allocator: arena);
+
+ int? readDword(String name) {
+ final valueName = name.toNativeUtf16(allocator: arena);
+ final data = arena<Uint32>();
+ final cbData = arena<Uint32>()..value = sizeOf<Uint32>();
+ final status = RegGetValue(HKEY_CURRENT_USER, subKey, valueName,
+ RRF_RT_REG_DWORD, nullptr, data, cbData);
+ return status == ERROR_SUCCESS ? data.value : null;
+ }
+
+ String? readString(String name) {
+ final valueName = name.toNativeUtf16(allocator: arena);
+ final cbData = arena<Uint32>();
+ var status = RegGetValue(HKEY_CURRENT_USER, subKey, valueName,
+ RRF_RT_REG_SZ, nullptr, nullptr, cbData);
+ if (status != ERROR_SUCCESS || cbData.value == 0) return null;
+ final buffer = arena.allocate<Utf16>(cbData.value);
+ status = RegGetValue(HKEY_CURRENT_USER, subKey, valueName,
+ RRF_RT_REG_SZ, nullptr, buffer, cbData);
+ return status == ERROR_SUCCESS ? buffer.toDartString() : null;
+ }
+
+ // WinINET semantics: ProxyEnable=0 means no proxy regardless of a
+ // leftover ProxyServer value. PAC (AutoConfigURL) and WPAD are not
+ // supported and are treated as no proxy.
+ if ((readDword('ProxyEnable') ?? 0) == 0) {
+ return const SystemProxyState();
+ }
+ return buildState(
+ readString('ProxyServer') ?? '',
+ readString('ProxyOverride') ?? '',
+ );
+ });
+ }
+
+ static void _startWatcher() {
+ if (_watcherStarted) return;
+ _watcherStarted = true;
+ final port = ReceivePort();
+ port.listen((_) => _onRegistryChanged());
+ Isolate.spawn(
+ _watchRegistry,
+ port.sendPort,
+ debugName: 'SystemProxyWatcher',
+ ).then((_) {}, onError: (Object e) {
+ KazumiLogger().w('Proxy: 系统代理监视启动失败 $e');
+ port.close();
+ });
+ }
+
+ static void _onRegistryChanged() {
+ // Proxy tools write several registry values in a burst; debounce them
+ // into a single refresh.
+ _debounce?.cancel();
+ _debounce = Timer(const Duration(milliseconds: 300), () {
+ if (!refresh()) return;
+ // A manual proxy overrides the system proxy, so clients need no rebuild.
+ if (GStorage.getSetting(SettingsKeys.proxyEnable)) return;
+ ProxyManager.applyProxy();
+ });
+ }
+
+ /// Isolate entry: blocks on registry change notifications for the process
+ /// lifetime and pings the main isolate on each change.
+ static void _watchRegistry(SendPort sendPort) {
+ using((arena) {
+ final subKey = _regPath.toNativeUtf16(allocator: arena);
+ final phKey = arena<IntPtr>();
+ if (RegOpenKeyEx(HKEY_CURRENT_USER, subKey, 0, KEY_NOTIFY, phKey) !=
+ ERROR_SUCCESS) {
+ return;
+ }
+ final hKey = phKey.value;
+ try {
+ while (true) {
+ final status = RegNotifyChangeKeyValue(
+ hKey,
+ TRUE,
+ REG_NOTIFY_CHANGE_NAME | REG_NOTIFY_CHANGE_LAST_SET,
+ NULL,
+ FALSE,
+ );
+ if (status != ERROR_SUCCESS) break;
+ sendPort.send(true);
+ }
+ } finally {
+ RegCloseKey(hKey);
+ }
+ });
+ }
+}
diff --git a/pubspec.lock b/pubspec.lock
index 2d465055..982c14dd 100644
--- a/pubspec.lock
+++ b/pubspec.lock
@@ -421,7 +421,7 @@ packages:
source: hosted
version: "1.3.3"
ffi:
- dependency: transitive
+ dependency: "direct main"
description:
name: ffi
sha256: "6d7fd89431262d8f3125e81b50d3847a091d846eafcd4fdb88dd06f36d705a45"
@@ -1872,7 +1872,7 @@ packages:
source: git
version: "0.4.0"
win32:
- dependency: transitive
+ dependency: "direct main"
description:
name: win32
sha256: d7cb55e04cd34096cd3a79b3330245f54cb96a370a1c27adb3c84b917de8b08e
diff --git a/pubspec.yaml b/pubspec.yaml
index 8797c812..f26d7b5a 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -106,6 +106,8 @@ dependencies:
material_color_utilities: any
path: any
photo_view: ^0.15.0
+ win32: ^5.15.0
+ ffi: ^2.1.0
image_picker: ^1.2.1
webview_windows:
git: