added permissions for website

This commit is contained in:
2024-11-04 11:55:14 -05:00
parent 5c845ffe61
commit 014e4c8b7b
19 changed files with 507 additions and 71 deletions
+29 -16
View File
@@ -1,8 +1,9 @@
import { StaticNetFilteringEngine } from '@gorhill/ubo-core';
import fs from 'fs/promises';
import fsPromise from 'fs/promises';
import { checkInternetConnectivity } from '../utils/misc.js';
import loggermod from '../utils/logger.cjs';
const { logger } = loggermod;
const { logger } = loggermod,
adblockcachepath = 'cache/adblock';
const blocklists = [
@@ -26,33 +27,42 @@ const blocklists = [
async function fetchList(url) {
return fetch(url).then(r => {
return r.text();
}).then(raw => {
return { raw };
}).catch(reason => {
logger.error(reason);
});
return fetch(url)
.then(r => r.text())
.then(raw => ({ raw }))
.catch(reason => logger.error(reason));
}
const snfe = await StaticNetFilteringEngine.create();
// const rsf = await fetch('https://api.github.com/repos/uBlockOrigin/uAssets/contents/filters'),
// safeLists = (await rsf.json()).map(o => o.download_url);
const apiUrl = `https://api.github.com/repos/uBlockOrigin/uAssets/contents/filters`;
const getFilesFromDirectory = async () => {
try {
const response = await fetch(apiUrl);
const data = await response.json();
// check if the response data is an array (list of files)
if (Array.isArray(data)) return data.filter(file => file.type === 'file').map(o => o.download_url);
else console.log('No files found or the directory path is incorrect.');
} catch (error) {
console.error('Error fetching files:', error.message);
}
};
const pathToSelfie = 'cache/selfie.txt';
if (!(await import('fs')).existsSync(adblockcachepath)) await fsPromise.mkdir(adblockcachepath);
// Up to date serialization data (aka selfie) available?
let selfie;
const ageInDays = await fs.stat(pathToSelfie).then(stat => {
const ageInDays = await fsPromise.stat(pathToSelfie).then(stat => {
const fileDate = new Date(stat.mtime);
return (Date.now() - fileDate.getTime()) / (7 * 24 * 60 * 60);
}).catch(() => Number.MAX_SAFE_INTEGER);
// Use a selfie if available and not older than 7 days
if (ageInDays <= 7) {
selfie = await fs.readFile(pathToSelfie, { encoding: 'utf8' })
selfie = await fsPromise.readFile(pathToSelfie, { encoding: 'utf8' })
.then(data => typeof data === 'string' && data !== '' && data)
.catch(() => { });
if (typeof selfie === 'string') {
@@ -63,11 +73,14 @@ if (ageInDays <= 7) {
// Fetch filter lists if no up to date selfie available
if (!selfie && (await checkInternetConnectivity())) {
logger.info(`Fetching lists...`);
await snfe.useLists(blocklists.map(fetchList).filter(o => o));
const totalLists = new Set(blocklists.concat(...(await getFilesFromDirectory())));
await snfe.useLists([...totalLists].map(fetchList).filter(o => o));
logger.info(`using ${totalLists.size} adblock lists`)
const selfie = await snfe.serialize();
fs.mkdir('cache', { recursive: true });
await fs.writeFile(pathToSelfie, selfie);
fsPromise.mkdir('cache', { recursive: true });
await fsPromise.writeFile(pathToSelfie, selfie);
}
+26 -2
View File
@@ -52,9 +52,33 @@ async function setupRedis() {
.connect();
// clear history on browser boot
client.flushDb();
let cursor = 0;
do {
const result = await client.scan(cursor, { MATCH: `searchHistory:*`, COUNT: 100 });
cursor = result.cursor || 0;
const keys = result.keys;
if (keys.length > 0) await client.del(...keys);
} while (cursor !== 0);
// await client.flushDb();
logger.info('Redis Client Connected!');
}
module.exports = { setupRedis, redisclient: client, getHistory, addHistory, displayHistory, quitRedis };
/**
*
* @returns {Promise<import('redis').RedisClientType>}
*/
const getClient = () => {
return new Promise(resolve => {
const intid = setInterval(() => {
if (!client) return;
resolve(client);
clearInterval(intid);
}, 100);
})
}
module.exports = { setupRedis, redisclient: getClient, getHistory, addHistory, displayHistory, quitRedis };
+7 -3
View File
@@ -6,11 +6,12 @@ import { getSavedTabs, loadTabs } from '../utils/clearCache.js';
import flushCookies from '../utils/cookies.js';
import { askUserQuestion } from '../utils/dialogue.js';
import ipcinit from '../utils/ipc.js';
import { checkInternetConnectivity } from '../utils/misc.js';
import { checkInternetConnectivity, isValidURL } from '../utils/misc.js';
import { findPath } from '../utils/paths.js';
import { createWebview, handleWebViewInit } from '../utils/webviewHelpers.js';
import blocked from './adblock.js';
const { setupRedis, quitRedis } = await import('../serverJS/history.cjs');
const { setupRedis, quitRedis, redisclient } = await import('../serverJS/history.cjs');
const { logger } = loggermod;
export {
@@ -28,5 +29,8 @@ export {
createWebview,
handleWebViewInit,
setupRedis,
quitRedis
quitRedis,
redisclient,
blocked,
isValidURL
};
+8 -15
View File
@@ -31,7 +31,14 @@ export default async function intercept(request, uid) {
try {
const u = new URL(request.url);
if (u.protocol === 'file:' || u.hostname.startsWith('ion-local')) {
if (u.hostname.startsWith('ion-adblock')) {
const links = await request.json();
console.log(links);
const r = links.map(l => ([l, blocked(l)]));
return new Response(JSON.stringify(r));
}
else if (u.protocol === 'file:' || u.hostname.startsWith('ion-local')) {
const filePath = await findPath(u.pathname.split('/')?.at(-1), true);
if (!filePath || !fs.existsSync(filePath)) return new Response(`file"${filePath}" not found!`, { status: 404 });
@@ -117,20 +124,6 @@ export default async function intercept(request, uid) {
}
return r;
/*
REMOVED BECAUSE IT'S TOO EXPENSIVE (SIGKILL-ed)
// https://accounts.google.com/v3/signin/_/AccountsSignInUi/browserinfo?f.sid=3210847140573431127&bl=boq_identityfrontendauthuiserver_20241015.01_p0&hl=en&_reqid=139420&rt=j
if (request.url === 'https://www.youtube.com/' || skip.includes(u.hostname)) return net.fetch(request);
let newURL = request.url;
if (u.hostname.includes('duckduckgo.com')) newURL += (u.search) ? '&kae=d&kp=-2' : '?kae=d';
else if (u.hostname.includes('google.com')) newURL += (u.search) ? '&safe=off&&pccc=1' : '?pccc=1';
const r = await net.fetch(request);
return r;
*/
}
}
catch (err) {
+1
View File
@@ -15,6 +15,7 @@ export default async function setUpShortcuts(uid) {
// getCurrentWindow().isFocused() ? getCurrentTab()?.toggleDevTools() : null
});
globalShortcut.register('Control+H', () => getCurrentTab()?.webContents.executeJavaScript('window.electronAPI.displayHistory()'));
globalShortcut.register('Control+P', () => getCurrentTab()?.webContents.print());
// zoom
globalShortcut.register('Control+=', () => changeZoom(getCurrentTab(), true));