-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(cloud-function): ad bsa support this refactors the pong lib: - bundle click.js, get.js, viewed.js in pong.js - add pong2.js based on bsa's api - remove fallback * add stage config * remove log * decode copy to be safe * feedback
- Loading branch information
Showing
23 changed files
with
452 additions
and
323 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
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
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,94 @@ | ||
import * as url from "node:url"; | ||
|
||
import type { Request, Response } from "express"; | ||
|
||
import { Coder } from "../internal/pong/index.js"; | ||
import { | ||
createPong2GetHandler, | ||
createPong2ClickHandler, | ||
createPong2ViewedHandler, | ||
fetchImage, | ||
} from "../internal/pong/index.js"; | ||
|
||
import * as env from "../env.js"; | ||
|
||
import { getRequestCountry } from "../utils.js"; | ||
|
||
const { SIGN_SECRET, BSA_ZONE_KEYS } = env; | ||
|
||
const coder = new Coder(SIGN_SECRET); | ||
const handleGet = createPong2GetHandler(BSA_ZONE_KEYS, coder, env); | ||
const handleClick = createPong2ClickHandler(coder); | ||
const handleViewed = createPong2ViewedHandler(coder); | ||
|
||
export async function proxyBSA(req: Request, res: Response) { | ||
const countryCode = getRequestCountry(req); | ||
|
||
const userAgent = req.headers["user-agent"] ?? ""; | ||
|
||
const parsedUrl = url.parse(req.url); | ||
const pathname = parsedUrl.pathname ?? ""; | ||
const search = parsedUrl.search ?? ""; | ||
|
||
if (pathname === "/pong/get") { | ||
if (req.method !== "POST") { | ||
return res.sendStatus(405).end(); | ||
} | ||
|
||
const { body } = req; | ||
const { statusCode: status, payload } = await handleGet( | ||
body, | ||
countryCode, | ||
userAgent | ||
); | ||
|
||
return res | ||
.status(status) | ||
.setHeader("cache-control", "no-store") | ||
.setHeader("content-type", "application/json") | ||
.end(JSON.stringify(payload)); | ||
} else if (req.path === "/pong/click") { | ||
if (req.method !== "GET") { | ||
return res.sendStatus(405).end(); | ||
} | ||
const params = new URLSearchParams(search); | ||
try { | ||
const { status, location } = await handleClick(params); | ||
if (location && (status === 301 || status === 302)) { | ||
return res.redirect(location); | ||
} else { | ||
return res.sendStatus(502).end(); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} else if (pathname === "/pong/viewed") { | ||
if (req.method !== "POST") { | ||
return res.sendStatus(405).end(); | ||
} | ||
const params = new URLSearchParams(search); | ||
try { | ||
await handleViewed(params); | ||
return res.sendStatus(201).end(); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} else if (pathname.startsWith("/pimg/")) { | ||
const src = coder.decodeAndVerify( | ||
decodeURIComponent(pathname.substring("/pimg/".length)) | ||
); | ||
if (!src) { | ||
return res.sendStatus(400).end(); | ||
} | ||
const { buf, contentType } = await fetchImage(src); | ||
return res | ||
.status(200) | ||
.set({ | ||
"cache-control": "max-age=86400", | ||
"content-type": contentType, | ||
}) | ||
.end(Buffer.from(buf)); | ||
} | ||
|
||
return res.status(204).end(); | ||
} |
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,16 @@ | ||
import { BSA_ENABLED, BSA_URL_PREFIX } from "../env.js"; | ||
import { proxyBSA } from "./proxy-bsa.js"; | ||
import { proxyKevel } from "./proxy-kevel.js"; | ||
|
||
import type { Request, Response } from "express"; | ||
|
||
export async function proxyPong(req: Request, res: Response) { | ||
if (BSA_ENABLED) { | ||
const referrer = req.get("referrer") || ""; | ||
const version = Number(req.query["version"]) || 1; | ||
if (referrer.startsWith(BSA_URL_PREFIX) || version === 2) { | ||
return proxyBSA(req, res); | ||
} | ||
} | ||
return proxyKevel(req, res); | ||
} |
Oops, something went wrong.