1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-17 02:19:52 +02:00

Added preference to control rng type.

This commit is contained in:
kkapsner 2016-08-06 19:17:36 +02:00
parent 447ff54222
commit 1b2c4cb487
9 changed files with 233 additions and 126 deletions

View File

@ -14,7 +14,7 @@
if (context){
var nodeName;
try {
nodeName = context.nodeName
nodeName = context.nodeName;
}
catch (e){}
if (nodeName === "CANVAS"){

View File

@ -1,4 +1,4 @@
/* global console,exports */
/* global exports */
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this

View File

@ -5,10 +5,22 @@
(function(){
"use strict";
const {changedFunctions} = require("./modifiedAPI");
const {changedFunctions, setRandomSupply} = require("./modifiedAPI");
const randomSupplies = require("./randomSupplies");
setRandomSupply(randomSupplies.nonPersistent);
var apiNames = Object.keys(changedFunctions);
var undef;
function setRandomSupplyByType(type){
switch (type){
case "persistent":
setRandomSupply(randomSupplies.persistent);
break;
default:
setRandomSupply(randomSupplies.nonPersistent);
}
}
exports.setRandomSupplyByType = setRandomSupplyByType;
exports.intercept = function intercept({subject: window}, {check, ask, notify, prefs}){
apiNames.forEach(function(name){
var changedFunction = changedFunctions[name];
@ -34,8 +46,8 @@
case "allow":
return original;
case "fake":
setRandomSupplyByType(prefs("rng"));
if (changedFunction.fake){
return changedFunction.fake;
}
else {

View File

@ -1,20 +1,13 @@
/* jslint moz: true, bitwise: true */
/* jslint moz: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
const rnd = {};
var randomSupply = null;
function getFakeCanvas(window, original){
var domain = window.document.location.host;
if (!rnd[domain]){
rnd[domain] = new Uint8Array(128);
for (var i = 0; i < rnd[domain].length; i += 1){
rnd[domain][i] = Math.floor(Math.random() * 0xFF);
}
console.log("created new rnd for domain", domain);
}
var context = window.HTMLCanvasElement.prototype.getContext.call(original, "2d");
var imageData, data, source;
if (context){
@ -38,14 +31,10 @@
}
data = imageData.data;
var l = data.length;
var rng = randomSupply.getRng(l, window);
for (var i = 0; i < l; i += 1){
var value = source[i];
var index = value & 0x7F;
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
var bit = (rnd[domain][index] >>> bitIndex) & 0x01;
if (bitIndex % 2) console.log(rnd[domain][index], index, bitIndex, bit);
data[i] = value ^ bit;
data[i] = rng(source[i], i);
}
var canvas = original.cloneNode(true);
context = window.HTMLCanvasElement.prototype.getContext.call(canvas, "2d");
@ -55,6 +44,10 @@
function getWindow(canvas){
return canvas.ownerDocument.defaultView;
}
exports.setRandomSupply = function(supply){
randomSupply = supply;
};
// changed functions and their fakes
exports.changedFunctions = {
getContext: {
@ -111,7 +104,7 @@
imageData.data[i] = data[i];
}
return imageData;
}
};
}
},
readPixels: {

68
lib/randomSupplies.js Normal file
View File

@ -0,0 +1,68 @@
/* jslint moz: true, bitwise: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
const persistentRnd = {};
exports.persistent = {
getRng: function(length, window){
var domain = window.document.location.host;
if (!persistentRnd[domain]){
persistentRnd[domain] = new Uint8Array(128);
for (var i = 0; i < persistentRnd[domain].length; i += 1){
persistentRnd[domain][i] = Math.floor(Math.random() * 0xFF);
}
}
var bitSet = persistentRnd[domain];
return function(value, i){
var index = value & 0x7F;
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
var bit = (bitSet[index] >>> bitIndex) & 0x01;
return value ^ bit;
};
}
};
exports.nonPersistent = {
getRng: function(length, window){
var randomI = 65536;
// var randomOffset = 0;
var randomNumbers = new Uint8Array(Math.min(65536, length));
return function(value, i){
if (randomI >= randomNumbers.length){
randomI = 0;
// randomOffset += randomNumbers.length;
if (length - i < 65536){
randomNumbers = new Uint8Array(length - i);
}
window.crypto.getRandomValues(randomNumbers);
}
var rnd = randomNumbers[randomI];
if (value >= 0x80){
value = value ^ (rnd & 0x1F);
}
else if (value >= 0x40){
value = value ^ (rnd & 0x0F);
}
else if (value >= 0x20){
value = value ^ (rnd & 0x07);
}
else if (value >= 0x10){
value = value ^ (rnd & 0x03);
}
else if (value >= 0x08){
value = value ^ (rnd & 0x01);
}
// else if (value >= 0x04){
// value = value ^ (rnd * 0x00);
// }
randomI += 1;
return value;
};
}
};
}());

View File

@ -11,6 +11,7 @@
"askOnlyOnce_title": "Nur einmal nachfragen",
"blackList_description": "Domänen oder URLs, die die <canvas>-API niemals verwenden dürfen. Mehrere Einträge müssen durch ein Komma getrennt werden.",
"blackList_title": "Blacklist",
"blockMode_description": "",
"blockMode_options.allow everything": "alles erlauben",
"blockMode_options.allow only white list": "nur Einträge der Whitelist erlauben",
@ -21,13 +22,21 @@
"blockMode_options.block readout API": "Auslese-API blockieren",
"blockMode_options.fake readout API": "Auslese-API vortäuschen",
"blockMode_title": "Blockiermodus",
"maxFakeSize_description": "Maximale Vortäuschgröße",
"maxFakeSize_": "Canvas, die eine größe Fläche als die hier angegeben Zahl haben, werden nicht vorgetäuscht. (Null eingeben, um es zu deaktivieren.)",
"maxFakeSize_description": "Canvas, die eine größe Fläche als die hier angegeben Zahl haben, werden nicht vorgetäuscht. (Null eingeben, um es zu deaktivieren.)",
"maxFakeSize_title": "Maximale Vortäuschgröße",
"rng_description": "",
"rng_options.persistent": "persistent",
"rng_options.non persistent": "nicht persistent",
"rng_title": "Zufallszahlengenerator",
"disableNotifications": "Benachrichtigungen deaktivieren",
"displayCallingStack": "Aufrufestack anzeigen",
"displayFullURL": "URL anzeigen",
"enableStackList_description": "",
"enableStackList_title": "Dateispezifische Whitelist verwenden",
"fakedReadout": "Auslese vorgetäuscht auf {url}",
"ignoreList_description": "Domänen oder URLs, bei denen keine Benachrichtigung angezeigt werden. Mehrere Einträge müssen durch ein Komma getrennt werden.",
"ignoreList_title": "Ignorierliste",

View File

@ -11,6 +11,7 @@
"askOnlyOnce_title": "Ask only once",
"blackList_description": "Domains or URLs where the <canvas>-API should always be blocked. To add multiple entries, separate them by commas.",
"blackList_title": "Black list",
"blockMode_description": "",
"blockMode_options.allow everything": "allow everything",
"blockMode_options.allow only white list": "allow only white list",
@ -21,13 +22,21 @@
"blockMode_options.block readout API": "block readout API",
"blockMode_options.fake readout API": "fake readout API",
"blockMode_title": "Block mode",
"maxFakeSize_description": "Maximal fake size",
"maxFakeSize_": "Canvas with a bigger area than this number will not be faked. (Enter zero to disable.)",
"maxFakeSize_description": "Canvas with a bigger area than this number will not be faked. (Enter zero to disable.)",
"maxFakeSize_title": "Maximal fake size",
"rng_description": "",
"rng_options.persistent": "persistent",
"rng.non persistent": "non persistent",
"rng_title": "Random number generator",
"disableNotifications": "disable notifications",
"displayCallingStack": "display calling stack",
"displayFullURL": "display full URL",
"enableStackList_description": "",
"enableStackList_title": "Use file specific white list",
"fakedReadout": "Faked readout on {url}",
"ignoreList_description": "Domains or URLs where no notification will be shown. To add multiple entries, separate them by commas.",
"ignoreList_title": "Ignore list",

View File

@ -68,6 +68,22 @@
"type": "integer",
"value": 0
},
{
"name": "rng",
"title": "Random number generator",
"type": "menulist",
"value": "nonPersistent",
"options": [
{
"value": "nonPersistent",
"label": "non persistent"
},
{
"value": "persistent",
"label": "persistent"
}
]
},
{
"name": "askOnlyOnce",
"title": "Ask only once",