mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-13 22:40:01 -04:00
send cookies
This commit is contained in:
parent
4549f3e672
commit
19b3a89549
5 changed files with 948 additions and 5 deletions
916
pnpm-lock.yaml
generated
916
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load diff
7
src/shared/headers.ts
Normal file
7
src/shared/headers.ts
Normal file
|
@ -0,0 +1,7 @@
|
|||
export class ScramjetHeaders {
|
||||
headers = {};
|
||||
|
||||
set(key: string, v: string) {
|
||||
this.headers[key.toLowerCase()] = v;
|
||||
}
|
||||
}
|
|
@ -7,12 +7,14 @@ import { rewriteWorkers } from "./rewriters/worker";
|
|||
import { isScramjetFile } from "./rewriters/html";
|
||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
||||
import { parseDomain } from "parse-domain";
|
||||
import { ScramjetHeaders } from "./headers";
|
||||
|
||||
self.$scramjet.shared = {
|
||||
util: {
|
||||
isScramjetFile,
|
||||
parseDomain,
|
||||
BareClient,
|
||||
ScramjetHeaders,
|
||||
},
|
||||
url: {
|
||||
encodeUrl,
|
||||
|
|
2
src/types.d.ts
vendored
2
src/types.d.ts
vendored
|
@ -9,6 +9,7 @@ import { isScramjetFile } from "./shared/rewriters/html";
|
|||
import type { Codec } from "./codecs";
|
||||
import { BareClient } from "@mercuryworkshop/bare-mux";
|
||||
import { parseDomain } from "parse-domain";
|
||||
import { ScramjetHeaders } from "./shared/headers";
|
||||
|
||||
interface ScramjetConfig {
|
||||
prefix: string;
|
||||
|
@ -43,6 +44,7 @@ declare global {
|
|||
};
|
||||
util: {
|
||||
BareClient: typeof BareClient;
|
||||
ScramjetHeaders: typeof ScramjetHeaders;
|
||||
isScramjetFile: typeof isScramjetFile;
|
||||
parseDomain: typeof parseDomain;
|
||||
};
|
||||
|
|
|
@ -7,7 +7,7 @@ import { renderError } from "./error";
|
|||
const { encodeUrl, decodeUrl } = self.$scramjet.shared.url;
|
||||
const { rewriteHeaders, rewriteHtml, rewriteJs, rewriteCss, rewriteWorkers } =
|
||||
self.$scramjet.shared.rewrite;
|
||||
const { parseDomain } = self.$scramjet.shared.util;
|
||||
const { parseDomain, ScramjetHeaders } = self.$scramjet.shared.util;
|
||||
|
||||
export async function swfetch(
|
||||
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()) {
|
||||
headers.set(key, value);
|
||||
}
|
||||
|
||||
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, {
|
||||
method: request.method,
|
||||
body: request.body,
|
||||
headers,
|
||||
headers: headers.headers,
|
||||
credentials: "omit",
|
||||
mode: request.mode === "cors" ? request.mode : "same-origin",
|
||||
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, {
|
||||
durability: "relaxed",
|
||||
prefix: "Cookies",
|
||||
});
|
||||
|
||||
let headers = maybeHeaders instanceof Array ? maybeHeaders : [maybeHeaders];
|
||||
|
||||
for (const cookie of headers) {
|
||||
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");
|
||||
let host = hostArg ? hostArg[1] : undefined;
|
||||
|
||||
if (url.protocol === "http" && cookieParsed.includes(["Secure"])) continue;
|
||||
if (url.protocol === "http:" && cookieParsed.includes(["Secure"])) continue;
|
||||
if (
|
||||
cookieParsed.includes(["SameSite", "None"]) &&
|
||||
!cookieParsed.includes(["Secure"])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue