2022-11-08 21:47:12 +01:00
|
|
|
import cache from 'memory-cache';
|
2022-11-06 10:35:41 -08:00
|
|
|
|
2022-11-08 21:47:12 +01:00
|
|
|
import getServiceWidget from 'utils/config/service-helpers';
|
|
|
|
import { formatApiCall } from 'utils/proxy/api-helpers';
|
|
|
|
import widgets from 'widgets/widgets';
|
|
|
|
import createLogger from 'utils/logger';
|
|
|
|
import { httpProxy } from 'utils/proxy/http';
|
2022-11-06 10:35:41 -08:00
|
|
|
|
|
|
|
const proxyName = 'pyloadProxyHandler';
|
|
|
|
const logger = createLogger(proxyName);
|
|
|
|
const sessionCacheKey = `${proxyName}__sessionId`;
|
2022-11-10 13:16:10 -08:00
|
|
|
const isNgCacheKey = `${proxyName}__isNg`;
|
2022-11-06 10:35:41 -08:00
|
|
|
|
2022-12-09 21:51:24 -08:00
|
|
|
async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
2022-11-06 10:35:41 -08:00
|
|
|
const options = {
|
2022-11-08 21:47:12 +01:00
|
|
|
body: params
|
|
|
|
? Object.keys(params)
|
|
|
|
.map((prop) => `${prop}=${params[prop]}`)
|
|
|
|
.join('&')
|
|
|
|
: `session=${sessionId}`,
|
|
|
|
method: 'POST',
|
2022-11-06 10:35:41 -08:00
|
|
|
headers: {
|
2022-11-08 21:47:12 +01:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
2022-11-06 10:35:41 -08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-10 13:16:10 -08:00
|
|
|
// see https://github.com/benphelps/homepage/issues/517
|
2022-12-09 21:51:24 -08:00
|
|
|
const isNg = cache.get(`${isNgCacheKey}.${service}`);
|
2022-11-10 13:16:10 -08:00
|
|
|
if (isNg && !params) {
|
|
|
|
delete options.body;
|
2022-12-09 21:51:24 -08:00
|
|
|
options.headers.Cookie = cache.get(`${sessionCacheKey}.${service}`);
|
2022-11-10 13:16:10 -08:00
|
|
|
}
|
|
|
|
|
2022-11-06 11:24:59 -08:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2022-11-10 13:16:10 -08:00
|
|
|
const [status, contentType, data, responseHeaders] = await httpProxy(url, options);
|
2022-11-11 12:39:54 -08:00
|
|
|
let returnData;
|
|
|
|
try {
|
|
|
|
returnData = JSON.parse(Buffer.from(data).toString());
|
|
|
|
} catch(e) {
|
|
|
|
logger.error(`Error logging into pyload API: ${JSON.stringify(data)}`);
|
|
|
|
returnData = data;
|
|
|
|
}
|
|
|
|
return [status, returnData, responseHeaders];
|
2022-11-06 10:35:41 -08:00
|
|
|
}
|
|
|
|
|
2022-12-09 21:51:24 -08:00
|
|
|
async function login(loginUrl, service, username, password = '') {
|
|
|
|
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(loginUrl, null, { username, password }, service);
|
2022-11-11 12:39:54 -08:00
|
|
|
|
|
|
|
// this API actually returns status 200 even on login failure
|
|
|
|
if (status !== 200 || sessionId === false) {
|
|
|
|
logger.error(`HTTP ${status} logging into Pyload API, returned: ${JSON.stringify(sessionId)}`);
|
|
|
|
} else if (responseHeaders['set-cookie']?.join().includes('pyload_session')) {
|
2022-11-10 13:16:10 -08:00
|
|
|
// Support pyload-ng, see https://github.com/benphelps/homepage/issues/517
|
2022-12-09 21:51:24 -08:00
|
|
|
cache.put(`${isNgCacheKey}.${service}`, true);
|
2022-11-11 12:39:54 -08:00
|
|
|
const sessionCookie = responseHeaders['set-cookie'][0];
|
2022-12-09 21:51:24 -08:00
|
|
|
cache.put(`${sessionCacheKey}.${service}`, sessionCookie, 60 * 60 * 23 * 1000); // cache for 23h
|
2022-11-11 12:39:54 -08:00
|
|
|
} else {
|
2022-12-09 21:51:24 -08:00
|
|
|
cache.put(`${sessionCacheKey}.${service}`, sessionId);
|
2022-11-06 11:24:59 -08:00
|
|
|
}
|
2022-11-11 12:39:54 -08:00
|
|
|
|
|
|
|
return sessionId;
|
2022-11-06 10:35:41 -08:00
|
|
|
}
|
2022-11-06 11:05:31 +01:00
|
|
|
|
|
|
|
export default async function pyloadProxyHandler(req, res) {
|
|
|
|
const { group, service, endpoint } = req.query;
|
|
|
|
|
2022-11-06 11:24:59 -08:00
|
|
|
try {
|
|
|
|
if (group && service) {
|
|
|
|
const widget = await getServiceWidget(group, service);
|
2022-11-08 21:47:12 +01:00
|
|
|
|
2022-11-06 11:24:59 -08:00
|
|
|
if (widget) {
|
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
|
|
|
const loginUrl = `${widget.url}/api/login`;
|
2022-11-06 10:35:41 -08:00
|
|
|
|
2022-12-09 21:51:24 -08:00
|
|
|
let sessionId = cache.get(`${sessionCacheKey}.${service}`) ?? await login(loginUrl, service, widget.username, widget.password);
|
|
|
|
let [status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
2022-11-06 11:05:31 +01:00
|
|
|
|
2022-11-11 12:39:54 -08:00
|
|
|
if (status === 403 || status === 401) {
|
|
|
|
logger.info('Failed to retrieve data from Pyload API, trying to login again...');
|
2022-12-09 21:51:24 -08:00
|
|
|
cache.del(`${sessionCacheKey}.${service}`);
|
|
|
|
sessionId = await login(loginUrl, service, widget.username, widget.password);
|
|
|
|
[status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
2022-11-06 11:24:59 -08:00
|
|
|
}
|
2022-11-08 21:47:12 +01:00
|
|
|
|
2022-11-06 11:24:59 -08:00
|
|
|
if (data?.error || status !== 200) {
|
2022-11-11 12:39:54 -08:00
|
|
|
try {
|
2022-10-26 08:35:38 -07:00
|
|
|
return res.status(status).send({error: {message: "HTTP error communicating with Plex API", data: Buffer.from(data).toString()}});
|
2022-11-11 12:39:54 -08:00
|
|
|
} catch (e) {
|
2022-10-26 08:35:38 -07:00
|
|
|
return res.status(status).send({error: {message: "HTTP error communicating with Plex API", data}});
|
2022-11-11 12:39:54 -08:00
|
|
|
}
|
2022-11-06 11:24:59 -08:00
|
|
|
}
|
2022-11-06 10:35:41 -08:00
|
|
|
|
2022-11-06 11:24:59 -08:00
|
|
|
return res.json(data);
|
2022-11-06 10:35:41 -08:00
|
|
|
}
|
2022-11-06 11:05:31 +01:00
|
|
|
}
|
2022-11-06 11:24:59 -08:00
|
|
|
} catch (e) {
|
|
|
|
logger.error(e);
|
2022-10-26 08:35:38 -07:00
|
|
|
return res.status(500).send({error: {message: `Error communicating with Plex API: ${e.toString()}`}});
|
2022-11-06 11:05:31 +01:00
|
|
|
}
|
|
|
|
|
2022-11-08 21:47:12 +01:00
|
|
|
return res.status(400).json({ error: 'Invalid proxy service type' });
|
|
|
|
}
|