rentgen/request-cluster.ts

107 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-10-03 16:33:23 +02:00
import { EventEmitter } from "events";
import ExtendedRequest from "./extended-request";
import { MergedStolenDataEntry, StolenDataEntry } from "./stolen-data-entry";
2021-10-04 18:51:51 +02:00
2021-11-07 19:03:00 +01:00
import { allSubhosts, reduceConcat, unique } from "./util";
2021-10-03 20:13:36 +02:00
2021-10-03 16:33:23 +02:00
export class RequestCluster extends EventEmitter {
public requests: ExtendedRequest[] = [];
constructor(public id: string) {
super();
}
add(request: ExtendedRequest) {
this.requests.push(request);
this.emit("change");
}
hasCookies() {
for (const request of this.requests) {
if (request.hasCookie()) {
return true;
}
}
return false;
}
2021-11-06 20:02:02 +01:00
getStolenData(filter: {
minValueLength: number;
cookiesOnly: boolean;
}): MergedStolenDataEntry[] {
2021-10-04 18:51:51 +02:00
return this.requests
.map((request) => request.stolenData)
2021-10-03 16:33:23 +02:00
.reduce((a, b) => a.concat(b), [])
2021-10-04 18:51:51 +02:00
.filter((entry) => {
return entry.value.length >= filter.minValueLength;
})
2021-11-06 20:02:02 +01:00
.filter((entry) => !filter.cookiesOnly || entry.source === "cookie")
.sort((entryA, entryB) => (entryA.name > entryB.name ? -1 : 1))
2021-10-04 18:51:51 +02:00
.filter((element, index, array) => {
// remove duplicates by name/value
2021-10-04 18:51:51 +02:00
if (index == 0) {
return true;
}
if (
element.name != array[index - 1].name ||
element.value != array[index - 1].value
) {
return true;
}
})
.sort((entryA, entryB) => (entryA.value > entryB.value ? -1 : 1))
.reduce(
(acc: MergedStolenDataEntry[], entry: StolenDataEntry) => {
// group by value
const last_entry = acc.slice(-1)[0];
if (last_entry.hasValue(entry.value)) {
last_entry.mergeWith(entry);
} else {
acc.push(new MergedStolenDataEntry([entry]));
}
return acc;
},
[new MergedStolenDataEntry([])] as MergedStolenDataEntry[]
)
.sort((entry1, entry2) =>
entry1.getPriority() > entry2.getPriority() ? -1 : 1
);
2021-10-03 16:33:23 +02:00
}
static sortCompare(a: RequestCluster, b: RequestCluster) {
if (a.hasCookies() == b.hasCookies()) {
if (a.id < b.id) {
return -1;
} else {
return 1;
}
} else {
if (a.hasCookies()) {
return -1;
} else {
return 1;
}
}
}
2021-11-07 15:45:26 +01:00
getMarkedRequests() {
return this.requests.filter((request) => request.hasMark());
}
getFullHosts() {
return unique(
this.requests
.map((request) => allSubhosts(request.getHost()))
.reduce((a, b) => a.concat(b), [])
);
}
2021-11-07 19:03:00 +01:00
hasMarks() {
return this.requests.some((request) => request.hasMark());
}
getMarkedEntries() {
return this.requests
.map((request) => request.getMarkedEntries())
.reduce(reduceConcat, []);
}
2021-10-03 16:33:23 +02:00
}