mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-05-20 16:15:47 +08:00
修复ios每次升级历史记录无法查看问题
This commit is contained in:
@@ -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<void> 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<void> removeScript(int index) async {
|
||||
var item = list.removeAt(index);
|
||||
File(item.scriptPath!).delete();
|
||||
final home = await homePath();
|
||||
File(home + item.scriptPath!).delete();
|
||||
}
|
||||
|
||||
Future<void> 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);
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ class FavoriteStorage {
|
||||
static Future<void> removeFavorite(Favorite favorite) async {
|
||||
var list = await favorites;
|
||||
list.remove(favorite);
|
||||
|
||||
flushConfig();
|
||||
}
|
||||
|
||||
|
||||
@@ -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<String> _homePath() async {
|
||||
final home = await getApplicationSupportDirectory();
|
||||
return '${home.path}${Platform.pathSeparator}history';
|
||||
}
|
||||
|
||||
/// 获取历史记录
|
||||
List<HistoryItem> get histories {
|
||||
return _histories;
|
||||
@@ -60,8 +66,8 @@ class HistoryStorage {
|
||||
|
||||
///打开文件
|
||||
static Future<File> 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<List<HttpRequest>> 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);
|
||||
|
||||
@@ -105,8 +105,12 @@ class NetworkTabState extends State<NetworkTabController> 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),
|
||||
|
||||
@@ -260,7 +260,6 @@ class _HistoryState extends State<_HistoryWidget> {
|
||||
//写入文件
|
||||
_writeHarFile(List<HttpRequest> 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(() {}));
|
||||
|
||||
@@ -127,8 +127,10 @@ class RequestEditorState extends State<RequestEditor> {
|
||||
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) {
|
||||
|
||||
@@ -252,6 +252,7 @@ class _ScriptEditState extends State<ScriptEdit> {
|
||||
} else {
|
||||
widget.scriptItem?.name = nameController.text;
|
||||
widget.scriptItem?.url = urlController.text;
|
||||
widget.scriptItem?.urlReg = null;
|
||||
(await ScriptManager.instance).updateScript(widget.scriptItem!, script.text);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,10 +171,12 @@ class ConfigSyncState extends State<ConfigSyncWidget> {
|
||||
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();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -116,7 +116,6 @@ class _MobileHistoryState extends State<MobileHistory> {
|
||||
//写入文件
|
||||
_writeHarFile(HistoryStorage storage, List<HttpRequest> 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);
|
||||
|
||||
|
||||
@@ -42,7 +42,6 @@ class RequestListState extends State<RequestListWidget> {
|
||||
if (widget.list != null) {
|
||||
container.addAll(widget.list!);
|
||||
}
|
||||
print(domainListKey);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -123,7 +123,7 @@ class RequestEditorState extends State<MobileRequestEditor> 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);
|
||||
|
||||
|
||||
@@ -204,6 +204,7 @@ class _ScriptEditState extends State<ScriptEdit> {
|
||||
} 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<ScriptEdit> {
|
||||
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)))
|
||||
],
|
||||
))));
|
||||
}
|
||||
|
||||
12
lib/utils/files.dart
Normal file
12
lib/utils/files.dart
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user