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.
https://github.com/typescript-eslint/tslint-to-eslint-config
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! 💖
This ESLint config aims to work with both
the JavaScript and the TypeScript parts of the
codebase.
*/
module.exports = {
"env": {
"node": true,
"es6": true
},
"extends": [
"prettier"
root: true,
env: {
node: true,
es6: true,
webextensions: true,
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",
"parserOptions": {
"project": "tsconfig.json",
"sourceType": "module"
},
"plugins": [
"eslint-plugin-prefer-arrow",
"eslint-plugin-import",
"eslint-plugin-jsdoc",
"eslint-plugin-unicorn",
"@typescript-eslint",
],
"rules": {
"unicorn/prefer-ternary": "error",
"use-isnan": "error",
"valid-typeof": "off",
},
overrides: [
{
files: ["*.ts"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/adjacent-overload-signatures": "error",
"@typescript-eslint/array-type": [
"error",
{
"default": "array"
}
"error",
{
default: "array",
},
],
"@typescript-eslint/ban-types": [
"error",
{
"types": {
"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`."
},
"Boolean": {
"message": "Avoid using the `Boolean` type. Did you mean `boolean`?"
},
"Number": {
"message": "Avoid using the `Number` type. Did you mean `number`?"
},
"String": {
"message": "Avoid using the `String` type. Did you mean `string`?"
},
"Symbol": {
"message": "Avoid using the `Symbol` type. Did you mean `symbol`?"
}
}
}
"error",
{
types: {
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`.",
},
Boolean: {
message:
"Avoid using the `Boolean` type. Did you mean `boolean`?",
},
Number: {
message:
"Avoid using the `Number` type. Did you mean `number`?",
},
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/dot-notation": "error",
"@typescript-eslint/indent": "off",
"@typescript-eslint/member-delimiter-style": [
"off",
{
"multiline": {
"delimiter": "none",
"requireLast": true
},
"singleline": {
"delimiter": "semi",
"requireLast": false
}
}
"off",
{
multiline: {
delimiter: "none",
requireLast: true,
},
singleline: {
delimiter: "semi",
requireLast: false,
},
},
],
"@typescript-eslint/no-empty-function": "warn",
"@typescript-eslint/no-empty-interface": "error",
@ -87,10 +166,10 @@ module.exports = {
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-parameter-properties": "off",
"@typescript-eslint/no-shadow": [
"off",
{
"hoist": "all"
}
"off",
{
hoist: "all",
},
],
"@typescript-eslint/no-this-alias": "error",
"@typescript-eslint/no-unused-expressions": "warn",
@ -100,112 +179,24 @@ module.exports = {
"@typescript-eslint/prefer-function-type": "error",
"@typescript-eslint/prefer-namespace-keyword": "error",
"@typescript-eslint/quotes": "off",
"@typescript-eslint/semi": [
"off",
null
],
"@typescript-eslint/semi": ["off", null],
"@typescript-eslint/triple-slash-reference": [
"error",
{
"path": "always",
"types": "prefer-import",
"lib": "always"
}
"error",
{
path: "always",
types: "prefer-import",
lib: "always",
},
],
"@typescript-eslint/type-annotation-spacing": "off",
"@typescript-eslint/unified-signatures": "error",
"arrow-parens": [
"off",
"always"
"no-undef": "off",
"no-unused-vars": "off",
"@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") {
// Url of the top-level document itself.
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.
// If frameAncestors == [], request comes from the top-level-document.
url = details.frameAncestors.length

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

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

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

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

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

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

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

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

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

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

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

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

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

@ -7,203 +7,221 @@ let storageController = null;
let logAggregator = 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) => {
//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
console.log("Opening socket connections...");
}
// 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);
}
}
export let 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;
// Connect to databases for saving data
if (storageControllerAddress != null) {
storageController = new socket.SendingSocket();
const rv = await storageController.connect(
storageControllerAddress[0],
storageControllerAddress[1],
);
console.log("StorageController started?", rv);
}
console.log("Opening socket connections...");
// Connect to MPLogger for extension info/debug/error logging
if (logAddress != null) {
logAggregator = new socket.SendingSocket();
let rv = await logAggregator.connect(logAddress[0], logAddress[1]);
console.log("logSocket started?", rv)
}
// 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}`);
});
// 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() {
if (storageController != null) {
storageController.close();
}
if (logAggregator != null) {
logAggregator.close();
}
export const close = function () {
if (storageController != null) {
storageController.close();
}
if (logAggregator != null) {
logAggregator.close();
}
};
let makeLogJSON = function(lvl, msg) {
var log_json = {
'name': 'Extension-Logger',
'level': lvl,
'pathname': 'FirefoxExtension',
'lineno': 1,
'msg': escapeString(msg),
'args': null,
'exc_info': null,
'func': null
}
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)]));
const makeLogJSON = function (lvl, msg) {
const log_json = {
name: "Extension-Logger",
level: lvl,
pathname: "FirefoxExtension",
lineno: 1,
msg: escapeString(msg),
args: null,
exc_info: null,
func: null,
};
return log_json;
};
export let logDebug = function(msg) {
// Always log to browser console
console.log(msg);
export const logInfo = function (msg) {
// Always log to browser console
console.log(msg);
if (debugging) {
return;
}
if (debugging) {
return;
}
// Log level DEBUG == 10 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(10, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)]));
// Log level INFO == 20 (https://docs.python.org/2/library/logging.html#logging-levels)
const log_json = makeLogJSON(20, msg);
logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
};
export let logWarn = function(msg) {
// Always log to browser console
console.warn(msg);
export const logDebug = function (msg) {
// Always log to browser console
console.log(msg);
if (debugging) {
return;
}
if (debugging) {
return;
}
// Log level WARN == 30 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(30, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)]));
// Log level DEBUG == 10 (https://docs.python.org/2/library/logging.html#logging-levels)
const log_json = makeLogJSON(10, msg);
logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
};
export let logError = function(msg) {
// Always log to browser console
console.error(msg);
export const logWarn = function (msg) {
// Always log to browser console
console.warn(msg);
if (debugging) {
return;
}
if (debugging) {
return;
}
// Log level INFO == 40 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(40, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)]));
// Log level WARN == 30 (https://docs.python.org/2/library/logging.html#logging-levels)
const log_json = makeLogJSON(30, msg);
logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
};
export let logCritical = function(msg) {
// Always log to browser console
console.error(msg);
export const logError = function (msg) {
// Always log to browser console
console.error(msg);
if (debugging) {
return;
}
if (debugging) {
return;
}
// Log level CRITICAL == 50 (https://docs.python.org/2/library/logging.html#logging-levels)
var log_json = makeLogJSON(50, msg);
logAggregator.send(JSON.stringify(['EXT', JSON.stringify(log_json)]));
// Log level INFO == 40 (https://docs.python.org/2/library/logging.html#logging-levels)
const log_json = makeLogJSON(40, msg);
logAggregator.send(JSON.stringify(["EXT", JSON.stringify(log_json)]));
};
export let dataReceiver = {
saveRecord(a, b) {
console.log(a,b);
},
export const logCritical = function (msg) {
// Always log to browser console
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) {
record["visit_id"] = visitID;
export const dataReceiver = {
saveRecord(a, b) {
console.log(a, b);
},
};
if (!visitID && !debugging) {
// Navigations to about:blank can be triggered by OpenWPM. We drop those.
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;
}
export const saveRecord = function (instrument, record) {
record.visit_id = visitID;
// send to console if debugging
if (debugging) {
console.log("EXTENSION", instrument, record);
if (!visitID && !debugging) {
// Navigations to about:blank can be triggered by OpenWPM. We drop those.
if (instrument === "navigations" && record.url === "about:blank") {
logDebug(
"Extension-" +
crawlID +
" : Dropping navigation to about:blank in intermediate period",
);
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
export let saveContent = async function(content, contentHash) {
export const saveContent = async function (content, contentHash) {
// Send page content to the data aggregator
// deduplicated by contentHash in a levelDB database
if (debugging) {
console.log("LDB contentHash:",contentHash,"with length",content.length);
console.log("LDB contentHash:", contentHash, "with length", content.length);
return;
}
// Since the content might not be a valid utf8 string and it needs to be
// json encoded later, it is encoded using base64 first.
const b64 = Uint8ToBase64(content);
storageController.send(JSON.stringify(['page_content', [b64, contentHash]]));
storageController.send(JSON.stringify(["page_content", [b64, contentHash]]));
};
function encode_utf8(s) {
@ -212,12 +230,12 @@ function encode_utf8(s) {
// Base64 encoding, found on:
// https://stackoverflow.com/questions/12710001/how-to-convert-uint8-array-to-base64-encoded-string/25644409#25644409
function Uint8ToBase64(u8Arr){
var CHUNK_SIZE = 0x8000; //arbitrary number
var index = 0;
var length = u8Arr.length;
var result = '';
var slice;
function Uint8ToBase64(u8Arr) {
const CHUNK_SIZE = 0x8000; // arbitrary number
let index = 0;
const length = u8Arr.length;
let result = "";
let slice;
while (index < length) {
slice = u8Arr.subarray(index, Math.min(index + CHUNK_SIZE, length));
result += String.fromCharCode.apply(null, slice);
@ -226,14 +244,13 @@ function Uint8ToBase64(u8Arr){
return btoa(result);
}
export let escapeString = function(string) {
// Convert to string if necessary
if(typeof string != "string")
string = "" + string;
export const escapeString = function (string) {
// Convert to string if necessary
if (typeof string !== "string") string = "" + string;
return encode_utf8(string);
return encode_utf8(string);
};
export let boolToInt = function(bool) {
return bool ? 1 : 0;
export const boolToInt = function (bool) {
return bool ? 1 : 0;
};

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

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

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

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