Fixed twimg redirects

Closes https://github.com/libredirect/libredirect/issues/307
This commit is contained in:
Hygna 2022-10-06 14:19:05 +01:00
parent 3bd83642f3
commit 04fd2de0bb
No known key found for this signature in database
1 changed files with 21 additions and 32 deletions

View File

@ -150,9 +150,13 @@ function redirect(url, type, initiator) {
if (search !== "") search = `?${search}` if (search !== "") search = `?${search}`
if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") { if (url.host.split(".")[0] === "pbs" || url.host.split(".")[0] === "video") {
const [, id, format, extra] = url.search.match(/(.*)\?format=(.*)&(.*)/) try {
const query = encodeURIComponent(`${id}.${format}?${extra}`) const [, id, format, extra] = search.match(/(.*)\?format=(.*)&(.*)/)
return `${randomInstance}/pic${search}${query}` const query = encodeURIComponent(`${id}.${format}?${extra}`)
return `${randomInstance}/pic${url.pathname}${query}`
} catch {
return `${randomInstance}/pic${url.pathname}${search}`
}
} }
if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${search}` if (url.pathname.split("/").includes("tweets")) return `${randomInstance}${url.pathname.replace("/tweets", "")}${search}`
@ -412,20 +416,13 @@ function computeService(url, returnFrontend) {
const options = r.options const options = r.options
for (const service in config.services) { for (const service in config.services) {
if (regexArray(service, url, config)) { if (regexArray(service, url, config)) {
if (returnFrontend) { resolve(service)
resolve([service, null])
} else {
resolve(service)
}
return return
} else { } else {
for (const frontend in config.services[service].frontends) { for (const frontend in config.services[service].frontends) {
if (all(service, frontend, options, config, redirects).includes(utils.protocolHost(url))) { if (all(service, frontend, options, config, redirects).includes(utils.protocolHost(url))) {
if (returnFrontend) { if (returnFrontend) resolve([service, frontend, url.hostname])
resolve([service, frontend]) else resolve(service)
} else {
resolve(service)
}
return return
} }
} }
@ -601,12 +598,8 @@ function initDefaults() {
const latency = {} const latency = {}
for (const service in config.services) { for (const service in config.services) {
options[service] = {} options[service] = {}
if (config.services[service].targets == "datajson") { if (config.services[service].targets == "datajson") targets[service] = redirects[service]
targets[service] = redirects[service] for (const defaultOption in config.services[service].options) options[service][defaultOption] = config.services[service].options[defaultOption]
}
for (const defaultOption in config.services[service].options) {
options[service][defaultOption] = config.services[service].options[defaultOption]
}
for (const frontend in config.services[service].frontends) { for (const frontend in config.services[service].frontends) {
if (config.services[service].frontends[frontend].instanceList) { if (config.services[service].frontends[frontend].instanceList) {
options[frontend] = {} options[frontend] = {}
@ -717,24 +710,20 @@ function processUpdate() {
let targets = r.targets let targets = r.targets
let config = JSON.parse(configData) let config = JSON.parse(configData)
for (const service in config.services) { for (const service in config.services) {
if (!options[service]) { if (!options[service]) options[service] = {}
options[service] = {} if (config.services[service].targets == "datajson") targets[service] = redirects[service]
if (config.services[service].targets == "datajson") { for (const defaultOption in config.services[service].options) if (!options[service][defaultOption]) options[service][defaultOption] = config.services[service].options[defaultOption]
targets[service] = redirects[service]
}
for (const defaultOption in config.services[service].options) {
options[service][defaultOption] = config.services[service].options[defaultOption]
}
}
for (const frontend in config.services[service].frontends) { for (const frontend in config.services[service].frontends) {
if (!options[service][frontend]) { if (config.services[service].frontends[frontend].instanceList) {
if (config.services[service].frontends[frontend].instanceList) { for (const network in config.networks) {
options[frontend] = {} if (!options[frontend][network]) {
for (const network in config.networks) {
options[frontend][network] = {} options[frontend][network] = {}
options[frontend][network].enabled = JSON.parse(data)[frontend][network] options[frontend][network].enabled = JSON.parse(data)[frontend][network]
options[frontend][network].custom = [] options[frontend][network].custom = []
} }
}
if (!options[service][frontend]) {
options[frontend] = {}
for (const blacklist in r.blacklists) { for (const blacklist in r.blacklists) {
for (const instance of r.blacklists[blacklist]) { for (const instance of r.blacklists[blacklist]) {
let i = options[frontend].clearnet.enabled.indexOf(instance) let i = options[frontend].clearnet.enabled.indexOf(instance)