diff --git a/lib/network/util/script_manager.dart b/lib/network/util/script_manager.dart index b57784d..f7c46df 100644 --- a/lib/network/util/script_manager.dart +++ b/lib/network/util/script_manager.dart @@ -107,7 +107,8 @@ async function onResponse(context, request, response) { if (_scriptMap.containsKey(item)) { return _scriptMap[item]!; } - var script = await File(item.scriptPath!).readAsString(); + final home = await homePath(); + var script = await File(home + item.scriptPath!).readAsString(); _scriptMap[item] = script; return script; } @@ -115,10 +116,11 @@ async function onResponse(context, request, response) { ///添加脚本 Future addScript(ScriptItem item, String script) async { final path = await homePath(); - var file = File('$path${separator}scripts$separator${DateTime.now().millisecondsSinceEpoch}.js'); + String scriptPath = "${separator}scripts$separator${DateTime.now().millisecondsSinceEpoch}.js"; + var file = File(path + scriptPath); await file.create(recursive: true); file.writeAsString(script); - item.scriptPath = file.path; + item.scriptPath = scriptPath; list.add(item); _scriptMap[item] = script; } @@ -128,15 +130,25 @@ async function onResponse(context, request, response) { if (_scriptMap[item] == script) { return; } - - File(item.scriptPath!).writeAsString(script); + final home = await homePath(); + File(home + item.scriptPath!).writeAsString(script); _scriptMap[item] = script; } ///删除脚本 Future removeScript(int index) async { var item = list.removeAt(index); - File(item.scriptPath!).delete(); + final home = await homePath(); + File(home + item.scriptPath!).delete(); + } + + Future clean() async { + while (list.isNotEmpty) { + var item = list.removeLast(); + final home = await homePath(); + File(home + item.scriptPath!).delete(); + } + await flushConfig(); } ///刷新配置 @@ -262,7 +274,6 @@ async function onResponse(context, request, response) { response.body = map['body']?.toString().codeUnits; return response; } - } class ScriptItem { @@ -276,10 +287,6 @@ class ScriptItem { //匹配url bool match(String url) { - if (!this.url.startsWith('http://') && !this.url.startsWith('https://')) { - //不是http开头的url 需要去掉协议 - url = url.substring(url.indexOf('://') + 3); - } urlReg ??= RegExp(this.url.replaceAll("*", ".*")); return urlReg!.hasMatch(url); } diff --git a/lib/storage/favorites.dart b/lib/storage/favorites.dart index ef2569a..88c3815 100644 --- a/lib/storage/favorites.dart +++ b/lib/storage/favorites.dart @@ -53,7 +53,6 @@ class FavoriteStorage { static Future removeFavorite(Favorite favorite) async { var list = await favorites; list.remove(favorite); - flushConfig(); } diff --git a/lib/storage/histories.dart b/lib/storage/histories.dart index eae3bc3..09395ac 100644 --- a/lib/storage/histories.dart +++ b/lib/storage/histories.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'package:date_format/date_format.dart'; import 'package:file_selector/file_selector.dart'; import 'package:network_proxy/network/http/http.dart'; +import 'package:network_proxy/utils/files.dart'; import 'package:network_proxy/utils/har.dart'; import 'package:path_provider/path_provider.dart'; @@ -43,6 +44,11 @@ class HistoryStorage { } } + static Future _homePath() async { + final home = await getApplicationSupportDirectory(); + return '${home.path}${Platform.pathSeparator}history'; + } + /// 获取历史记录 List get histories { return _histories; @@ -60,8 +66,8 @@ class HistoryStorage { ///打开文件 static Future openFile(String name) async { - final directory = await getApplicationSupportDirectory(); - var file = File('${directory.path}${Platform.pathSeparator}history${Platform.pathSeparator}$name'); + final homePath = await _homePath(); + var file = File('$homePath${Platform.pathSeparator}$name'); return file.create(recursive: true); } @@ -96,17 +102,18 @@ class HistoryStorage { ///删除 void removeHistory(int index) async { var history = _histories.removeAt(index); - var file = File(history.path); - if (await file.exists()) { - await file.delete(); - } + final homePath = await _homePath(); + var file = File('$homePath${Platform.pathSeparator}${Files.getName(history.path)}'); + file.delete(); (await _path).writeAsString(jsonEncode(_histories)); } //获取请求列表 Future> getRequests(HistoryItem history) async { if (history.requests == null) { - var file = File(history.path); + final homePath = await _homePath(); + String path = '$homePath${Platform.pathSeparator}${Files.getName(history.path)}'; + var file = File(path); history.requests = await Har.readFile(file); history.requestLength = history.requests!.length; file.length().then((size) => history.fileSize = size); diff --git a/lib/ui/content/panel.dart b/lib/ui/content/panel.dart index eed8d03..6e7f892 100644 --- a/lib/ui/content/panel.dart +++ b/lib/ui/content/panel.dart @@ -105,8 +105,12 @@ class NetworkTabState extends State with SingleTickerProvi return const SizedBox(); } var response = widget.response.get(); + String requestUrl = request.requestUrl; + try { + requestUrl = Uri.decodeFull(request.requestUrl); + } catch (_) {} var content = [ - rowWidget("Request URL", request.requestUrl), + rowWidget("Request URL", requestUrl), const SizedBox(height: 20), rowWidget("Request Method", request.method.name), const SizedBox(height: 20), diff --git a/lib/ui/desktop/left/history.dart b/lib/ui/desktop/left/history.dart index b7e27fe..2a23ef4 100644 --- a/lib/ui/desktop/left/history.dart +++ b/lib/ui/desktop/left/history.dart @@ -260,7 +260,6 @@ class _HistoryState extends State<_HistoryWidget> { //写入文件 _writeHarFile(List container, String name) async { var file = await HistoryStorage.openFile("${DateTime.now().millisecondsSinceEpoch}.txt"); - print(file); RandomAccessFile open = await file.open(mode: FileMode.append); HistoryItem item = await storage.addHistory(name, file, 0); writeTask = WriteTask(item, open, storage, callback: () => setState(() {})); diff --git a/lib/ui/desktop/left/request_editor.dart b/lib/ui/desktop/left/request_editor.dart index 7005e36..b736b88 100644 --- a/lib/ui/desktop/left/request_editor.dart +++ b/lib/ui/desktop/left/request_editor.dart @@ -127,8 +127,10 @@ class RequestEditorState extends State { var headers = requestKey.currentState?.getHeaders(); var requestBody = requestKey.currentState?.getBody(); - HttpRequest request = HttpRequest(HttpMethod.valueOf(currentState.requestMethod), currentState.requestUrl); + HttpRequest request = + HttpRequest(HttpMethod.valueOf(currentState.requestMethod), Uri.encodeFull(currentState.requestUrl)); request.headers.addAll(headers); + request.body = requestBody == null ? null : utf8.encode(requestBody); HttpClients.proxyRequest(request).then((response) { diff --git a/lib/ui/desktop/toolbar/setting/script.dart b/lib/ui/desktop/toolbar/setting/script.dart index 090f250..5ae81d9 100644 --- a/lib/ui/desktop/toolbar/setting/script.dart +++ b/lib/ui/desktop/toolbar/setting/script.dart @@ -252,6 +252,7 @@ class _ScriptEditState extends State { } else { widget.scriptItem?.name = nameController.text; widget.scriptItem?.url = urlController.text; + widget.scriptItem?.urlReg = null; (await ScriptManager.instance).updateScript(widget.scriptItem!, script.text); } diff --git a/lib/ui/mobile/connect_remote.dart b/lib/ui/mobile/connect_remote.dart index 4e74116..54f58cd 100644 --- a/lib/ui/mobile/connect_remote.dart +++ b/lib/ui/mobile/connect_remote.dart @@ -171,10 +171,12 @@ class ConfigSyncState extends State { widget.configuration.flushRequestRewriteConfig(); } if (syncScript) { - await ScriptManager.instance.then((script) async { + ScriptManager.instance.then((script) async { + await script.clean(); script.list.clear(); - await widget.config['scripts'] - .forEach((it) async => await script.addScript(ScriptItem.fromJson(it), it['script'])); + for (var item in widget.config['scripts']) { + await script.addScript(ScriptItem.fromJson(item), item['script']); + } await script.flushConfig(); }); } diff --git a/lib/ui/mobile/request/history.dart b/lib/ui/mobile/request/history.dart index ac95493..53b29f3 100644 --- a/lib/ui/mobile/request/history.dart +++ b/lib/ui/mobile/request/history.dart @@ -116,7 +116,6 @@ class _MobileHistoryState extends State { //写入文件 _writeHarFile(HistoryStorage storage, List container, String name) async { var file = await HistoryStorage.openFile("${DateTime.now().millisecondsSinceEpoch.toRadixString(36)}.txt"); - print(file); RandomAccessFile open = await file.open(mode: FileMode.append); HistoryItem history = await storage.addHistory(name, file, 0); diff --git a/lib/ui/mobile/request/list.dart b/lib/ui/mobile/request/list.dart index 874267b..2e45164 100644 --- a/lib/ui/mobile/request/list.dart +++ b/lib/ui/mobile/request/list.dart @@ -42,7 +42,6 @@ class RequestListState extends State { if (widget.list != null) { container.addAll(widget.list!); } - print(domainListKey); } @override diff --git a/lib/ui/mobile/request/request_editor.dart b/lib/ui/mobile/request/request_editor.dart index 0dfeedc..9b81624 100644 --- a/lib/ui/mobile/request/request_editor.dart +++ b/lib/ui/mobile/request/request_editor.dart @@ -123,7 +123,7 @@ class RequestEditorState extends State with SingleTickerPro var headers = requestKey.currentState?.getHeaders(); var requestBody = requestKey.currentState?.getBody(); - HttpRequest request = HttpRequest(HttpMethod.valueOf(currentState.requestMethod), currentState.requestUrl); + HttpRequest request = HttpRequest(HttpMethod.valueOf(currentState.requestMethod), Uri.encodeFull(currentState.requestUrl)); request.headers.addAll(headers); request.body = requestBody == null ? null : utf8.encode(requestBody); diff --git a/lib/ui/mobile/setting/script.dart b/lib/ui/mobile/setting/script.dart index 255f500..79e3bcf 100644 --- a/lib/ui/mobile/setting/script.dart +++ b/lib/ui/mobile/setting/script.dart @@ -204,6 +204,7 @@ class _ScriptEditState extends State { } else { widget.scriptItem?.name = nameController.text; widget.scriptItem?.url = urlController.text; + widget.scriptItem?.urlReg = null; (await ScriptManager.instance).updateScript(widget.scriptItem!, script.text); } @@ -226,12 +227,11 @@ class _ScriptEditState extends State { const SizedBox(height: 10), const Text("脚本:"), const SizedBox(height: 5), - SizedBox( - height: 520, - child: CodeTheme( - data: CodeThemeData(styles: monokaiSublimeTheme), - child: SingleChildScrollView( - child: CodeField(textStyle: const TextStyle(fontSize: 14), controller: script)))) + CodeTheme( + data: CodeThemeData(styles: monokaiSublimeTheme), + child: SingleChildScrollView( + child: CodeField( + textStyle: const TextStyle(fontSize: 14), controller: script))) ], )))); } diff --git a/lib/utils/files.dart b/lib/utils/files.dart new file mode 100644 index 0000000..99eb5c5 --- /dev/null +++ b/lib/utils/files.dart @@ -0,0 +1,12 @@ + + +import 'dart:io'; + +class Files { + //获取文件名称 + + static String getName(String path) { + var index = path.lastIndexOf(Platform.pathSeparator); + return path.substring(index + 1); + } +} diff --git a/pubspec.lock b/pubspec.lock index 34f2678..affe4a3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -267,10 +267,10 @@ packages: dependency: "direct dev" description: name: flutter_lints - sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04 + sha256: ad76540d21c066228ee3f9d1dad64a9f7e46530e8bb7c85011a88bc1fd874bc5 url: "https://pub.flutter-io.cn" source: hosted - version: "2.0.3" + version: "3.0.0" flutter_plugin_android_lifecycle: dependency: transitive description: @@ -365,10 +365,10 @@ packages: dependency: transitive description: name: lints - sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452" + sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290 url: "https://pub.flutter-io.cn" source: hosted - version: "2.1.1" + version: "3.0.0" logger: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 210c8f0..af19a33 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,7 +37,7 @@ dependencies: dev_dependencies: flutter_test: sdk: flutter - flutter_lints: ^2.0.1 + flutter_lints: ^3.0.0 # The following section is specific to Flutter packages. flutter: