From c523080e053f630c7fc87d4d4aff557be5b0327b Mon Sep 17 00:00:00 2001 From: wanghongen Date: Thu, 6 Jul 2023 01:07:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=8F=E5=B0=91=E5=88=B7=E6=96=B0=E9=A2=91?= =?UTF-8?q?=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/network/channel.dart | 6 ++- lib/network/http/http_headers.dart | 4 ++ lib/ui/desktop/toolbar/setting/setting.dart | 2 +- lib/ui/mobile/request.dart | 51 ++++++++++++++------- 4 files changed, 45 insertions(+), 18 deletions(-) diff --git a/lib/network/channel.dart b/lib/network/channel.dart index 4a8fec7..6a16e95 100644 --- a/lib/network/channel.dart +++ b/lib/network/channel.dart @@ -124,6 +124,10 @@ class ChannelPipeline extends ChannelHandler { } if (data is HttpRequest) { data.hostAndPort = channel.getAttribute(AttributeKeys.host) ?? getHostAndPort(data); + if (data.headers.host() != null) { + data.hostAndPort?.host = data.headers.host()!; + } + data.remoteDomain = data.hostAndPort?.url; data.requestUrl = data.uri.startsWith("/") ? '${data.remoteDomain}${data.uri}' : data.uri; try { @@ -157,7 +161,7 @@ class HostAndPort { static const String httpScheme = "http://"; static const String httpsScheme = "https://"; final String scheme; - final String host; + String host; final int port; HostAndPort(this.scheme, this.host, this.port); diff --git a/lib/network/http/http_headers.dart b/lib/network/http/http_headers.dart index 4e1d833..125c543 100644 --- a/lib/network/http/http_headers.dart +++ b/lib/network/http/http_headers.dart @@ -22,6 +22,9 @@ class HttpHeaders { void add(String name, String value) { if (_headers.containsKey(name.toLowerCase())) { _headers[name.toLowerCase()]!.add(value); + if (!_originalHeaderNames.containsKey(name)) { + _originalHeaderNames[name] = []; + } _originalHeaderNames[name]!.add(value); return; } @@ -76,6 +79,7 @@ class HttpHeaders { set contentType(String contentType) => set(CONTENT_TYPE, contentType); String get contentType => get(CONTENT_TYPE) ?? ""; + String? host() => get(HOST); @override String toString() { return 'HttpHeaders{$_originalHeaderNames}'; diff --git a/lib/ui/desktop/toolbar/setting/setting.dart b/lib/ui/desktop/toolbar/setting/setting.dart index cf6b1e1..3e0905d 100644 --- a/lib/ui/desktop/toolbar/setting/setting.dart +++ b/lib/ui/desktop/toolbar/setting/setting.dart @@ -38,7 +38,7 @@ class _SettingState extends State { valueListenable: enableDesktopListenable, builder: (_, val, __) => SwitchListTile( hoverColor: Colors.transparent, - title: const Text("启用电脑抓包", style: TextStyle(fontSize: 12)), + title: const Text("抓取电脑请求", style: TextStyle(fontSize: 12)), visualDensity: const VisualDensity(horizontal: -4), dense: true, value: val, diff --git a/lib/ui/mobile/request.dart b/lib/ui/mobile/request.dart index b605cd1..c0e9fec 100644 --- a/lib/ui/mobile/request.dart +++ b/lib/ui/mobile/request.dart @@ -81,11 +81,13 @@ class RequestSequence extends StatefulWidget { } class RequestSequenceState extends State { - GlobalKey listKey = GlobalKey(); + // GlobalKey listKey = GlobalKey(); Map> indexes = HashMap(); late Queue list = Queue(); + bool changing = false; + @override initState() { super.initState(); @@ -95,7 +97,17 @@ class RequestSequenceState extends State { ///添加请求 add(HttpRequest request) { list.addFirst(request); - listKey.currentState?.insertItem(0); + // listKey.currentState?.insertItem(0); + + //防止频繁刷新 + if (!changing) { + changing = true; + Future.delayed(const Duration(milliseconds: 500), () { + setState(() { + changing = false; + }); + }); + } } ///添加响应 @@ -109,22 +121,19 @@ class RequestSequenceState extends State { setState(() { list.clear(); indexes.clear(); - listKey.currentState?.removeAllItems((context, animation) => Container()); + // listKey.currentState?.removeAllItems((context, animation) => Container()); }); } @override Widget build(BuildContext context) { - return AnimatedList( - key: listKey, - initialItemCount: list.length, - itemBuilder: (context, index, Animation animation) { + return ListView.separated( + separatorBuilder: (context, index) => Divider(height: 0.5, color: Theme.of(context).focusColor), + itemCount: list.length, + itemBuilder: (context, index) { GlobalKey key = GlobalKey(); indexes[list.elementAt(index)] = key; - return Container( - decoration: - BoxDecoration(border: Border(bottom: BorderSide(width: 0.5, color: Theme.of(context).focusColor))), - child: RequestRow(key: key, request: list.elementAt(index))); + return RequestRow(key: key, request: list.elementAt(index)); }); } } @@ -215,6 +224,7 @@ class DomainListState extends State { LinkedHashSet container = LinkedHashSet(); List list = []; HostAndPort? showHostAndPort; + bool changing = false; @override initState() { @@ -248,7 +258,15 @@ class DomainListState extends State { } this.list = [...container].reversed.toList(); - setState(() {}); + //防止频繁刷新 + if (!changing) { + changing = true; + Future.delayed(const Duration(milliseconds: 500), () { + setState(() { + changing = false; + }); + }); + } } addResponse(HttpResponse response) { @@ -259,19 +277,20 @@ class DomainListState extends State { clean() { setState(() { - containerMap.clear(); + list.clear(); container.clear(); + containerMap.clear(); }); } @override Widget build(BuildContext context) { return ListView.separated( - separatorBuilder: (context, index) => Divider(height: 0.5, color: Theme.of(context).focusColor), + separatorBuilder: (context, index) => Divider(height: 0.5, color: Theme.of(context).focusColor), itemCount: list.length, itemBuilder: (context, index) { - var time = formatDate( - containerMap[list.elementAt(index)]!.last.requestTime, [m, '/', d, ' ', HH, ':', nn, ':', ss]); + var time = + formatDate(containerMap[list.elementAt(index)]!.last.requestTime, [m, '/', d, ' ', HH, ':', nn, ':', ss]); return ListTile( title: Text(list.elementAt(index).url, maxLines: 1, overflow: TextOverflow.ellipsis), trailing: const Icon(Icons.chevron_right),