rentgen/request-cluster.ts

207 lines
6.6 KiB
TypeScript
Raw Normal View History

2022-01-19 13:12:28 +01:00
import ExtendedRequest from './extended-request';
2022-05-02 17:04:56 +02:00
import { SaferEmitter } from './safer-emitter';
2022-01-19 13:12:28 +01:00
import { Sources, StolenDataEntry } from './stolen-data-entry';
2021-10-04 18:51:51 +02:00
2022-01-19 13:12:28 +01:00
import { allSubhosts, isSameURL, reduceConcat, unique } from './util';
const source_priority: Array<Sources> = ['cookie', 'pathname', 'queryparams', 'header'];
2021-10-03 20:13:36 +02:00
2022-05-02 17:04:56 +02:00
export class RequestCluster extends SaferEmitter {
2022-01-19 13:12:28 +01:00
public requests: ExtendedRequest[] = [];
public representativeStolenData: StolenDataEntry[] = [];
2022-07-09 15:51:34 +02:00
public expanded: boolean = false;
2022-07-09 15:28:37 +02:00
public lastModified: number = 0;
public lastFullUrl: string | null = null;
2022-01-19 13:12:28 +01:00
constructor(public id: string) {
super();
}
2022-07-09 15:51:34 +02:00
2022-01-19 13:12:28 +01:00
add(request: ExtendedRequest) {
this.requests.push(request);
this.emit('change');
2022-07-09 15:28:37 +02:00
this.lastModified = Date.now();
if (request.originalURL) {
this.lastFullUrl = request.originalURL;
}
2021-10-03 16:33:23 +02:00
}
2022-01-19 13:12:28 +01:00
toggleExpanded(state: boolean) {
this.expanded = state;
this.emit('change');
}
2021-11-22 17:54:15 +01:00
2022-01-19 13:12:28 +01:00
hasCookies() {
for (const request of this.requests) {
if (request.hasCookie()) {
return true;
}
2021-10-04 18:51:51 +02:00
}
2022-01-19 13:12:28 +01:00
return false;
}
hasMarkedCookies() {
return this.getMarkedEntries().some((entry) => entry.source === 'cookie');
}
2022-01-19 13:12:28 +01:00
calculateRepresentativeStolenData(
filter: {
minValueLength: number;
cookiesOnly: boolean;
cookiesOrOriginOnly: boolean;
} = {
minValueLength: 0,
cookiesOnly: false,
cookiesOrOriginOnly: false,
2021-11-22 17:54:15 +01:00
}
2022-01-19 13:12:28 +01:00
): StolenDataEntry[] {
this.representativeStolenData = this.requests
.map((request) => request.stolenData)
.reduce((a, b) => a.concat(b), [])
.filter((entry) => {
return entry.value.length >= filter.minValueLength;
})
.filter((entry) => !filter.cookiesOnly || entry.source === 'cookie')
.filter(
(entry) =>
!filter.cookiesOrOriginOnly ||
entry.source === 'cookie' ||
entry.classification === 'history'
)
.sort((entry1, entry2) => {
if (entry1.value > entry2.value) {
return -1;
} else if (entry1.value < entry2.value) {
return 1;
} else {
const indexA = source_priority.indexOf(entry1.source);
const indexB = source_priority.indexOf(entry2.source);
if (indexA < indexB) {
return -1;
} else if (indexA > indexB) {
return 1;
} else if (entry1.value.length > entry2.value.length) {
return -1;
} else if (entry1.value.length < entry2.value.length) {
return 1;
} else if (entry1.isMarked && !entry2.isMarked) {
return -1;
} else if (!entry1.isMarked && entry2.isMarked) {
return 1;
} else {
return 0;
}
}
})
.filter((_, index, array) => {
// removing value duplicates
if (index == 0) {
return true;
}
if (
array[index].getValuePreview() === array[index - 1].getValuePreview() ||
2022-01-19 13:12:28 +01:00
isSameURL(array[index].value, array[index - 1].value)
) {
return false;
} else {
return true;
}
})
.sort((entry1, entry2) => {
if (entry1.name < entry2.name) {
return -1;
} else if (entry1.name > entry2.name) {
return 1;
} else {
if (entry1.value.length > entry2.value.length) {
return 1;
} else {
return -1;
}
}
})
.filter((_, index, array) => {
// removing name duplicates, keeping only the first - which is the longest. Some data loss may occur.
if (index == 0) {
return true;
}
if (array[index].name === array[index - 1].name) {
return false;
} else {
return true;
}
})
.sort((entry1, entry2) => (entry1.getPriority() > entry2.getPriority() ? -1 : 1));
2022-01-19 13:12:28 +01:00
return this.representativeStolenData;
}
static sortCompare(a: RequestCluster, b: RequestCluster) {
if (a.hasCookies() == b.hasCookies()) {
if (a.id < b.id) {
return -1;
} else {
return 1;
}
2021-11-22 17:54:15 +01:00
} else {
2022-01-19 13:12:28 +01:00
if (a.hasCookies()) {
return -1;
} else {
return 1;
}
2021-11-22 17:54:15 +01:00
}
2021-10-03 16:33:23 +02:00
}
2021-11-07 15:45:26 +01:00
2022-01-19 13:12:28 +01:00
getMarkedRequests() {
return this.requests.filter((request) => request.hasMark());
}
2022-01-19 13:12:28 +01:00
getFullHosts() {
return unique(
this.requests
.map((request) => allSubhosts(request.getHost()))
.reduce((a, b) => a.concat(b), [])
);
}
2021-11-07 19:03:00 +01:00
2022-01-19 13:12:28 +01:00
hasMarks() {
return this.requests.some((request) => request.hasMark());
}
2021-11-07 19:03:00 +01:00
2022-01-19 13:12:28 +01:00
getMarkedEntries(): StolenDataEntry[] {
return this.requests.map((request) => request.getMarkedEntries()).reduce(reduceConcat, []);
2022-01-19 13:12:28 +01:00
}
2022-01-19 13:12:28 +01:00
exposesOrigin() {
return this.requests.some((request) => request.exposesOrigin());
}
2021-11-24 14:19:12 +01:00
2022-01-19 13:12:28 +01:00
autoMark() {
this.calculateRepresentativeStolenData();
this.representativeStolenData.forEach((entry) => {
entry.autoMark();
});
}
2022-05-02 13:46:53 +02:00
undoMark() {
this.calculateRepresentativeStolenData();
this.representativeStolenData.forEach((entry) => {
entry.unmark();
});
}
2022-07-07 21:16:48 +02:00
getDataTypeDescription(noun = 'Twojej') {
let types_of_data: string[] = [];
if (this.exposesOrigin()) {
types_of_data.push(`część ${noun} historii przeglądania`);
}
if (this.hasMarkedCookies()) {
types_of_data.push('unikalne ID z cookies');
}
if (types_of_data.length > 1) {
types_of_data[types_of_data.length - 1] =
'oraz ' + types_of_data[types_of_data.length - 1];
}
2022-07-07 21:16:48 +02:00
return types_of_data.join(', ');
}
2021-10-03 16:33:23 +02:00
}