merge: 10.9 from main
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import * as cluster from "../../misc/cluster.js";
|
||||
|
||||
import { Agent } from "undici";
|
||||
import { env } from "../../config.js";
|
||||
import { Green, Yellow } from "../../misc/console-text.js";
|
||||
|
||||
const defaultAgent = new Agent();
|
||||
|
||||
let session;
|
||||
|
||||
const validateSession = (sessionResponse) => {
|
||||
@@ -32,7 +35,11 @@ const loadSession = async () => {
|
||||
const sessionServerUrl = new URL(env.ytSessionServer);
|
||||
sessionServerUrl.pathname = "/token";
|
||||
|
||||
const newSession = await fetch(sessionServerUrl).then(a => a.json());
|
||||
const newSession = await fetch(
|
||||
sessionServerUrl,
|
||||
{ dispatcher: defaultAgent }
|
||||
).then(a => a.json());
|
||||
|
||||
validateSession(newSession);
|
||||
|
||||
if (!session || session.updated < newSession?.updated) {
|
||||
|
||||
@@ -118,14 +118,13 @@ export function normalizeRequest(request) {
|
||||
));
|
||||
}
|
||||
|
||||
export function getIP(req) {
|
||||
export function getIP(req, prefix = 56) {
|
||||
const strippedIP = req.ip.replace(/^::ffff:/, '');
|
||||
const ip = ipaddr.parse(strippedIP);
|
||||
if (ip.kind() === 'ipv4') {
|
||||
return strippedIP;
|
||||
}
|
||||
|
||||
const prefix = 56;
|
||||
const v6Bytes = ip.toByteArray();
|
||||
v6Bytes.fill(0, prefix / 8);
|
||||
|
||||
|
||||
@@ -90,7 +90,9 @@ export const services = {
|
||||
"r/u_:user/comments/:id/:title",
|
||||
"r/u_:user/comments/:id/comment/:commentId",
|
||||
|
||||
"r/:sub/s/:shareId"
|
||||
"r/:sub/s/:shareId",
|
||||
|
||||
"video/:shortId",
|
||||
],
|
||||
subdomains: "*",
|
||||
},
|
||||
|
||||
@@ -23,7 +23,8 @@ export const testers = {
|
||||
pattern.id?.length <= 16 && !pattern.sub && !pattern.user
|
||||
|| (pattern.sub?.length <= 22 && pattern.id?.length <= 16)
|
||||
|| (pattern.user?.length <= 22 && pattern.id?.length <= 16)
|
||||
|| (pattern.sub?.length <= 22 && pattern.shareId?.length <= 16),
|
||||
|| (pattern.sub?.length <= 22 && pattern.shareId?.length <= 16)
|
||||
|| (pattern.shortId?.length <= 16),
|
||||
|
||||
"rutube": pattern =>
|
||||
(pattern.id?.length === 32 && pattern.key?.length <= 32) ||
|
||||
|
||||
@@ -527,7 +527,7 @@ export default function instagram(obj) {
|
||||
// for some reason instagram decides to return HTML
|
||||
// instead of a redirect when requesting with a normal
|
||||
// browser user-agent
|
||||
'curl/7.88.1'
|
||||
{'User-Agent': 'curl/7.88.1'}
|
||||
).then(match => instagram({
|
||||
...obj, ...match,
|
||||
shareId: undefined
|
||||
|
||||
@@ -50,12 +50,24 @@ async function getAccessToken() {
|
||||
|
||||
export default async function(obj) {
|
||||
let params = obj;
|
||||
const accessToken = await getAccessToken();
|
||||
const headers = {
|
||||
'user-agent': genericUserAgent,
|
||||
authorization: accessToken && `Bearer ${accessToken}`,
|
||||
accept: 'application/json'
|
||||
};
|
||||
|
||||
if (params.shortId) {
|
||||
params = await resolveRedirectingURL(
|
||||
`https://www.reddit.com/video/${params.shortId}`,
|
||||
obj.dispatcher, headers
|
||||
);
|
||||
}
|
||||
|
||||
if (!params.id && params.shareId) {
|
||||
params = await resolveRedirectingURL(
|
||||
`https://www.reddit.com/r/${params.sub}/s/${params.shareId}`,
|
||||
obj.dispatcher,
|
||||
genericUserAgent
|
||||
obj.dispatcher, headers
|
||||
);
|
||||
}
|
||||
|
||||
@@ -63,17 +75,10 @@ export default async function(obj) {
|
||||
|
||||
const url = new URL(`https://www.reddit.com/comments/${params.id}.json`);
|
||||
|
||||
const accessToken = await getAccessToken();
|
||||
if (accessToken) url.hostname = 'oauth.reddit.com';
|
||||
|
||||
let data = await fetch(
|
||||
url, {
|
||||
headers: {
|
||||
'User-Agent': genericUserAgent,
|
||||
accept: 'application/json',
|
||||
authorization: accessToken && `Bearer ${accessToken}`
|
||||
}
|
||||
}
|
||||
url, { headers }
|
||||
).then(r => r.json()).catch(() => {});
|
||||
|
||||
if (!data || !Array.isArray(data)) {
|
||||
|
||||
@@ -106,6 +106,14 @@ function aliasURL(url) {
|
||||
url.pathname = `/share/${idPart.slice(-32)}`;
|
||||
}
|
||||
break;
|
||||
|
||||
case "redd":
|
||||
/* reddit short video links can be treated by changing https://v.redd.it/<id>
|
||||
to https://reddit.com/video/<id>.*/
|
||||
if (url.hostname === "v.redd.it" && parts.length === 2) {
|
||||
url = new URL(`https://www.reddit.com/video/${parts[1]}`);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return url;
|
||||
@@ -231,11 +239,11 @@ export function extract(url) {
|
||||
return { host, patternMatch };
|
||||
}
|
||||
|
||||
export async function resolveRedirectingURL(url, dispatcher, userAgent) {
|
||||
export async function resolveRedirectingURL(url, dispatcher, headers) {
|
||||
const originalService = getHostIfValid(normalizeURL(url));
|
||||
if (!originalService) return;
|
||||
|
||||
const canonicalURL = await getRedirectingURL(url, dispatcher, userAgent);
|
||||
const canonicalURL = await getRedirectingURL(url, dispatcher, headers);
|
||||
if (!canonicalURL) return;
|
||||
|
||||
const { host, patternMatch } = extract(normalizeURL(canonicalURL));
|
||||
|
||||
Reference in New Issue
Block a user