api: move service disabling to DISABLED_SERVICES env

This commit is contained in:
wukko 2024-08-23 00:16:26 +06:00
parent ee375a27cd
commit 7a557a97c3
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
4 changed files with 23 additions and 26 deletions

View file

@ -1,5 +1,14 @@
import { services } from "./processing/service-config.js";
const genericUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"; const genericUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36";
const disabledServices = process.env.DISABLED_SERVICES?.split(',') || [];
const enabledServices = new Set(Object.keys(services).filter(e => {
if (!disabledServices.includes(e)) {
return e;
}
}));
const env = { const env = {
apiURL: process.env.API_URL || '', apiURL: process.env.API_URL || '',
apiPort: process.env.API_PORT || 9000, apiPort: process.env.API_PORT || 9000,
@ -27,6 +36,8 @@ const env = {
turnstileSecret: process.env.TURNSTILE_SECRET, turnstileSecret: process.env.TURNSTILE_SECRET,
jwtSecret: process.env.JWT_SECRET, jwtSecret: process.env.JWT_SECRET,
jwtLifetime: process.env.JWT_EXPIRY || 120, jwtLifetime: process.env.JWT_EXPIRY || 120,
enabledServices,
} }
export { export {

View file

@ -203,8 +203,8 @@ export const runAPI = (express, app, __dirname) => {
} }
const parsed = extract(normalizedRequest.url); const parsed = extract(normalizedRequest.url);
if (parsed === null) { if ("error" in parsed) {
return fail(res, "error.api.service.unsupported"); return fail(res, `error.api.service.${parsed.error}`);
} }
try { try {

View file

@ -5,7 +5,6 @@ export const hlsExceptions = ["dailymotion", "vimeo", "rutube"];
export const services = { export const services = {
bilibili: { bilibili: {
enabled: true,
patterns: [ patterns: [
"video/:comId", "video/:comId",
"_shortLink/:comShortLink", "_shortLink/:comShortLink",
@ -15,11 +14,9 @@ export const services = {
subdomains: ["m"], subdomains: ["m"],
}, },
dailymotion: { dailymotion: {
enabled: true,
patterns: ["video/:id"], patterns: ["video/:id"],
}, },
facebook: { facebook: {
enabled: true,
patterns: [ patterns: [
"_shortLink/:shortLink", "_shortLink/:shortLink",
":username/videos/:caption/:id", ":username/videos/:caption/:id",
@ -31,7 +28,6 @@ export const services = {
altDomains: ["fb.watch"], altDomains: ["fb.watch"],
}, },
instagram: { instagram: {
enabled: true,
patterns: [ patterns: [
"reels/:postId", "reels/:postId",
":username/reel/:postId", ":username/reel/:postId",
@ -44,11 +40,9 @@ export const services = {
altDomains: ["ddinstagram.com"], altDomains: ["ddinstagram.com"],
}, },
loom: { loom: {
enabled: true,
patterns: ["share/:id"], patterns: ["share/:id"],
}, },
ok: { ok: {
enabled: true,
patterns: [ patterns: [
"video/:id", "video/:id",
"videoembed/:id" "videoembed/:id"
@ -56,7 +50,6 @@ export const services = {
tld: "ru", tld: "ru",
}, },
pinterest: { pinterest: {
enabled: true,
patterns: [ patterns: [
"pin/:id", "pin/:id",
"pin/:id/:garbage", "pin/:id/:garbage",
@ -64,7 +57,6 @@ export const services = {
], ],
}, },
reddit: { reddit: {
enabled: true,
patterns: [ patterns: [
"r/:sub/comments/:id/:title", "r/:sub/comments/:id/:title",
"user/:user/comments/:id/:title" "user/:user/comments/:id/:title"
@ -72,7 +64,6 @@ export const services = {
subdomains: "*", subdomains: "*",
}, },
rutube: { rutube: {
enabled: true,
patterns: [ patterns: [
"video/:id", "video/:id",
"play/embed/:id", "play/embed/:id",
@ -84,7 +75,6 @@ export const services = {
tld: "ru", tld: "ru",
}, },
snapchat: { snapchat: {
enabled: true,
patterns: [ patterns: [
":shortLink", ":shortLink",
"spotlight/:spotlightId", "spotlight/:spotlightId",
@ -97,7 +87,6 @@ export const services = {
subdomains: ["t", "story"], subdomains: ["t", "story"],
}, },
soundcloud: { soundcloud: {
enabled: true,
patterns: [ patterns: [
":author/:song/s-:accessKey", ":author/:song/s-:accessKey",
":author/:song", ":author/:song",
@ -106,7 +95,6 @@ export const services = {
subdomains: ["on", "m"], subdomains: ["on", "m"],
}, },
streamable: { streamable: {
enabled: true,
patterns: [ patterns: [
":id", ":id",
"o/:id", "o/:id",
@ -115,7 +103,6 @@ export const services = {
], ],
}, },
tiktok: { tiktok: {
enabled: true,
patterns: [ patterns: [
":user/video/:postId", ":user/video/:postId",
":id", ":id",
@ -126,7 +113,6 @@ export const services = {
subdomains: ["vt", "vm", "m"], subdomains: ["vt", "vm", "m"],
}, },
tumblr: { tumblr: {
enabled: true,
patterns: [ patterns: [
"post/:id", "post/:id",
"blog/view/:user/:id", "blog/view/:user/:id",
@ -136,12 +122,10 @@ export const services = {
subdomains: "*", subdomains: "*",
}, },
twitch: { twitch: {
enabled: true,
patterns: [":channel/clip/:clip"], patterns: [":channel/clip/:clip"],
tld: "tv", tld: "tv",
}, },
twitter: { twitter: {
enabled: true,
patterns: [ patterns: [
":user/status/:id", ":user/status/:id",
":user/status/:id/video/:index", ":user/status/:id/video/:index",
@ -153,12 +137,10 @@ export const services = {
altDomains: ["x.com", "vxtwitter.com", "fixvx.com"], altDomains: ["x.com", "vxtwitter.com", "fixvx.com"],
}, },
vine: { vine: {
enabled: true,
patterns: ["v/:id"], patterns: ["v/:id"],
tld: "co", tld: "co",
}, },
vimeo: { vimeo: {
enabled: true,
patterns: [ patterns: [
":id", ":id",
"video/:id", "video/:id",
@ -168,7 +150,6 @@ export const services = {
subdomains: ["player"], subdomains: ["player"],
}, },
vk: { vk: {
enabled: true,
patterns: [ patterns: [
"video:userId_:videoId", "video:userId_:videoId",
"clip:userId_:videoId", "clip:userId_:videoId",
@ -177,7 +158,6 @@ export const services = {
subdomains: ["m"], subdomains: ["m"],
}, },
youtube: { youtube: {
enabled: true,
patterns: [ patterns: [
"watch?v=:id", "watch?v=:id",
"embed/:id", "embed/:id",

View file

@ -1,6 +1,8 @@
import { services } from "./service-config.js";
import { strict as assert } from "node:assert";
import psl from "psl"; import psl from "psl";
import { strict as assert } from "node:assert";
import { env } from "../config.js";
import { services } from "./service-config.js";
function aliasURL(url) { function aliasURL(url) {
assert(url instanceof URL); assert(url instanceof URL);
@ -160,8 +162,12 @@ export function extract(url) {
const host = getHostIfValid(url); const host = getHostIfValid(url);
if (!host || !services[host].enabled) { if (!host) {
return null; return { error: "unsupported" };
}
if (!env.enabledServices.has(host)) {
return { error: "disabled" };
} }
let patternMatch; let patternMatch;