Toolbox JS fetch request supports proxy (#300)

This commit is contained in:
wanghongenpin
2025-04-09 03:53:03 +08:00
parent 6a97bd5b02
commit f03330f656
4 changed files with 48 additions and 8 deletions

View File

@@ -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<void> enableFetch2() async {
enableXhr2();
Future<void> 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<http.Client> 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;
}
}

View File

@@ -48,7 +48,7 @@ class _JavaScriptState extends State<JavaScript> {
channelCallbacks!["ConsoleLog"] = consoleLog;
Md5Bridge.registerMd5(flutterJs!);
FileBridge.registerFile(flutterJs!);
flutterJs?.enableFetch2();
flutterJs?.enableFetch2(enabledProxy: true);
code = CodeController(language: javascript, text: 'console.log("Hello, World!")');
}

View File

@@ -14,6 +14,7 @@
#include <screen_retriever_windows/screen_retriever_windows_plugin_c_api.h>
#include <share_plus/share_plus_windows_plugin_c_api.h>
#include <url_launcher_windows/url_launcher_windows.h>
#include <vclibs/vclibs_plugin_c_api.h>
#include <win32audio/win32audio_plugin_c_api.h>
#include <window_manager/window_manager_plugin.h>
#include <windows_single_instance/windows_single_instance_plugin.h>
@@ -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(

View File

@@ -11,6 +11,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
screen_retriever_windows
share_plus
url_launcher_windows
vclibs
win32audio
window_manager
windows_single_instance