send cookies

This commit is contained in:
Toshit Chawda 2024-08-02 16:15:31 -07:00
parent 4549f3e672
commit 19b3a89549
No known key found for this signature in database
GPG key ID: 91480ED99E2B3D9D
5 changed files with 948 additions and 5 deletions

916
pnpm-lock.yaml generated

File diff suppressed because it is too large Load diff

7
src/shared/headers.ts Normal file
View file

@ -0,0 +1,7 @@
export class ScramjetHeaders {
headers = {};
set(key: string, v: string) {
this.headers[key.toLowerCase()] = v;
}
}

View file

@ -7,12 +7,14 @@ import { rewriteWorkers } from "./rewriters/worker";
import { isScramjetFile } from "./rewriters/html"; import { isScramjetFile } from "./rewriters/html";
import { BareClient } from "@mercuryworkshop/bare-mux"; import { BareClient } from "@mercuryworkshop/bare-mux";
import { parseDomain } from "parse-domain"; import { parseDomain } from "parse-domain";
import { ScramjetHeaders } from "./headers";
self.$scramjet.shared = { self.$scramjet.shared = {
util: { util: {
isScramjetFile, isScramjetFile,
parseDomain, parseDomain,
BareClient, BareClient,
ScramjetHeaders,
}, },
url: { url: {
encodeUrl, encodeUrl,

2
src/types.d.ts vendored
View file

@ -9,6 +9,7 @@ import { isScramjetFile } from "./shared/rewriters/html";
import type { Codec } from "./codecs"; import type { Codec } from "./codecs";
import { BareClient } from "@mercuryworkshop/bare-mux"; import { BareClient } from "@mercuryworkshop/bare-mux";
import { parseDomain } from "parse-domain"; import { parseDomain } from "parse-domain";
import { ScramjetHeaders } from "./shared/headers";
interface ScramjetConfig { interface ScramjetConfig {
prefix: string; prefix: string;
@ -43,6 +44,7 @@ declare global {
}; };
util: { util: {
BareClient: typeof BareClient; BareClient: typeof BareClient;
ScramjetHeaders: typeof ScramjetHeaders;
isScramjetFile: typeof isScramjetFile; isScramjetFile: typeof isScramjetFile;
parseDomain: typeof parseDomain; parseDomain: typeof parseDomain;
}; };

View file

@ -7,7 +7,7 @@ import { renderError } from "./error";
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url; const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } = const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } =
self.$scramjet.shared.rewrite; self.$scramjet.shared.rewrite;
const { parseDomain } = self.$scramjet.shared.util; const { parseDomain, ScramjetHeaders } = self.$scramjet.shared.util;
export async function swfetch( export async function swfetch(
this: ScramjetServiceWorker, this: ScramjetServiceWorker,
@ -51,17 +51,31 @@ export async function swfetch(
); );
} }
const headers = new Headers(); const headers = new ScramjetHeaders();
for (const [key, value] of request.headers.entries()) { for (const [key, value] of request.headers.entries()) {
headers.set(key, value); headers.set(key, value);
} }
headers.set("Referer", decodeUrl(request.referrer)); headers.set("Referer", decodeUrl(request.referrer));
const cookieStore = new IDBMap(url.host, {
durability: "relaxed",
prefix: "Cookies",
});
let cookies = await cookieStore.entries();
if (url.protocol !== "https:") {
cookies = cookies.filter(([_k, v]) => !v.args.includes(["Secure"]));
}
cookies = Array.from(cookies.map(([k, v]) => `${k}=${v.value}`));
if (cookies.length) {
headers.set("Cookie", cookies.join(";"));
}
const response: BareResponseFetch = await this.client.fetch(url, { const response: BareResponseFetch = await this.client.fetch(url, {
method: request.method, method: request.method,
body: request.body, body: request.body,
headers, headers: headers.headers,
credentials: "omit", credentials: "omit",
mode: request.mode === "cors" ? request.mode : "same-origin", mode: request.mode === "cors" ? request.mode : "same-origin",
cache: request.cache, cache: request.cache,
@ -160,12 +174,14 @@ async function handleResponse(
}); });
} }
async function handleCookies(url: URL, headers: string[]) { async function handleCookies(url: URL, maybeHeaders: string[] | string) {
const cookieStore = new IDBMap(url.host, { const cookieStore = new IDBMap(url.host, {
durability: "relaxed", durability: "relaxed",
prefix: "Cookies", prefix: "Cookies",
}); });
let headers = maybeHeaders instanceof Array ? maybeHeaders : [maybeHeaders];
for (const cookie of headers) { for (const cookie of headers) {
let cookieParsed = cookie.split(";").map((x) => x.trim().split("=")); let cookieParsed = cookie.split(";").map((x) => x.trim().split("="));
@ -177,7 +193,7 @@ async function handleCookies(url: URL, headers: string[]) {
cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain"); cookieParsed = cookieParsed.filter((x) => x[0] !== "Domain");
let host = hostArg ? hostArg[1] : undefined; let host = hostArg ? hostArg[1] : undefined;
if (url.protocol === "http" && cookieParsed.includes(["Secure"])) continue; if (url.protocol === "http:" && cookieParsed.includes(["Secure"])) continue;
if ( if (
cookieParsed.includes(["SameSite", "None"]) && cookieParsed.includes(["SameSite", "None"]) &&
!cookieParsed.includes(["Secure"]) !cookieParsed.includes(["Secure"])