refactor: custom proxy handler for FreshRSS
This commit is contained in:
parent
c9e9b9692f
commit
651e06aa25
@ -9,17 +9,13 @@ export default function Component({ service }) {
|
|||||||
|
|
||||||
const { widget } = service;
|
const { widget } = service;
|
||||||
|
|
||||||
const { data: subscriptionsData, error: subscriptionsError } = useWidgetAPI(widget, "subscriptions");
|
const { data: freshrssData, error: freshrssError } = useWidgetAPI(widget, "info");
|
||||||
const { data: unreadData, error: unreadError } = useWidgetAPI(widget, "unread");
|
|
||||||
|
|
||||||
if (subscriptionsError) {
|
if (freshrssError) {
|
||||||
return <Container error={subscriptionsError} />;
|
return <Container error={freshrssError} />;
|
||||||
}
|
|
||||||
if (unreadError) {
|
|
||||||
return <Container error={unreadError} />;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!subscriptionsData || !unreadData) {
|
if (!freshrssData) {
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="freshrss.unread" />
|
<Block label="freshrss.unread" />
|
||||||
@ -30,8 +26,8 @@ export default function Component({ service }) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<Container service={service}>
|
<Container service={service}>
|
||||||
<Block label="freshrss.unread" value={t("common.number", { value: unreadData.count })} />
|
<Block label="freshrss.unread" value={t("common.number", { value: freshrssData.unread })} />
|
||||||
<Block label="freshrss.subscriptions" value={t("common.number", { value: subscriptionsData.count })} />
|
<Block label="freshrss.subscriptions" value={t("common.number", { value: freshrssData.subscriptions })} />
|
||||||
</Container>
|
</Container>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
97
src/widgets/freshrss/proxy.js
Normal file
97
src/widgets/freshrss/proxy.js
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
import cache from "memory-cache";
|
||||||
|
|
||||||
|
import { httpProxy } from "utils/proxy/http";
|
||||||
|
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||||
|
import getServiceWidget from "utils/config/service-helpers";
|
||||||
|
import createLogger from "utils/logger";
|
||||||
|
import widgets from "widgets/widgets";
|
||||||
|
|
||||||
|
const proxyName = "freshrssProxyHandler";
|
||||||
|
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
|
||||||
|
const logger = createLogger(proxyName);
|
||||||
|
|
||||||
|
async function login(widget, service) {
|
||||||
|
const endpoint = "accounts/ClientLogin";
|
||||||
|
const api = widgets?.[widget.type]?.api
|
||||||
|
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||||
|
const headers = { "Content-Type": "application/x-www-form-urlencoded" };
|
||||||
|
|
||||||
|
const [, , data,] = await httpProxy(loginUrl, {
|
||||||
|
method: "POST",
|
||||||
|
body: new URLSearchParams({
|
||||||
|
Email: widget.username,
|
||||||
|
Passwd: widget.password
|
||||||
|
}).toString(),
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
const [, token] = data.toString().split("\n").find(line => line.startsWith("Auth=")).split("=")
|
||||||
|
cache.put(`${sessionTokenCacheKey}.${service}`, token, 30 * 60 * 1000); // 30m
|
||||||
|
return { token };
|
||||||
|
} catch (e) {
|
||||||
|
logger.error("Unable to login to FreshRSS API: %s", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { token: false };
|
||||||
|
}
|
||||||
|
|
||||||
|
async function apiCall(widget, endpoint, service) {
|
||||||
|
const key = `${sessionTokenCacheKey}.${service}`;
|
||||||
|
const headers = {
|
||||||
|
"Authorization": `GoogleLogin auth=${cache.get(key)}`,
|
||||||
|
}
|
||||||
|
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||||
|
const method = "GET";
|
||||||
|
|
||||||
|
let [status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (status === 401) {
|
||||||
|
logger.debug("FreshRSS API rejected the request, attempting to obtain new session token");
|
||||||
|
const { token } = await login(widget, service);
|
||||||
|
headers.Authorization = `GoogleLogin auth=${token}`;
|
||||||
|
|
||||||
|
// retry the request, now with the new session token
|
||||||
|
[status, contentType, data, responseHeaders] = await httpProxy(url, {
|
||||||
|
method,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (status !== 200) {
|
||||||
|
logger.error("Error getting data from FreshRSS: %s status %d. Data: %s", url, status, data);
|
||||||
|
return { status, contentType, data: null, responseHeaders };
|
||||||
|
}
|
||||||
|
|
||||||
|
return { status, contentType, data: JSON.parse(data.toString()), responseHeaders };
|
||||||
|
}
|
||||||
|
|
||||||
|
export default async function freshrssProxyHandler(req, res) {
|
||||||
|
const { group, service } = req.query;
|
||||||
|
|
||||||
|
if (!group || !service) {
|
||||||
|
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const widget = await getServiceWidget(group, service);
|
||||||
|
if (!widget) {
|
||||||
|
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||||
|
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cache.get(`${sessionTokenCacheKey}.${service}`)) {
|
||||||
|
await login(widget, service);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data: subscriptionData } = await apiCall(widget, "reader/api/0/subscription/list", service);
|
||||||
|
const { data: unreadCountData } = await apiCall(widget, "reader/api/0/unread-count", service);
|
||||||
|
|
||||||
|
return res.status(200).send({
|
||||||
|
subscriptions: subscriptionData?.subscriptions.length,
|
||||||
|
unread: unreadCountData?.max
|
||||||
|
});
|
||||||
|
}
|
||||||
@ -1,22 +1,11 @@
|
|||||||
import { asJson } from "utils/proxy/api-helpers";
|
import freshrssProxyHandler from "./proxy";
|
||||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
|
||||||
|
|
||||||
const widget = {
|
const widget = {
|
||||||
api: "{url}/api/greader.php/{endpoint}?output=json",
|
api: "{url}/api/greader.php/{endpoint}?output=json",
|
||||||
proxyHandler: credentialedProxyHandler,
|
proxyHandler: freshrssProxyHandler,
|
||||||
|
|
||||||
mappings: {
|
mappings: {
|
||||||
subscriptions: {
|
info: {
|
||||||
endpoint: "reader/api/0/subscription/list",
|
endpoint: "/"
|
||||||
map: (data) => ({
|
|
||||||
count: asJson(data).subscriptions.length
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
unread: {
|
|
||||||
endpoint: "reader/api/0/unread-count",
|
|
||||||
map: (data) => ({
|
|
||||||
count: asJson(data).max
|
|
||||||
}),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user