rentgen/components/report-window/har-converter.tsx

116 lines
3.8 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { HAREntry } from '../../extended-request';
import { StolenDataEntry } from '../../stolen-data-entry';
import { getshorthost, unique } from '../../util';
2021-11-08 20:14:28 +01:00
function handleNewFile(
2022-01-24 12:21:28 +01:00
element: HTMLInputElement,
entries: StolenDataEntry[],
2022-01-29 20:41:03 +01:00
setFiltered: (arg0: Blob) => void
): void {
2022-01-24 12:21:28 +01:00
const reader = new FileReader();
reader.addEventListener('load', () => {
const content = JSON.parse(reader.result as string);
2022-04-24 22:11:33 +02:00
content.log.entries = content.log.entries.filter((har_entry: HAREntry) =>
entries.some((entry) => entry.matchesHAREntry(har_entry))
2022-01-24 12:21:28 +01:00
);
2022-04-24 22:11:33 +02:00
setFiltered(new Blob([JSON.stringify(content)], { type: 'application/json' }));
2022-01-24 12:21:28 +01:00
});
2022-07-09 15:28:37 +02:00
const file = element?.files?.[0];
if (!file) throw new Error('file empty?');
reader.readAsText(file);
2021-11-08 20:14:28 +01:00
}
2021-11-22 17:54:15 +01:00
function generateFakeHAR(entries: StolenDataEntry[]) {
2022-01-24 12:21:28 +01:00
const requests = unique(entries.map((entry) => entry.request))
.sort((request1, request2) => {
if (request1.shorthost < request2.shorthost) {
return -1;
} else if (request1.shorthost > request2.shorthost) {
return 1;
} else {
2022-04-24 22:11:33 +02:00
return request2.getBalancedPriority() - request1.getBalancedPriority();
2022-01-24 12:21:28 +01:00
}
})
.filter((_, index, array) => {
if (index == 0) return true;
if (array[index].shorthost == array[index - 1].shorthost) {
return false;
}
return true;
})
2022-04-24 22:11:33 +02:00
.sort((entry1, entry2) => entry2.getBalancedPriority() - entry1.getBalancedPriority());
2022-01-24 12:21:28 +01:00
return {
log: {
version: '1.2',
creator: {
name: 'Firefox',
version: '94.0',
},
browser: {
name: 'Firefox',
version: '94.0',
},
pages: [
{
startedDateTime: '2021-11-08T20:27:23.195+01:00',
id: 'page_1',
title: 'HAR DUmp',
pageTimings: {
onContentLoad: 467,
onLoad: 4226,
},
},
],
entries: requests.map((r) => r.toHAR()),
},
2022-01-24 12:21:28 +01:00
};
}
2022-04-24 22:11:33 +02:00
export default function HARConverter({ entries }: { entries: StolenDataEntry[] }) {
const [filtered, setFiltered] = React.useState<Blob | null>(null);
const [filename, setFilename] = React.useState('');
2022-04-24 22:11:33 +02:00
const [fakeHAR, setFakeHAR] = React.useState<ReturnType<typeof generateFakeHAR>>();
React.useEffect(() => {
2022-01-24 12:21:28 +01:00
setFakeHAR(generateFakeHAR(entries));
}, []);
2022-01-24 12:21:28 +01:00
return (
<div>
<input
type="file"
accept=".har"
onChange={(e) => {
2022-07-09 15:28:37 +02:00
const file = e.target?.files?.[0];
if (file) {
setFilename(file.name);
handleNewFile(e.target, entries, setFiltered);
}
2022-01-24 12:21:28 +01:00
}}
/>
{(filtered && (
<a
href={URL.createObjectURL(filtered)}
download={filename.replace('.har', '-filtered.har')}
>
Pobierz wyfiltrowany HAR
</a>
)) ||
null}
<a
href={URL.createObjectURL(
new Blob([JSON.stringify(fakeHAR)], {
type: 'application/json',
})
)}
download={`${getshorthost(
2022-07-09 15:51:34 +02:00
entries[0].request.origin
2022-01-24 12:21:28 +01:00
)}-${new Date().toJSON()}-trimmed.har`}
>
Pobierz "zredukowany" HAR
</a>
</div>
);
2021-11-08 20:14:28 +01:00
}