2020-06-19 05:57:34 +00:00
|
|
|
/**
|
|
|
|
* Check that a host is part of Instagram's CDN.
|
|
|
|
* @param {string} host
|
|
|
|
*/
|
|
|
|
function verifyHost(host) {
|
|
|
|
const domains = ["fbcdn.net", "cdninstagram.com"]
|
|
|
|
return domains.some(against => host === against || host.endsWith("." + against))
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that a resource is on Instagram.
|
|
|
|
* @param {URL} completeURL
|
|
|
|
*/
|
|
|
|
function verifyURL(completeURL) {
|
|
|
|
const params = completeURL.searchParams
|
|
|
|
if (!params.get("url")) return {status: "fail", value: [400, "Must supply `url` query parameter"]}
|
|
|
|
try {
|
|
|
|
var url = new URL(params.get("url"))
|
|
|
|
} catch (e) {
|
|
|
|
return {status: "fail", value: [400, "`url` query parameter is not a valid URL"]}
|
|
|
|
}
|
|
|
|
// check url protocol
|
|
|
|
if (url.protocol !== "https:") return {status: "fail", value: [400, "URL protocol must be `https:`"]}
|
|
|
|
// check url host
|
|
|
|
if (!verifyHost(url.host)) return {status: "fail", value: [400, "URL host is not allowed"]}
|
|
|
|
return {status: "ok", url}
|
|
|
|
}
|
|
|
|
|
2020-01-18 15:38:14 +00:00
|
|
|
function proxyImage(url, width) {
|
2020-01-14 14:38:33 +00:00
|
|
|
const params = new URLSearchParams()
|
2020-01-18 15:38:14 +00:00
|
|
|
if (width) params.set("width", width)
|
2020-01-14 14:38:33 +00:00
|
|
|
params.set("url", url)
|
|
|
|
return "/imageproxy?"+params.toString()
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:57:31 +00:00
|
|
|
function proxyProfilePic(url, userID) {
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
params.set("userID", userID)
|
|
|
|
params.set("url", url)
|
|
|
|
return "/imageproxy?"+params.toString()
|
|
|
|
}
|
|
|
|
|
2020-01-29 15:20:20 +00:00
|
|
|
function proxyVideo(url) {
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
params.set("url", url)
|
|
|
|
return "/videoproxy?"+params.toString()
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:56:59 +00:00
|
|
|
/**
|
|
|
|
* @param {import("../types").ExtendedOwner} owner
|
|
|
|
*/
|
|
|
|
function proxyExtendedOwner(owner) {
|
|
|
|
const clone = {...owner}
|
2020-06-19 05:57:34 +00:00
|
|
|
clone.profile_pic_url = proxyProfilePic(clone.profile_pic_url, clone.id)
|
2020-01-26 14:56:59 +00:00
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2020-01-14 14:38:33 +00:00
|
|
|
module.exports.proxyImage = proxyImage
|
2020-04-04 14:57:31 +00:00
|
|
|
module.exports.proxyProfilePic = proxyProfilePic
|
2020-01-29 15:20:20 +00:00
|
|
|
module.exports.proxyVideo = proxyVideo
|
2020-01-26 14:56:59 +00:00
|
|
|
module.exports.proxyExtendedOwner = proxyExtendedOwner
|
2020-06-19 05:57:34 +00:00
|
|
|
module.exports.verifyHost = verifyHost
|
|
|
|
module.exports.verifyURL = verifyURL
|