1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-04 20:28:52 +01:00
CanvasBlocker/lib/randomSupplies.js

272 lines
7.0 KiB
JavaScript
Raw Normal View History

2016-08-06 19:17:36 +02:00
/* 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";
2019-11-28 01:26:35 +01:00
let scope;
2017-06-25 22:33:12 +02:00
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
2019-03-12 22:24:23 +01:00
scope = require.register("./randomSupplies", {});
2017-06-25 22:33:12 +02:00
}
const rngTemplate = {
getBitRng: function(length, window){
const rng = this.getRng(Math.ceil(length / 32), window);
let rnd = 0;
let mask = 0xffffffff * 2;
return function(value, i){
if (mask > 0xffffffff){
mask = 1;
rnd = rng(i / 32);
}
let bit = 1 * (!!(rnd & mask));
mask *= 2;
return bit;
};
},
getIndexRng: function(length, maxIndex, window){
const rng = this.getRng(length, window);
return function(i){
return Math.floor(rng(i) / 0xffffffff * maxIndex);
};
},
getValueRng: function(length, window){
const rng = this.getBitRng(length, window);
return function(value, i){
2019-11-28 01:26:35 +01:00
const rnd = rng(value, i);
// XOR the last bit to alter it... or not
return value ^ (rnd & 0x01);
};
},
getPixelRng: function(length, window, ignoredColors){
2019-11-28 01:26:35 +01:00
const rng = this.getValueRng(length, window);
// eslint-disable-next-line max-params
return function(r, g, b, a, i){
const index = String.fromCharCode(r, g, b, a);
if (ignoredColors[index]){
return [r, g, b, a];
}
2019-11-28 01:26:35 +01:00
const baseIndex = i * 4;
return [
rng(r, baseIndex + 0),
rng(g, baseIndex + 1),
rng(b, baseIndex + 2),
rng(a, baseIndex + 3)
];
};
}
};
const settings = require("./settings");
const logging = require("./logging");
const extension = require("./extension");
2017-06-25 22:33:12 +02:00
function getDomain(window){
if (settings.sharePersistentRndBetweenDomains){
return "shared://domain";
}
if (!window.location.href || window.location.href === "about:blank"){
if (window !== window.parent && window.parent){
return getDomain(window.parent);
}
else if (window.opener){
return getDomain(window.opener);
}
}
return window.location.host;
}
2019-11-28 01:26:35 +01:00
let persistentRnd = Object.create(null);
let cookieStoreId = false;
settings.onloaded(function(){
try {
let storedData = JSON.parse(
extension.inIncognitoContext?
settings.persistentIncognitoRndStorage:
settings.persistentRndStorage
);
2019-11-28 01:26:35 +01:00
for (let domain in storedData){
const value = storedData[domain];
if (
Array.isArray(value) &&
value.length === 128 &&
value.every(function(value){
return typeof value === "number" && value >= 0 && value < 256;
})
){
persistentRnd[domain] = new Uint8Array(value);
}
}
}
2019-11-28 01:26:35 +01:00
catch (error){
// JSON is not valid -> ignore it
}
});
const getPersistentRnd = (function(){
extension.message.on(function(data){
2017-06-25 22:33:12 +02:00
if (data["canvasBlocker-set-domain-rnd"]){
2019-11-28 01:26:35 +01:00
const {domain, incognito, rnd} = data["canvasBlocker-set-domain-rnd"];
if (incognito === extension.inIncognitoContext){
persistentRnd[domain] = new Uint8Array(rnd);
}
2017-06-25 22:33:12 +02:00
}
if (data["canvasBlocker-clear-domain-rnd"]){
persistentRnd = Object.create(null);
}
});
return function getPersistentRnd(window){
while (cookieStoreId === false){
logging.message("Starting synchronous request to wait for cookie store id.");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
2019-11-28 01:26:35 +01:00
catch (error){
logging.verbose("Error in XHR:", error);
}
}
2019-11-28 01:26:35 +01:00
const domain = cookieStoreId + getDomain(window);
2016-08-06 19:17:36 +02:00
if (!persistentRnd[domain]){
// create the (sub-)domains random numbers if not existing
2016-08-06 19:17:36 +02:00
persistentRnd[domain] = new Uint8Array(128);
window.crypto.getRandomValues(persistentRnd[domain]);
extension.message.send({
"canvasBlocker-new-domain-rnd": {
domain,
incognito: extension.inIncognitoContext,
rnd: Array.from(persistentRnd[domain])
}
});
2016-08-06 19:17:36 +02:00
}
return persistentRnd[domain];
2017-10-03 15:35:31 +02:00
};
}());
scope.persistent = Object.create(rngTemplate);
scope.persistent.name = "persistent";
scope.persistent.setDomainRnd = function(domain, rnd){
persistentRnd[domain] = new Uint8Array(rnd);
};
scope.persistent.setCookieStoreId = function(newCookieStoreId){
if (typeof newCookieStoreId === "string"){
cookieStoreId = (
newCookieStoreId !== "" &&
newCookieStoreId !== "firefox-default"
)? newCookieStoreId + "@": "";
}
};
scope.persistent.getRng = function(length, window){
2019-11-28 01:26:35 +01:00
const bitSet = new Uint32Array(getPersistentRnd(window).buffer);
const bitSetLength = bitSet.length;
return function(i){
return bitSet[i % bitSetLength];
};
};
scope.persistent.getBitRng = function(length, window){
2019-11-28 01:26:35 +01:00
const bitSet = getPersistentRnd(window);
return function(value, i){
// use the last 7 bits from the value for the index of the
// random number
2019-11-28 01:26:35 +01:00
const index = value & 0x7F;
2016-08-06 19:17:36 +02:00
// use the last 3 bits from the position and the first bit from
// from the value to get bit to use from the random number
2019-11-28 01:26:35 +01:00
const bitIndex = ((i & 0x03) << 1) | (value >>> 7);
// extract the bit
2019-11-28 01:26:35 +01:00
const bit = (bitSet[index] >>> bitIndex) & 0x01;
return bit;
};
2016-08-06 19:17:36 +02:00
};
scope.constant = Object.create(rngTemplate);
scope.constant.name = "constant";
scope.constant.getRng = function(length, window){
return scope.nonPersistent.getRng(length, window);
2017-08-07 17:43:57 +02:00
};
scope.constant.getPixelRng = (function(){
2019-11-28 01:26:35 +01:00
const colors = Object.create(null);
return function getConstantPixelRng(length, window, ignoredColors){
2019-11-28 01:26:35 +01:00
const rng = scope.nonPersistent.getValueRng(1024, window);
2016-08-06 19:17:36 +02:00
2019-12-24 00:32:50 +01:00
// eslint-disable-next-line max-params
2019-11-28 01:26:35 +01:00
return function(r, g, b, a, i){
const index = String.fromCharCode(r, g, b, a);
if (ignoredColors[index]){
return [r, g, b, a];
}
2019-11-28 01:26:35 +01:00
let color = colors[index];
if (!color){
color = [
rng(r, 0),
rng(g, 0),
rng(b, 0),
rng(a, 0)
];
colors[index] = color;
}
return color;
2017-10-03 15:35:31 +02:00
};
};
}());
scope.nonPersistent = Object.create(rngTemplate);
scope.nonPersistent.name = "nonPersistent";
scope.nonPersistent.getRng = function(length, window){
const maxLength = 0x4000;
2019-11-28 01:26:35 +01:00
let randomI = maxLength;
let randomNumbers = new Uint32Array(Math.min(maxLength, length));
return function(i){
if (randomI >= randomNumbers.length){
// refill the random number bucket if empty
randomI = 0;
if (length - i < maxLength){
randomNumbers = new Uint32Array(Math.max(1, length - i));
}
window.crypto.getRandomValues(randomNumbers);
}
2019-11-28 01:26:35 +01:00
const rnd = randomNumbers[randomI];
randomI += 1;
return rnd;
};
2016-08-06 19:17:36 +02:00
};
2017-11-24 17:06:43 +01:00
scope.white = {
name: "white",
getRng: function(){
return function(){
return 255;
};
},
getBitRng: function(){
return function(){
return 1;
};
},
getIndexRng: function(){
return function(){
return 0;
};
},
getValueRng: function(){
return this.getRng();
},
2017-11-24 17:06:43 +01:00
getPixelRng: function(){
return function(){
return [255, 255, 255, 255];
};
}
};
2017-10-03 15:35:31 +02:00
}());