refactor(proxy): w/ cycling, rm fallback.

refactor(config): adjust for cycling.
This commit is contained in:
vulet 2021-07-23 14:02:32 +08:00
parent 2f58d6bb84
commit b7e73fc533
5 changed files with 55 additions and 39 deletions

View file

@ -1,8 +1,3 @@
const headers = ({ domain, userAgent }) => ({
Host: `${domain}`,
'User-Agent': `${userAgent}`,
});
const invidious = async (instance, url) => {
const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req;
@ -31,24 +26,28 @@ const card = (video, path) =>
`<br />(${video.date})</b> <br />
</blockquote>`;
const getInstance = config =>
const getInstance = (domain, config) =>
axios.create({
baseURL: `https://${config.domain}/api/v1/videos`,
headers: headers(config),
baseURL: `https://${domain}/api/v1/videos`,
headers: {
Host: `${domain}`,
'User-Agent': `${config.userAgent}`,
},
transformResponse: [],
timeout: 10 * 1000,
});
const run = async (roomId, userInput) => {
const video = await invidious(getInstance(config.invidious), userInput)
.catch(_ => invidious(getInstance(Object.assign(config.invidious, { domain: config.invidious.fallback })), userInput));
const cfg = config.invidious;
const video = await matrix.utils.retryPromise(cfg.domains.redirect, domain => invidious(getInstance(domain, cfg), userInput));
return matrixClient.sendHtmlNotice(roomId, '', card(video, userInput));
};
exports.runQuery = async (roomId, event, userInput) => {
try {
const url = new URL(userInput);
if (!config.invidious.domains.includes(url.hostname)) throw '';
const { redirect, original } = config.invidious.domains;
if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw '';
if (/^\/[\w-]{11}$/.test(url.pathname)) return await run(roomId, url.pathname.slice(1));
const params = new URLSearchParams(url.search).get('v');
if (!/^[\w-]{11}$/.test(params)) throw '';

View file

@ -1,10 +1,5 @@
const { JSDOM } = require('jsdom');
const headers = ({ domain, userAgent }) => ({
Host: `${domain}`,
'User-Agent': `${userAgent}`,
});
const nitter = async (instance, url) => {
const req = await instance({ method: 'GET', url });
if (req.statusText !== 'OK') throw req;
@ -52,24 +47,28 @@ const card = (tweet, check, path) =>
(tweet.isReply ? tweet.isReply === 'unavailable' ? '<blockquote>Replied Tweet is unavailable</blockquote>' : `<blockquote><b><a href="${tweet.url}${tweet.isReply.path}">Replied Tweet</a></b><br /><b><i>${tweet.isReply.text.replace('\n', '<br />')}</i></b></blockquote>` : '') +
(tweet.quote ? `<blockquote><b><a href="${tweet.url}${tweet.quote.path}">Quoted Tweet</a></b><br /><b><i>${tweet.quote.text.replace('\n', '<br />')}</i></b></blockquote>` : '');
const getInstance = config =>
const getInstance = (domain, config) =>
axios.create({
baseURL: `https://${config.domain}`,
headers: headers(config),
baseURL: `https://${domain}`,
headers: {
Host: `${domain}`,
'User-Agent': `${config.userAgent}`,
},
transformResponse: [],
timeout: 10 * 1000,
});
const run = async (roomId, userInput) => {
const tweet = await nitter(getInstance(config.nitter), userInput)
.catch(_ => nitter(getInstance(Object.assign(config.nitter, { domain: config.nitter.fallback })), userInput));
return matrixClient.sendHtmlNotice(roomId, '', card(tweet, config.nitter.check, userInput));
const cfg = config.nitter;
const tweet = await matrix.utils.retryPromise(cfg.domains.redirect, domain => nitter(getInstance(domain, cfg), userInput));
return matrixClient.sendHtmlNotice(roomId, '', card(tweet, cfg.check, userInput));
};
exports.runQuery = async (roomId, event, userInput) => {
try {
const url = new URL(userInput);
if (!config.nitter.domains.includes(url.hostname)) throw '';
const { redirect, original } = config.nitter.domains;
if (!redirect.includes(url.hostname) && !original.includes(url.hostname)) throw '';
if (!/^\/[^/]+\/status\/\d+\/?$/.test(url.pathname)) throw '';
return await run(roomId, url.pathname);
} catch (e) {

4
commands/proxy.js Normal file
View file

@ -0,0 +1,4 @@
const msg = 'Invalid proxy domain!';
exports.runQuery = (roomId, event, userInput) =>
matrixClient.sendHtmlNotice(roomId, msg, `<b>${msg}</b>`).catch(() => {});

View file

@ -23,16 +23,18 @@ module.exports = {
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)'
},
nitter: {
domain: 'nitter.fdn.fr',
fallback: 'nitter.snopyta.org',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'nitter.snopyta.org', 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com', 'nitter.fdn.fr' ],
check: '(✅)'
domains: {
redirect: [ 'nitter.fdn.fr', 'nitter.snopyta.org', 'nitter.net' ],
original: [ 'nitter.net', 'www.nitter.net', 'twitter.com', 'www.twitter.com', 'mobile.twitter.com', 'm.twitter.com' ]
},
check: '(✅)',
},
invidious: {
domain: 'invidious.fdn.fr',
fallback: 'invidious.snopyta.org',
userAgent: 'Mozilla/4.0 (compatible; Beep Boop)',
domains: [ 'invidious.snopyta.org', 'invidious.xyz', 'youtube.com', 'www.youtube.com', 'youtu.be', 'm.youtube.com', 'invidious.fdn.fr' ]
domains: {
redirect: [ 'invidious.fdn.fr', 'invidious.snopyta.org', 'invidious.xyz', 'inv.riverside.rocks', 'vid.puffyan.us', 'y.com.cm' ],
original: [ 'youtube.com', 'www.youtube.com', 'youtu.be', 'm.youtube.com' ]
}
}
};

View file

@ -49,9 +49,11 @@ const eventHandler = (args, roomId, command, event) => {
case 'proxy': case 'p':
try {
const url = new URL(userInput);
command = config.invidious.domains.includes(url.hostname)
const invidio = config.invidious.domains;
const nitter = config.nitter.domains;
command = invidio.redirect.includes(url.hostname) || invidio.original.includes(url.hostname)
? 'invidious'
: config.nitter.domains.includes(url.hostname)
: nitter.redirect.includes(url.hostname) || nitter.original.includes(url.hostname)
? 'nitter'
: 'proxy';
} catch (e) { sendError(event, roomId, e); }
@ -131,3 +133,13 @@ module.exports.selfReact = async (event) => {
if (config.fediverse.tipping === true) addReact(event, '🌧️');
}
};
module.exports.retryPromise = async (argList, promiseFn) => {
let err;
for(var arg of argList) {
try {
return await promiseFn(arg);
} catch(e) { err = e; }
}
throw err || new Error('retryPromise error');
};