refactor(Extension): fix .eslintrs.js and execute npm run fix

This commit is contained in:
vringar 2022-03-24 16:04:53 +01:00
Родитель be9dfb43d7
Коммит 044f9d2f01
12 изменённых файлов: 452 добавлений и 426 удалений

Просмотреть файл

@ -1,84 +1,163 @@
/* eslint-env node */
/* /*
👋 Hi! This file was autogenerated by tslint-to-eslint-config. This ESLint config aims to work with both
https://github.com/typescript-eslint/tslint-to-eslint-config the JavaScript and the TypeScript parts of the
codebase.
It represents the closest reasonable ESLint configuration to this
project's original TSLint configuration.
We recommend eventually switching this configuration to extend from
the recommended rulesets in typescript-eslint.
https://github.com/typescript-eslint/tslint-to-eslint-config/blob/master/docs/FAQs.md
Happy linting! 💖
*/ */
module.exports = { module.exports = {
"env": { root: true,
"node": true, env: {
"es6": true node: true,
}, es6: true,
"extends": [ webextensions: true,
"prettier" browser: true,
},
extends: ["prettier", "eslint:recommended"],
ignorePatterns: [
"bundled/feature.js",
"bundled/content.js",
"bundled/privileged",
"build/**",
"dist/**",
"node_modules/**",
],
plugins: [
"eslint-plugin-import",
"eslint-plugin-jsdoc",
"eslint-plugin-unicorn",
],
rules: {
"arrow-parens": ["off", "always"],
"brace-style": ["off", "off"],
"comma-dangle": "off",
complexity: "off",
"constructor-super": "error",
"eol-last": "off",
eqeqeq: ["warn", "smart"],
"guard-for-in": "warn",
"id-blacklist": "warn",
"id-match": "warn",
"import/no-extraneous-dependencies": ["error"],
"import/no-internal-modules": "error",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
"jsdoc/newline-after-description": "error",
"linebreak-style": "off",
"max-classes-per-file": ["error", 1],
"max-len": "off",
"new-parens": "off",
"newline-per-chained-call": "off",
"no-bitwise": "warn",
"no-caller": "error",
"no-cond-assign": "error",
"no-debugger": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-empty": "warn",
"no-eval": "error",
"no-extra-bind": "error",
"no-extra-semi": "off",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-irregular-whitespace": "off",
"no-multiple-empty-lines": "off",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-redeclare": "error",
"no-return-await": "error",
"no-sequences": "error",
"no-sparse-arrays": "error",
"no-template-curly-in-string": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "off",
"no-undef-init": "error",
"no-underscore-dangle": "warn",
"no-unsafe-finally": "error",
"no-unused-labels": "error",
"no-var": "error",
"object-shorthand": "error",
"one-var": ["warn", "never"],
"prefer-const": "error",
"prefer-object-spread": "error",
"quote-props": "off",
radix: "error",
"space-before-function-paren": "off",
"space-in-parens": ["off", "never"],
"spaced-comment": [
"error",
"always",
{
markers: ["/"],
},
], ],
"parser": "@typescript-eslint/parser", "unicorn/prefer-ternary": "error",
"parserOptions": { "use-isnan": "error",
"project": "tsconfig.json", "valid-typeof": "off",
"sourceType": "module" },
}, overrides: [
"plugins": [ {
"eslint-plugin-prefer-arrow", files: ["*.ts"],
"eslint-plugin-import", parser: "@typescript-eslint/parser",
"eslint-plugin-jsdoc", parserOptions: {
"eslint-plugin-unicorn", project: "tsconfig.json",
"@typescript-eslint", sourceType: "module",
], },
"rules": { plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/adjacent-overload-signatures": "error", "@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [ "@typescript-eslint/array-type": [
"error", "error",
{ {
"default": "array" default: "array",
} },
], ],
"@typescript-eslint/ban-types": [ "@typescript-eslint/ban-types": [
"error", "error",
{ {
"types": { types: {
"Object": { Object: {
"message": "Avoid using the `Object` type. Did you mean `object`?" message:
}, "Avoid using the `Object` type. Did you mean `object`?",
"Function": { },
"message": "Avoid using the `Function` type. Prefer a specific function type, like `() => void`." Function: {
}, message:
"Boolean": { "Avoid using the `Function` type. Prefer a specific function type, like `() => void`.",
"message": "Avoid using the `Boolean` type. Did you mean `boolean`?" },
}, Boolean: {
"Number": { message:
"message": "Avoid using the `Number` type. Did you mean `number`?" "Avoid using the `Boolean` type. Did you mean `boolean`?",
}, },
"String": { Number: {
"message": "Avoid using the `String` type. Did you mean `string`?" message:
}, "Avoid using the `Number` type. Did you mean `number`?",
"Symbol": { },
"message": "Avoid using the `Symbol` type. Did you mean `symbol`?" String: {
} message:
} "Avoid using the `String` type. Did you mean `string`?",
} },
Symbol: {
message:
"Avoid using the `Symbol` type. Did you mean `symbol`?",
},
},
},
], ],
"@typescript-eslint/consistent-type-assertions": "off", "@typescript-eslint/consistent-type-assertions": "off",
"@typescript-eslint/dot-notation": "error", "@typescript-eslint/dot-notation": "error",
"@typescript-eslint/indent": "off", "@typescript-eslint/indent": "off",
"@typescript-eslint/member-delimiter-style": [ "@typescript-eslint/member-delimiter-style": [
"off", "off",
{ {
"multiline": { multiline: {
"delimiter": "none", delimiter: "none",
"requireLast": true requireLast: true,
}, },
"singleline": { singleline: {
"delimiter": "semi", delimiter: "semi",
"requireLast": false requireLast: false,
} },
} },
], ],
"@typescript-eslint/no-empty-function": "warn", "@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/no-empty-interface": "error", "@typescript-eslint/no-empty-interface": "error",
@ -87,10 +166,10 @@ module.exports = {
"@typescript-eslint/no-namespace": "error", "@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off", "@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-shadow": [ "@typescript-eslint/no-shadow": [
"off", "off",
{ {
"hoist": "all" hoist: "all",
} },
], ],
"@typescript-eslint/no-this-alias": "error", "@typescript-eslint/no-this-alias": "error",
"@typescript-eslint/no-unused-expressions": "warn", "@typescript-eslint/no-unused-expressions": "warn",
@ -100,112 +179,24 @@ module.exports = {
"@typescript-eslint/prefer-function-type": "error", "@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error", "@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/quotes": "off", "@typescript-eslint/quotes": "off",
"@typescript-eslint/semi": [ "@typescript-eslint/semi": ["off", null],
"off",
null
],
"@typescript-eslint/triple-slash-reference": [ "@typescript-eslint/triple-slash-reference": [
"error", "error",
{ {
"path": "always", path: "always",
"types": "prefer-import", types: "prefer-import",
"lib": "always" lib: "always",
} },
], ],
"@typescript-eslint/type-annotation-spacing": "off", "@typescript-eslint/type-annotation-spacing": "off",
"@typescript-eslint/unified-signatures": "error", "@typescript-eslint/unified-signatures": "error",
"arrow-parens": [ "no-undef": "off",
"off", "no-unused-vars": "off",
"always" "@typescript-eslint/no-unused-vars": [
"error",
{ argsIgnorePattern: "^_" },
], ],
"brace-style": [ },
"off", },
"off" ],
],
"comma-dangle": "off",
"complexity": "off",
"constructor-super": "error",
"eol-last": "off",
"eqeqeq": [
"warn",
"always"
],
"guard-for-in": "warn",
"id-blacklist": "warn",
"id-match": "warn",
"import/no-extraneous-dependencies": [
"error",
{
"devDependencies": false
}
],
"import/no-internal-modules": "error",
"jsdoc/check-alignment": "error",
"jsdoc/check-indentation": "error",
"jsdoc/newline-after-description": "error",
"linebreak-style": "off",
"max-classes-per-file": [
"error",
1
],
"max-len": "off",
"new-parens": "off",
"newline-per-chained-call": "off",
"no-bitwise": "warn",
"no-caller": "error",
"no-cond-assign": "error",
"no-console": "warn",
"no-debugger": "error",
"no-duplicate-case": "error",
"no-duplicate-imports": "error",
"no-empty": "warn",
"no-eval": "error",
"no-extra-bind": "error",
"no-extra-semi": "off",
"no-fallthrough": "off",
"no-invalid-this": "off",
"no-irregular-whitespace": "off",
"no-multiple-empty-lines": "off",
"no-new-func": "error",
"no-new-wrappers": "error",
"no-redeclare": "error",
"no-return-await": "error",
"no-sequences": "error",
"no-sparse-arrays": "error",
"no-template-curly-in-string": "error",
"no-throw-literal": "error",
"no-trailing-spaces": "off",
"no-undef-init": "error",
"no-underscore-dangle": "warn",
"no-unsafe-finally": "error",
"no-unused-labels": "error",
"no-var": "error",
"object-shorthand": "error",
"one-var": [
"warn",
"never"
],
"prefer-arrow/prefer-arrow-functions": "warn",
"prefer-const": "error",
"prefer-object-spread": "error",
"quote-props": "off",
"radix": "error",
"space-before-function-paren": "off",
"space-in-parens": [
"off",
"never"
],
"spaced-comment": [
"error",
"always",
{
"markers": [
"/"
]
}
],
"unicorn/prefer-ternary": "error",
"use-isnan": "error",
"valid-typeof": "off"
}
}; };

Просмотреть файл

@ -443,7 +443,9 @@ export class HttpInstrument {
if (details.type === "main_frame") { if (details.type === "main_frame") {
// Url of the top-level document itself. // Url of the top-level document itself.
url = details.url; url = details.url;
} else if (details.hasOwnProperty("frameAncestors")) { } else if (
Object.prototype.hasOwnProperty.call(details, "frameAncestors")
) {
// In case of nested frames, retrieve url from top-most ancestor. // In case of nested frames, retrieve url from top-most ancestor.
// If frameAncestors == [], request comes from the top-level-document. // If frameAncestors == [], request comes from the top-level-document.
url = details.frameAncestors.length url = details.frameAncestors.length

Просмотреть файл

@ -79,7 +79,7 @@ export class JavascriptInstrument {
public handleJsInstrumentationMessage(message, sender: MessageSender) { public handleJsInstrumentationMessage(message, sender: MessageSender) {
switch (message.type) { switch (message.type) {
case "logCall": case "logCall":
case "logValue": case "logValue": {
const update = JavascriptInstrument.processCallsAndValues( const update = JavascriptInstrument.processCallsAndValues(
message.data, message.data,
sender, sender,
@ -91,6 +91,7 @@ export class JavascriptInstrument {
this.pendingRecords.push(update); this.pendingRecords.push(update);
} }
break; break;
}
} }
} }

Просмотреть файл

@ -9,13 +9,15 @@ export class CallstackInstrument {
this.dataReceiver = dataReceiver; this.dataReceiver = dataReceiver;
} }
run(browser_id) { run(browser_id) {
(browser as any).stackDump.onStackAvailable.addListener((request_id, call_stack) => { (browser as any).stackDump.onStackAvailable.addListener(
const record = { (request_id, call_stack) => {
browser_id, const record = {
request_id, browser_id,
call_stack request_id,
}; call_stack,
this.dataReceiver.saveRecord("callstacks", record); };
}); this.dataReceiver.saveRecord("callstacks", record);
},
);
} }
} }

Просмотреть файл

@ -1,4 +1,9 @@
import {injectJavascriptInstrumentPageScript} from "./content/javascript-instrument-content-scope"; import { injectJavascriptInstrumentPageScript } from "./content/javascript-instrument-content-scope";
injectJavascriptInstrumentPageScript(window.openWpmContentScriptConfig || {testing:false, jsInstrumentationSettings: []}); injectJavascriptInstrumentPageScript(
window.openWpmContentScriptConfig || {
testing: false,
jsInstrumentationSettings: [],
},
);
delete window.openWpmContentScriptConfig; delete window.openWpmContentScriptConfig;

Просмотреть файл

@ -2,7 +2,7 @@ import { getInstrumentJS, JSInstrumentRequest } from "../lib/js-instruments";
import { pageScript } from "./javascript-instrument-page-scope"; import { pageScript } from "./javascript-instrument-page-scope";
import { openWpmContentScriptConfig } from "../types/javascript-instrument"; import { openWpmContentScriptConfig } from "../types/javascript-instrument";
function getPageScriptAsString ( function getPageScriptAsString(
jsInstrumentationSettings: JSInstrumentRequest[], jsInstrumentationSettings: JSInstrumentRequest[],
): string { ): string {
// The JS Instrument Requests are setup and validated python side // The JS Instrument Requests are setup and validated python side
@ -21,7 +21,7 @@ const jsInstrumentationSettings = ${JSON.stringify(jsInstrumentationSettings)};
// End. // End.
`; `;
return pageScriptString; return pageScriptString;
}; }
function insertScript( function insertScript(
pageScriptString: string, pageScriptString: string,
@ -36,16 +36,16 @@ function insertScript(
script.setAttribute("data-testing", `${testing}`); script.setAttribute("data-testing", `${testing}`);
parent.insertBefore(script, parent.firstChild); parent.insertBefore(script, parent.firstChild);
parent.removeChild(script); parent.removeChild(script);
}; }
function emitMsg (type, msg) { function emitMsg(type, msg) {
msg.timeStamp = new Date().toISOString(); msg.timeStamp = new Date().toISOString();
browser.runtime.sendMessage({ browser.runtime.sendMessage({
namespace: "javascript-instrumentation", namespace: "javascript-instrumentation",
type, type,
data: msg, data: msg,
}); });
}; }
const eventId = Math.random().toString(); const eventId = Math.random().toString();
@ -62,10 +62,12 @@ document.addEventListener(eventId, (e: CustomEvent) => {
} }
}); });
export const injectJavascriptInstrumentPageScript = (contentScriptConfig: openWpmContentScriptConfig) => { export const injectJavascriptInstrumentPageScript = (
contentScriptConfig: openWpmContentScriptConfig,
) => {
insertScript( insertScript(
getPageScriptAsString(contentScriptConfig.jsInstrumentationSettings), getPageScriptAsString(contentScriptConfig.jsInstrumentationSettings),
eventId, eventId,
contentScriptConfig.testing, contentScriptConfig.testing,
); );
} };

