xiMatrix

filter net requests according to source, destination and type  https://addons.mozilla.org/firefox/addon/ximatrix/
git clone https://git.ce9e.org/xiMatrix.git

commit
278c272b0ae3b9cd5e7c8f1af4e942e59a1aa219
parent
0d34234c977690e3ca07ddba7f3c120ce82ff3bd
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-11-27 09:46
refactor storage access

Diffstat

M src/bg.js 43 ++++++++++++++++++++++++++++---------------

1 files changed, 28 insertions, 15 deletions


diff --git a/src/bg.js b/src/bg.js

@@ -1,5 +1,10 @@
    1     1 /* global browser shared */
    2     2 
   -1     3 var STORAGE_DEFAULTS = {
   -1     4     'rules': {},
   -1     5     'requests': {},
   -1     6 };
   -1     7 
    3     8 var recording = false;
    4     9 
    5    10 var getHostname = function(url) {
@@ -7,16 +12,24 @@ var getHostname = function(url) {
    7    12     return u.hostname;
    8    13 };
    9    14 
   10    -1 var getRules = function() {
   11    -1     return browser.storage.local.get('rules').then(data => data.rules || {});
   -1    15 var storageGet = function(key) {
   -1    16     return browser.storage.local.get(key).then(data => {
   -1    17         return data[key] ?? STORAGE_DEFAULTS[key];
   -1    18     });
   12    19 };
   13    20 
   14    -1 var getRequests = function() {
   15    -1     return browser.storage.local.get('requests').then(data => data.requests || {});
   -1    21 var storageChange = function(key, fn) {
   -1    22     // even though storage access is async, this is safe as long as the code
   -1    23     // in between is sync.
   -1    24     return storageGet(key).then(oldValue => {
   -1    25         var data = {};
   -1    26         data[key] = fn(oldValue);
   -1    27         return browser.storage.local.set(data);
   -1    28     });
   16    29 };
   17    30 
   18    31 var setRule = function(context, hostname, type, rule) {
   19    -1     return getRules().then(rules => {
   -1    32     return storageChange('rules', rules => {
   20    33         if (hostname === 'first-party') {
   21    34             context = '*';
   22    35         }
@@ -37,7 +50,7 @@ var setRule = function(context, hostname, type, rule) {
   37    50                 delete rules[context];
   38    51             }
   39    52         }
   40    -1         return browser.storage.local.set({'rules': rules});
   -1    53         return rules;
   41    54     });
   42    55 };
   43    56 
@@ -52,7 +65,7 @@ var pushRequest = function(tabId, hostname, type) {
   52    65     if (!recording) {
   53    66         return Promise.resolve();
   54    67     }
   55    -1     return getRequests().then(requests => {
   -1    68     return storageChange('requests', requests => {
   56    69         if (!requests[tabId]) {
   57    70             requests[tabId] = {};
   58    71         }
@@ -63,16 +76,16 @@ var pushRequest = function(tabId, hostname, type) {
   63    76             requests[tabId][hostname][type] = 0;
   64    77         }
   65    78         requests[tabId][hostname][type] += 1;
   66    -1         return browser.storage.local.set({'requests': requests});
   -1    79         return requests;
   67    80     });
   68    81 };
   69    82 
   70    83 var clearRequests = function(tabId) {
   71    -1     return getRequests().then(requests => {
   -1    84     return storageChange('requests', requests => {
   72    85         if (requests[tabId]) {
   73    86             delete requests[tabId];
   74    87         }
   75    -1         return browser.storage.local.set({'requests': requests});
   -1    88         return requests;
   76    89     });
   77    90 };
   78    91 
@@ -87,8 +100,8 @@ browser.runtime.onMessage.addListener((msg, sender) => {
   87   100     if (msg.type === 'get') {
   88   101         return Promise.all([
   89   102             getCurrentTab(),
   90    -1             getRules(),
   91    -1             getRequests(),
   -1   103             storageGet('rules'),
   -1   104             storageGet('requests'),
   92   105         ]).then(([tab, rules, requests]) => {
   93   106             var context = msg.data || getHostname(tab.url);
   94   107             return {
@@ -104,7 +117,7 @@ browser.runtime.onMessage.addListener((msg, sender) => {
  104   117             msg.data.hostname,
  105   118             msg.data.type,
  106   119             msg.data.value,
  107    -1         ).then(getRules).then(rules => {
   -1   120         ).then(() => storageGet('rules')).then(rules => {
  108   121             return restrictRules(rules, msg.data.context);
  109   122         });
  110   123     } else if (msg.type === 'securitypolicyviolation') {
@@ -135,7 +148,7 @@ browser.webRequest.onBeforeRequest.addListener(details => {
  135   148 
  136   149     return Promise.all([
  137   150         pushRequest(details.tabId, hostname, type),
  138    -1         getRules(),
   -1   151         storageGet('rules'),
  139   152     ]).then(([_, rules]) => {
  140   153         if (!shared.shouldAllow(rules, context, hostname, type)) {
  141   154             if (details.type === 'sub_frame') {
@@ -149,7 +162,7 @@ browser.webRequest.onBeforeRequest.addListener(details => {
  149   162 }, {urls: ['<all_urls>']}, ['blocking']);
  150   163 
  151   164 browser.webRequest.onHeadersReceived.addListener(function(details) {
  152    -1     return getRules().then(rules => {
   -1   165     return storageGet('rules').then(rules => {
  153   166         var context = getHostname(details.url);
  154   167 
  155   168         var csp = (type, value) => {