diff --git a/.github/test.sh b/.github/test.sh index 85683b91..80de1fcd 100755 --- a/.github/test.sh +++ b/.github/test.sh @@ -18,7 +18,7 @@ test_api() { -X POST \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ - -d '{"url":"https://www.youtube.com/watch?v=jNQXAC9IVRw"}') + -d '{"url":"https://vine.co/v/huwVJIEJW50", "isAudioOnly": true}') echo "$API_RESPONSE" STATUS=$(echo "$API_RESPONSE" | jq -r .status) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 193ddd01..4ac2daf3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,3 +36,27 @@ jobs: uses: actions/checkout@v4 - name: Run test script run: .github/test.sh api + + check-services: + name: test service functionality + runs-on: ubuntu-latest + outputs: + services: ${{ steps.checkServices.outputs.service_list }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - id: checkServices + run: npm ci && echo "service_list=$(node src/util/test-ci get-services)" >> "$GITHUB_OUTPUT" + + test-services: + needs: check-services + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + service: ${{ fromJson(needs.check-services.outputs.services) }} + name: "test service: ${{ matrix.service }}" + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - run: npm ci && node src/util/test-ci run-tests-for ${{ matrix.service }} \ No newline at end of file diff --git a/README.md b/README.md index d2bb064c..d5fe8164 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,13 @@ this list is not final and keeps expanding over time. if support for a service y | bilibili.com & bilibili.tv | ✅ | ✅ | ✅ | ➖ | ➖ | | dailymotion | ✅ | ✅ | ✅ | ✅ | ✅ | | instagram posts & reels | ✅ | ✅ | ✅ | ➖ | ➖ | +| facebook videos | ✅ | ❌ | ❌ | ➖ | ➖ | | loom | ✅ | ❌ | ✅ | ✅ | ➖ | | ok video | ✅ | ❌ | ✅ | ✅ | ✅ | | pinterest | ✅ | ✅ | ✅ | ➖ | ➖ | | reddit | ✅ | ✅ | ✅ | ❌ | ❌ | | rutube | ✅ | ✅ | ✅ | ✅ | ✅ | +| snapchat stories & spotlights | ✅ | ✅ | ✅ | ➖ | ➖ | | soundcloud | ➖ | ✅ | ➖ | ✅ | ✅ | | streamable | ✅ | ✅ | ✅ | ➖ | ➖ | | tiktok | ✅ | ✅ | ✅ | ❌ | ❌ | @@ -45,8 +47,10 @@ this list is not final and keeps expanding over time. if support for a service y | service | notes or features | | :-------- | :----- | | instagram | supports reels, photos, and videos. lets you pick what to save from multi-media posts. | +| facebook | supports public accessible videos content only. | | pinterest | supports photos, gifs, videos and stories. | | reddit | supports gifs and videos. | +| snapchat | supports spotlights and stories. lets you pick what to save from stories. | | rutube | supports yappy & private links. | | soundcloud | supports private links. | | tiktok | supports videos with or without watermark, images from slideshow without watermark, and full (original) audios. | diff --git a/docs/run-an-instance.md b/docs/run-an-instance.md index a440d11d..d31a67f5 100644 --- a/docs/run-an-instance.md +++ b/docs/run-an-instance.md @@ -56,8 +56,9 @@ sudo service nscd start | `API_LISTEN_ADDRESS` | `0.0.0.0` | `127.0.0.1` | changes address from which api server is accessible. **if you are using docker, you usually don't need to configure this.** | | `API_URL` | ➖ | `https://api.cobalt.tools/` | changes url from which api server is accessible.
***REQUIRED TO RUN THE API***. | | `API_NAME` | `unknown` | `ams-1` | api server name that is shown in `/api/serverInfo`. | +| `API_EXTERNAL_PROXY` | ➖ | `http://user:password@127.0.0.1:8080`| url of the proxy that will be passed to [`ProxyAgent`](https://undici.nodejs.org/#/docs/api/ProxyAgent) and used for all external requests. HTTP(S) only. | | `CORS_WILDCARD` | `1` | `0` | toggles cross-origin resource sharing.
`0`: disabled. `1`: enabled. | -| `CORS_URL` | not used | `https://cobalt.tools/` | cross-origin resource sharing url. api will be available only from this url if `CORS_WILDCARD` is set to `0`. | +| `CORS_URL` | not used | `https://cobalt.tools` | cross-origin resource sharing url. api will be available only from this url if `CORS_WILDCARD` is set to `0`. | | `COOKIE_PATH` | not used | `/cookies.json` | path for cookie file relative to main folder. | | `PROCESSING_PRIORITY` | not used | `10` | changes `nice` value* for ffmpeg subprocess. available only on unix systems. | | `FREEBIND_CIDR` | ➖ | `2001:db8::/32` | IPv6 prefix used for randomly assigning addresses to cobalt requests. only supported on linux systems. see below for more info. | diff --git a/src/core/api.js b/src/core/api.js index 8eb4cb40..5f4ee804 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -1,5 +1,6 @@ import cors from "cors"; import rateLimit from "express-rate-limit"; +import { setGlobalDispatcher, ProxyAgent } from "undici"; import { env, version } from "../modules/config.js"; @@ -26,7 +27,7 @@ const corsConfig = env.corsWildcard ? {} : { export function runAPI(express, app, gitCommit, gitBranch, __dirname) { const startTime = new Date(); const startTimestamp = startTime.getTime(); - + const serverInfo = { version: version, commit: gitCommit, @@ -81,38 +82,23 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { app.use((req, res, next) => { try { decodeURIComponent(req.path) - } catch { + } catch { return res.redirect('/') } next(); }) - app.use('/api/json', express.json({ - verify: (req, res, buf) => { - if (String(req.header('Accept')) === "application/json") { - if (buf.length > 720) throw new Error(); - JSON.parse(buf); - } else { - throw new Error(); - } - } - })) - - // handle express.json errors properly (https://github.com/expressjs/express/issues/4065) - app.use('/api/json', (err, req, res, next) => { - let errorText = "invalid json body"; - const acceptHeader = String(req.header('Accept')) !== "application/json"; - - if (err || acceptHeader) { - if (acceptHeader) errorText = "invalid accept header"; + app.use('/api/json', express.json({ limit: 1024 })); + app.use('/api/json', (err, _, res, next) => { + if (err) { return res.status(400).json({ status: "error", - text: errorText + text: "invalid json body" }); - } else { - next(); } - }) + + next(); + }); app.post('/api/json', async (req, res) => { const request = req.body; @@ -123,6 +109,10 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { res.status(status).json(body); } + if (!acceptRegex.test(req.header('Accept'))) { + return fail('ErrorInvalidAcceptHeader'); + } + if (!acceptRegex.test(req.header('Content-Type'))) { return fail('ErrorInvalidContentType'); } @@ -219,6 +209,14 @@ export function runAPI(express, app, gitCommit, gitBranch, __dirname) { randomizeCiphers(); setInterval(randomizeCiphers, 1000 * 60 * 30); // shuffle ciphers every 30 minutes + if (env.externalProxy) { + if (env.freebindCIDR) { + throw new Error('Freebind is not available when external proxy is enabled') + } + + setGlobalDispatcher(new ProxyAgent(env.externalProxy)) + } + app.listen(env.apiPort, env.listenAddress, () => { console.log(`\n` + `${Cyan("cobalt")} API ${Bright(`v.${version}-${gitCommit} (${gitBranch})`)}\n` + diff --git a/src/localization/languages/en.json b/src/localization/languages/en.json index eecd9ac1..2b10f41d 100644 --- a/src/localization/languages/en.json +++ b/src/localization/languages/en.json @@ -159,6 +159,7 @@ "UpdateOneMillion": "1 million users and blazing speed", "ErrorYTAgeRestrict": "this youtube video is age-restricted, so i can't see it. try another one!", "ErrorYTLogin": "couldn't get this youtube video because it requires an account to view.\n\nthis limitation is done by google to seemingly stop scraping, affecting all 3rd party tools and even their own clients.\n\ntry again, but if issue persists, {ContactLink}.", - "ErrorYTRateLimit": "i got rate limited by youtube. try again in a few seconds, but if issue persists, {ContactLink}." + "ErrorYTRateLimit": "i got rate limited by youtube. try again in a few seconds, but if issue persists, {ContactLink}.", + "ErrorInvalidAcceptHeader": "invalid accept header" } } diff --git a/src/modules/config.js b/src/modules/config.js index 530c5f0b..662d8b05 100644 --- a/src/modules/config.js +++ b/src/modules/config.js @@ -48,7 +48,9 @@ const processingPriority: process.platform !== 'win32' && process.env.PROCESSING_PRIORITY - && parseInt(process.env.PROCESSING_PRIORITY) + && parseInt(process.env.PROCESSING_PRIORITY), + + externalProxy: process.env.API_EXTERNAL_PROXY, } export const diff --git a/src/modules/processing/match.js b/src/modules/processing/match.js index d9cbe5a4..accdeaee 100644 --- a/src/modules/processing/match.js +++ b/src/modules/processing/match.js @@ -24,8 +24,11 @@ import streamable from "./services/streamable.js"; import twitch from "./services/twitch.js"; import rutube from "./services/rutube.js"; import dailymotion from "./services/dailymotion.js"; +import snapchat from "./services/snapchat.js"; import loom from "./services/loom.js"; +import facebook from "./services/facebook.js"; import newgrounds from "./services/newgrounds.js"; + let freebind; export default async function(host, patternMatch, lang, obj) { @@ -188,11 +191,23 @@ export default async function(host, patternMatch, lang, obj) { case "dailymotion": r = await dailymotion(patternMatch); break; + case "snapchat": + r = await snapchat({ + url, + ...patternMatch + }); + break; case "loom": r = await loom({ id: patternMatch.id }); break; + case "facebook": + r = await facebook({ + ...patternMatch, + sourceUrl: url.href + }); + break; case "newgrounds": r = await newgrounds({ type: patternMatch.type, diff --git a/src/modules/processing/matchActionDecider.js b/src/modules/processing/matchActionDecider.js index 74f0f8c7..7643d491 100644 --- a/src/modules/processing/matchActionDecider.js +++ b/src/modules/processing/matchActionDecider.js @@ -73,6 +73,7 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di switch (host) { case "instagram": case "twitter": + case "snapchat": params = { picker: r.picker }; break; case "tiktok": @@ -130,11 +131,13 @@ export default function(r, host, userFormat, isAudioOnly, lang, isAudioMuted, di params = { type: "bridge" }; break; + case "facebook": case "vine": case "instagram": case "tumblr": case "pinterest": case "streamable": + case "snapchat": case "loom": responseType = "redirect"; break; diff --git a/src/modules/processing/services/facebook.js b/src/modules/processing/services/facebook.js new file mode 100644 index 00000000..45d31b5f --- /dev/null +++ b/src/modules/processing/services/facebook.js @@ -0,0 +1,62 @@ +import { genericUserAgent } from "../../config.js"; + +const headers = { + 'User-Agent': genericUserAgent, + 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', + 'Accept-Language': 'en-US,en;q=0.5', + 'Accept-Encoding': 'gzip, deflate, br', + 'Sec-Fetch-Mode': 'navigate', + 'Sec-Fetch-Site': 'none', +} + +function resolveUrl(url) { + return fetch(url, { headers }) + .then(r => { + if (r.headers.get('location')) { + return decodeURIComponent(r.headers.get('location')) + } + if (r.headers.get('link')) { + const linkMatch = r.headers.get('link').match(/<(.*?)\/>/) + return decodeURIComponent(linkMatch[1]) + } + return false + }) + .catch(() => false) +} + +export default async function({ sourceUrl, shortLink, username, id }) { + const isShortLink = !!shortLink?.length + const isSharedLink = !!sourceUrl.match(/\/share\/\w\//)?.length + + let url = isShortLink + ? `https://fb.watch/${shortLink}` + : `https://web.facebook.com/${username}/videos/${id}` + + if (isShortLink) url = await resolveUrl(url) + if (isSharedLink) url = sourceUrl + + const html = await fetch(url, { headers }) + .then(r => r.text()) + .catch(() => false) + + if (!html) return { error: 'ErrorCouldntFetch' }; + + const urls = [] + const hd = html.match('"browser_native_hd_url":(".*?")') + const sd = html.match('"browser_native_sd_url":(".*?")') + + if (hd?.[1]) urls.push(JSON.parse(hd[1])) + if (sd?.[1]) urls.push(JSON.parse(sd[1])) + + if (!urls.length) { + return { error: 'ErrorEmptyDownload' }; + } + + let filename = `facebook_${id || shortLink}.mp4` + + return { + urls: urls[0], + filename, + audioFilename: `${filename.slice(0, -4)}_audio`, + }; +} \ No newline at end of file diff --git a/src/modules/processing/services/ok.js b/src/modules/processing/services/ok.js index 97bbcf82..33847cd8 100644 --- a/src/modules/processing/services/ok.js +++ b/src/modules/processing/services/ok.js @@ -45,7 +45,7 @@ export default async function(o) { let fileMetadata = { title: cleanString(videoData.movie.title.trim()), - author: cleanString(videoData.author.name.trim()), + author: cleanString((videoData.author?.name || videoData.compilationTitle).trim()), } if (bestVideo) return { diff --git a/src/modules/processing/services/snapchat.js b/src/modules/processing/services/snapchat.js new file mode 100644 index 00000000..44a2b84e --- /dev/null +++ b/src/modules/processing/services/snapchat.js @@ -0,0 +1,96 @@ +import { genericUserAgent } from "../../config.js"; +import { getRedirectingURL } from "../../sub/utils.js"; +import { extract, normalizeURL } from "../url.js"; + +const SPOTLIGHT_VIDEO_REGEX = //; +const NEXT_DATA_REGEX = /