Просмотреть файл

@ -3,7 +3,7 @@
// Also, no webpack/es6 imports may be used in this file since the script // Also, no webpack/es6 imports may be used in this file since the script
// is exported as a page script as a string // is exported as a page script as a string
export function pageScript (getInstrumentJS, jsInstrumentationSettings) { export function pageScript(getInstrumentJS, jsInstrumentationSettings) {
// messages the injected script // messages the injected script
const sendMessagesToLogger = (eventId, messages) => { const sendMessagesToLogger = (eventId, messages) => {
document.dispatchEvent( document.dispatchEvent(
@ -34,4 +34,4 @@ export function pageScript (getInstrumentJS, jsInstrumentationSettings) {
"(if spec is '<unavailable>' check web console.)", "(if spec is '<unavailable>' check web console.)",
); );
} }
}; }

Просмотреть файл

@ -1,3 +1,4 @@
/* eslint no-eval: "off" */
import { CookieInstrument } from "./background/cookie-instrument"; import { CookieInstrument } from "./background/cookie-instrument";
import { DnsInstrument } from "./background/dns-instrument"; import { DnsInstrument } from "./background/dns-instrument";
import { HttpInstrument } from "./background/http-instrument"; import { HttpInstrument } from "./background/http-instrument";
@ -9,18 +10,17 @@ import { CallstackInstrument } from "./callstack-instrument";
async function main() { async function main() {
// Read the browser configuration from file // Read the browser configuration from file
let filename = "browser_params.json"; const filename = "browser_params.json";
let config = await (browser as any).profileDirIO.readFile(filename); let config = await (browser as any).profileDirIO.readFile(filename);
if (config) { if (config) {
config = JSON.parse(config); config = JSON.parse(config);
console.log("Browser Config:", config); console.log("Browser Config:", config);
} else { } else {
config = { config = {
navigation_instrument:true, navigation_instrument: true,
cookie_instrument:true, cookie_instrument: true,
js_instrument:true, js_instrument: true,
cleaned_js_instrument_settings: cleaned_js_instrument_settings: [
[
{ {
object: `window.CanvasRenderingContext2D.prototype`, object: `window.CanvasRenderingContext2D.prototype`,
instrumentedName: "CanvasRenderingContext2D", instrumentedName: "CanvasRenderingContext2D",
@ -34,67 +34,76 @@ async function main() {
preventSets: false, preventSets: false,
recursive: false, recursive: false,
depth: 5, depth: 5,
} },
}, },
], ],
http_instrument:true, http_instrument: true,
callstack_instrument:true, callstack_instrument: true,
save_content:false, save_content: false,
testing:true, testing: true,
browser_id:0, browser_id: 0,
custom_params: {} custom_params: {},
}; };
console.log("WARNING: config not found. Assuming this is a test run of", console.log(
"the extension. Outputting all queries to console.", {config}); "WARNING: config not found. Assuming this is a test run of",
"the extension. Outputting all queries to console.",
{ config },
);
} }
await loggingDB.open(config['storage_controller_address'], await loggingDB.open(
config['logger_address'], config.storage_controller_address,
config['browser_id']); config.logger_address,
config.browser_id,
);
if (config["custom_params"]["pre_instrumentation_code"]) { if (config.custom_params.pre_instrumentation_code) {
eval(config["custom_params"]["pre_instrumentation_code"]) eval(config.custom_params.pre_instrumentation_code);
} }
if (config["navigation_instrument"]) { if (config.navigation_instrument) {
loggingDB.logDebug("Navigation instrumentation enabled"); loggingDB.logDebug("Navigation instrumentation enabled");
let navigationInstrument = new NavigationInstrument(loggingDB); const navigationInstrument = new NavigationInstrument(loggingDB);
navigationInstrument.run(config["browser_id"]); navigationInstrument.run(config.browser_id);
} }
if (config['cookie_instrument']) { if (config.cookie_instrument) {
loggingDB.logDebug("Cookie instrumentation enabled"); loggingDB.logDebug("Cookie instrumentation enabled");
let cookieInstrument = new CookieInstrument(loggingDB); const cookieInstrument = new CookieInstrument(loggingDB);
cookieInstrument.run(config['browser_id']); cookieInstrument.run(config.browser_id);
} }
if (config['js_instrument']) { if (config.js_instrument) {
loggingDB.logDebug("Javascript instrumentation enabled"); loggingDB.logDebug("Javascript instrumentation enabled");
let jsInstrument = new JavascriptInstrument(loggingDB); const jsInstrument = new JavascriptInstrument(loggingDB);
jsInstrument.run(config['browser_id']); jsInstrument.run(config.browser_id);
await jsInstrument.registerContentScript(config['testing'], config['cleaned_js_instrument_settings']); await jsInstrument.registerContentScript(
config.testing,
config.cleaned_js_instrument_settings,
);
} }
if (config['http_instrument']) { if (config.http_instrument) {
loggingDB.logDebug("HTTP Instrumentation enabled"); loggingDB.logDebug("HTTP Instrumentation enabled");
let httpInstrument = new HttpInstrument(loggingDB); const httpInstrument = new HttpInstrument(loggingDB);
httpInstrument.run(config['browser_id'], httpInstrument.run(config.browser_id, config.save_content);
config['save_content']);
} }
if (config['callstack_instrument']) { if (config.callstack_instrument) {
loggingDB.logDebug("Callstack Instrumentation enabled"); loggingDB.logDebug("Callstack Instrumentation enabled");
let callstackInstrument = new CallstackInstrument(loggingDB); const callstackInstrument = new CallstackInstrument(loggingDB);
callstackInstrument.run(config['browser_id']); callstackInstrument.run(config.browser_id);
}
if (config['dns_instrument']) {
loggingDB.logDebug("DNS instrumentation enabled");
let dnsInstrument = new DnsInstrument(loggingDB);
dnsInstrument.run(config['browser_id']);
} }
await (browser as any).profileDirIO.writeFile("OPENWPM_STARTUP_SUCCESS.txt", ""); if (config.dns_instrument) {
loggingDB.logDebug("DNS instrumentation enabled");
const dnsInstrument = new DnsInstrument(loggingDB);
dnsInstrument.run(config.browser_id);
}
await (browser as any).profileDirIO.writeFile(
"OPENWPM_STARTUP_SUCCESS.txt",
"",
);
} }
main(); main();

Просмотреть файл

@ -1,3 +1,4 @@
/* eslint no-eval: "off" */
// Intrumentation injection code is based on privacybadgerfirefox // Intrumentation injection code is based on privacybadgerfirefox
// https://github.com/EFForg/privacybadgerfirefox/blob/master/data/fingerprinting.js // https://github.com/EFForg/privacybadgerfirefox/blob/master/data/fingerprinting.js
@ -133,8 +134,7 @@ export function getInstrumentJS(eventId: string, sendMessagesToLogger) {
let siblingIndex = 1; let siblingIndex = 1;
const siblings = element.parentNode.childNodes; const siblings = element.parentNode.childNodes;
for (let i = 0; i < siblings.length; i++) { for (const sibling of siblings) {
const sibling = siblings[i];
if (sibling === element) { if (sibling === element) {
let path = getPathToDomElement(element.parentNode, visibilityAttr); let path = getPathToDomElement(element.parentNode, visibilityAttr);
path += "/" + element.tagName + "[" + siblingIndex; path += "/" + element.tagName + "[" + siblingIndex;
@ -621,7 +621,7 @@ export function getInstrumentJS(eventId: string, sendMessagesToLogger) {
returnValue = originalSetter.call(this, value); returnValue = originalSetter.call(this, value);
} else if ("value" in propDesc) { } else if ("value" in propDesc) {
inLog = true; inLog = true;
if (object.isPrototypeOf(this)) { if (Object.prototype.isPrototypeOf.call(object, this)) {
Object.defineProperty(this, propertyName, { Object.defineProperty(this, propertyName, {
value, value,
}); });
@ -733,7 +733,7 @@ export function getInstrumentJS(eventId: string, sendMessagesToLogger) {
const sendFactory = function (eventId, $sendMessagesToLogger) { const sendFactory = function (eventId, $sendMessagesToLogger) {
let messages = []; let messages = [];
// debounce sending queued messages // debounce sending queued messages
const _send = debounce(function () { const send = debounce(function () {
$sendMessagesToLogger(eventId, messages); $sendMessagesToLogger(eventId, messages);
// clear the queue // clear the queue
@ -743,7 +743,7 @@ export function getInstrumentJS(eventId: string, sendMessagesToLogger) {
return function (msgType, msg) { return function (msgType, msg) {
// queue the message // queue the message
messages.push({ type: msgType, content: msg }); messages.push({ type: msgType, content: msg });
_send(); send();
}; };
}; };

Просмотреть файл

@ -7,203 +7,221 @@ let storageController = null;
let logAggregator = null; let logAggregator = null;
let listeningSocket = null; let listeningSocket = null;
const listeningSocketCallback = async (data) => {
// This works even if data is an int
const action = data.action;
let newVisitID = data.visit_id;
switch (action) {
case "Initialize":
if (visitID) {
logWarn("Set visit_id while another visit_id was set");
}
visitID = newVisitID;
data.browser_id = crawlID;
storageController.send(JSON.stringify(["meta_information", data]));
break;
case "Finalize":
if (!visitID) {
logWarn("Received Finalize while no visit_id was set");
}
if (newVisitID !== visitID) {
logError(
"Received Finalize but visit_id didn't match. " +
`Current visit_id ${newVisitID}, received visit_id ${visitID}.`,
);
}
data.browser_id = crawlID;
data.success = true;
storageController.send(JSON.stringify(["meta_information", data]));
visitID = null;
break;
default:
// Just making sure that it's a valid number before logging
newVisitID = parseInt(data, 10);
logDebug("Setting visit_id the legacy way");
visitID = newVisitID;
}
};
export const open = async function (
storageControllerAddress,
logAddress,
curr_crawlID,
) {
if (
storageControllerAddress == null &&
logAddress == null &&
curr_crawlID === 0
) {
console.log("Debugging, everything will output to console");
debugging = true;
return;
}
crawlID = curr_crawlID;
let listeningSocketCallback = async (data) => { console.log("Opening socket connections...");
//This works even if data is an int
let action = data["action"];
let _visitID = data["visit_id"]
switch (action) {
case "Initialize":
if (visitID) {
logWarn("Set visit_id while another visit_id was set")
}
visitID = _visitID;
data["browser_id"] = crawlID;
storageController.send(JSON.stringify(["meta_information", data]));
break;
case "Finalize":
if (!visitID) {
logWarn("Received Finalize while no visit_id was set")
}
if (_visitID !== visitID ) {
logError("Received Finalize but visit_id didn't match. " +
`Current visit_id ${_visitID}, received visit_id ${visitID}.`);
}
data["browser_id"] = crawlID;
data["success"] = true;
storageController.send(JSON.stringify(["meta_information", data]));
visitID = null;
break;
default:
// Just making sure that it's a valid number before logging
_visitID = parseInt(data, 10);
logDebug("Setting visit_id the legacy way");
visitID = _visitID
} // Connect to MPLogger for extension info/debug/error logging
if (logAddress != null) {
logAggregator = new socket.SendingSocket();
const rv = await logAggregator.connect(logAddress[0], logAddress[1]);
console.log("logSocket started?", rv);
}
} // Connect to databases for saving data
export let open = async function(storageControllerAddress, logAddress, curr_crawlID) { if (storageControllerAddress != null) {
if (storageControllerAddress == null && logAddress == null && curr_crawlID === 0) { storageController = new socket.SendingSocket();
console.log("Debugging, everything will output to console"); const rv = await storageController.connect(
debugging = true; storageControllerAddress[0],
return; storageControllerAddress[1],
} );
crawlID = curr_crawlID; console.log("StorageController started?", rv);
}
console.log("Opening socket connections..."); // Listen for incoming urls as visit ids
listeningSocket = new socket.ListeningSocket(listeningSocketCallback);
// Connect to MPLogger for extension info/debug/error logging console.log("Starting socket listening for incoming connections.");
if (logAddress != null) { await listeningSocket.startListening().then(() => {
logAggregator = new socket.SendingSocket(); (browser as any).profileDirIO.writeFile(
let rv = await logAggregator.connect(logAddress[0], logAddress[1]); "extension_port.txt",
console.log("logSocket started?", rv) `${listeningSocket.port}`,
} );
});
// Connect to databases for saving data
if (storageControllerAddress != null) {
storageController = new socket.SendingSocket();
let rv = await storageController.connect(storageControllerAddress[0], storageControllerAddress[1]);
console.log("StorageController started?",rv);
}
// Listen for incoming urls as visit ids
listeningSocket = new socket.ListeningSocket(listeningSocketCallback);
console.log("Starting socket listening for incoming connections.");
await listeningSocket.startListening().then(() => {
(browser as any).profileDirIO.writeFile("extension_port.txt", `${listeningSocket.port}`);
});
}; };
export let close = function() { export const close = function () {
if (storageController != null) { if (storageController != null) {
storageController.close(); storageController.close();
} }
if (logAggregator != null) { if (logAggregator != null) {
logAggregator.close(); logAggregator.close();
} }
}; };
let makeLogJSON = function(lvl, msg) { const makeLogJSON = function (lvl, msg) {
var log_json = { const log_json = {
'name': 'Extension-Logger', name: "Extension-Logger",
'level': lvl, level: lvl,
'pathname': 'FirefoxExtension', pathname: "FirefoxExtension",
'lineno': 1, lineno: 1,
'msg': escapeString(msg), msg: escapeString(msg),
'args': null, args: null,
'exc_info': null, exc_info: null,
'func': null func: null,
} };
return log_json; return log_json;
}
export let logInfo = function(msg) {
// Always log to browser console
console.log(msg);
if (debugging) {
return;
}
// Log level INFO == 20 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(20, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)]));
}; };
export let logDebug = function(msg) { export const logInfo = function (msg) {
// Always log to browser console // Always log to browser console
console.log(msg); console.log(msg);
if (debugging) { if (debugging) {
return; return;
} }
// Log level DEBUG == 10 (https://docs.python.org/2/library/logging.html#logging-levels) // Log level INFO == 20 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(10, msg); const log_json = makeLogJSON(20, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)])); logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
}; };
export let logWarn = function(msg) { export const logDebug = function (msg) {
// Always log to browser console // Always log to browser console
console.warn(msg); console.log(msg);
if (debugging) { if (debugging) {
return; return;
} }
// Log level WARN == 30 (https://docs.python.org/2/library/logging.html#logging-levels) // Log level DEBUG == 10 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(30, msg); const log_json = makeLogJSON(10, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)])); logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
}; };
export let logError = function(msg) { export const logWarn = function (msg) {
// Always log to browser console // Always log to browser console
console.error(msg); console.warn(msg);
if (debugging) { if (debugging) {
return; return;
} }
// Log level INFO == 40 (https://docs.python.org/2/library/logging.html#logging-levels) // Log level WARN == 30 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(40, msg); const log_json = makeLogJSON(30, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)])); logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
}; };
export let logCritical = function(msg) { export const logError = function (msg) {
// Always log to browser console // Always log to browser console
console.error(msg); console.error(msg);
if (debugging) { if (debugging) {
return; return;
} }
// Log level CRITICAL == 50 (https://docs.python.org/2/library/logging.html#logging-levels) // Log level INFO == 40 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(50, msg); const log_json = makeLogJSON(40, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)])); logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
}; };
export let dataReceiver = { export const logCritical = function (msg) {
saveRecord(a, b) { // Always log to browser console
console.log(a,b); console.error(msg);
},
if (debugging) {
return;
}
// Log level CRITICAL == 50 (https://docs.python.org/2/library/logging.html#logging-levels)
const log_json = makeLogJSON(50, msg);
logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
}; };
export let saveRecord = function(instrument, record) { export const dataReceiver = {
record["visit_id"] = visitID; saveRecord(a, b) {
console.log(a, b);
},
};
if (!visitID && !debugging) { export const saveRecord = function (instrument, record) {
// Navigations to about:blank can be triggered by OpenWPM. We drop those. record.visit_id = visitID;
if(instrument === 'navigations' && record['url'] === 'about:blank') {
logDebug('Extension-' + crawlID + ' : Dropping navigation to about:blank in intermediate period');
return;
}
logWarn(`Extension-${crawlID} : visitID is null while attempting to insert into table ${instrument}\n` +
JSON.stringify(record));
record["visit_id"] = -1;
}
// send to console if debugging if (!visitID && !debugging) {
if (debugging) { // Navigations to about:blank can be triggered by OpenWPM. We drop those.
console.log("EXTENSION", instrument, record); if (instrument === "navigations" && record.url === "about:blank") {
logDebug(
"Extension-" +
crawlID +
" : Dropping navigation to about:blank in intermediate period",
);
return; return;
} }
storageController.send(JSON.stringify([instrument, record])); logWarn(
`Extension-${crawlID} : visitID is null while attempting to insert into table ${instrument}\n` +
JSON.stringify(record),
);
record.visit_id = -1;
}
// send to console if debugging
if (debugging) {
console.log("EXTENSION", instrument, record);
return;
}
storageController.send(JSON.stringify([instrument, record]));
}; };
// Stub for now // Stub for now
export let saveContent = async function(content, contentHash) { export const saveContent = async function (content, contentHash) {
// Send page content to the data aggregator // Send page content to the data aggregator
// deduplicated by contentHash in a levelDB database // deduplicated by contentHash in a levelDB database
if (debugging) { if (debugging) {
console.log("LDB contentHash:",contentHash,"with length",content.length); console.log("LDB contentHash:", contentHash, "with length", content.length);
return; return;
} }
// Since the content might not be a valid utf8 string and it needs to be // Since the content might not be a valid utf8 string and it needs to be
// json encoded later, it is encoded using base64 first. // json encoded later, it is encoded using base64 first.
const b64 = Uint8ToBase64(content); const b64 = Uint8ToBase64(content);
storageController.send(JSON.stringify(['page_content', [b64, contentHash]])); storageController.send(JSON.stringify(["page_content", [b64, contentHash]]));
}; };
function encode_utf8(s) { function encode_utf8(s) {
@ -212,12 +230,12 @@ function encode_utf8(s) {
// Base64 encoding, found on: // Base64 encoding, found on:
// https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/25644409#25644409 // https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/25644409#25644409
function Uint8ToBase64(u8Arr){ function Uint8ToBase64(u8Arr) {
var CHUNK_SIZE = 0x8000; //arbitrary number const CHUNK_SIZE = 0x8000; // arbitrary number
var index = 0; let index = 0;
var length = u8Arr.length; const length = u8Arr.length;
var result = ''; let result = "";
var slice; let slice;
while (index < length) { while (index < length) {
slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length)); slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
result += String.fromCharCode.apply(null, slice); result += String.fromCharCode.apply(null, slice);
@ -226,14 +244,13 @@ function Uint8ToBase64(u8Arr){
return btoa(result); return btoa(result);
} }
export let escapeString = function(string) { export const escapeString = function (string) {
// Convert to string if necessary // Convert to string if necessary
if(typeof string != "string") if (typeof string !== "string") string = "" + string;
string = "" + string;
return encode_utf8(string); return encode_utf8(string);
}; };
export let boolToInt = function(bool) { export const boolToInt = function (bool) {
return bool ? 1 : 0; return bool ? 1 : 0;
}; };

Просмотреть файл

@ -1,4 +1,5 @@
let DataReceiver = { /* eslint-disable max-classes-per-file */
const DataReceiver = {
callbacks: new Map(), callbacks: new Map(),
onDataReceived: (aSocketId, aData, aJSON) => { onDataReceived: (aSocketId, aData, aJSON) => {
if (!DataReceiver.callbacks.has(aSocketId)) { if (!DataReceiver.callbacks.has(aSocketId)) {
@ -11,28 +12,27 @@ let DataReceiver = {
}, },
}; };
(browser as any).sockets.onDataReceived.addListener(DataReceiver.onDataReceived); (browser as any).sockets.onDataReceived.addListener(
DataReceiver.onDataReceived,
);
export class ListeningSocket { export class ListeningSocket {
callback: any; callback: any;
port: any; port: any;
constructor(callback) { constructor(callback) {
this.callback = callback this.callback = callback;
} }
async startListening() { async startListening() {
this.port = await (browser as any).sockets.createServerSocket(); this.port = await (browser as any).sockets.createServerSocket();
DataReceiver.callbacks.set(this.port, this.callback); DataReceiver.callbacks.set(this.port, this.callback);
(browser as any).sockets.startListening(this.port); (browser as any).sockets.startListening(this.port);
console.log('Listening on port ' + this.port); console.log("Listening on port " + this.port);
} }
} }
export class SendingSocket { export class SendingSocket {
id: any; id: any;
constructor() {
}
async connect(host, port) { async connect(host, port) {
this.id = await (browser as any).sockets.createSendingSocket(); this.id = await (browser as any).sockets.createSendingSocket();
@ -40,12 +40,12 @@ export class SendingSocket {
console.log(`Connected to ${host}:${port}`); console.log(`Connected to ${host}:${port}`);
} }
send(aData, aJSON=true) { send(aData, aJSON = true) {
try { try {
(browser as any).sockets.sendData(this.id, aData, !!aJSON); (browser as any).sockets.sendData(this.id, aData, !!aJSON);
return true; return true;
} catch (err) { } catch (err) {
console.error(err,err.message); console.error(err, err.message);
return false; return false;
} }
} }
@ -54,4 +54,3 @@ export class SendingSocket {
(browser as any).sockets.close(this.id); (browser as any).sockets.close(this.id);
} }
} }

Просмотреть файл

@ -1,5 +1,3 @@
/* global require, module, __dirname */
const path = require("path"); const path = require("path");
module.exports = { module.exports = {