Skip to content

Commit e35539a

Browse files
committed
🐛 support passing init params
1 parent 1731090 commit e35539a

File tree

8 files changed

+80
-40
lines changed

8 files changed

+80
-40
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@slax-lab/liveproxy-sw",
3-
"version": "1.0.2-dev.4",
3+
"version": "1.0.2-dev.5",
44
"description": "liveproxy service worker",
55
"main": "index.js",
66
"type": "module",

src/config.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ export const disablePostHost = [/mp\.weixin\.qq\.com/];
22

33
export const proxyPrefix = "https://wabac-test.slax.dev/proxy/";
44

5-
export const REPLAY_URL_PREFIX =
6-
/(?:\/proxy\/(?:([0-9]*)([a-z]{2,3})_)?\/|\/w\/liveproxy\/(?:([a-z]{2,3})_)?\/)(https?:\/\/.+)/;
5+
export let REPLAY_URL_PREFIX;
6+
export let REPLAY_URL_PREFIX_REGEXP_STR;
7+
export let REPLAY_URL_PREFIX_REGEXP;
78

89
export const globalOverrides = [
910
"window",
@@ -12,3 +13,9 @@ export const globalOverrides = [
1213
"document",
1314
"location",
1415
];
16+
17+
export function setProxyPathPrefix(prefix: string, regexp: string) {
18+
REPLAY_URL_PREFIX = prefix;
19+
REPLAY_URL_PREFIX_REGEXP_STR = regexp;
20+
REPLAY_URL_PREFIX_REGEXP = new RegExp(regexp);
21+
}

src/event.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { handleProxyRequest } from "./proxy";
2-
import { REPLAY_URL_PREFIX } from "./config";
2+
import {
3+
REPLAY_URL_PREFIX,
4+
REPLAY_URL_PREFIX_REGEXP,
5+
setProxyPathPrefix,
6+
} from "./config";
37

48
export function handleInstallEvent(event: ExtendableEvent) {
59
event.waitUntil(self.skipWaiting());
@@ -31,12 +35,12 @@ export function handleFetchEvent(event: FetchEvent) {
3135
const url = event.request.url;
3236
const urlObj = new URL(url);
3337

34-
const replayMatch = urlObj.pathname.match(REPLAY_URL_PREFIX);
38+
const replayMatch = urlObj.pathname.match(REPLAY_URL_PREFIX_REGEXP);
3539
if (!replayMatch) {
3640
const referer = event.request.headers.get("Referer");
37-
if (referer && referer.match(REPLAY_URL_PREFIX)) {
41+
if (referer && referer.match(REPLAY_URL_PREFIX_REGEXP)) {
3842
const refererObj = new URL(referer);
39-
const refMatch = refererObj.pathname.match(REPLAY_URL_PREFIX);
43+
const refMatch = refererObj.pathname.match(REPLAY_URL_PREFIX_REGEXP);
4044
if (refMatch) {
4145
const timestamp = "";
4246
const mod = "if_";
@@ -60,7 +64,7 @@ export function handleFetchEvent(event: FetchEvent) {
6064
timestamp = replayMatch[1] || "";
6165
mod = replayMatch[2] || "";
6266
origUrl = replayMatch[4] || "";
63-
} else if (matchPath.includes("/w/liveproxy/")) {
67+
} else if (matchPath.includes(REPLAY_URL_PREFIX)) {
6468
mod = replayMatch[3] || "";
6569
origUrl = replayMatch[4] || "";
6670
} else {
@@ -96,6 +100,9 @@ export function handleFetchEvent(event: FetchEvent) {
96100
export function handleMessageEvent(event: MessageEvent) {
97101
console.log("handleMessageEvent", event);
98102
if (event.data.msg_type === "init") {
103+
const proxyPrefix = event.data.proxy_prefix;
104+
const proxyPrefixRegexp = event.data.proxy_prefix_regexp;
105+
setProxyPathPrefix(proxyPrefix, proxyPrefixRegexp);
99106
event.source?.postMessage({
100107
msg_type: "init_done",
101108
timestamp: Date.now(),

src/inject.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import { parseUrl } from "./url";
22
import { SlaxLocation } from "./inject/location";
33
import { SlaxEnv } from "./inject/proxy";
44
import { extractOriginalUrl } from "./inject/utils";
5+
56
const originURL = "${originURL}";
67
const proxyURL = "${proxyURL}";
8+
//@ts-ignore
9+
window.proxyPrefixPath = "${proxyPrefixPath}";
10+
//@ts-ignore
11+
window.proxyPrefixPathRegexp = new RegExp("${proxyPrefixPathRegexpStr}");
712

813
(function () {
914
if ((window as any).__URL_REWRITER_INITIALIZED__) return;
@@ -36,7 +41,8 @@ const proxyURL = "${proxyURL}";
3641
return url;
3742
}
3843

39-
return `${proxyURL}/w/liveproxy/${mod}/${fullUrl}`;
44+
//@ts-ignore
45+
return `${proxyURL}${window.proxyPrefixPath}/${mod}/${fullUrl}`;
4046
} catch (error) {
4147
return url;
4248
}

src/inject/location.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,26 @@ export class SlaxLocation implements ILocation {
3434
this._originalLocation = originalLocation;
3535

3636
const originalHref = extractOriginalUrl(originalLocation.href);
37-
38-
this._url = new URL(originalHref!);
39-
this._href = originalHref!;
40-
this._protocol = this._url.protocol;
41-
this._host = this._url.host;
42-
this._hostname = this._url.hostname;
43-
this._port = this._url.port;
44-
this._pathname = this._url.pathname;
45-
this._search = this._url.search;
46-
this._hash = this._url.hash;
47-
this._origin = this._url.origin;
37+
try {
38+
this._url = new URL(originalHref!);
39+
this._href = originalHref!;
40+
this._protocol = this._url.protocol;
41+
this._host = this._url.host;
42+
this._hostname = this._url.hostname;
43+
this._port = this._url.port;
44+
this._pathname = this._url.pathname;
45+
this._search = this._url.search;
46+
this._hash = this._url.hash;
47+
this._origin = this._url.origin;
48+
} catch (e) {
49+
console.error(
50+
"Error creating URL:",
51+
e,
52+
originalHref,
53+
originalLocation.href
54+
);
55+
throw e;
56+
}
4857
}
4958

5059
get href(): string {

src/inject/utils.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
export function extractOriginalUrl(url: string | null): string | null {
2-
if (!url || typeof url !== "string") return url;
3-
4-
const newProxyMatch = url.match(/\/w\/liveproxy\/[^\/]*([a-z_]+)\/(.+)/);
1+
export function extractOriginalUrl(url: string): string {
2+
//@ts-ignore
3+
const newProxyMatch = url.match(window.proxyPrefixPathRegexp);
54
if (newProxyMatch) {
6-
return newProxyMatch[2];
5+
return newProxyMatch[4];
76
}
87

98
const oldProxyMatch = url.match(/\/proxy\/[^\/]*([a-z_]+)\/(.+)/);

src/proxy.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { completeHtmlRewrite, rewriteCSS, rewriteJS } from "./rewrite";
2-
import { proxyPrefix, REPLAY_URL_PREFIX } from "./config";
2+
import {
3+
proxyPrefix,
4+
REPLAY_URL_PREFIX,
5+
REPLAY_URL_PREFIX_REGEXP,
6+
REPLAY_URL_PREFIX_REGEXP_STR,
7+
} from "./config";
38
import { isCdnUrl } from "./cdn";
49

510
export function processResponseHeaders(headers: Headers): Headers {
@@ -86,9 +91,9 @@ export async function handleProxyRequest(
8691
const refererUrl = new URL(request.referrer || request.url);
8792
if (
8893
refererUrl.pathname.includes("/proxy/") ||
89-
refererUrl.pathname.includes("/w/liveproxy/")
94+
refererUrl.pathname.includes(REPLAY_URL_PREFIX)
9095
) {
91-
const refMatch = refererUrl.pathname.match(REPLAY_URL_PREFIX);
96+
const refMatch = refererUrl.pathname.match(REPLAY_URL_PREFIX_REGEXP);
9297
if (refMatch) {
9398
let refOrigUrl;
9499
if (refMatch[4]) {
@@ -108,7 +113,7 @@ export async function handleProxyRequest(
108113
if (refOrigUrl) {
109114
headers.set("X-Proxy-Referer", refOrigUrl);
110115
const currentOrigin = self.location.origin || "";
111-
const proxyReferer = `${currentOrigin}/w/liveproxy/mp_/${refOrigUrl}`;
116+
const proxyReferer = `${currentOrigin}${REPLAY_URL_PREFIX}/mp_/${refOrigUrl}`;
112117
headers.set("Referer", proxyReferer);
113118
}
114119
}
@@ -174,12 +179,12 @@ export async function handleProxyRequest(
174179

175180
if (location.startsWith("/")) {
176181
const urlObj = new URL(origUrl);
177-
newLocation = `/w/liveproxy/mp_/${urlObj.origin}${location}`;
182+
newLocation = `${REPLAY_URL_PREFIX}/mp_/${urlObj.origin}${location}`;
178183
} else if (location.startsWith("http")) {
179-
newLocation = `/w/liveproxy/mp_/${location}`;
184+
newLocation = `${REPLAY_URL_PREFIX}/mp_/${location}`;
180185
} else if (location.startsWith("//")) {
181186
const urlObj = new URL(origUrl);
182-
newLocation = `/w/liveproxy/mp_/${urlObj.protocol.replace(
187+
newLocation = `${REPLAY_URL_PREFIX}/mp_/${urlObj.protocol.replace(
183188
":",
184189
""
185190
)}:${location}`;
@@ -188,7 +193,7 @@ export async function handleProxyRequest(
188193
const pathParts = urlObj.pathname.split("/");
189194
pathParts.pop();
190195
const basePath = pathParts.join("/");
191-
newLocation = `/w/liveproxy/mp_/${urlObj.origin}${basePath}/${location}`;
196+
newLocation = `${REPLAY_URL_PREFIX}/mp_/${urlObj.origin}${basePath}/${location}`;
192197
}
193198

194199
newLocation = newLocation.replace(/([^:])\/+/g, "$1/");

src/rewrite.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { globalOverrides } from "./config";
1+
import {
2+
globalOverrides,
3+
REPLAY_URL_PREFIX,
4+
REPLAY_URL_PREFIX_REGEXP,
5+
REPLAY_URL_PREFIX_REGEXP_STR,
6+
} from "./config";
27
import { parseUrl } from "./url";
38
import * as acorn from "acorn";
49

@@ -345,7 +350,7 @@ export function completeHtmlRewrite(
345350
const srcMatch = match.match(/src\s*=\s*["']([^"']+)["']/i);
346351
if (srcMatch && !srcMatch[1].startsWith("data:")) {
347352
const fullUrl = parseUrl(srcMatch[1], baseUrl);
348-
const proxyUrl = `${currentOrigin}/w/liveproxy/mp_/${fullUrl}`;
353+
const proxyUrl = `${currentOrigin}${REPLAY_URL_PREFIX}/mp_/${fullUrl}`;
349354
result = result.replace(srcMatch[0], `src="${proxyUrl}"`);
350355
}
351356
}
@@ -354,7 +359,7 @@ export function completeHtmlRewrite(
354359
const dataSrcMatch = match.match(/data-src\s*=\s*["']([^"']+)["']/i);
355360
if (dataSrcMatch) {
356361
const fullUrl = parseUrl(dataSrcMatch[1], baseUrl);
357-
const proxyUrl = `${currentOrigin}/w/liveproxy/mp_/${fullUrl}`;
362+
const proxyUrl = `${currentOrigin}${REPLAY_URL_PREFIX}/mp_/${fullUrl}`;
358363
result = result.replace(dataSrcMatch[0], `data-src="${proxyUrl}"`);
359364

360365
if (
@@ -382,7 +387,7 @@ export function completeHtmlRewrite(
382387
const [url, ...rest] = part.trim().split(/\s+/);
383388
if (url && !url.startsWith("data:")) {
384389
const fullUrl = parseUrl(url, baseUrl);
385-
const proxyUrl = `${currentOrigin}/w/liveproxy/mp_/${fullUrl}`;
390+
const proxyUrl = `${currentOrigin}${REPLAY_URL_PREFIX}/mp_/${fullUrl}`;
386391
return [proxyUrl, ...rest].join(" ");
387392
}
388393
return part;
@@ -485,7 +490,7 @@ export function completeHtmlRewrite(
485490
}
486491

487492
const fullUrl = parseUrl(poster, baseUrl);
488-
const proxyUrl = `${self.location.origin}/w/liveproxy/mp_/${fullUrl}`;
493+
const proxyUrl = `${self.location.origin}${REPLAY_URL_PREFIX}/mp_/${fullUrl}`;
489494
return match.replace(poster, proxyUrl);
490495
}
491496
);
@@ -511,7 +516,7 @@ export function completeHtmlRewrite(
511516
}
512517

513518
const fullUrl = parseUrl(url, baseUrl);
514-
const proxyUrl = `${self.location.origin}/w/liveproxy/mp_/${fullUrl}`;
519+
const proxyUrl = `${self.location.origin}${REPLAY_URL_PREFIX}/mp_/${fullUrl}`;
515520
return match.replace(url, proxyUrl);
516521
}
517522
);
@@ -550,7 +555,9 @@ export function completeHtmlRewrite(
550555
<script>
551556
${liveProxyCode
552557
.replace("${originURL}", baseUrl)
553-
.replace("${proxyURL}", self.location.origin)}
558+
.replace("${proxyURL}", self.location.origin)
559+
.replace("${proxyPrefixPath}", REPLAY_URL_PREFIX)
560+
.replace("${proxyPrefixPathRegexpStr}", REPLAY_URL_PREFIX_REGEXP_STR)}
554561
</script>
555562
<style>
556563
body {

0 commit comments

Comments
 (0)