Skip to content

Commit

Permalink
fix: allow querying with both url and hostname when caching headers a…
Browse files Browse the repository at this point in the history
…nd cookies (#123)
  • Loading branch information
0xtsukino authored Jan 21, 2025
1 parent e3166d6 commit 3b98fc3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
47 changes: 38 additions & 9 deletions src/entries/Background/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Level } from 'level';
import type { RequestHistory } from './rpc';
import { PluginConfig, PluginMetadata, sha256 } from '../../utils/misc';
import { PluginConfig, PluginMetadata, sha256, urlify } from '../../utils/misc';
import mutex from './mutex';
import { minimatch } from 'minimatch';
const charwise = require('charwise');

export const db = new Level('./ext-db', {
Expand Down Expand Up @@ -325,18 +326,32 @@ export async function clearCookies(host: string) {
});
}

export async function getCookies(host: string, name: string) {
export async function getCookies(link: string, name: string) {
try {
const existing = await cookiesDb.sublevel(host).get(name);
const existing = await cookiesDb.sublevel(link).get(name);
return existing;
} catch (e) {
return null;
}
}

export async function getCookiesByHost(host: string) {
export async function getCookiesByHost(link: string) {
const ret: { [key: string]: string } = {};
for await (const [key, value] of cookiesDb.sublevel(host).iterator()) {
const links: { [k: string]: boolean } = {};
const url = urlify(link);

for await (const sublevel of cookiesDb.keys({ keyEncoding: 'utf8' })) {
const l = sublevel.split('!')[1];
links[l] = true;
}

const cookieLink = url
? Object.keys(links).filter((l) => minimatch(l, link))[0]
: Object.keys(links).filter((l) => urlify(l)?.host === link)[0];

if (!cookieLink) return ret;

for await (const [key, value] of cookiesDb.sublevel(cookieLink).iterator()) {
ret[key] = value;
}
return ret;
Expand All @@ -359,10 +374,10 @@ export async function getConnection(origin: string) {
}
}

export async function setHeaders(host: string, name: string, value?: string) {
export async function setHeaders(link: string, name: string, value?: string) {
if (!value) return null;
return mutex.runExclusive(async () => {
await headersDb.sublevel(host).put(name, value);
await headersDb.sublevel(link).put(name, value);
return true;
});
}
Expand All @@ -382,9 +397,23 @@ export async function getHeaders(host: string, name: string) {
return null;
}
}
export async function getHeadersByHost(host: string) {
export async function getHeadersByHost(link: string) {
const ret: { [key: string]: string } = {};
for await (const [key, value] of headersDb.sublevel(host).iterator()) {
const url = urlify(link);

const links: { [k: string]: boolean } = {};
for await (const sublevel of headersDb.keys({ keyEncoding: 'utf8' })) {
const l = sublevel.split('!')[1];
links[l] = true;
}

const headerLink = url
? Object.keys(links).filter((l) => minimatch(l, link))[0]
: Object.keys(links).filter((l) => urlify(l)?.host === link)[0];

if (!headerLink) return ret;

for await (const [key, value] of headersDb.sublevel(headerLink).iterator()) {
ret[key] = value;
}
return ret;
Expand Down
12 changes: 7 additions & 5 deletions src/entries/Background/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import mutex from './mutex';
import browser from 'webextension-polyfill';
import { addRequest } from '../../reducers/requests';
import { urlify } from '../../utils/misc';
import { setCookies, setHeaders } from './db';
import { getHeadersByHost, setCookies, setHeaders } from './db';
export const onSendHeaders = (
details: browser.WebRequest.OnSendHeadersDetailsType,
) => {
Expand All @@ -14,20 +14,22 @@ export const onSendHeaders = (
if (method !== 'OPTIONS') {
const cache = getCacheByTabId(tabId);
const existing = cache.get<RequestLog>(requestId);
const { hostname } = urlify(details.url) || {};
const { origin, pathname } = urlify(details.url) || {};

if (hostname && details.requestHeaders) {
const link = [origin, pathname].join('');

if (link && details.requestHeaders) {
details.requestHeaders.forEach((header) => {
const { name, value } = header;
if (/^cookie$/i.test(name) && value) {
value
.split(';')
.map((v) => v.split('='))
.forEach((cookie) => {
setCookies(hostname, cookie[0].trim(), cookie[1]);
setCookies(link, cookie[0].trim(), cookie[1]);
});
} else {
setHeaders(hostname, name, value);
setHeaders(link, name, value);
}
});
}
Expand Down
16 changes: 8 additions & 8 deletions src/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,20 +303,20 @@ export const makePlugin = async (
}

if (config?.cookies) {
const cookies: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.cookies) {
const cache = await getCookiesByHost(host);
cookies[host] = cache;
const cookies: { [link: string]: { [key: string]: string } } = {};
for (const link of config.cookies) {
const cache = await getCookiesByHost(link);
cookies[link] = cache;
}
// @ts-ignore
injectedConfig.cookies = JSON.stringify(cookies);
}

if (config?.headers) {
const headers: { [hostname: string]: { [key: string]: string } } = {};
for (const host of config.headers) {
const cache = await getHeadersByHost(host);
headers[host] = cache;
const headers: { [link: string]: { [key: string]: string } } = {};
for (const link of config.headers) {
const cache = await getHeadersByHost(link);
headers[link] = cache;
}
// @ts-ignore
injectedConfig.headers = JSON.stringify(headers);
Expand Down

0 comments on commit 3b98fc3

Please sign in to comment.