Skip to content

Commit

Permalink
Merge pull request #22 from movie-web/user-agent-support
Browse files Browse the repository at this point in the history
User agent proxying support
  • Loading branch information
binaryoverload authored Jan 6, 2024
2 parents 15b438b + d348892 commit 3a1e868
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 61 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "simple-proxy",
"private": true,
"version": "2.1.0",
"version": "2.1.1",
"scripts": {
"prepare": "nitropack prepare",
"dev": "nitropack dev",
Expand All @@ -15,8 +15,8 @@
"preinstall": "npx only-allow pnpm"
},
"dependencies": {
"@tsndr/cloudflare-worker-jwt": "^2.3.2",
"h3": "^1.8.1",
"h3": "^1.9.0",
"jose": "^5.2.0",
"nitropack": "latest"
},
"devDependencies": {
Expand Down
52 changes: 32 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getBodyBuffer } from '@/utils/body';
import {
getProxyHeaders,
getAfterResponseHeaders,
cleanupHeadersBeforeProxy,
getBlacklistedHeaders,
} from '@/utils/headers';
import {
createTokenIfNeeded,
Expand Down Expand Up @@ -39,8 +39,8 @@ export default defineEventHandler(async (event) => {
const token = await createTokenIfNeeded(event);

// proxy
cleanupHeadersBeforeProxy(event);
await proxyRequest(event, destination, {
await specificProxyRequest(event, destination, {
blacklistedHeaders: getBlacklistedHeaders(),
fetchOptions: {
redirect: 'follow',
headers: getProxyHeaders(event.headers),
Expand Down
35 changes: 15 additions & 20 deletions src/utils/headers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { H3Event } from 'h3';
const headerMap: Record<string, string> = {
'X-Cookie': 'Cookie',
'X-Referer': 'Referer',
'X-Origin': 'Origin',
'X-User-Agent': 'User-Agent',
'X-X-Real-Ip': 'X-Real-Ip',
};

const blacklistedHeaders = [
'cf-connecting-ip',
Expand All @@ -11,6 +17,7 @@ const blacklistedHeaders = [
'x-forwarded-proto',
'forwarded',
'x-real-ip',
...Object.keys(headerMap),
];

function copyHeader(
Expand All @@ -26,20 +33,16 @@ function copyHeader(
export function getProxyHeaders(headers: Headers): Headers {
const output = new Headers();

const headerMap: Record<string, string> = {
'X-Cookie': 'Cookie',
'X-Referer': 'Referer',
'X-Origin': 'Origin',
};
Object.entries(headerMap).forEach((entry) => {
copyHeader(headers, output, entry[0], entry[1]);
});

// default user agent
output.set(
'User-Agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0',
);

Object.entries(headerMap).forEach((entry) => {
copyHeader(headers, output, entry[0], entry[1]);
});

return output;
}

Expand All @@ -60,14 +63,6 @@ export function getAfterResponseHeaders(
};
}

export function removeHeadersFromEvent(event: H3Event, key: string) {
const normalizedKey = key.toLowerCase();
if (event.node.req.headers[normalizedKey])
delete event.node.req.headers[normalizedKey];
}

export function cleanupHeadersBeforeProxy(event: H3Event) {
blacklistedHeaders.forEach((key) => {
removeHeadersFromEvent(event, key);
});
export function getBlacklistedHeaders() {
return blacklistedHeaders;
}
84 changes: 84 additions & 0 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import {
H3Event,
Duplex,
ProxyOptions,
getProxyRequestHeaders,
RequestHeaders,
} from 'h3';

const PayloadMethods = new Set(['PATCH', 'POST', 'PUT', 'DELETE']);

export interface ExtraProxyOptions {
blacklistedHeaders?: string[];
}

function mergeHeaders(
defaults: HeadersInit,
...inputs: (HeadersInit | RequestHeaders | undefined)[]
) {
const _inputs = inputs.filter(Boolean) as HeadersInit[];
if (_inputs.length === 0) {
return defaults;
}
const merged = new Headers(defaults);
for (const input of _inputs) {
if (input.entries) {
for (const [key, value] of (input.entries as any)()) {
if (value !== undefined) {
merged.set(key, value);
}
}
} else {
for (const [key, value] of Object.entries(input)) {
if (value !== undefined) {
merged.set(key, value);
}
}
}
}
return merged;
}

export async function specificProxyRequest(
event: H3Event,
target: string,
opts: ProxyOptions & ExtraProxyOptions = {},
) {
let body;
let duplex: Duplex | undefined;
if (PayloadMethods.has(event.method)) {
if (opts.streamRequest) {
body = getRequestWebStream(event);
duplex = 'half';
} else {
body = await readRawBody(event, false).catch(() => undefined);
}
}

const method = opts.fetchOptions?.method || event.method;
const oldHeaders = getProxyRequestHeaders(event);
opts.blacklistedHeaders?.forEach((header) => {
const keys = Object.keys(oldHeaders).filter(
(v) => v.toLowerCase() === header.toLowerCase(),
);
keys.forEach((k) => delete oldHeaders[k]);
});

const fetchHeaders = mergeHeaders(
oldHeaders,
opts.fetchOptions?.headers,
opts.headers,
);
(fetchHeaders.forEach as any)(console.log);

return sendProxy(event, target, {
...opts,
fetchOptions: {
method,
body,
duplex,
...opts.fetchOptions,
headers: fetchHeaders,
},
});
}
33 changes: 18 additions & 15 deletions src/utils/turnstile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { H3Event, EventHandlerRequest } from 'h3';
import jsonwebtoken from '@tsndr/cloudflare-worker-jwt';
import { SignJWT, jwtVerify } from 'jose';
import { getIp } from '@/utils/ip';

const turnstileSecret = process.env.TURNSTILE_SECRET ?? null;
Expand All @@ -15,13 +15,10 @@ export function isTurnstileEnabled() {

export async function makeToken(ip: string) {
if (!jwtSecret) throw new Error('Cannot make token without a secret');
return await jsonwebtoken.sign(
{
ip,
exp: Math.floor(Date.now() / 1000) + 60 * 10, // 10 Minutes
},
jwtSecret,
);
return await new SignJWT({ ip })
.setProtectedHeader({ alg: 'HS256' })
.setExpirationTime('10m')
.sign(new TextEncoder().encode(jwtSecret));
}

export function setTokenHeader(
Expand Down Expand Up @@ -54,13 +51,19 @@ export async function isAllowedToMakeRequest(

if (token.startsWith(jwtPrefix)) {
const jwtToken = token.slice(jwtPrefix.length);
const isValid = await jsonwebtoken.verify(jwtToken, jwtSecret, {
algorithm: 'HS256',
});
if (!isValid) return false;
const jwtBody = jsonwebtoken.decode<{ ip: string }>(jwtToken);
if (!jwtBody.payload) return false;
if (getIp(event) !== jwtBody.payload.ip) return false;
let jwtPayload: { ip: string } | null = null;
try {
const jwtResult = await jwtVerify<{ ip: string }>(
jwtToken,
new TextEncoder().encode(jwtSecret),
{
algorithms: ['HS256'],
},
);
jwtPayload = jwtResult.payload;
} catch {}
if (!jwtPayload) return false;
if (getIp(event) !== jwtPayload.ip) return false;
return true;
}

Expand Down

0 comments on commit 3a1e868

Please sign in to comment.