diff --git a/lib/network/components/js/xhr.dart b/lib/network/components/js/xhr.dart index 652d99a..992547b 100644 --- a/lib/network/components/js/xhr.dart +++ b/lib/network/components/js/xhr.dart @@ -1,10 +1,15 @@ import 'dart:async'; import 'dart:convert'; +import 'dart:io'; +import 'package:desktop_multi_window/desktop_multi_window.dart'; import 'package:flutter_js/javascript_runtime.dart'; import 'package:http/http.dart' as http; +import 'package:http/io_client.dart'; +import 'package:proxypin/network/bin/server.dart'; import 'package:proxypin/network/util/file_read.dart'; import 'package:proxypin/network/util/logger.dart'; +import 'package:proxypin/utils/platform.dart'; /* * Based on bits and pieces from different OSS sources @@ -232,22 +237,54 @@ extension JavascriptRuntimeXhrExtension on JavascriptRuntime { return dartContext[XHR_PENDING_CALLS_KEY]; } - bool hasPendingXhrCalls() => getPendingXhrCalls()!.length > 0; + bool hasPendingXhrCalls() => getPendingXhrCalls()!.isNotEmpty; void clearXhrPendingCalls() { dartContext[XHR_PENDING_CALLS_KEY] = []; } - Future enableFetch2() async { - enableXhr2(); + Future enableFetch2({bool enabledProxy = false}) async { + enableXhr2(enabledProxy: enabledProxy); final fetchPolyfill = await FileRead.readAsString('assets/js/fetch.js'); final evalFetchResult = evaluate(fetchPolyfill); - logger.d('Eval Fetch Result: $evalFetchResult'); + logger.d('Eval Fetch Result: $evalFetchResult'); } + Future createClient(enabledProxy) async { + if (!enabledProxy) { + return http.Client(); + } - JavascriptRuntime enableXhr2() { - httpClient = httpClient ?? http.Client(); + // ProxyServer.current.isRunning + var httpClient = HttpClient(); + print(ProxyServer.current?.isRunning); + String proxy; + if (Platforms.isDesktop()) { + Map? proxyResult = await DesktopMultiWindow.invokeMethod(0, 'getProxyInfo'); + if (proxyResult == null) { + return http.Client(); + } + proxy = "${proxyResult['host']}:${proxyResult['port']}"; + } else { + if (ProxyServer.current?.isRunning == true) { + proxy = "127.0.0.1:${ProxyServer.current!.port}"; + } else { + return http.Client(); + } + } + + httpClient.findProxy = (uri) { + return "PROXY $proxy"; + }; + + httpClient.badCertificateCallback = (X509Certificate cert, String host, int port) => true; + + // 创建一个 IOClient 实例,将 HttpClient 传入 + return IOClient(httpClient); + } + + void enableXhr2({bool enabledProxy = false}) async { + httpClient = httpClient ?? await createClient(enabledProxy); dartContext[XHR_PENDING_CALLS_KEY] = []; Timer.periodic(Duration(milliseconds: 40), (timer) { @@ -394,7 +431,6 @@ extension JavascriptRuntimeXhrExtension on JavascriptRuntime { if (_XHR_DEBUG) print('Exception calling sendNative on Dart: >>>> $e'); } }); - return this; } } diff --git a/lib/ui/component/toolbox/js_run.dart b/lib/ui/component/toolbox/js_run.dart index 8593674..1a280f2 100644 --- a/lib/ui/component/toolbox/js_run.dart +++ b/lib/ui/component/toolbox/js_run.dart @@ -48,7 +48,7 @@ class _JavaScriptState extends State { channelCallbacks!["ConsoleLog"] = consoleLog; Md5Bridge.registerMd5(flutterJs!); FileBridge.registerFile(flutterJs!); - flutterJs?.enableFetch2(); + flutterJs?.enableFetch2(enabledProxy: true); code = CodeController(language: javascript, text: 'console.log("Hello, World!")'); } diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index 47cc4cd..bde187d 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,8 @@ void RegisterPlugins(flutter::PluginRegistry* registry) { registry->GetRegistrarForPlugin("SharePlusWindowsPluginCApi")); UrlLauncherWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("UrlLauncherWindows")); + VclibsPluginCApiRegisterWithRegistrar( + registry->GetRegistrarForPlugin("VclibsPluginCApi")); Win32audioPluginCApiRegisterWithRegistrar( registry->GetRegistrarForPlugin("Win32audioPluginCApi")); WindowManagerPluginRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index b982dcd..370786e 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -11,6 +11,7 @@ list(APPEND FLUTTER_PLUGIN_LIST screen_retriever_windows share_plus url_launcher_windows + vclibs win32audio window_manager windows_single_instance