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

@@ -465,9 +465,10 @@ let Network = {
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param data - The data to send with the request. * @param data - The data to send with the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: ArrayBuffer}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: ArrayBuffer}>} The response from the request.
*/ */
async fetchBytes(method, url, headers, data) { async fetchBytes(method, url, headers, data, extra) {
let result = await sendMessage({ let result = await sendMessage({
method: 'http', method: 'http',
http_method: method, http_method: method,
@@ -475,6 +476,7 @@ let Network = {
url: url, url: url,
headers: headers, headers: headers,
data: data, data: data,
extra: extra,
}); });
if (result.error) { if (result.error) {
@@ -490,15 +492,17 @@ let Network = {
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param data - The data to send with the request. * @param data - The data to send with the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async sendRequest(method, url, headers, data) { async sendRequest(method, url, headers, data, extra) {
let result = await sendMessage({ let result = await sendMessage({
method: 'http', method: 'http',
http_method: method, http_method: method,
url: url, url: url,
headers: headers, headers: headers,
data: data, data: data,
extra: extra,
}); });
if (result.error) { if (result.error) {
@@ -512,10 +516,11 @@ let Network = {
* Sends an HTTP GET request. * Sends an HTTP GET request.
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async get(url, headers) { async get(url, headers, extra) {
return this.sendRequest('GET', url, headers); return this.sendRequest('GET', url, headers, extra);
}, },
/** /**
@@ -523,10 +528,11 @@ let Network = {
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param data - The data to send with the request. * @param data - The data to send with the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async post(url, headers, data) { async post(url, headers, data, extra) {
return this.sendRequest('POST', url, headers, data); return this.sendRequest('POST', url, headers, data, extra);
}, },
/** /**
@@ -534,10 +540,11 @@ let Network = {
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param data - The data to send with the request. * @param data - The data to send with the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async put(url, headers, data) { async put(url, headers, data, extra) {
return this.sendRequest('PUT', url, headers, data); return this.sendRequest('PUT', url, headers, data, extra);
}, },
/** /**
@@ -545,20 +552,22 @@ let Network = {
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param data - The data to send with the request. * @param data - The data to send with the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async patch(url, headers, data) { async patch(url, headers, data, extra) {
return this.sendRequest('PATCH', url, headers, data); return this.sendRequest('PATCH', url, headers, data, extra);
}, },
/** /**
* Sends an HTTP DELETE request. * Sends an HTTP DELETE request.
* @param {string} url - The URL to send the request to. * @param {string} url - The URL to send the request to.
* @param {Object} headers - The headers to include in the request. * @param {Object} headers - The headers to include in the request.
* @param {Object} extra - Extra options to pass to the interceptor.
* @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request.
*/ */
async delete(url, headers) { async delete(url, headers, extra) {
return this.sendRequest('DELETE', url, headers); return this.sendRequest('DELETE', url, headers, extra);
}, },
/** /**

View File

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

View File

@@ -96,11 +96,28 @@ class MyLogInterceptor implements Interceptor {
@override @override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) { void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
const String headerMask = "********";
const String dataMask = "****** DATA_PROTECTED ******";
Log.info( Log.info(
"Network", "Network",
"${options.method} ${options.uri}\n" "${options.method} ${options.uri}\n"
"headers:\n${options.headers}\n" "headers:\n${
"data:\n${options.data}"); 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.connectTimeout = const Duration(seconds: 15);
options.receiveTimeout = const Duration(seconds: 15); options.receiveTimeout = const Duration(seconds: 15);
options.sendTimeout = const Duration(seconds: 15); options.sendTimeout = const Duration(seconds: 15);