proxy i.redd.it and v.redd.it media links in comments (#307)
This commit is contained in:
parent
1b6e1e5494
commit
3612345fab
|
@ -195,7 +195,54 @@ module.exports = function(request, fs) {
|
||||||
let instagramRegex = /(?<=href=")(https?:\/\/)(www+\.)?instagram.com(?=.+")/gm;
|
let instagramRegex = /(?<=href=")(https?:\/\/)(www+\.)?instagram.com(?=.+")/gm;
|
||||||
|
|
||||||
let protocol = config.https_enabled || config.api_force_https ? 'https://' : 'http://'
|
let protocol = config.https_enabled || config.api_force_https ? 'https://' : 'http://'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Special handling for reddit media domains in comments hrefs.
|
||||||
|
* For example a comment might have a direct links to images in i.redd.it:
|
||||||
|
* <a href="https://i.redd.it/hly9gyg9gjh81.png">Just refer to this </a>
|
||||||
|
* We want to rewrite these hrefs, but we also need to include the media
|
||||||
|
* for our backend, so we know where to fetch the media from.
|
||||||
|
* That comment URL then becomes like this after rewriting, for example:
|
||||||
|
* <a href="https://teddit.net/hly9gyg9gjh81.png?teddit_proxy=i.redd.it">Just refer to this </a>
|
||||||
|
* And then in our backend, we check if we have a 'teddit_proxy' in the req
|
||||||
|
* query, and proceed to proxy if it does.
|
||||||
|
*/
|
||||||
|
const replacable_media_domains = ['i.redd.it', 'v.redd.it', 'preview.redd.it']
|
||||||
|
replacable_media_domains.forEach((domain) => {
|
||||||
|
if (str.includes(domain + "/")) {
|
||||||
|
const href_regex = new RegExp(`(?<=href=")(https?:\/\/)([A-z.]+\.)?(${domain})(.+?(?="))`, 'gm')
|
||||||
|
const hrefs = str.match(href_regex)
|
||||||
|
if (!hrefs) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hrefs.forEach((url) => {
|
||||||
|
let original_url = url
|
||||||
|
const valid_exts = ['png', 'jpg', 'jpeg', 'mp4', 'gif', 'gifv']
|
||||||
|
const file_ext = getFileExtension(url)
|
||||||
|
if (valid_exts.includes(file_ext)) {
|
||||||
|
url = url.replace(domain, config.domain)
|
||||||
|
|
||||||
|
// append the domain info to the query, for teddit backend
|
||||||
|
let u = new URL(url)
|
||||||
|
if (u.query) {
|
||||||
|
url += '&teddit_proxy=' + domain
|
||||||
|
} else {
|
||||||
|
url += '?teddit_proxy=' + domain
|
||||||
|
}
|
||||||
|
|
||||||
|
// also replace the protocol for instances using http only
|
||||||
|
if (protocol === 'http://' && u.protocol === 'https:') {
|
||||||
|
url.replace('https://', protocol)
|
||||||
|
}
|
||||||
|
str = str.replace(original_url, url)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Continue the normal replace logic
|
||||||
|
|
||||||
str = str.replace(redditRegex, protocol + config.domain)
|
str = str.replace(redditRegex, protocol + config.domain)
|
||||||
|
|
||||||
if(typeof(user_preferences) == 'undefined')
|
if(typeof(user_preferences) == 'undefined')
|
||||||
|
|
|
@ -18,15 +18,31 @@ homeRoute.get('/:sort?', async (req, res, next) => {
|
||||||
let proxyable =
|
let proxyable =
|
||||||
sortby.includes('.jpg') ||
|
sortby.includes('.jpg') ||
|
||||||
sortby.includes('.png') ||
|
sortby.includes('.png') ||
|
||||||
sortby.includes('.jpeg')
|
sortby.includes('.jpeg') ||
|
||||||
|
sortby.includes('.mp4') ||
|
||||||
|
sortby.includes('.gif') ||
|
||||||
|
sortby.includes('.gifv')
|
||||||
? true
|
? true
|
||||||
: false;
|
: false;
|
||||||
if (proxyable) {
|
if (proxyable) {
|
||||||
let params = new URLSearchParams(req.query).toString();
|
let media_url = '';
|
||||||
let image_url = `https://preview.redd.it/${sortby}?${params}`;
|
const replacable_media_domains = ['i.redd.it', 'v.redd.it']
|
||||||
let proxied_image = await downloadAndSave(image_url);
|
if (req.query.teddit_proxy) {
|
||||||
if (proxied_image) {
|
if (replacable_media_domains.includes(req.query.teddit_proxy)) {
|
||||||
return res.redirect(proxied_image);
|
let full_url = req.protocol + '://' + req.get('host') + req.originalUrl;
|
||||||
|
let u = new URL(full_url);
|
||||||
|
let filename = u.pathname || '';
|
||||||
|
let query = u.search || '';
|
||||||
|
media_url = `https://${req.query.teddit_proxy}${filename}${query}`;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let params = new URLSearchParams(req.query).toString();
|
||||||
|
media_url = `https://preview.redd.it/${sortby}?${params}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
let proxied_media = await downloadAndSave(media_url);
|
||||||
|
if (proxied_media) {
|
||||||
|
return res.redirect(proxied_media);
|
||||||
} else {
|
} else {
|
||||||
return res.redirect('/');
|
return res.redirect('/');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue