interceptor: mask log (#618)

This commit is contained in:
Pacalini
2025-11-29 14:21:30 +08:00
committed by GitHub
parent 7e3addf7a6
commit da5b64abb0
3 changed files with 45 additions and 15 deletions

View File

@@ -217,6 +217,7 @@ class JsEngine with _JSEngineApi, JsUiApi, Init {
try {
var headers = Map<String, dynamic>.from(req["headers"] ?? {});
var extra = Map<String, dynamic>.from(req["extra"] ?? {});
if (headers["user-agent"] == null && headers["User-Agent"] == null) {
headers["User-Agent"] = webUA;
}
@@ -244,7 +245,10 @@ class JsEngine with _JSEngineApi, JsUiApi, Init {
responseType: req["bytes"] == true
? ResponseType.bytes
: ResponseType.plain,
headers: headers));
headers: headers,
extra: extra,
)
);
} catch (e) {
error = e.toString();
}

View File

@@ -96,11 +96,28 @@ class MyLogInterceptor implements Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
const String headerMask = "********";
const String dataMask = "****** DATA_PROTECTED ******";
Log.info(
"Network",
"${options.method} ${options.uri}\n"
"headers:\n${options.headers}\n"
"data:\n${options.data}");
"headers:\n${
options.extra.containsKey("maskHeadersInLog")
? options.headers.map((key, value) =>
MapEntry(
key,
options.extra["maskHeadersInLog"].contains(key)
? headerMask
: value
))
: options.headers
}\n"
"data:\n${
options.extra["maskDataInLog"] == true
? dataMask
: options.data
}"
);
options.connectTimeout = const Duration(seconds: 15);
options.receiveTimeout = const Duration(seconds: 15);
options.sendTimeout = const Duration(seconds: 15);