|
|
@ -11,7 +11,7 @@ const logger = createLogger(proxyName);
|
|
|
|
const sessionCacheKey = `${proxyName}__sessionId`;
|
|
|
|
const sessionCacheKey = `${proxyName}__sessionId`;
|
|
|
|
const isNgCacheKey = `${proxyName}__isNg`;
|
|
|
|
const isNgCacheKey = `${proxyName}__isNg`;
|
|
|
|
|
|
|
|
|
|
|
|
async function fetchFromPyloadAPI(url, sessionId, params) {
|
|
|
|
async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
|
|
|
const options = {
|
|
|
|
const options = {
|
|
|
|
body: params
|
|
|
|
body: params
|
|
|
|
? Object.keys(params)
|
|
|
|
? Object.keys(params)
|
|
|
@ -25,10 +25,10 @@ async function fetchFromPyloadAPI(url, sessionId, params) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// see https://github.com/benphelps/homepage/issues/517
|
|
|
|
// see https://github.com/benphelps/homepage/issues/517
|
|
|
|
const isNg = cache.get(isNgCacheKey);
|
|
|
|
const isNg = cache.get(`${isNgCacheKey}.${service}`);
|
|
|
|
if (isNg && !params) {
|
|
|
|
if (isNg && !params) {
|
|
|
|
delete options.body;
|
|
|
|
delete options.body;
|
|
|
|
options.headers.Cookie = cache.get(sessionCacheKey);
|
|
|
|
options.headers.Cookie = cache.get(`${sessionCacheKey}.${service}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
|
// eslint-disable-next-line no-unused-vars
|
|
|
@ -43,19 +43,19 @@ async function fetchFromPyloadAPI(url, sessionId, params) {
|
|
|
|
return [status, returnData, responseHeaders];
|
|
|
|
return [status, returnData, responseHeaders];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async function login(loginUrl, username, password = '') {
|
|
|
|
async function login(loginUrl, service, username, password = '') {
|
|
|
|
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(loginUrl, null, { username, password });
|
|
|
|
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(loginUrl, null, { username, password }, service);
|
|
|
|
|
|
|
|
|
|
|
|
// this API actually returns status 200 even on login failure
|
|
|
|
// this API actually returns status 200 even on login failure
|
|
|
|
if (status !== 200 || sessionId === false) {
|
|
|
|
if (status !== 200 || sessionId === false) {
|
|
|
|
logger.error(`HTTP ${status} logging into Pyload API, returned: ${JSON.stringify(sessionId)}`);
|
|
|
|
logger.error(`HTTP ${status} logging into Pyload API, returned: ${JSON.stringify(sessionId)}`);
|
|
|
|
} else if (responseHeaders['set-cookie']?.join().includes('pyload_session')) {
|
|
|
|
} else if (responseHeaders['set-cookie']?.join().includes('pyload_session')) {
|
|
|
|
// Support pyload-ng, see https://github.com/benphelps/homepage/issues/517
|
|
|
|
// Support pyload-ng, see https://github.com/benphelps/homepage/issues/517
|
|
|
|
cache.put(isNgCacheKey, true);
|
|
|
|
cache.put(`${isNgCacheKey}.${service}`, true);
|
|
|
|
const sessionCookie = responseHeaders['set-cookie'][0];
|
|
|
|
const sessionCookie = responseHeaders['set-cookie'][0];
|
|
|
|
cache.put(sessionCacheKey, sessionCookie, 60 * 60 * 23 * 1000); // cache for 23h
|
|
|
|
cache.put(`${sessionCacheKey}.${service}`, sessionCookie, 60 * 60 * 23 * 1000); // cache for 23h
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
cache.put(sessionCacheKey, sessionId);
|
|
|
|
cache.put(`${sessionCacheKey}.${service}`, sessionId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return sessionId;
|
|
|
|
return sessionId;
|
|
|
@ -72,14 +72,14 @@ export default async function pyloadProxyHandler(req, res) {
|
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
|
|
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
|
|
|
const loginUrl = `${widget.url}/api/login`;
|
|
|
|
const loginUrl = `${widget.url}/api/login`;
|
|
|
|
|
|
|
|
|
|
|
|
let sessionId = cache.get(sessionCacheKey) ?? await login(loginUrl, widget.username, widget.password);
|
|
|
|
let sessionId = cache.get(`${sessionCacheKey}.${service}`) ?? await login(loginUrl, service, widget.username, widget.password);
|
|
|
|
let [status, data] = await fetchFromPyloadAPI(url, sessionId);
|
|
|
|
let [status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
|
|
|
|
|
|
|
|
|
|
|
if (status === 403 || status === 401) {
|
|
|
|
if (status === 403 || status === 401) {
|
|
|
|
logger.info('Failed to retrieve data from Pyload API, trying to login again...');
|
|
|
|
logger.info('Failed to retrieve data from Pyload API, trying to login again...');
|
|
|
|
cache.del(sessionCacheKey);
|
|
|
|
cache.del(`${sessionCacheKey}.${service}`);
|
|
|
|
sessionId = await login(loginUrl, widget.username, widget.password);
|
|
|
|
sessionId = await login(loginUrl, service, widget.username, widget.password);
|
|
|
|
[status, data] = await fetchFromPyloadAPI(url, sessionId);
|
|
|
|
[status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (data?.error || status !== 200) {
|
|
|
|
if (data?.error || status !== 200) {
|
|
|
|