This repository has been archived on 2023-12-17. You can view files and clone it, but cannot push or open issues or pull requests.
extension/src/contentProviders/baseProvider.ts

86 lines
2.0 KiB
TypeScript

import {
BlockingResponse,
OnBeforeRequestDetailsType,
OnBeforeSendHeadersDetailsType,
OnHeadersReceivedDetailsType,
OnRequestDetailsType,
} from "../types";
import WebEngine from "../webEngine.js";
import { getTld, isDomain, isIp, normalizeDomain } from "../util.js";
import tldEnum from "@lumeweb/tld-enum";
import { getAuthStatus } from "../main/vars.js";
import { scanRecords } from "../dns.js";
export default abstract class BaseProvider {
private engine: WebEngine;
constructor(engine: WebEngine) {
this.engine = engine;
}
async shouldHandleRequest(
details: OnBeforeRequestDetailsType
): Promise<boolean> {
return false;
}
async handleRequest(
details: OnBeforeRequestDetailsType
): Promise<BlockingResponse | boolean> {
return false;
}
async handleReqHeaders(
details: OnBeforeSendHeadersDetailsType
): Promise<BlockingResponse | boolean> {
return false;
}
async handleProxy(details: OnRequestDetailsType): Promise<any | boolean> {
return false;
}
async handleHeaders(
details: OnHeadersReceivedDetailsType
): Promise<BlockingResponse | boolean> {
return false;
}
protected async resolveDns(
details: OnBeforeRequestDetailsType,
recordTypes?: string[]
) {
const originalUrl = new URL(details.url);
const hostname = normalizeDomain(originalUrl.hostname);
if (typeof getAuthStatus === "undefined") {
debugger;
}
if (getAuthStatus().loginComplete !== true) {
return false;
}
if (tldEnum.list.includes(getTld(hostname))) {
return false;
}
if (isIp(hostname) && !isDomain(hostname)) {
return false;
}
return await scanRecords(hostname, recordTypes);
}
protected getData(details: OnBeforeRequestDetailsType, key: string) {
return this.engine.getRequestData(details.requestId, key);
}
protected setData(
details: OnBeforeRequestDetailsType,
key: string,
value: any
) {
return this.engine.setRequestData(details.requestId, key, value);
}
}