From 3faf91062bedee4eab5e43dfef4aa315a9ef8a37 Mon Sep 17 00:00:00 2001 From: wanghongenpin Date: Wed, 21 Aug 2024 01:32:10 +0800 Subject: [PATCH] Request edit records http request (#253) --- lib/network/bin/server.dart | 6 +++++- lib/ui/component/multi_window.dart | 5 +++++ lib/ui/configuration.dart | 10 +++------- lib/ui/content/body.dart | 6 +++++- lib/ui/desktop/desktop.dart | 4 ++-- lib/ui/desktop/request/request_editor.dart | 20 +++++++++++++------- lib/ui/mobile/mobile.dart | 4 ++-- lib/ui/mobile/request/request_editor.dart | 18 ++++++++++++------ pubspec.yaml | 10 +++++----- 9 files changed, 52 insertions(+), 31 deletions(-) diff --git a/lib/network/bin/server.dart b/lib/network/bin/server.dart index 51f5b02..74b181d 100644 --- a/lib/network/bin/server.dart +++ b/lib/network/bin/server.dart @@ -37,6 +37,8 @@ Future main() async { /// 代理服务器 class ProxyServer { + static ProxyServer? current; + //socket服务 Server? server; @@ -46,7 +48,9 @@ class ProxyServer { //配置 final Configuration configuration; - ProxyServer(this.configuration); + ProxyServer(this.configuration) { + current = this; + } //是否启动 bool get isRunning => server?.isRunning ?? false; diff --git a/lib/ui/component/multi_window.dart b/lib/ui/component/multi_window.dart index 4ce9af8..463a212 100644 --- a/lib/ui/component/multi_window.dart +++ b/lib/ui/component/multi_window.dart @@ -5,6 +5,7 @@ import 'package:desktop_multi_window/desktop_multi_window.dart'; import 'package:file_selector/file_selector.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; +import 'package:network_proxy/network/bin/server.dart'; import 'package:network_proxy/network/components/request_rewrite_manager.dart'; import 'package:network_proxy/network/components/script_manager.dart'; import 'package:network_proxy/network/http/http.dart'; @@ -144,6 +145,10 @@ void registerMethodHandler() { DesktopMultiWindow.setMethodHandler((call, fromWindowId) async { logger.d('${call.method} $fromWindowId ${call.arguments}'); + if (call.method == 'getProxyInfo') { + return ProxyServer.current?.isRunning == true ? {'host': '127.0.0.1', 'port': ProxyServer.current!.port} : null; + } + if (call.method == 'refreshScript') { await ScriptManager.instance.then((value) { return value.reloadScript(); diff --git a/lib/ui/configuration.dart b/lib/ui/configuration.dart index b927794..f3fc10f 100644 --- a/lib/ui/configuration.dart +++ b/lib/ui/configuration.dart @@ -26,7 +26,7 @@ class AppConfiguration { Locale? _language; //是否显示更新内容公告 - bool upgradeNoticeV10 = true; + bool upgradeNoticeV12 = true; /// 是否启用画中画 ValueNotifier pipEnabled = ValueNotifier(true); @@ -37,8 +37,6 @@ class AppConfiguration { /// header默认展示 bool headerExpanded = true; - bool? iosVpnBackgroundAudioEnable; - //桌面window大小 Size? windowSize; @@ -123,12 +121,11 @@ class AppConfiguration { var mode = ThemeMode.values.firstWhere((element) => element.name == config['mode'], orElse: () => ThemeMode.system); _theme = ThemeModel(mode: mode, useMaterial3: config['useMaterial3'] ?? true); - upgradeNoticeV10 = config['upgradeNoticeV10'] ?? true; + upgradeNoticeV12 = config['upgradeNoticeV12'] ?? true; _language = config['language'] == null ? null : Locale.fromSubtags(languageCode: config['language']); pipEnabled.value = config['pipEnabled'] ?? true; pipIcon.value = config['pipIcon'] ?? false; headerExpanded = config['headerExpanded'] ?? true; - iosVpnBackgroundAudioEnable = config['iosVpnBackgroundAudioEnable']; windowSize = config['windowSize'] == null ? null : Size(config['windowSize']['width'], config['windowSize']['height']); @@ -156,14 +153,13 @@ class AppConfiguration { return { 'mode': _theme.mode.name, 'useMaterial3': _theme.useMaterial3, - 'upgradeNoticeV10': upgradeNoticeV10, + 'upgradeNoticeV12': upgradeNoticeV12, "language": _language?.languageCode, 'pipEnabled': pipEnabled.value, 'pipIcon': pipIcon.value ? true : null, "headerExpanded": headerExpanded, "windowSize": windowSize == null ? null : {"width": windowSize?.width, "height": windowSize?.height}, "windowPosition": windowPosition == null ? null : {"dx": windowPosition?.dx, "dy": windowPosition?.dy}, - "iosVpnBackgroundAudioEnable": iosVpnBackgroundAudioEnable == false ? null : iosVpnBackgroundAudioEnable }; } } diff --git a/lib/ui/content/body.dart b/lib/ui/content/body.dart index d202d8d..092a5e1 100644 --- a/lib/ui/content/body.dart +++ b/lib/ui/content/body.dart @@ -78,6 +78,10 @@ class HttpBodyState extends State { @override Widget build(BuildContext context) { + if (widget.httpMessage == null) { + return const SizedBox(); + } + if ((widget.httpMessage?.body == null || widget.httpMessage?.body?.isEmpty == true) && widget.httpMessage?.messages.isNotEmpty == false) { return const SizedBox(); @@ -85,7 +89,7 @@ class HttpBodyState extends State { var tabs = Tabs.of(widget.httpMessage?.contentType, isJsonText()); - if (tabIndex >= tabs.list.length) tabIndex = tabs.list.length - 1; + if (tabIndex > 0 && tabIndex >= tabs.list.length) tabIndex = tabs.list.length - 1; bodyKey.currentState?.changeState(widget.httpMessage, tabs.list[tabIndex]); List list = [ diff --git a/lib/ui/desktop/desktop.dart b/lib/ui/desktop/desktop.dart index d2ce030..5220b99 100644 --- a/lib/ui/desktop/desktop.dart +++ b/lib/ui/desktop/desktop.dart @@ -65,7 +65,7 @@ class _DesktopHomePagePageState extends State implements EventL proxyServer.addListener(this); panel = NetworkTabController(tabStyle: const TextStyle(fontSize: 16), proxyServer: proxyServer); - if (widget.appConfiguration.upgradeNoticeV10) { + if (widget.appConfiguration.upgradeNoticeV12) { WidgetsBinding.instance.addPostFrameCallback((_) { showUpgradeNotice(); }); @@ -113,7 +113,7 @@ class _DesktopHomePagePageState extends State implements EventL actions: [ TextButton( onPressed: () { - widget.appConfiguration.upgradeNoticeV10 = false; + widget.appConfiguration.upgradeNoticeV12 = false; widget.appConfiguration.flushConfig(); Navigator.pop(context); }, diff --git a/lib/ui/desktop/request/request_editor.dart b/lib/ui/desktop/request/request_editor.dart index b465ac9..06a79d7 100644 --- a/lib/ui/desktop/request/request_editor.dart +++ b/lib/ui/desktop/request/request_editor.dart @@ -22,6 +22,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_toastr/flutter_toastr.dart'; +import 'package:network_proxy/network/host_port.dart'; import 'package:network_proxy/network/http/http.dart'; import 'package:network_proxy/network/http/http_headers.dart'; import 'package:network_proxy/network/http_client.dart'; @@ -141,16 +142,21 @@ class RequestEditorState extends State { var requestBody = requestKey.currentState?.getBody(); String url = currentState.requestUrl.text; - HttpRequest request = - HttpRequest(HttpMethod.valueOf(currentState.requestMethod), Uri.parse(url).toString()); + HttpRequest request = HttpRequest(HttpMethod.valueOf(currentState.requestMethod), Uri.parse(url).toString()); request.headers.addAll(headers); request.body = requestBody == null ? null : utf8.encode(requestBody); - HttpClients.proxyRequest(request).then((response) { + responseKey.currentState?.change(null); + responseChange.value = !responseChange.value; + + Map? proxyResult = await DesktopMultiWindow.invokeMethod(0, 'getProxyInfo'); + ProxyInfo? proxyInfo = proxyResult == null ? null : ProxyInfo.of(proxyResult['host'], proxyResult['port']); + + HttpClients.proxyRequest(request, proxyInfo: proxyInfo).then((response) { FlutterToastr.show(localizations.requestSuccess, context); this.response = response; - responseChange.value = !responseChange.value; responseKey.currentState?.change(response); + responseChange.value = !responseChange.value; }).catchError((e) { FlutterToastr.show('${localizations.fail}$e', context); }); @@ -256,10 +262,10 @@ class _HttpState extends State<_HttpWidget> { } } - change(HttpMessage message) { + change(HttpMessage? message) { this.message = message; - body?.text = message.bodyAsString; - headerKey.currentState?.refreshParam(message.headers.getHeaders()); + body?.text = message?.bodyAsString ?? ''; + headerKey.currentState?.refreshParam(message?.headers.getHeaders()); } @override diff --git a/lib/ui/mobile/mobile.dart b/lib/ui/mobile/mobile.dart index 53bd166..8a276f2 100644 --- a/lib/ui/mobile/mobile.dart +++ b/lib/ui/mobile/mobile.dart @@ -131,7 +131,7 @@ class MobileHomeState extends State implements EventListener, Li } }); - if (widget.appConfiguration.upgradeNoticeV10) { + if (widget.appConfiguration.upgradeNoticeV12) { WidgetsBinding.instance.addPostFrameCallback((_) { showUpgradeNotice(); }); @@ -237,7 +237,7 @@ class MobileHomeState extends State implements EventListener, Li '4. fix script binary body convert;\n' ''; showAlertDialog(isCN ? '更新内容V1.1.2' : "Update content V1.1.2", content, () { - widget.appConfiguration.upgradeNoticeV10 = false; + widget.appConfiguration.upgradeNoticeV12 = false; widget.appConfiguration.flushConfig(); }); } diff --git a/lib/ui/mobile/request/request_editor.dart b/lib/ui/mobile/request/request_editor.dart index 88f9673..651d280 100644 --- a/lib/ui/mobile/request/request_editor.dart +++ b/lib/ui/mobile/request/request_editor.dart @@ -4,7 +4,6 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_toastr/flutter_toastr.dart'; -import 'package:network_proxy/native/vpn.dart'; import 'package:network_proxy/network/bin/server.dart'; import 'package:network_proxy/network/host_port.dart'; import 'package:network_proxy/network/http/http.dart'; @@ -161,13 +160,17 @@ class RequestEditorState extends State with SingleTickerPro request.body = requestBody == null ? null : utf8.encode(requestBody); var proxyInfo = - Vpn.isVpnStarted && widget.proxyServer != null ? ProxyInfo.of("127.0.0.1", widget.proxyServer?.port) : null; + widget.proxyServer?.isRunning == true ? ProxyInfo.of("127.0.0.1", widget.proxyServer?.port) : null; + + responseKey.currentState?.change(null); + responseChange.value = !responseChange.value; + HttpClients.proxyRequest(proxyInfo: proxyInfo, request).then((response) { FlutterToastr.show(localizations.requestSuccess, context); this.response = response; this.response?.request = request; - responseChange.value = !responseChange.value; responseKey.currentState?.change(response); + responseChange.value = !responseChange.value; tabController.animateTo(1); }).catchError((e) { FlutterToastr.show('${localizations.fail}$e', context); @@ -231,10 +234,13 @@ class _HttpState extends State<_HttpWidget> with AutomaticKeepAliveClientMixin { } } - change(HttpMessage message) { + change(HttpMessage? message) { this.message = message; - body = message.bodyAsString; - headerKey.currentState?.refreshParam(message.headers.getHeaders()); + body = message?.bodyAsString; + headerKey.currentState?.refreshParam(message?.headers.getHeaders()); + setState(() { + + }); } HttpHeaders? getHeaders() { diff --git a/pubspec.yaml b/pubspec.yaml index da663eb..fc9f538 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,16 +31,16 @@ dependencies: qrscan: ^0.3.3 flutter_barcode_scanner: ^2.0.0 flutter_toastr: ^1.0.3 - share_plus: ^10.0.0 + share_plus: ^10.0.2 brotli: ^0.6.0 - file_selector: ^1.0.3 flutter_js: ^0.8.1 flutter_code_editor: ^0.3.2 - file_picker: ^8.0.7 flutter_desktop_context_menu: ^0.2.0 + file_picker: ^8.1.2 + file_selector: ^1.0.3 win32audio: ^1.3.1 - device_info_plus: ^10.1.1 - shared_preferences: ^2.3.1 + device_info_plus: ^10.1.2 + shared_preferences: ^2.3.2 dev_dependencies: flutter_test: