-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- injection does not rerender the page - allow defer, async script tag attributes - allow JSON/YAML configuration in label
- Loading branch information
Roy Razon
committed
Sep 6, 2023
1 parent
2348c15
commit c2fc5e3
Showing
17 changed files
with
467 additions
and
343 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ jobs: | |
- run: yarn | ||
|
||
- run: yarn lint | ||
|
||
- run: yarn lint | ||
working-directory: tunnel-server |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import yaml, { DocumentOptions, ParseOptions, SchemaOptions, ToJSOptions } from 'yaml' | ||
|
||
type Reviver = (this: any, key: string, value: any) => any | ||
type Options = ParseOptions & DocumentOptions & SchemaOptions & ToJSOptions | ||
|
||
export function tryParseYaml(src: string, options?: Options): any | ||
export function tryParseYaml(src: string, reviver: Reviver, options?: Options): any | ||
export function tryParseYaml(src: string, ...rest: unknown[]) { | ||
try { | ||
return yaml.parse(src, ...rest as any[]) | ||
} catch (e) { | ||
return undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { IncomingMessage, ServerResponse } from 'http' | ||
import zlib from 'zlib' | ||
import stream from 'stream' | ||
import { parse as parseContentType } from 'content-type' | ||
import iconv from 'iconv-lite' | ||
import { INJECT_SCRIPTS_HEADER } from '../common' | ||
import { InjectHtmlScriptTransform } from './inject-transform' | ||
import { ScriptInjection } from '../../tunnel-store' | ||
|
||
const compressionsForContentEncoding = (contentEncoding: string) => { | ||
if (contentEncoding === 'gzip') { | ||
return [zlib.createGunzip(), zlib.createGzip()] as const | ||
} | ||
if (contentEncoding === 'deflate') { | ||
return [zlib.createInflate(), zlib.createDeflate()] as const | ||
} | ||
if (contentEncoding === 'br') { | ||
return [zlib.createBrotliDecompress(), zlib.createBrotliCompress()] as const | ||
} | ||
if (contentEncoding === 'identity') { | ||
return undefined | ||
} | ||
throw new Error(`unsupported content encoding: "${contentEncoding}"`) | ||
} | ||
|
||
export const injectScripts = async ( | ||
proxyRes: stream.Readable & Pick<IncomingMessage, 'headers' | 'statusCode'>, | ||
req: Pick<IncomingMessage, 'headers'>, | ||
res: stream.Writable & Pick<ServerResponse<IncomingMessage>, 'writeHead'>, | ||
) => { | ||
const headerValue = req.headers[INJECT_SCRIPTS_HEADER] as string | undefined | ||
if (!headerValue) { | ||
return undefined | ||
} | ||
|
||
const injects = JSON.parse(headerValue) as Omit<ScriptInjection, 'pathRegex'>[] | ||
|
||
const { | ||
type: contentType, | ||
parameters: { charset: reqCharset }, | ||
} = parseContentType(proxyRes) | ||
|
||
if (!contentType.includes('text/html')) { | ||
return undefined | ||
} | ||
|
||
res.writeHead(proxyRes.statusCode as number, proxyRes.headers) | ||
|
||
const compress = compressionsForContentEncoding(proxyRes.headers['content-encoding'] || 'identity') | ||
|
||
const [input, output] = compress | ||
? [proxyRes.pipe(compress[0]), res.pipe(compress[1])] | ||
: [proxyRes, res] | ||
|
||
const transform = new InjectHtmlScriptTransform(injects) | ||
|
||
input | ||
.pipe(iconv.decodeStream(reqCharset || 'utf-8')) | ||
.pipe(transform) | ||
.pipe(iconv.encodeStream(reqCharset || 'utf-8')) | ||
.pipe(output) | ||
|
||
return undefined | ||
} |
Oops, something went wrong.