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
1cfca1a5a60a3dac6275ffc6562003a385f6e4f2
parent
4c4576a713158d9bbad11f211b69ce9194d5539f
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-12-07 23:28
domain patterns

Example: treat all subdomains of example.com as a single domain:

    "_patterns": ["*.example.com"]

Diffstat

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

1 files changed, 29 insertions, 7 deletions


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

@@ -17,8 +17,20 @@ var STORAGE_AREAS = {
   17    17     'recording': browser.storage.local,
   18    18 };
   19    19 
   20    -1 var getHostname = function(url) {
   -1    20 var glob = function(s, pattern) {
   -1    21     var p = pattern.split('*');
   -1    22     return s.startsWith(p[0]) && s.endsWith(p.at(-1));
   -1    23 };
   -1    24 
   -1    25 var getHostname = function(url, patterns) {
   21    26     var u = new URL(url);
   -1    27 
   -1    28     for (var pattern of patterns) {
   -1    29         if (glob(u.hostname, pattern)) {
   -1    30             return pattern;
   -1    31         }
   -1    32     }
   -1    33 
   22    34     return u.hostname;
   23    35 };
   24    36 
@@ -66,6 +78,11 @@ var setRule = async function(context, hostname, type, rule) {
   66    78     });
   67    79 };
   68    80 
   -1    81 var getPatterns = async function() {
   -1    82     var savedRules = await storageGet('savedRules');
   -1    83     return savedRules._patterns || [];
   -1    84 };
   -1    85 
   69    86 var getRules = async function(context) {
   70    87     var [rules, savedRules] = await Promise.all([
   71    88         storageGet('rules'),
@@ -116,8 +133,11 @@ var getCurrentTab = async function() {
  116   133 
  117   134 browser.runtime.onMessage.addListener(async (msg, sender) => {
  118   135     if (msg.type === 'get') {
  119    -1         const tab = await getCurrentTab();
  120    -1         const context = getHostname(tab.url);
   -1   136         const [tab, patterns] = await Promise.all([
   -1   137             getCurrentTab(),
   -1   138             getPatterns(),
   -1   139         ]);
   -1   140         const context = getHostname(tab.url, patterns);
  121   141         const [rules, requests, recording] = await Promise.all([
  122   142             getRules(context),
  123   143             storageGet('requests'),
@@ -172,12 +192,13 @@ browser.webNavigation.onBeforeNavigate.addListener(async details => {
  172   192 });
  173   193 
  174   194 browser.webRequest.onBeforeSendHeaders.addListener(async details => {
  175    -1     var context = getHostname(details.documentUrl || details.url);
   -1   195     var patterns = await getPatterns();
   -1   196     var context = getHostname(details.documentUrl || details.url, patterns);
  176   197     if (details.frameAncestors && details.frameAncestors.length) {
  177   198         var last = details.frameAncestors.length - 1;
  178    -1         context = getHostname(details.frameAncestors[last].url);
   -1   199         context = getHostname(details.frameAncestors[last].url, patterns);
  179   200     }
  180    -1     var hostname = getHostname(details.url);
   -1   201     var hostname = getHostname(details.url, patterns);
  181   202     var type = shared.TYPE_MAP[details.type] || 'other';
  182   203 
  183   204     var promises = [
@@ -215,7 +236,8 @@ browser.webRequest.onBeforeSendHeaders.addListener(async details => {
  215   236 }, {urls: ['<all_urls>']}, ['blocking', 'requestHeaders']);
  216   237 
  217   238 browser.webRequest.onHeadersReceived.addListener(async details => {
  218    -1     var context = getHostname(details.url);
   -1   239     var patterns = await getPatterns();
   -1   240     var context = getHostname(details.url, patterns);
  219   241     var [rules, recording] = await Promise.all([
  220   242         getRules(context),
  221   243         storageGet('recording'),