~cadence/tube-devel

cloudtube: Proxy captions via new /proxy route v3 SUPERSEDED

Lomanic: 1
 Proxy captions via new /proxy route

 3 files changed, 29 insertions(+), 25 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~cadence/tube-devel/patches/22717/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH cloudtube v3] Proxy captions via new /proxy route Export this patch

We can add more authorized paths to authorizedPaths if we need
more resources to be pulled from the NewLeaf/Invidious backend
on the same domain.

This route forwards to the client a few headers like Bibliogram
https://git.sr.ht/~cadence/bibliogram/tree/ea7cd5d5/item/src/site/api/proxy.js#L28-29
so we can also use this route to possibly proxy videos in the
future.
---
 api/captions.js | 25 -------------------------
 api/proxy.js    | 24 ++++++++++++++++++++++++
 api/video.js    |  5 +++++
 3 files changed, 29 insertions(+), 25 deletions(-)
 delete mode 100644 api/captions.js
 create mode 100644 api/proxy.js

diff --git a/api/captions.js b/api/captions.js
deleted file mode 100644
index 29611ef..0000000
--- a/api/captions.js
@@ -1,25 +0,0 @@
const fetch = require("node-fetch")
const {getUser} = require("../utils/getuser")
const constants = require("../utils/constants.js")

module.exports = [
	{
		route: `/api/v1/captions/(${constants.regex.video_id})`, methods: ["GET"], code: async ({req, fill, url}) => {
			const instanceOrigin = getUser(req).getSettingsOrDefaults().instance
			const fetchURL = new URL(`${url.pathname}${url.search}`, instanceOrigin)
			return fetch(fetchURL.toString()).then(res => {
				return res.text().then(text => {
					if (res.status === 200) {
						// Remove the position annotations that youtube unhelpfully provides
						text = text.replace(/(--> \S+).*/g, "$1")
					}
					return {
						statusCode: res.status,
						contentType: res.headers.get("content-type"),
						content: text
					}
				})
			})
		}
	}
]
diff --git a/api/proxy.js b/api/proxy.js
new file mode 100644
index 0000000..2aa4de6
--- /dev/null
+++ b/api/proxy.js
@@ -0,0 +1,24 @@
const {proxy} = require("pinski/plugins")
const {getUser} = require("../utils/getuser")
const constants = require("../utils/constants.js")

// list of paths relative to the backend this route is authorized to serve
const authorizedPaths = [`/api/v1/captions/(${constants.regex.video_id})`]

module.exports = [
	{
		route: `/proxy`, methods: ["GET"], code: async ({req, fill, url}) => {
			const instanceOrigin = getUser(req).getSettingsOrDefaults().instance
			const remotePath = url.searchParams.get("url")
			const fetchURL = new URL(remotePath, instanceOrigin)
			if (!authorizedPaths.some(element => fetchURL.pathname.match(new RegExp(`^${element}$`)))) {
				return {
					statusCode: 401,
					content: "CloudTube: Unauthorized",
					contentType: "text/plain"
				}
			}
			return proxy(fetchURL, {}, (h) => Object.keys(h).filter(key => ["content-type", "date", "last-modified", "expires", "cache-control", "accept-ranges", "content-range", "origin", "etag", "content-length", "transfer-encoding"].includes(key)).reduce((res, key) => (res[key] = h[key], res), {}))
		}
	}
]
diff --git a/api/video.js b/api/video.js
index 9a27a26..850f7f7 100644
--- a/api/video.js
+++ b/api/video.js
@@ -168,6 +168,11 @@ module.exports = [
				// rewrite description
				video.descriptionHtml = converters.rewriteVideoDescription(video.descriptionHtml, id)

				// rewrite captions urls so they are served on the same domain via the /proxy route
				for (const caption of video.captions) {
					caption.url = `/proxy?${new URLSearchParams({"url": caption.url})}`
				}

				return render(200, "pug/video.pug", {
					video, formats, subscribed, instanceOrigin, mediaFragment, autoplay, continuous,
					sessionWatched, sessionWatchedNext
-- 
2.20.1