Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type cleanup #1069

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Extension/src/background/dns-instrument.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { PendingResponse } from "../lib/pending-response";
import { DnsResolved } from "../schema";
import { WebRequestOnCompletedEventDetails } from "../types/browser-web-request-event-details";
import { allTypes } from "./http-instrument";
import RequestFilter = browser.webRequest.RequestFilter;

Expand Down Expand Up @@ -29,7 +28,9 @@ export class DnsInstrument {
/*
* Attach handlers to event listeners
*/
this.onCompleteListener = (details: WebRequestOnCompletedEventDetails) => {
this.onCompleteListener = (
details: browser.webRequest._OnCompletedDetails,
) => {
// Ignore requests made by extensions
if (requestStemsFromExtension(details)) {
return;
Expand Down Expand Up @@ -70,7 +71,7 @@ export class DnsInstrument {
}

private async onCompleteDnsHandler(
details: WebRequestOnCompletedEventDetails,
details: browser.webRequest._OnCompletedDetails,
crawlID,
) {
// Create and populate DnsResolve object
Expand Down
49 changes: 26 additions & 23 deletions Extension/src/background/http-instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,7 @@ import { PendingRequest } from "../lib/pending-request";
import { PendingResponse } from "../lib/pending-response";
import { boolToInt, escapeString, escapeUrl } from "../lib/string-utils";
import { HttpRedirect, HttpRequest, HttpResponse } from "../schema";
import {
WebRequestOnBeforeRedirectEventDetails,
WebRequestOnBeforeRequestEventDetails,
WebRequestOnBeforeSendHeadersEventDetails,
WebRequestOnCompletedEventDetails,
} from "../types/browser-web-request-event-details";
import { WebRequestOnBeforeSendHeadersEventDetails } from "../types/browser-web-request-event-details";
import ResourceType = browser.webRequest.ResourceType;
import RequestFilter = browser.webRequest.RequestFilter;
import BlockingResponse = browser.webRequest.BlockingResponse;
Expand Down Expand Up @@ -52,23 +47,31 @@ const allTypes: ResourceType[] = [
export { allTypes };

export class HttpInstrument {
private readonly dataReceiver;
private readonly dataReceiver: typeof import("../loggingdb");
private pendingRequests: {
[requestId: number]: PendingRequest;
[requestId: string]: PendingRequest;
} = {};
private pendingResponses: {
[requestId: number]: PendingResponse;
[requestId: string]: PendingResponse;
} = {};
private onBeforeRequestListener;
private onBeforeSendHeadersListener;
private onBeforeRedirectListener;
private onCompletedListener;

constructor(dataReceiver) {
private onBeforeRequestListener: (
details: browser.webRequest._OnBeforeRequestDetails,
) => BlockingResponse;
private onBeforeSendHeadersListener: (
details: WebRequestOnBeforeSendHeadersEventDetails,
) => void;
private onBeforeRedirectListener: (
details: browser.webRequest._OnBeforeRedirectDetails,
) => void;
private onCompletedListener: (
details: browser.webRequest._OnCompletedDetails,
) => void;

constructor(dataReceiver: typeof import("../loggingdb")) {
this.dataReceiver = dataReceiver;
}

public run(crawlID, saveContentOption: SaveContentOption) {
public run(crawlID: number, saveContentOption: SaveContentOption) {
const filter: RequestFilter = { urls: ["<all_urls>"], types: allTypes };

const requestStemsFromExtension = (details) => {
Expand All @@ -82,7 +85,7 @@ export class HttpInstrument {
*/

this.onBeforeRequestListener = (
details: WebRequestOnBeforeRequestEventDetails,
details: browser.webRequest._OnBeforeRequestDetails,
) => {
const blockingResponseThatDoesNothing: BlockingResponse = {};
// Ignore requests made by extensions
Expand Down Expand Up @@ -216,14 +219,14 @@ export class HttpInstrument {
);
}

private getPendingRequest(requestId): PendingRequest {
private getPendingRequest(requestId: string): PendingRequest {
if (!this.pendingRequests[requestId]) {
this.pendingRequests[requestId] = new PendingRequest();
}
return this.pendingRequests[requestId];
}

private getPendingResponse(requestId): PendingResponse {
private getPendingResponse(requestId: string): PendingResponse {
if (!this.pendingResponses[requestId]) {
this.pendingResponses[requestId] = new PendingResponse();
}
Expand All @@ -236,7 +239,7 @@ export class HttpInstrument {

private async onBeforeSendHeadersHandler(
details: WebRequestOnBeforeSendHeadersEventDetails,
crawlID,
crawlID: number,
eventOrdinal: number,
) {
const tab =
Expand Down Expand Up @@ -460,7 +463,7 @@ export class HttpInstrument {
}

private async onBeforeRedirectHandler(
details: WebRequestOnBeforeRedirectEventDetails,
details: browser.webRequest._OnBeforeRedirectDetails,
crawlID,
eventOrdinal: number,
) {
Expand Down Expand Up @@ -569,7 +572,7 @@ export class HttpInstrument {
*/

private async logWithResponseBody(
details: WebRequestOnBeforeRequestEventDetails,
details: browser.webRequest._OnBeforeRequestDetails,
update: HttpResponse,
) {
const pendingResponse = this.getPendingResponse(details.requestId);
Expand Down Expand Up @@ -604,7 +607,7 @@ export class HttpInstrument {

// Instrument HTTP responses
private async onCompletedHandler(
details: WebRequestOnCompletedEventDetails,
details: browser.webRequest._OnCompletedDetails,
crawlID,
eventOrdinal,
saveContent,
Expand Down
4 changes: 2 additions & 2 deletions Extension/src/callstack-instrument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
*/
export class CallstackInstrument {
dataReceiver: any;
constructor(dataReceiver) {
constructor(dataReceiver: typeof import("./loggingdb")) {
this.dataReceiver = dataReceiver;
}
run(browser_id) {
run(browser_id: number) {
(browser as any).stackDump.onStackAvailable.addListener(
(request_id, call_stack) => {
const record = {
Expand Down
12 changes: 5 additions & 7 deletions Extension/src/feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { CallstackInstrument } from "./callstack-instrument";
async function main() {
// Read the browser configuration from file
const filename = "browser_params.json";
let config = await (browser as any).profileDirIO.readFile(filename);
if (config) {
config = JSON.parse(config);
const raw_config = await browser.profileDirIO.readFile(filename);
let config: any;
if (raw_config) {
config = JSON.parse(raw_config);
console.log("Browser Config:", config);
} else {
config = {
Expand Down Expand Up @@ -100,10 +101,7 @@ async function main() {
dnsInstrument.run(config.browser_id);
}

await (browser as any).profileDirIO.writeFile(
"OPENWPM_STARTUP_SUCCESS.txt",
"",
);
await browser.profileDirIO.writeFile("OPENWPM_STARTUP_SUCCESS.txt", "");
}

main();
5 changes: 2 additions & 3 deletions Extension/src/lib/http-post-parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WebRequestOnBeforeRequestEventDetails } from "../types/browser-web-request-event-details";
import { escapeString, Uint8ToBase64 } from "./string-utils";

export interface ParsedPostRequest {
Expand All @@ -8,11 +7,11 @@ export interface ParsedPostRequest {
}

export class HttpPostParser {
private readonly onBeforeRequestEventDetails: WebRequestOnBeforeRequestEventDetails;
private readonly onBeforeRequestEventDetails: browser.webRequest._OnBeforeRequestDetails;
private readonly dataReceiver;

constructor(
onBeforeRequestEventDetails: WebRequestOnBeforeRequestEventDetails,
onBeforeRequestEventDetails: browser.webRequest._OnBeforeRequestDetails,
dataReceiver,
) {
this.onBeforeRequestEventDetails = onBeforeRequestEventDetails;
Expand Down
9 changes: 3 additions & 6 deletions Extension/src/lib/pending-request.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {
WebRequestOnBeforeRequestEventDetails,
WebRequestOnBeforeSendHeadersEventDetails,
} from "../types/browser-web-request-event-details";
import { WebRequestOnBeforeSendHeadersEventDetails } from "../types/browser-web-request-event-details";

/**
* Ties together the two separate events that together holds information about both request headers and body
*/
export class PendingRequest {
public readonly onBeforeRequestEventDetails: Promise<WebRequestOnBeforeRequestEventDetails>;
public readonly onBeforeRequestEventDetails: Promise<browser.webRequest._OnBeforeRequestDetails>;
public readonly onBeforeSendHeadersEventDetails: Promise<WebRequestOnBeforeSendHeadersEventDetails>;
public resolveOnBeforeRequestEventDetails: (
details: WebRequestOnBeforeRequestEventDetails,
details: browser.webRequest._OnBeforeRequestDetails,
) => void;
public resolveOnBeforeSendHeadersEventDetails: (
details: WebRequestOnBeforeSendHeadersEventDetails,
Expand Down
14 changes: 5 additions & 9 deletions Extension/src/lib/pending-response.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import {
WebRequestOnBeforeRequestEventDetails,
WebRequestOnCompletedEventDetails,
} from "../types/browser-web-request-event-details";
import { ResponseBodyListener } from "./response-body-listener";

/**
* Ties together the two separate events that together holds information about both response headers and body
*/
export class PendingResponse {
public readonly onBeforeRequestEventDetails: Promise<WebRequestOnBeforeRequestEventDetails>;
public readonly onCompletedEventDetails: Promise<WebRequestOnCompletedEventDetails>;
public readonly onBeforeRequestEventDetails: Promise<browser.webRequest._OnBeforeRequestDetails>;
public readonly onCompletedEventDetails: Promise<browser.webRequest._OnCompletedDetails>;
public responseBodyListener: ResponseBodyListener;
public resolveOnBeforeRequestEventDetails: (
details: WebRequestOnBeforeRequestEventDetails,
details: browser.webRequest._OnBeforeRequestDetails,
) => void;
public resolveOnCompletedEventDetails: (
details: WebRequestOnCompletedEventDetails,
details: browser.webRequest._OnCompletedDetails,
) => void;
constructor() {
this.onBeforeRequestEventDetails = new Promise((resolve) => {
Expand All @@ -26,7 +22,7 @@ export class PendingResponse {
});
}
public addResponseResponseBodyListener(
details: WebRequestOnBeforeRequestEventDetails,
details: browser.webRequest._OnBeforeRequestDetails,
) {
this.responseBodyListener = new ResponseBodyListener(details);
}
Expand Down
3 changes: 1 addition & 2 deletions Extension/src/lib/response-body-listener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { WebRequestOnBeforeRequestEventDetails } from "../types/browser-web-request-event-details";
import { digestMessage } from "./sha256";

export class ResponseBodyListener {
Expand All @@ -7,7 +6,7 @@ export class ResponseBodyListener {
private resolveResponseBody: (responseBody: Uint8Array) => void;
private resolveContentHash: (contentHash: string) => void;

constructor(details: WebRequestOnBeforeRequestEventDetails) {
constructor(details: browser.webRequest._OnBeforeRequestDetails) {
this.responseBody = new Promise((resolve) => {
this.resolveResponseBody = resolve;
});
Expand Down
8 changes: 4 additions & 4 deletions Extension/src/loggingdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ const listeningSocketCallback = async (data) => {
}
};
export const open = async function (
storageControllerAddress,
logAddress,
curr_crawlID,
storageControllerAddress: any[],
logAddress: any[],
curr_crawlID: number,
) {
if (
storageControllerAddress == null &&
Expand Down Expand Up @@ -81,7 +81,7 @@ export const open = async function (
listeningSocket = new socket.ListeningSocket(listeningSocketCallback);
console.log("Starting socket listening for incoming connections.");
await listeningSocket.startListening().then(() => {
(browser as any).profileDirIO.writeFile(
browser.profileDirIO.writeFile(
"extension_port.txt",
`${listeningSocket.port}`,
);
Expand Down
29 changes: 14 additions & 15 deletions Extension/src/socket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable max-classes-per-file */

const DataReceiver = {
callbacks: new Map(),
onDataReceived: (aSocketId, aData, aJSON) => {
onDataReceived: (aSocketId: number, aData: string, aJSON: boolean): void => {
if (!DataReceiver.callbacks.has(aSocketId)) {
return;
}
Expand All @@ -12,45 +13,43 @@ const DataReceiver = {
},
};

(browser as any).sockets.onDataReceived.addListener(
DataReceiver.onDataReceived,
);
browser.sockets.onDataReceived.addListener(DataReceiver.onDataReceived);

export class ListeningSocket {
callback: any;
port: any;
port: number;
constructor(callback) {
this.callback = callback;
}

async startListening() {
this.port = await (browser as any).sockets.createServerSocket();
this.port = await browser.sockets.createServerSocket();
DataReceiver.callbacks.set(this.port, this.callback);
(browser as any).sockets.startListening(this.port);
browser.sockets.startListening(this.port);
console.log("Listening on port " + this.port);
}
}

export class SendingSocket {
id: any;
id: number;

async connect(host, port) {
this.id = await (browser as any).sockets.createSendingSocket();
(browser as any).sockets.connect(this.id, host, port);
async connect(host: string, port: number) {
this.id = await browser.sockets.createSendingSocket();
browser.sockets.connect(this.id, host, port);
console.log(`Connected to ${host}:${port}`);
}

send(aData, aJSON = true) {
send(aData: string, aJSON = true): boolean {
try {
(browser as any).sockets.sendData(this.id, aData, !!aJSON);
browser.sockets.sendData(this.id, aData, !!aJSON);
return true;
} catch (err) {
console.error(err, err.message);
return false;
}
}

close() {
(browser as any).sockets.close(this.id);
close(): void {
browser.sockets.close(this.id);
}
}
Loading
Loading