mirror of
https://github.com/DI0IK/homepage-plus.git
synced 2025-07-09 23:08:48 +00:00
Merge remote-tracking branch 'forkorigin/main' into features/basic-docker-swarm
This commit is contained in:
commit
f51e755216
167 changed files with 7060 additions and 969 deletions
|
@ -3,7 +3,7 @@ import List from "components/bookmarks/list";
|
|||
|
||||
export default function BookmarksGroup({ group }) {
|
||||
return (
|
||||
<div key={group.name} className="basis-full md:basis-1/2 lg:basis-1/3 xl:basis-1/4 flex-1">
|
||||
<div key={group.name} className="flex-1">
|
||||
<h2 className="text-theme-800 dark:text-theme-300 text-xl font-medium">{group.name}</h2>
|
||||
<ErrorBoundary>
|
||||
<List bookmarks={group.bookmarks} />
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { useContext } from "react";
|
||||
|
||||
import { SettingsContext } from "utils/contexts/settings";
|
||||
import ResolvedIcon from "components/resolvedicon";
|
||||
|
||||
export default function Item({ bookmark }) {
|
||||
const { hostname } = new URL(bookmark.href);
|
||||
|
@ -16,7 +17,12 @@ export default function Item({ bookmark }) {
|
|||
>
|
||||
<div className="flex">
|
||||
<div className="flex-shrink-0 flex items-center justify-center w-11 bg-theme-500/10 dark:bg-theme-900/50 text-theme-700 hover:text-theme-700 dark:text-theme-200 text-sm font-medium rounded-l-md">
|
||||
{bookmark.abbr}
|
||||
{bookmark.icon &&
|
||||
<div className="flex-shrink-0 w-5 h-5">
|
||||
<ResolvedIcon icon={bookmark.icon} />
|
||||
</div>
|
||||
}
|
||||
{!bookmark.icon && bookmark.abbr}
|
||||
</div>
|
||||
<div className="flex-1 flex items-center justify-between rounded-r-md ">
|
||||
<div className="flex-1 grow pl-3 py-2 text-xs">{bookmark.name}</div>
|
||||
|
|
|
@ -2,7 +2,7 @@ import { useTranslation } from "react-i18next";
|
|||
import { useEffect, useState, useRef, useCallback, useContext } from "react";
|
||||
import classNames from "classnames";
|
||||
|
||||
import { resolveIcon } from "./services/item";
|
||||
import ResolvedIcon from "./resolvedicon";
|
||||
|
||||
import { SettingsContext } from "utils/contexts/settings";
|
||||
|
||||
|
@ -107,18 +107,19 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
|
|||
|
||||
function highlightText(text) {
|
||||
const parts = text.split(new RegExp(`(${searchString})`, 'gi'));
|
||||
return <span>{parts.map(part => part.toLowerCase() === searchString.toLowerCase() ? <span className="bg-theme-300/10">{part}</span> : part)}</span>;
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
return <span>{parts.map((part, i) => part.toLowerCase() === searchString.toLowerCase() ? <span key={`${searchString}_${i}`} className="bg-theme-300/10">{part}</span> : part)}</span>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={classNames(
|
||||
"relative z-10 ease-in-out duration-300 transition-opacity",
|
||||
"relative z-20 ease-in-out duration-300 transition-opacity",
|
||||
hidden && !isOpen && "hidden",
|
||||
!hidden && isOpen && "opacity-100",
|
||||
!isOpen && "opacity-0",
|
||||
)} role="dialog" aria-modal="true">
|
||||
<div className="fixed inset-0 bg-gray-500 bg-opacity-50" />
|
||||
<div className="fixed inset-0 z-10 overflow-y-auto">
|
||||
<div className="fixed inset-0 z-20 overflow-y-auto">
|
||||
<div className="flex min-h-full min-w-full items-start justify-center text-center">
|
||||
<dialog className="mt-[10%] min-w-[80%] max-w-[90%] md:min-w-[40%] rounded-md p-0 block font-medium text-theme-700 dark:text-theme-200 dark:hover:text-theme-300 shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-50 dark:bg-theme-800">
|
||||
<input placeholder="Search" className={classNames(
|
||||
|
@ -135,7 +136,7 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
|
|||
)} onClick={handleItemClick}>
|
||||
<div className="flex flex-row items-center mr-4 pointer-events-none">
|
||||
<div className="w-5 text-xs mr-4">
|
||||
{r.icon && resolveIcon(r.icon)}
|
||||
{r.icon && <ResolvedIcon icon={r.icon} />}
|
||||
{r.abbr && r.abbr}
|
||||
</div>
|
||||
<div className="flex flex-col md:flex-row text-left items-baseline mr-4 pointer-events-none">
|
||||
|
@ -147,7 +148,7 @@ export default function QuickLaunch({servicesAndBookmarks, searchString, setSear
|
|||
}
|
||||
</div>
|
||||
</div>
|
||||
<div className="text-xs text-theme-600 font-bold pointer-events-none">{r.abbr ? t("quicklaunch.bookmark") : t("quicklaunch.service")}</div>
|
||||
<div className="text-xs text-theme-600 font-bold pointer-events-none">{r.type === 'service' ? t("quicklaunch.service") : t("quicklaunch.bookmark")}</div>
|
||||
</button>
|
||||
</li>
|
||||
))}
|
||||
|
|
37
src/components/resolvedicon.jsx
Normal file
37
src/components/resolvedicon.jsx
Normal file
|
@ -0,0 +1,37 @@
|
|||
import Image from "next/future/image";
|
||||
|
||||
export default function ResolvedIcon({ icon }) {
|
||||
// direct or relative URLs
|
||||
if (icon.startsWith("http") || icon.startsWith("/")) {
|
||||
return <Image src={`${icon}`} width={32} height={32} alt="logo" />;
|
||||
}
|
||||
|
||||
// mdi- prefixed, material design icons
|
||||
if (icon.startsWith("mdi-")) {
|
||||
const iconName = icon.replace("mdi-", "").replace(".svg", "");
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
width: 32,
|
||||
height: 32,
|
||||
maxWidth: '100%',
|
||||
maxHeight: '100%',
|
||||
background: "linear-gradient(180deg, rgb(var(--color-logo-start)), rgb(var(--color-logo-stop)))",
|
||||
mask: `url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${iconName}.svg) no-repeat center / contain`,
|
||||
WebkitMask: `url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${iconName}.svg) no-repeat center / contain`,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// fallback to dashboard-icons
|
||||
const iconName = icon.replace(".png", "");
|
||||
return (
|
||||
<Image
|
||||
src={`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${iconName}.png`}
|
||||
width={32}
|
||||
height={32}
|
||||
alt="logo"
|
||||
/>
|
||||
);
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
import classNames from "classnames";
|
||||
|
||||
import List from "components/services/list";
|
||||
import ResolvedIcon from "components/resolvedicon";
|
||||
|
||||
export default function ServicesGroup({ services, layout }) {
|
||||
return (
|
||||
|
@ -11,7 +12,14 @@ export default function ServicesGroup({ services, layout }) {
|
|||
"flex-1 p-1"
|
||||
)}
|
||||
>
|
||||
<h2 className="text-theme-800 dark:text-theme-300 text-xl font-medium">{services.name}</h2>
|
||||
<div className="flex select-none items-center">
|
||||
{layout?.icon &&
|
||||
<div className="flex-shrink-0 mr-2 w-7 h-7">
|
||||
<ResolvedIcon icon={layout.icon} />
|
||||
</div>
|
||||
}
|
||||
<h2 className="text-theme-800 dark:text-theme-300 text-xl font-medium">{services.name}</h2>
|
||||
</div>
|
||||
<List services={services.services} layout={layout} />
|
||||
</div>
|
||||
);
|
||||
|
|
|
@ -1,46 +1,13 @@
|
|||
import Image from "next/future/image";
|
||||
import classNames from "classnames";
|
||||
import { useContext, useState } from "react";
|
||||
|
||||
import Status from "./status";
|
||||
import Widget from "./widget";
|
||||
import Ping from "./ping";
|
||||
|
||||
import Docker from "widgets/docker/component";
|
||||
import { SettingsContext } from "utils/contexts/settings";
|
||||
|
||||
export function resolveIcon(icon) {
|
||||
// direct or relative URLs
|
||||
if (icon.startsWith("http") || icon.startsWith("/")) {
|
||||
return <Image src={`${icon}`} width={32} height={32} alt="logo" />;
|
||||
}
|
||||
|
||||
// mdi- prefixed, material design icons
|
||||
if (icon.startsWith("mdi-")) {
|
||||
const iconName = icon.replace("mdi-", "").replace(".svg", "");
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
width: 32,
|
||||
height: 32,
|
||||
background: "linear-gradient(180deg, rgb(var(--color-logo-start)), rgb(var(--color-logo-stop)))",
|
||||
mask: `url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${iconName}.svg) no-repeat center / contain`,
|
||||
WebkitMask: `url(https://cdn.jsdelivr.net/npm/@mdi/svg@latest/svg/${iconName}.svg) no-repeat center / contain`,
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// fallback to dashboard-icons
|
||||
const iconName = icon.replace(".png", "");
|
||||
return (
|
||||
<Image
|
||||
src={`https://cdn.jsdelivr.net/gh/walkxcode/dashboard-icons/png/${iconName}.png`}
|
||||
width={32}
|
||||
height={32}
|
||||
alt="logo"
|
||||
/>
|
||||
);
|
||||
}
|
||||
import ResolvedIcon from "components/resolvedicon";
|
||||
|
||||
export default function Item({ service }) {
|
||||
const hasLink = service.href && service.href !== "#";
|
||||
|
@ -64,7 +31,7 @@ export default function Item({ service }) {
|
|||
<div
|
||||
className={`${
|
||||
hasLink ? "cursor-pointer " : " "
|
||||
}transition-all h-15 mb-3 p-1 rounded-md font-medium text-theme-700 dark:text-theme-200 dark:hover:text-theme-300 shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 hover:bg-theme-300/20 dark:bg-white/5 dark:hover:bg-white/10`}
|
||||
}transition-all h-15 mb-3 p-1 rounded-md font-medium text-theme-700 dark:text-theme-200 dark:hover:text-theme-300 shadow-md shadow-theme-900/10 dark:shadow-theme-900/20 bg-theme-100/20 hover:bg-theme-300/20 dark:bg-white/5 dark:hover:bg-white/10 relative`}
|
||||
>
|
||||
<div className="flex select-none">
|
||||
{service.icon &&
|
||||
|
@ -75,10 +42,12 @@ export default function Item({ service }) {
|
|||
rel="noreferrer"
|
||||
className="flex-shrink-0 flex items-center justify-center w-12 "
|
||||
>
|
||||
{resolveIcon(service.icon)}
|
||||
<ResolvedIcon icon={service.icon} />
|
||||
</a>
|
||||
) : (
|
||||
<div className="flex-shrink-0 flex items-center justify-center w-12 ">{resolveIcon(service.icon)}</div>
|
||||
<div className="flex-shrink-0 flex items-center justify-center w-12 ">
|
||||
<ResolvedIcon icon={service.icon} />
|
||||
</div>
|
||||
))}
|
||||
|
||||
{hasLink ? (
|
||||
|
@ -102,22 +71,31 @@ export default function Item({ service }) {
|
|||
</div>
|
||||
)}
|
||||
|
||||
{service.container && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (statsOpen ? closeStats() : setStatsOpen(true))}
|
||||
className="flex-shrink-0 flex items-center justify-center w-12 cursor-pointer"
|
||||
>
|
||||
<Status service={service} />
|
||||
<span className="sr-only">View container stats</span>
|
||||
</button>
|
||||
)}
|
||||
<div className="absolute top-0 right-0 w-1/2 flex flex-row justify-end gap-2 mr-2">
|
||||
{service.ping && (
|
||||
<div className="flex-shrink-0 flex items-center justify-center cursor-pointer">
|
||||
<Ping service={service} />
|
||||
<span className="sr-only">Ping status</span>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{service.container && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => (statsOpen ? closeStats() : setStatsOpen(true))}
|
||||
className="flex-shrink-0 flex items-center justify-center cursor-pointer"
|
||||
>
|
||||
<Status service={service} />
|
||||
<span className="sr-only">View container stats</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{service.container && service.server && (
|
||||
<div
|
||||
className={classNames(
|
||||
statsOpen && !statsClosing ? "max-h-[55px] opacity-100" : " max-h-[0] opacity-0",
|
||||
statsOpen && !statsClosing ? "max-h-[110px] opacity-100" : " max-h-[0] opacity-0",
|
||||
"w-full overflow-hidden transition-all duration-300 ease-in-out"
|
||||
)}
|
||||
>
|
||||
|
|
44
src/components/services/ping.jsx
Normal file
44
src/components/services/ping.jsx
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
export default function Ping({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { data, error } = useSWR(`/api/ping?${new URLSearchParams({ping: service.ping}).toString()}`, {
|
||||
refreshInterval: 30000
|
||||
});
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden">
|
||||
<div className="text-[8px] font-bold text-rose-500 uppercase">{t("ping.error")}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden">
|
||||
<div className="text-[8px] font-bold text-black/20 dark:text-white/40 uppercase">{t("ping.ping")}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
const statusText = `${service.ping}: HTTP status ${data.status}`;
|
||||
|
||||
if (data && data.status !== 200) {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={statusText}>
|
||||
<div className="text-[8px] font-bold text-rose-500/80">{data.status}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (data && data.status === 200) {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={statusText}>
|
||||
<div className="text-[8px] font-bold text-emerald-500/80">{t("common.ms", { value: data.latency, style: "unit", unit: "millisecond", unitDisplay: "narrow", maximumFractionDigits: 0 })}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,52 @@
|
|||
import { useTranslation } from "react-i18next";
|
||||
import useSWR from "swr";
|
||||
|
||||
export default function Status({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { data, error } = useSWR(`/api/docker/status/${service.container}/${service.server || ""}`);
|
||||
|
||||
if (error) {
|
||||
return <div className="w-3 h-3 bg-rose-300 dark:bg-rose-500 rounded-full" />;
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={data.status}>
|
||||
<div className="text-[8px] font-bold text-rose-500/80 uppercase">{t("docker.error")}</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
if (data && data.status === "running") {
|
||||
return <div className="w-3 h-3 bg-emerald-300 dark:bg-emerald-500 rounded-full" />;
|
||||
if (data.health === "starting") {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={data.health}>
|
||||
<div className="text-[8px] font-bold text-blue-500/80 uppercase">{data.health}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (data.health === "unhealthy") {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={data.health}>
|
||||
<div className="text-[8px] font-bold text-orange-400/50 dark:text-orange-400/80 uppercase">{data.health}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={data.health ?? data.status}>
|
||||
<div className="text-[8px] font-bold text-emerald-500/80 uppercase">{data.health ?? data.status}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (data && data.status === "not found") {
|
||||
return <div className="h-2.5 w-2.5 bg-orange-400/50 dark:bg-yellow-200/40 -rotate-45" />;
|
||||
if (data && (data.status === "not found" || data.status === "exited")) {
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden" title={data.status}>
|
||||
<div className="text-[8px] font-bold text-orange-400/50 dark:text-orange-400/80 uppercase">{data.status}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return <div className="w-3 h-3 bg-black/20 dark:bg-white/40 rounded-full" />;
|
||||
return (
|
||||
<div className="w-auto px-1.5 py-0.5 text-center bg-theme-500/10 dark:bg-theme-900/50 rounded-b-[3px] overflow-hidden">
|
||||
<div className="text-[8px] font-bold text-black/20 dark:text-white/40 uppercase">{t("docker.unknown")}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ export default function Block({ value, label }) {
|
|||
return (
|
||||
<div
|
||||
className={classNames(
|
||||
"bg-theme-200/50 dark:bg-theme-900/20 rounded m-1 flex-1 flex flex-col items-center justify-center p-1",
|
||||
"bg-theme-200/50 dark:bg-theme-900/20 rounded m-1 flex-1 flex flex-col items-center justify-center text-center p-1",
|
||||
value === undefined ? "animate-pulse" : ""
|
||||
)}
|
||||
>
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
import Error from "./error";
|
||||
|
||||
export default function Container({ error = false, children, service }) {
|
||||
if (error) {
|
||||
return (
|
||||
<div className="bg-theme-200/50 dark:bg-theme-900/20 rounded m-1 flex-1 flex flex-col items-center justify-center p-1">
|
||||
<div className="font-thin text-sm">{error}</div>
|
||||
</div>
|
||||
);
|
||||
return <Error error={error} />
|
||||
}
|
||||
|
||||
let visibleChildren = children;
|
||||
|
|
50
src/components/services/widget/error.jsx
Normal file
50
src/components/services/widget/error.jsx
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { useTranslation } from "react-i18next";
|
||||
import { IoAlertCircle } from "react-icons/io5";
|
||||
|
||||
function displayError(error) {
|
||||
return JSON.stringify(error[1] ? error[1] : error, null, 4);
|
||||
}
|
||||
|
||||
function displayData(data) {
|
||||
return (data.type === 'Buffer') ? Buffer.from(data).toString() : JSON.stringify(data, 4);
|
||||
}
|
||||
|
||||
export default function Error({ error }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
if (error?.data?.error) {
|
||||
error = error.data.error; // eslint-disable-line no-param-reassign
|
||||
}
|
||||
|
||||
return (
|
||||
<details className="px-1 pb-1">
|
||||
<summary className="block text-center mt-1 mb-0 mx-auto p-3 rounded bg-rose-900/80 hover:bg-rose-900/95 text-theme-900 cursor-pointer">
|
||||
<div className="flex items-center justify-center text-xs font-bold">
|
||||
<IoAlertCircle className="mr-1 w-5 h-5"/>{t("widget.api_error")} {error.message && t("widget.information")}
|
||||
</div>
|
||||
</summary>
|
||||
<div className="bg-white dark:bg-theme-200/50 mt-2 rounded text-rose-900 text-xs font-mono whitespace-pre-wrap break-all">
|
||||
<ul className="p-4">
|
||||
{error.message && <li>
|
||||
<span className="text-black">{t("widget.api_error")}:</span> {error.message}
|
||||
</li>}
|
||||
{error.url && <li className="mt-2">
|
||||
<span className="text-black">{t("widget.url")}:</span> {error.url}
|
||||
</li>}
|
||||
{error.rawError && <li className="mt-2">
|
||||
<span className="text-black">{t("widget.raw_error")}:</span>
|
||||
<div className="ml-2">
|
||||
{displayError(error.rawError)}
|
||||
</div>
|
||||
</li>}
|
||||
{error.data && <li className="mt-2">
|
||||
<span className="text-black">{t("widget.response_data")}:</span>
|
||||
<div className="ml-2">
|
||||
{displayData(error.data)}
|
||||
</div>
|
||||
</li>}
|
||||
</ul>
|
||||
</div>
|
||||
</details>
|
||||
);
|
||||
}
|
|
@ -8,9 +8,9 @@ import cachedFetch from "utils/proxy/cached-fetch";
|
|||
export default function Version() {
|
||||
const { t, i18n } = useTranslation();
|
||||
|
||||
const buildTime = process.env.NEXT_PUBLIC_BUILDTIME ?? new Date().toISOString();
|
||||
const revision = process.env.NEXT_PUBLIC_REVISION ?? "dev";
|
||||
const version = process.env.NEXT_PUBLIC_VERSION ?? "dev";
|
||||
const buildTime = process.env.NEXT_PUBLIC_BUILDTIME?.length ? process.env.NEXT_PUBLIC_BUILDTIME : new Date().toISOString();
|
||||
const revision = process.env.NEXT_PUBLIC_REVISION?.length ? process.env.NEXT_PUBLIC_REVISION : "dev";
|
||||
const version = process.env.NEXT_PUBLIC_VERSION?.length ? process.env.NEXT_PUBLIC_VERSION : "dev";
|
||||
|
||||
const cachedFetcher = (resource) => cachedFetch(resource, 5).then((res) => res.json());
|
||||
|
||||
|
@ -36,17 +36,14 @@ export default function Version() {
|
|||
{version} ({revision.substring(0, 7)}, {formatDate(buildTime)})
|
||||
</>
|
||||
) : (
|
||||
releaseData &&
|
||||
compareVersions(latestRelease.tag_name, version) > 0 && (
|
||||
<a
|
||||
href={`https://github.com/benphelps/homepage/releases/tag/${version}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="ml-2 text-xs text-theme-500 dark:text-theme-400 flex flex-row items-center"
|
||||
>
|
||||
{version} ({revision.substring(0, 7)}, {formatDate(buildTime)})
|
||||
</a>
|
||||
)
|
||||
<a
|
||||
href={`https://github.com/benphelps/homepage/releases/tag/${version}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="ml-2 text-xs text-theme-500 dark:text-theme-400 flex flex-row items-center"
|
||||
>
|
||||
{version} ({revision.substring(0, 7)}, {formatDate(buildTime)})
|
||||
</a>
|
||||
)}
|
||||
</span>
|
||||
{version === "main" || version === "dev" || version === "nightly"
|
||||
|
|
|
@ -15,22 +15,23 @@ const textSizes = {
|
|||
export default function DateTime({ options }) {
|
||||
const { text_size: textSize, format } = options;
|
||||
const { i18n } = useTranslation();
|
||||
const [date, setDate] = useState(new Date());
|
||||
|
||||
const [date, setDate] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
const dateFormat = new Intl.DateTimeFormat(i18n.language, { ...format });
|
||||
const interval = setInterval(() => {
|
||||
setDate(new Date());
|
||||
setDate(dateFormat.format(new Date()));
|
||||
}, 1000);
|
||||
return () => clearInterval(interval);
|
||||
}, [setDate]);
|
||||
|
||||
const dateFormat = new Intl.DateTimeFormat(i18n.language, { ...format });
|
||||
}, [date, setDate, i18n.language, format]);
|
||||
|
||||
return (
|
||||
<div className="flex flex-row items-center grow justify-end">
|
||||
<span className={`text-theme-800 dark:text-theme-200 ${textSizes[textSize || "lg"]}`}>
|
||||
{dateFormat.format(date)}
|
||||
</span>
|
||||
<div className="flex flex-col justify-center first:ml-0 ml-4">
|
||||
<div className="flex flex-row items-center grow justify-end">
|
||||
<span className={`text-theme-800 dark:text-theme-200 ${textSizes[textSize || "lg"]}`}>
|
||||
{date}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import mapIcon from "utils/weather/owm-condition-map";
|
||||
import mapIcon from "utils/weather/openmeteo-condition-map";
|
||||
|
||||
export default function Icon({ condition, timeOfDay }) {
|
||||
const IconComponent = mapIcon(condition, timeOfDay);
|
||||
|
|
|
@ -54,7 +54,7 @@ function Widget({ options }) {
|
|||
<div className="hidden sm:flex flex-col items-center">
|
||||
<Icon
|
||||
condition={data.weather[0].id}
|
||||
timeOfDay={data.dt > data.sys.sunrise && data.dt < data.sys.sundown ? "day" : "night"}
|
||||
timeOfDay={data.dt > data.sys.sunrise && data.dt < data.sys.sunset ? "day" : "night"}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-col ml-3 text-left">
|
||||
|
|
|
@ -52,7 +52,7 @@ export default function Memory({ expanded }) {
|
|||
<div className="flex flex-col ml-3 text-left min-w-[85px]">
|
||||
<span className="text-theme-800 dark:text-theme-200 text-xs flex flex-row justify-between">
|
||||
<div className="pl-0.5">
|
||||
{t("common.bytes", { value: data.memory.freeMemMb * 1024 * 1024, maximumFractionDigits: 0, binary: true })}
|
||||
{t("common.bytes", { value: data.memory.freeMemMb * 1024 * 1024, maximumFractionDigits: 1, binary: true })}
|
||||
</div>
|
||||
<div className="pr-1">{t("resources.free")}</div>
|
||||
</span>
|
||||
|
@ -61,7 +61,7 @@ export default function Memory({ expanded }) {
|
|||
<div className="pl-0.5">
|
||||
{t("common.bytes", {
|
||||
value: data.memory.totalMemMb * 1024 * 1024,
|
||||
maximumFractionDigits: 0,
|
||||
maximumFractionDigits: 1,
|
||||
binary: true,
|
||||
})}
|
||||
</div>
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Widget({ options }) {
|
|||
options.type = "unifi_console";
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(options, "stat/sites", { index: options.index });
|
||||
|
||||
if (statsError || statsData?.error) {
|
||||
if (statsError) {
|
||||
return (
|
||||
<div className="flex flex-col justify-center first:ml-0 ml-4">
|
||||
<div className="flex flex-row items-center justify-end">
|
||||
|
|
|
@ -9,9 +9,6 @@ export default function Document() {
|
|||
content="A highly customizable homepage (or startpage / application dashboard) with Docker and service API integrations."
|
||||
/>
|
||||
<meta name="apple-mobile-web-app-capable" content="yes" />
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png?v=4" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png?v=4" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png?v=4" />
|
||||
<link rel="manifest" href="/site.webmanifest?v=4" />
|
||||
<link rel="mask-icon" href="/safari-pinned-tab.svg?v=4" color="#1e9cd7" />
|
||||
</Head>
|
||||
|
|
|
@ -69,7 +69,7 @@ export default async function handler(req, res) {
|
|||
});
|
||||
} catch {
|
||||
res.status(500).send({
|
||||
error: "unknown error",
|
||||
error: {message: "Unknown error"},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ export default async function handler(req, res) {
|
|||
|
||||
return res.status(200).json({
|
||||
status: info.State.Status,
|
||||
health: info.State.Health?.Status
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -56,6 +57,7 @@ export default async function handler(req, res) {
|
|||
|
||||
return res.status(200).json({
|
||||
status: info.State.Status,
|
||||
health: info.State.Health?.Status
|
||||
});
|
||||
}
|
||||
|
||||
|
|
35
src/pages/api/ping.js
Normal file
35
src/pages/api/ping.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { performance } from "perf_hooks";
|
||||
|
||||
import createLogger from "utils/logger";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
|
||||
const logger = createLogger("ping");
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const { ping: pingURL } = req.query;
|
||||
|
||||
if (!pingURL) {
|
||||
logger.debug("No ping URL specified");
|
||||
return res.status(400).send({
|
||||
error: "No ping URL given",
|
||||
});
|
||||
}
|
||||
|
||||
let startTime = performance.now();
|
||||
let [status] = await httpProxy(pingURL, {
|
||||
method: "HEAD"
|
||||
});
|
||||
let endTime = performance.now();
|
||||
|
||||
if (status >= 400) {
|
||||
// try one more time as a GET in case HEAD is rejected for whatever reason
|
||||
startTime = performance.now();
|
||||
[status] = await httpProxy(pingURL);
|
||||
endTime = performance.now();
|
||||
}
|
||||
|
||||
return res.status(200).json({
|
||||
status,
|
||||
latency: endTime - startTime
|
||||
});
|
||||
}
|
|
@ -1,8 +1,9 @@
|
|||
import cachedFetch from "utils/proxy/cached-fetch";
|
||||
|
||||
export default async function handler(req, res) {
|
||||
const { latitude, longitude, units, cache } = req.query;
|
||||
const { latitude, longitude, units, cache, timezone } = req.query;
|
||||
const degrees = units === "imperial" ? "fahrenheit" : "celsius";
|
||||
const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=sunrise,sunset¤t_weather=true&temperature_unit=${degrees}&timezone=auto`;
|
||||
const timezeone = timezone ?? 'auto'
|
||||
const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}&daily=sunrise,sunset¤t_weather=true&temperature_unit=${degrees}&timezone=${timezeone}`;
|
||||
return res.send(await cachedFetch(apiUrl, cache));
|
||||
}
|
|
@ -35,7 +35,7 @@ const Version = dynamic(() => import("components/version"), {
|
|||
ssr: false,
|
||||
});
|
||||
|
||||
const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "search", "datetime"];
|
||||
const rightAlignedWidgets = ["weatherapi", "openweathermap", "weather", "openmeteo", "search", "datetime"];
|
||||
|
||||
export async function getStaticProps() {
|
||||
let logger;
|
||||
|
@ -219,9 +219,17 @@ function Home({ initialSettings }) {
|
|||
<title>{initialSettings.title || "Homepage"}</title>
|
||||
{initialSettings.base && <base href={initialSettings.base} />}
|
||||
{initialSettings.favicon ? (
|
||||
<link rel="icon" href={initialSettings.favicon} />
|
||||
<>
|
||||
<link rel="apple-touch-icon" sizes="180x180" href={initialSettings.favicon} />
|
||||
<link rel="icon" href={initialSettings.favicon} />
|
||||
</>
|
||||
) : (
|
||||
<link rel="shortcut icon" href="/homepage.ico" />
|
||||
<>
|
||||
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png?v=4" />
|
||||
<link rel="shortcut icon" href="/homepage.ico" />
|
||||
<link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png?v=4" />
|
||||
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png?v=4" />
|
||||
</>
|
||||
)}
|
||||
<meta
|
||||
name="msapplication-TileColor"
|
||||
|
@ -272,7 +280,7 @@ function Home({ initialSettings }) {
|
|||
)}
|
||||
|
||||
{bookmarks && (
|
||||
<div className="grow flex flex-wrap pt-0 p-4 sm:p-8">
|
||||
<div className={`grow flex flex-wrap pt-0 p-4 sm:p-8 gap-2 grid-cols-1 lg:grid-cols-2 lg:grid-cols-${Math.min(6, bookmarks.length)}`}>
|
||||
{bookmarks.map((group) => (
|
||||
<BookmarksGroup key={group.name} group={group} />
|
||||
))}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
# For configuration options and examples, please see:
|
||||
# https://github.com/benphelps/homepage/wiki/Bookmarks
|
||||
# https://gethomepage.dev/en/configs/bookmarks
|
||||
|
||||
- Developer:
|
||||
- Github:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
# For configuration options and examples, please see:
|
||||
# https://github.com/benphelps/homepage/wiki/Docker-Integration
|
||||
# https://gethomepage.dev/en/configs/docker/
|
||||
|
||||
# my-docker:
|
||||
# host: 127.0.0.1
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
# For configuration options and examples, please see:
|
||||
# https://github.com/benphelps/homepage/wiki/Services
|
||||
# https://gethomepage.dev/en/configs/services
|
||||
|
||||
- My First Group:
|
||||
- My First Service:
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
# For configuration options and examples, please see:
|
||||
# https://github.com/benphelps/homepage/wiki/Settings
|
||||
# https://gethomepage.dev/en/configs/settings
|
||||
|
||||
providers:
|
||||
openweathermap: openweathermapapikey
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---
|
||||
# For configuration options and examples, please see:
|
||||
# https://github.com/benphelps/homepage/wiki/Information-Widgets
|
||||
# https://gethomepage.dev/en/configs/widgets
|
||||
|
||||
- resources:
|
||||
cpu: true
|
||||
|
|
|
@ -54,3 +54,7 @@ body {
|
|||
background-color: var(--scrollbar-thumb);
|
||||
border-radius: 0.25em;
|
||||
}
|
||||
|
||||
::-webkit-details-marker {
|
||||
display: none;
|
||||
}
|
|
@ -4,7 +4,7 @@ import path from "path";
|
|||
|
||||
import yaml from "js-yaml";
|
||||
|
||||
import checkAndCopyConfig from "utils/config/config";
|
||||
import checkAndCopyConfig, { getSettings } from "utils/config/config";
|
||||
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
|
||||
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";
|
||||
|
||||
|
@ -46,6 +46,7 @@ export async function widgetsResponse() {
|
|||
export async function servicesResponse() {
|
||||
let discoveredServices;
|
||||
let configuredServices;
|
||||
let initialSettings;
|
||||
|
||||
try {
|
||||
discoveredServices = cleanServiceGroups(await servicesFromDocker());
|
||||
|
@ -63,11 +64,21 @@ export async function servicesResponse() {
|
|||
configuredServices = [];
|
||||
}
|
||||
|
||||
try {
|
||||
initialSettings = await getSettings();
|
||||
} catch (e) {
|
||||
console.error("Failed to load settings.yaml, please check for errors");
|
||||
if (e) console.error(e);
|
||||
initialSettings = {};
|
||||
}
|
||||
|
||||
const mergedGroupsNames = [
|
||||
...new Set([discoveredServices.map((group) => group.name), configuredServices.map((group) => group.name)].flat()),
|
||||
];
|
||||
|
||||
const mergedGroups = [];
|
||||
const sortedGroups = [];
|
||||
const unsortedGroups = [];
|
||||
const definedLayouts = initialSettings.layout ? Object.keys(initialSettings.layout) : null;
|
||||
|
||||
mergedGroupsNames.forEach((groupName) => {
|
||||
const discoveredGroup = discoveredServices.find((group) => group.name === groupName) || { services: [] };
|
||||
|
@ -78,8 +89,14 @@ export async function servicesResponse() {
|
|||
services: [...discoveredGroup.services, ...configuredGroup.services].filter((service) => service),
|
||||
};
|
||||
|
||||
mergedGroups.push(mergedGroup);
|
||||
if (definedLayouts) {
|
||||
const layoutIndex = definedLayouts.findIndex(layout => layout === mergedGroup.name);
|
||||
if (layoutIndex > -1) sortedGroups[layoutIndex] = mergedGroup;
|
||||
else unsortedGroups.push(mergedGroup);
|
||||
} else {
|
||||
unsortedGroups.push(mergedGroup);
|
||||
}
|
||||
});
|
||||
|
||||
return mergedGroups;
|
||||
return [...sortedGroups.filter(g => g), ...unsortedGroups];
|
||||
}
|
||||
|
|
|
@ -32,5 +32,5 @@ export function getSettings() {
|
|||
|
||||
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
|
||||
const fileContents = readFileSync(settingsYaml, "utf8");
|
||||
return yaml.load(fileContents);
|
||||
return yaml.load(fileContents) ?? {};
|
||||
}
|
|
@ -118,6 +118,7 @@ export function cleanServiceGroups(groups) {
|
|||
container,
|
||||
currency, // coinmarketcap widget
|
||||
symbols,
|
||||
defaultinterval
|
||||
} = cleanedService.widget;
|
||||
|
||||
cleanedService.widget = {
|
||||
|
@ -129,6 +130,7 @@ export function cleanServiceGroups(groups) {
|
|||
|
||||
if (currency) cleanedService.widget.currency = currency;
|
||||
if (symbols) cleanedService.widget.symbols = symbols;
|
||||
if (defaultinterval) cleanedService.widget.defaultinterval = defaultinterval;
|
||||
|
||||
if (type === "docker") {
|
||||
if (server) cleanedService.widget.server = server;
|
||||
|
|
|
@ -4,10 +4,15 @@ import { format as utilFormat } from "node:util";
|
|||
|
||||
import winston from "winston";
|
||||
|
||||
import checkAndCopyConfig, { getSettings } from "utils/config/config";
|
||||
|
||||
let winstonLogger;
|
||||
|
||||
function init() {
|
||||
const configPath = join(process.cwd(), "config");
|
||||
checkAndCopyConfig("settings.yaml");
|
||||
const settings = getSettings();
|
||||
const logpath = settings.logpath || configPath;
|
||||
|
||||
function combineMessageAndSplat() {
|
||||
return {
|
||||
|
@ -57,7 +62,7 @@ function init() {
|
|||
winston.format.timestamp(),
|
||||
winston.format.printf(messageFormatter)
|
||||
),
|
||||
filename: `${configPath}/logs/homepage.log`,
|
||||
filename: `${logpath}/logs/homepage.log`,
|
||||
handleExceptions: true,
|
||||
handleRejections: true,
|
||||
}),
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import validateWidgetData from "utils/proxy/validate-widget-data";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
@ -31,6 +32,10 @@ export default async function credentialedProxyHandler(req, res) {
|
|||
headers.Authorization = `Bearer ${widget.key}`;
|
||||
} else if (widget.type === "proxmox") {
|
||||
headers.Authorization = `PVEAPIToken=${widget.username}=${widget.password}`;
|
||||
} else if (widget.type === "autobrr") {
|
||||
headers["X-API-Token"] = `${widget.key}`;
|
||||
} else if (widget.type === "tubearchivist") {
|
||||
headers.Authorization = `Token ${widget.key}`;
|
||||
} else {
|
||||
headers["X-API-Key"] = `${widget.key}`;
|
||||
}
|
||||
|
@ -47,7 +52,11 @@ export default async function credentialedProxyHandler(req, res) {
|
|||
}
|
||||
|
||||
if (status >= 400) {
|
||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||
logger.error("HTTP Error %d calling %s", status, url.toString());
|
||||
}
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, data)) {
|
||||
return res.status(500).json({error: {message: "Invalid data", url, data}});
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import validateWidgetData from "utils/proxy/validate-widget-data";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
@ -32,6 +33,11 @@ export default async function genericProxyHandler(req, res, map) {
|
|||
});
|
||||
|
||||
let resultData = data;
|
||||
|
||||
if (!validateWidgetData(widget, endpoint, resultData)) {
|
||||
return res.status(status).json({error: {message: "Invalid data", url, data: resultData}});
|
||||
}
|
||||
|
||||
if (status === 200 && map) {
|
||||
resultData = map(data);
|
||||
}
|
||||
|
@ -44,6 +50,7 @@ export default async function genericProxyHandler(req, res, map) {
|
|||
|
||||
if (status >= 400) {
|
||||
logger.debug("HTTP Error %d calling %s//%s%s...", status, url.protocol, url.hostname, url.pathname);
|
||||
return res.status(status).json({error: {message: "HTTP Error", url, data}});
|
||||
}
|
||||
|
||||
return res.status(status).send(resultData);
|
||||
|
|
82
src/utils/proxy/handlers/jsonrpc.js
Normal file
82
src/utils/proxy/handlers/jsonrpc.js
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { JSONRPCClient, JSONRPCErrorException } from "json-rpc-2.0";
|
||||
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const logger = createLogger("jsonrpcProxyHandler");
|
||||
|
||||
export async function sendJsonRpcRequest(url, method, params, username, password) {
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"accept": "application/json"
|
||||
}
|
||||
|
||||
if (username && password) {
|
||||
headers.authorization = `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`;
|
||||
}
|
||||
|
||||
const client = new JSONRPCClient(async (rpcRequest) => {
|
||||
const body = JSON.stringify(rpcRequest);
|
||||
const httpRequestParams = {
|
||||
method: "POST",
|
||||
headers,
|
||||
body
|
||||
};
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data] = await httpProxy(url, httpRequestParams);
|
||||
if (status === 200) {
|
||||
const json = JSON.parse(data.toString());
|
||||
|
||||
// in order to get access to the underlying error object in the JSON response
|
||||
// you must set `result` equal to undefined
|
||||
if (json.error && (json.result === null)) {
|
||||
json.result = undefined;
|
||||
}
|
||||
return client.receive(json);
|
||||
}
|
||||
|
||||
return Promise.reject(data?.error ? data : new Error(data.toString()));
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await client.request(method, params);
|
||||
return [200, "application/json", JSON.stringify(response)];
|
||||
}
|
||||
catch (e) {
|
||||
if (e instanceof JSONRPCErrorException) {
|
||||
logger.debug("Error calling JSONPRC endpoint: %s. %s", url, e.message);
|
||||
return [200, "application/json", JSON.stringify({result: null, error: {code: e.code, message: e.message}})];
|
||||
}
|
||||
|
||||
logger.warn("Error calling JSONPRC endpoint: %s. %s", url, e);
|
||||
return [500, "application/json", JSON.stringify({result: null, error: {code: 2, message: e.toString()}})];
|
||||
}
|
||||
}
|
||||
|
||||
export default async function jsonrpcProxyHandler(req, res) {
|
||||
const { group, service, endpoint: method } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
const api = widgets?.[widget.type]?.api;
|
||||
|
||||
if (!api) {
|
||||
return res.status(403).json({ error: "Service does not support API calls" });
|
||||
}
|
||||
|
||||
if (widget) {
|
||||
const url = formatApiCall(api, { ...widget });
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data] = await sendJsonRpcRequest(url, method, null, widget.username, widget.password);
|
||||
return res.status(status).end(data);
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Invalid or missing proxy service type '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
|
@ -18,10 +18,15 @@ function addCookieHandler(url, params) {
|
|||
};
|
||||
}
|
||||
|
||||
export function httpsRequest(url, params) {
|
||||
function handleRequest(requestor, url, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
addCookieHandler(url, params);
|
||||
const request = https.request(url, params, (response) => {
|
||||
if (params?.body) {
|
||||
params.headers = params.headers ?? {};
|
||||
params.headers['content-length'] = Buffer.byteLength(params.body);
|
||||
}
|
||||
|
||||
const request = requestor.request(url, params, (response) => {
|
||||
const data = [];
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
|
@ -38,7 +43,7 @@ export function httpsRequest(url, params) {
|
|||
reject([500, error]);
|
||||
});
|
||||
|
||||
if (params.body) {
|
||||
if (params?.body) {
|
||||
request.write(params.body);
|
||||
}
|
||||
|
||||
|
@ -46,32 +51,12 @@ export function httpsRequest(url, params) {
|
|||
});
|
||||
}
|
||||
|
||||
export function httpsRequest(url, params) {
|
||||
return handleRequest(https, url, params);
|
||||
}
|
||||
|
||||
export function httpRequest(url, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
addCookieHandler(url, params);
|
||||
const request = http.request(url, params, (response) => {
|
||||
const data = [];
|
||||
|
||||
response.on("data", (chunk) => {
|
||||
data.push(chunk);
|
||||
});
|
||||
|
||||
response.on("end", () => {
|
||||
addCookieToJar(url, response.headers);
|
||||
resolve([response.statusCode, response.headers["content-type"], Buffer.concat(data), response.headers]);
|
||||
});
|
||||
});
|
||||
|
||||
request.on("error", (error) => {
|
||||
reject([500, error]);
|
||||
});
|
||||
|
||||
if (params.body) {
|
||||
request.write(params.body);
|
||||
}
|
||||
|
||||
request.end();
|
||||
});
|
||||
return handleRequest(http, url, params);
|
||||
}
|
||||
|
||||
export async function httpProxy(url, params = {}) {
|
||||
|
@ -96,8 +81,8 @@ export async function httpProxy(url, params = {}) {
|
|||
return [status, contentType, data, responseHeaders];
|
||||
}
|
||||
catch (err) {
|
||||
logger.error("Error calling %s//%s%s...", url.protocol, url.hostname, url.pathname);
|
||||
logger.error("Error calling %s//%s%s...", constructedUrl.protocol, constructedUrl.hostname, constructedUrl.pathname);
|
||||
logger.error(err);
|
||||
return [500, "application/json", { error: "Unexpected error" }, null];
|
||||
return [500, "application/json", { error: {message: err?.message ?? "Unknown error", url, rawError: err} }, null];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,5 +3,11 @@ import useSWR from "swr";
|
|||
import { formatProxyUrl } from "./api-helpers";
|
||||
|
||||
export default function useWidgetAPI(widget, ...options) {
|
||||
return useSWR(formatProxyUrl(widget, ...options));
|
||||
const config = {};
|
||||
if (options?.refreshInterval) {
|
||||
config.refreshInterval = options.refreshInterval;
|
||||
}
|
||||
const { data, error } = useSWR(formatProxyUrl(widget, ...options), config);
|
||||
// make the data error the top-level error
|
||||
return { data, error: data?.error ?? error }
|
||||
}
|
||||
|
|
22
src/utils/proxy/validate-widget-data.js
Normal file
22
src/utils/proxy/validate-widget-data.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
import widgets from "widgets/widgets";
|
||||
|
||||
export default function validateWidgetData(widget, endpoint, data) {
|
||||
let valid = true;
|
||||
let dataParsed;
|
||||
try {
|
||||
dataParsed = JSON.parse(data);
|
||||
} catch (e) {
|
||||
valid = false;
|
||||
}
|
||||
|
||||
if (dataParsed && Object.entries(dataParsed).length) {
|
||||
const validate = widgets[widget.type]?.mappings?.[endpoint]?.validate;
|
||||
validate?.forEach(key => {
|
||||
if (dataParsed[key] === undefined) {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
211
src/utils/weather/openmeteo-condition-map.js
Normal file
211
src/utils/weather/openmeteo-condition-map.js
Normal file
|
@ -0,0 +1,211 @@
|
|||
import * as Icons from "react-icons/wi";
|
||||
|
||||
// see https://open-meteo.com/en/docs
|
||||
|
||||
const conditions = [
|
||||
{
|
||||
code: 1,
|
||||
icon: {
|
||||
day: Icons.WiDayCloudy,
|
||||
night: Icons.WiNightAltCloudy,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 2,
|
||||
icon: {
|
||||
day: Icons.WiDayCloudy,
|
||||
night: Icons.WiNightAltCloudy,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 3,
|
||||
icon: {
|
||||
day: Icons.WiDayCloudy,
|
||||
night: Icons.WiNightAltCloudy,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 45,
|
||||
icon: {
|
||||
day: Icons.WiDayFog,
|
||||
night: Icons.WiNightFog,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 48,
|
||||
icon: {
|
||||
day: Icons.WiDayFog,
|
||||
night: Icons.WiNightFog,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 51,
|
||||
icon: {
|
||||
day: Icons.WiDaySprinkle,
|
||||
night: Icons.WiNightAltSprinkle,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 53,
|
||||
icon: {
|
||||
day: Icons.WiDaySprinkle,
|
||||
night: Icons.WiNightAltSprinkle,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 55,
|
||||
icon: {
|
||||
day: Icons.WiDaySprinkle,
|
||||
night: Icons.WiNightAltSprinkle,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 56,
|
||||
icon: {
|
||||
day: Icons.WiDaySleet,
|
||||
night: Icons.WiNightAltSleet,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 57,
|
||||
icon: {
|
||||
day: Icons.WiDaySleet,
|
||||
night: Icons.WiNightAltSleet,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 61,
|
||||
icon: {
|
||||
day: Icons.WiDayShowers,
|
||||
night: Icons.WiNightAltShowers,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 63,
|
||||
icon: {
|
||||
day: Icons.WiDayShowers,
|
||||
night: Icons.WiNightAltShowers,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 65,
|
||||
icon: {
|
||||
day: Icons.WiDayShowers,
|
||||
night: Icons.WiNightAltShowers,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 66,
|
||||
icon: {
|
||||
day: Icons.WiDaySleet,
|
||||
night: Icons.WiNightAltSleet,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 67,
|
||||
icon: {
|
||||
day: Icons.WiDaySleet,
|
||||
night: Icons.WiNightAltSleet,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 71,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 73,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 75,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 77,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 80,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 81,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 82,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 85,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 86,
|
||||
icon: {
|
||||
day: Icons.WiDaySnow,
|
||||
night: Icons.WiNightAltSnow,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 95,
|
||||
icon: {
|
||||
day: Icons.WiDayThunderstorm,
|
||||
night: Icons.WiNightAltThunderstorm,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 96,
|
||||
icon: {
|
||||
day: Icons.WiDayThunderstorm,
|
||||
night: Icons.WiNightAltThunderstorm,
|
||||
},
|
||||
},
|
||||
{
|
||||
code: 99,
|
||||
icon: {
|
||||
day: Icons.WiDayThunderstorm,
|
||||
night: Icons.WiNightAltThunderstorm,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
export default function mapIcon(weatherStatusCode, timeOfDay) {
|
||||
const mapping = conditions.find((condition) => condition.code === weatherStatusCode);
|
||||
|
||||
if (mapping) {
|
||||
if (timeOfDay === "day") {
|
||||
return mapping.icon.day;
|
||||
}
|
||||
|
||||
if (timeOfDay === "night") {
|
||||
return mapping.icon.night;
|
||||
}
|
||||
}
|
||||
|
||||
return Icons.WiDaySunny;
|
||||
}
|
|
@ -12,9 +12,9 @@ export default function Component({ service }) {
|
|||
const { data: adguardData, error: adguardError } = useWidgetAPI(widget, "stats");
|
||||
|
||||
if (adguardError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={adguardError} />;
|
||||
}
|
||||
|
||||
|
||||
if (!adguardData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
|
|
|
@ -14,7 +14,8 @@ export default function Component({ service }) {
|
|||
const { data: failedLoginsData, error: failedLoginsError } = useWidgetAPI(widget, "login_failed");
|
||||
|
||||
if (usersError || loginsError || failedLoginsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = usersError ?? loginsError ?? failedLoginsError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!usersData || !loginsData || !failedLoginsData) {
|
||||
|
|
40
src/widgets/autobrr/component.jsx
Normal file
40
src/widgets/autobrr/component.jsx
Normal file
|
@ -0,0 +1,40 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "stats");
|
||||
const { data: filtersData, error: filtersError } = useWidgetAPI(widget, "filters");
|
||||
const { data: indexersData, error: indexersError } = useWidgetAPI(widget, "indexers");
|
||||
|
||||
if (statsError || filtersError || indexersError) {
|
||||
const finalError = statsError ?? filtersError ?? indexersError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!statsData || !filtersData || !indexersData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="autobrr.approvedPushes" />
|
||||
<Block label="autobrr.rejectedPushes" />
|
||||
<Block label="autobrr.filters" />
|
||||
<Block label="autobrr.indexers" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="autobrr.approvedPushes" value={t("common.number", { value: statsData.push_approved_count })} />
|
||||
<Block label="autobrr.rejectedPushes" value={t("common.number", { value: statsData.push_rejected_count })} />
|
||||
<Block label="autobrr.filters" value={t("common.number", { value: filtersData.length })} />
|
||||
<Block label="autobrr.indexers" value={t("common.number", { value: indexersData.length })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
24
src/widgets/autobrr/widget.js
Normal file
24
src/widgets/autobrr/widget.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
stats: {
|
||||
endpoint: "release/stats",
|
||||
validate: [
|
||||
"push_approved_count",
|
||||
"push_rejected_count"
|
||||
]
|
||||
},
|
||||
filters: {
|
||||
endpoint: "filters",
|
||||
},
|
||||
indexers: {
|
||||
endpoint: "release/indexers",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -12,8 +12,9 @@ export default function Component({ service }) {
|
|||
const { data: episodesData, error: episodesError } = useWidgetAPI(widget, "episodes");
|
||||
const { data: moviesData, error: moviesError } = useWidgetAPI(widget, "movies");
|
||||
|
||||
if (episodesError || moviesError) {
|
||||
return <Container error="widget.api_error" />;
|
||||
if (moviesError || episodesError) {
|
||||
const finalError = moviesError ?? episodesError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!episodesData || !moviesData) {
|
||||
|
|
|
@ -9,10 +9,14 @@ export default function Component({ service }) {
|
|||
|
||||
const { widget } = service;
|
||||
|
||||
const { data } = useWidgetAPI(widget, "info");
|
||||
const { data, error } = useWidgetAPI(widget, "info");
|
||||
|
||||
if (error) {
|
||||
return <Container error={error} />;
|
||||
}
|
||||
|
||||
if (!data) {
|
||||
return <Container error="widget.api_error" />;
|
||||
return <Container service={service} />;
|
||||
}
|
||||
|
||||
const totalObserved = Object.keys(data).length;
|
||||
|
|
|
@ -17,11 +17,12 @@ export default function Component({ service }) {
|
|||
{ label: t("coinmarketcap.30days"), value: "30d" },
|
||||
];
|
||||
|
||||
const [dateRange, setDateRange] = useState(dateRangeOptions[0].value);
|
||||
|
||||
const { widget } = service;
|
||||
const { symbols } = widget;
|
||||
const currencyCode = widget.currency ?? "USD";
|
||||
const interval = widget.defaultinterval ?? dateRangeOptions[0].value;
|
||||
|
||||
const [dateRange, setDateRange] = useState(interval);
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "v1/cryptocurrency/quotes/latest", {
|
||||
symbol: `${symbols.join(",")}`,
|
||||
|
@ -37,7 +38,7 @@ export default function Component({ service }) {
|
|||
}
|
||||
|
||||
if (statsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData || !dateRange) {
|
||||
|
|
|
@ -3,39 +3,52 @@ import dynamic from "next/dynamic";
|
|||
const components = {
|
||||
adguard: dynamic(() => import("./adguard/component")),
|
||||
authentik: dynamic(() => import("./authentik/component")),
|
||||
autobrr: dynamic(() => import("./autobrr/component")),
|
||||
bazarr: dynamic(() => import("./bazarr/component")),
|
||||
changedetectionio: dynamic(() => import("./changedetectionio/component")),
|
||||
coinmarketcap: dynamic(() => import("./coinmarketcap/component")),
|
||||
deluge: dynamic(() => import("./deluge/component")),
|
||||
diskstation: dynamic(() => import("./diskstation/component")),
|
||||
docker: dynamic(() => import("./docker/component")),
|
||||
emby: dynamic(() => import("./emby/component")),
|
||||
flood: dynamic(() => import("./flood/component")),
|
||||
gluetun: dynamic(() => import("./gluetun/component")),
|
||||
gotify: dynamic(() => import("./gotify/component")),
|
||||
hdhomerun: dynamic(() => import("./hdhomerun/component")),
|
||||
homebridge: dynamic(() => import("./homebridge/component")),
|
||||
jackett: dynamic(() => import("./jackett/component")),
|
||||
jellyfin: dynamic(() => import("./emby/component")),
|
||||
jellyseerr: dynamic(() => import("./jellyseerr/component")),
|
||||
lidarr: dynamic(() => import("./lidarr/component")),
|
||||
mastodon: dynamic(() => import("./mastodon/component")),
|
||||
navidrome: dynamic(() => import("./navidrome/component")),
|
||||
npm: dynamic(() => import("./npm/component")),
|
||||
nzbget: dynamic(() => import("./nzbget/component")),
|
||||
ombi: dynamic(() => import("./ombi/component")),
|
||||
overseerr: dynamic(() => import("./overseerr/component")),
|
||||
paperlessngx: dynamic(() => import("./paperlessngx/component")),
|
||||
pihole: dynamic(() => import("./pihole/component")),
|
||||
plex: dynamic(() => import("./plex/component")),
|
||||
portainer: dynamic(() => import("./portainer/component")),
|
||||
prowlarr: dynamic(() => import("./prowlarr/component")),
|
||||
proxmox: dynamic(() => import("./proxmox/component")),
|
||||
pyload: dynamic(() => import("./pyload/component")),
|
||||
qbittorrent: dynamic(() => import("./qbittorrent/component")),
|
||||
radarr: dynamic(() => import("./radarr/component")),
|
||||
readarr: dynamic(() => import("./readarr/component")),
|
||||
rutorrent: dynamic(() => import("./rutorrent/component")),
|
||||
sabnzbd: dynamic(() => import("./sabnzbd/component")),
|
||||
scrutiny: dynamic(() => import("./scrutiny/component")),
|
||||
sonarr: dynamic(() => import("./sonarr/component")),
|
||||
speedtest: dynamic(() => import("./speedtest/component")),
|
||||
strelaysrv: dynamic(() => import("./strelaysrv/component")),
|
||||
tautulli: dynamic(() => import("./tautulli/component")),
|
||||
traefik: dynamic(() => import("./traefik/component")),
|
||||
transmission: dynamic(() => import("./transmission/component")),
|
||||
tubearchivist: dynamic(() => import("./tubearchivist/component")),
|
||||
truenas: dynamic(() => import("./truenas/component")),
|
||||
unifi: dynamic(() => import("./unifi/component")),
|
||||
watchtower: dynamic(() => import("./watchtower/component")),
|
||||
};
|
||||
|
||||
export default components;
|
||||
|
|
52
src/widgets/deluge/component.jsx
Normal file
52
src/widgets/deluge/component.jsx
Normal file
|
@ -0,0 +1,52 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: torrentData, error: torrentError } = useWidgetAPI(widget);
|
||||
|
||||
if (torrentError) {
|
||||
return <Container error={torrentError} />;
|
||||
}
|
||||
|
||||
if (!torrentData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="deluge.leech" />
|
||||
<Block label="deluge.download" />
|
||||
<Block label="deluge.seed" />
|
||||
<Block label="deluge.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const { torrents } = torrentData;
|
||||
const keys = torrents ? Object.keys(torrents) : [];
|
||||
|
||||
let rateDl = 0;
|
||||
let rateUl = 0;
|
||||
let completed = 0;
|
||||
for (let i = 0; i < keys.length; i += 1) {
|
||||
const torrent = torrents[keys[i]];
|
||||
rateDl += torrent.download_payload_rate;
|
||||
rateUl += torrent.upload_payload_rate;
|
||||
completed += torrent.total_remaining === 0 ? 1 : 0;
|
||||
}
|
||||
|
||||
const leech = keys.length - completed || 0;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="deluge.leech" value={t("common.number", { value: leech })} />
|
||||
<Block label="deluge.download" value={t("common.bitrate", { value: rateDl })} />
|
||||
<Block label="deluge.seed" value={t("common.number", { value: completed })} />
|
||||
<Block label="deluge.upload" value={t("common.bitrate", { value: rateUl })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
63
src/widgets/deluge/proxy.js
Normal file
63
src/widgets/deluge/proxy.js
Normal file
|
@ -0,0 +1,63 @@
|
|||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { sendJsonRpcRequest } from "utils/proxy/handlers/jsonrpc";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const logger = createLogger("delugeProxyHandler");
|
||||
|
||||
const dataMethod = "web.update_ui";
|
||||
const dataParams = [
|
||||
["queue", "name", "total_wanted", "state", "progress", "download_payload_rate", "upload_payload_rate", "total_remaining"],
|
||||
{}
|
||||
];
|
||||
const loginMethod = "auth.login";
|
||||
|
||||
async function sendRpc(url, method, params) {
|
||||
const [status, contentType, data] = await sendJsonRpcRequest(url, method, params);
|
||||
const json = JSON.parse(data.toString());
|
||||
if (json?.error) {
|
||||
if (json.error.code === 1) {
|
||||
return [403, contentType, data];
|
||||
}
|
||||
return [500, contentType, data];
|
||||
}
|
||||
|
||||
return [status, contentType, data];
|
||||
}
|
||||
|
||||
function login(url, password) {
|
||||
return sendRpc(url, loginMethod, [password]);
|
||||
}
|
||||
|
||||
export default async function delugeProxyHandler(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" });
|
||||
}
|
||||
|
||||
const api = widgets?.[widget.type]?.api
|
||||
const url = new URL(formatApiCall(api, { ...widget }));
|
||||
|
||||
let [status, contentType, data] = await sendRpc(url, dataMethod, dataParams);
|
||||
if (status === 403) {
|
||||
[status, contentType, data] = await login(url, widget.password);
|
||||
if (status !== 200) {
|
||||
return res.status(status).end(data);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
[status, contentType, data] = await sendRpc(url, dataMethod, dataParams);
|
||||
}
|
||||
|
||||
return res.status(status).end(data);
|
||||
}
|
8
src/widgets/deluge/widget.js
Normal file
8
src/widgets/deluge/widget.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
import delugeProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/json",
|
||||
proxyHandler: delugeProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
41
src/widgets/diskstation/component.jsx
Normal file
41
src/widgets/diskstation/component.jsx
Normal file
|
@ -0,0 +1,41 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
const { data: listData, error: listError } = useWidgetAPI(widget, "list");
|
||||
|
||||
if (listError) {
|
||||
return <Container error={listError} />;
|
||||
}
|
||||
|
||||
const tasks = listData?.data?.tasks;
|
||||
if (!tasks) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="diskstation.leech" />
|
||||
<Block label="diskstation.download" />
|
||||
<Block label="diskstation.seed" />
|
||||
<Block label="diskstation.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const rateDl = tasks.reduce((acc, task) => acc + (task?.additional?.transfer?.speed_download ?? 0), 0);
|
||||
const rateUl = tasks.reduce((acc, task) => acc + (task?.additional?.transfer?.speed_upload ?? 0), 0);
|
||||
const completed = tasks.filter((task) => task?.additional?.transfer?.size_downloaded === task?.size)?.length || 0;
|
||||
const leech = tasks.length - completed || 0;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="diskstation.leech" value={t("common.number", { value: leech })} />
|
||||
<Block label="diskstation.download" value={t("common.bitrate", { value: rateDl })} />
|
||||
<Block label="diskstation.seed" value={t("common.number", { value: completed })} />
|
||||
<Block label="diskstation.upload" value={t("common.bitrate", { value: rateUl })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
70
src/widgets/diskstation/proxy.js
Normal file
70
src/widgets/diskstation/proxy.js
Normal file
|
@ -0,0 +1,70 @@
|
|||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
|
||||
const logger = createLogger("diskstationProxyHandler");
|
||||
const authApi = "{url}/webapi/auth.cgi?api=SYNO.API.Auth&version=2&method=login&account={username}&passwd={password}&session=DownloadStation&format=cookie"
|
||||
|
||||
async function login(widget) {
|
||||
const loginUrl = formatApiCall(authApi, widget);
|
||||
const [status, contentType, data] = await httpProxy(loginUrl);
|
||||
if (status !== 200) {
|
||||
return [status, contentType, data];
|
||||
}
|
||||
|
||||
const json = JSON.parse(data.toString());
|
||||
if (json?.success !== true) {
|
||||
// from https://global.download.synology.com/download/Document/Software/DeveloperGuide/Package/DownloadStation/All/enu/Synology_Download_Station_Web_API.pdf
|
||||
/*
|
||||
Code Description
|
||||
400 No such account or incorrect password
|
||||
401 Account disabled
|
||||
402 Permission denied
|
||||
403 2-step verification code required
|
||||
404 Failed to authenticate 2-step verification code
|
||||
*/
|
||||
let message = "Authentication failed.";
|
||||
if (json?.error?.code >= 403) message += " 2FA enabled.";
|
||||
logger.warn("Unable to login. Code: %d", json?.error?.code);
|
||||
return [401, "application/json", JSON.stringify({ code: json?.error?.code, message })];
|
||||
}
|
||||
|
||||
return [status, contentType, data];
|
||||
}
|
||||
|
||||
export default async function diskstationProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (!group || !service) {
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service);
|
||||
const api = widgets?.[widget.type]?.api;
|
||||
if (!api) {
|
||||
return res.status(403).json({ error: "Service does not support API calls" });
|
||||
}
|
||||
|
||||
const url = formatApiCall(api, { endpoint, ...widget });
|
||||
let [status, contentType, data] = await httpProxy(url);
|
||||
if (status !== 200) {
|
||||
logger.debug("Error %d calling endpoint %s", status, url);
|
||||
return res.status(status, data);
|
||||
}
|
||||
|
||||
const json = JSON.parse(data.toString());
|
||||
if (json?.success !== true) {
|
||||
logger.debug("Logging in to DiskStation");
|
||||
[status, contentType, data] = await login(widget);
|
||||
if (status !== 200) {
|
||||
return res.status(status).end(data)
|
||||
}
|
||||
|
||||
[status, contentType, data] = await httpProxy(url);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
14
src/widgets/diskstation/widget.js
Normal file
14
src/widgets/diskstation/widget.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
import diskstationProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/webapi/DownloadStation/task.cgi?api=SYNO.DownloadStation.Task&version=1&method={endpoint}",
|
||||
proxyHandler: diskstationProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"list": {
|
||||
endpoint: "list&additional=transfer",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -17,8 +17,9 @@ export default function Component({ service }) {
|
|||
|
||||
const { data: statsData, error: statsError } = useSWR(`/api/docker/stats/${widget.container}/${widget.server || ""}`);
|
||||
|
||||
if (statsError || statusError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
if (statsError || statsData?.error || statusError || statusData?.error) {
|
||||
const finalError = statsError ?? statsData?.error ?? statusError ?? statusData?.error;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (statusData && statusData.status !== "running") {
|
||||
|
@ -45,7 +46,9 @@ export default function Component({ service }) {
|
|||
return (
|
||||
<Container service={service}>
|
||||
<Block label="docker.cpu" value={t("common.percent", { value: calculateCPUPercent(statsData.stats) })} />
|
||||
<Block label="docker.mem" value={t("common.bytes", { value: statsData.stats.memory_stats.usage })} />
|
||||
{statsData.stats.memory_stats.usage &&
|
||||
<Block label="docker.mem" value={t("common.bytes", { value: statsData.stats.memory_stats.usage })} />
|
||||
}
|
||||
{network && (
|
||||
<>
|
||||
<Block label="docker.rx" value={t("common.bytes", { value: network.rx_bytes })} />
|
||||
|
|
|
@ -1,10 +1,10 @@
|
|||
import useSWR from "swr";
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { BsVolumeMuteFill, BsFillPlayFill, BsPauseFill, BsCpu, BsFillCpuFill } from "react-icons/bs";
|
||||
import { MdOutlineSmartDisplay } from "react-icons/md";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import { formatProxyUrl, formatProxyUrlWithSegments } from "utils/proxy/api-helpers";
|
||||
import { formatProxyUrlWithSegments } from "utils/proxy/api-helpers";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
function ticksToTime(ticks) {
|
||||
const milliseconds = ticks / 10000;
|
||||
|
@ -157,7 +157,7 @@ export default function Component({ service }) {
|
|||
data: sessionsData,
|
||||
error: sessionsError,
|
||||
mutate: sessionMutate,
|
||||
} = useSWR(formatProxyUrl(widget, "Sessions"), {
|
||||
} = useWidgetAPI(widget, "Sessions", {
|
||||
refreshInterval: 5000,
|
||||
});
|
||||
|
||||
|
@ -172,7 +172,7 @@ export default function Component({ service }) {
|
|||
}
|
||||
|
||||
if (sessionsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={sessionsError} />;
|
||||
}
|
||||
|
||||
if (!sessionsData) {
|
||||
|
|
|
@ -10,7 +10,7 @@ const widget = {
|
|||
},
|
||||
PlayControl: {
|
||||
method: "POST",
|
||||
enpoint: "Sessions/{sessionId}/Playing/{command}",
|
||||
endpoint: "Sessions/{sessionId}/Playing/{command}",
|
||||
segments: ["sessionId", "command"],
|
||||
},
|
||||
},
|
||||
|
|
53
src/widgets/flood/component.jsx
Normal file
53
src/widgets/flood/component.jsx
Normal file
|
@ -0,0 +1,53 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: torrentData, error: torrentError } = useWidgetAPI(widget, "torrents");
|
||||
|
||||
if (torrentError || !torrentData?.torrents) {
|
||||
return <Container error={torrentError ?? {message: "No torrent data returned"}} />;
|
||||
}
|
||||
|
||||
if (!torrentData || !torrentData.torrents) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="flood.leech" />
|
||||
<Block label="flood.download" />
|
||||
<Block label="flood.seed" />
|
||||
<Block label="flood.upload" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
let rateDl = 0;
|
||||
let rateUl = 0;
|
||||
let completed = 0;
|
||||
let leech = 0;
|
||||
|
||||
Object.values(torrentData.torrents).forEach(torrent => {
|
||||
rateDl += torrent.downRate;
|
||||
rateUl += torrent.upRate;
|
||||
if(torrent.status.includes('complete')){
|
||||
completed += 1;
|
||||
}
|
||||
if(torrent.status.includes('downloading')){
|
||||
leech += 1;
|
||||
}
|
||||
})
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="flood.leech" value={t("common.number", { value: leech })} />
|
||||
<Block label="flood.download" value={t("common.bitrate", { value: rateDl })} />
|
||||
<Block label="flood.seed" value={t("common.number", { value: completed })} />
|
||||
<Block label="flood.upload" value={t("common.bitrate", { value: rateUl })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
66
src/widgets/flood/proxy.js
Normal file
66
src/widgets/flood/proxy.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const logger = createLogger("floodProxyHandler");
|
||||
|
||||
async function login(widget) {
|
||||
logger.debug("flood is rejecting the request, logging in.");
|
||||
const loginUrl = new URL(`${widget.url}/api/auth/authenticate`).toString();
|
||||
|
||||
const loginParams = {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: null
|
||||
};
|
||||
|
||||
if (widget.username && widget.password) {
|
||||
loginParams.body = JSON.stringify({
|
||||
"username": widget.username,
|
||||
"password": widget.password
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data] = await httpProxy(loginUrl, loginParams);
|
||||
return [status, data];
|
||||
}
|
||||
|
||||
export default async function floodProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = 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" });
|
||||
}
|
||||
|
||||
const url = new URL(formatApiCall("{url}/api/{endpoint}", { endpoint, ...widget }));
|
||||
const params = { method: "GET", headers: {} };
|
||||
|
||||
let [status, contentType, data] = await httpProxy(url, params);
|
||||
if (status === 401) {
|
||||
[status, data] = await login(widget);
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d logging in to flood. Data: %s", status, data);
|
||||
return res.status(status).end(data);
|
||||
}
|
||||
|
||||
[status, contentType, data] = await httpProxy(url, params);
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d getting data from flood. Data: %s", status, data);
|
||||
}
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(data);
|
||||
}
|
7
src/widgets/flood/widget.js
Normal file
7
src/widgets/flood/widget.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
import floodProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
proxyHandler: floodProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
31
src/widgets/gluetun/component.jsx
Normal file
31
src/widgets/gluetun/component.jsx
Normal file
|
@ -0,0 +1,31 @@
|
|||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { widget } = service;
|
||||
|
||||
const { data: gluetunData, error: gluetunError } = useWidgetAPI(widget, "ip");
|
||||
|
||||
if (gluetunError) {
|
||||
return <Container error={gluetunError} />;
|
||||
}
|
||||
|
||||
if (!gluetunData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="gluetun.public_ip" />
|
||||
<Block label="gluetun.region" />
|
||||
<Block label="gluetun.country" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="gluetun.public_ip" value={gluetunData.public_ip} />
|
||||
<Block label="gluetun.region" value={gluetunData.region} />
|
||||
<Block label="gluetun.country" value={gluetunData.country} />
|
||||
</Container>
|
||||
);
|
||||
}
|
19
src/widgets/gluetun/widget.js
Normal file
19
src/widgets/gluetun/widget.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/v1/{endpoint}",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
ip: {
|
||||
endpoint: "publicip/ip",
|
||||
validate: [
|
||||
"public_ip",
|
||||
"region",
|
||||
"country"
|
||||
]
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -1,12 +1,8 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: appsData, error: appsError } = useWidgetAPI(widget, "application");
|
||||
|
@ -14,7 +10,8 @@ export default function Component({ service }) {
|
|||
const { data: clientsData, error: clientsError } = useWidgetAPI(widget, "client");
|
||||
|
||||
if (appsError || messagesError || clientsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = appsError ?? messagesError ?? clientsError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
|
||||
|
|
32
src/widgets/hdhomerun/component.jsx
Normal file
32
src/widgets/hdhomerun/component.jsx
Normal file
|
@ -0,0 +1,32 @@
|
|||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { widget } = service;
|
||||
|
||||
const { data: channelsData, error: channelsError } = useWidgetAPI(widget, "lineup");
|
||||
|
||||
if (channelsError) {
|
||||
return <Container error={channelsError} />;
|
||||
}
|
||||
|
||||
if (!channelsData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="hdhomerun.channels" />
|
||||
<Block label="hdhomerun.hd" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const hdChannels = channelsData?.filter((channel) => channel.HD === 1);
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="hdhomerun.channels" value={channelsData.length } />
|
||||
<Block label="hdhomerun.hd" value={hdChannels.length} />
|
||||
|
||||
</Container>
|
||||
);
|
||||
}
|
14
src/widgets/hdhomerun/widget.js
Normal file
14
src/widgets/hdhomerun/widget.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/{endpoint}",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"lineup": {
|
||||
endpoint: "lineup.json",
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -11,8 +11,8 @@ export default function Component({ service }) {
|
|||
|
||||
const { data: homebridgeData, error: homebridgeError } = useWidgetAPI(widget, "info");
|
||||
|
||||
if (homebridgeError || homebridgeData?.error) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
if (homebridgeError) {
|
||||
return <Container error={homebridgeError} />;
|
||||
}
|
||||
|
||||
if (!homebridgeData) {
|
||||
|
|
|
@ -10,7 +10,7 @@ const proxyName = "homebridgeProxyHandler";
|
|||
const sessionTokenCacheKey = `${proxyName}__sessionToken`;
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
async function login(widget) {
|
||||
async function login(widget, service) {
|
||||
const endpoint = "auth/login";
|
||||
const api = widgets?.[widget.type]?.api
|
||||
const loginUrl = new URL(formatApiCall(api, { endpoint, ...widget }));
|
||||
|
@ -26,7 +26,7 @@ async function login(widget) {
|
|||
try {
|
||||
const { access_token: accessToken, expires_in: expiresIn } = JSON.parse(data.toString());
|
||||
|
||||
cache.put(sessionTokenCacheKey, accessToken, (expiresIn * 1000) - 5 * 60 * 1000); // expiresIn (s) - 5m
|
||||
cache.put(`${sessionTokenCacheKey}.${service}`, accessToken, (expiresIn * 1000) - 5 * 60 * 1000); // expiresIn (s) - 5m
|
||||
return { accessToken };
|
||||
} catch (e) {
|
||||
logger.error("Unable to login to Homebridge API: %s", e);
|
||||
|
@ -35,10 +35,11 @@ async function login(widget) {
|
|||
return { accessToken: false };
|
||||
}
|
||||
|
||||
async function apiCall(widget, endpoint) {
|
||||
async function apiCall(widget, endpoint, service) {
|
||||
const key = `${sessionTokenCacheKey}.${service}`;
|
||||
const headers = {
|
||||
"content-type": "application/json",
|
||||
"Authorization": `Bearer ${cache.get(sessionTokenCacheKey)}`,
|
||||
"Authorization": `Bearer ${cache.get(key)}`,
|
||||
}
|
||||
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
|
@ -51,7 +52,7 @@ async function apiCall(widget, endpoint) {
|
|||
|
||||
if (status === 401) {
|
||||
logger.debug("Homebridge API rejected the request, attempting to obtain new session token");
|
||||
const { accessToken } = login(widget);
|
||||
const { accessToken } = login(widget, service);
|
||||
headers.Authorization = `Bearer ${accessToken}`;
|
||||
|
||||
// retry the request, now with the new session token
|
||||
|
@ -83,14 +84,14 @@ export default async function homebridgeProxyHandler(req, res) {
|
|||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
if (!cache.get(sessionTokenCacheKey)) {
|
||||
await login(widget);
|
||||
if (!cache.get(`${sessionTokenCacheKey}.${service}`)) {
|
||||
await login(widget, service);
|
||||
}
|
||||
|
||||
const { data: statusData } = await apiCall(widget, "status/homebridge");
|
||||
const { data: versionData } = await apiCall(widget, "status/homebridge-version");
|
||||
const { data: childBridgeData } = await apiCall(widget, "status/homebridge/child-bridges");
|
||||
const { data: pluginsData } = await apiCall(widget, "plugins");
|
||||
const { data: statusData } = await apiCall(widget, "status/homebridge", service);
|
||||
const { data: versionData } = await apiCall(widget, "status/homebridge-version", service);
|
||||
const { data: childBridgeData } = await apiCall(widget, "status/homebridge/child-bridges", service);
|
||||
const { data: pluginsData } = await apiCall(widget, "plugins", service);
|
||||
|
||||
return res.status(200).send({
|
||||
status: statusData?.status,
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: indexersData, error: indexersError } = useWidgetAPI(widget, "indexers");
|
||||
|
||||
if (indexersError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={indexersError} />;
|
||||
}
|
||||
|
||||
if (!indexersData) {
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "request/count");
|
||||
|
||||
if (statsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData) {
|
||||
|
|
|
@ -7,6 +7,11 @@ const widget = {
|
|||
mappings: {
|
||||
"request/count": {
|
||||
endpoint: "request/count",
|
||||
validate: [
|
||||
"pending",
|
||||
"approved",
|
||||
"available"
|
||||
]
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -14,7 +14,8 @@ export default function Component({ service }) {
|
|||
const { data: queueData, error: queueError } = useWidgetAPI(widget, "queue/status");
|
||||
|
||||
if (albumsError || wantedError || queueError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = albumsError ?? wantedError ?? queueError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!albumsData || !wantedData || !queueData) {
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "instance");
|
||||
|
||||
if (statsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData) {
|
||||
|
|
56
src/widgets/navidrome/component.jsx
Normal file
56
src/widgets/navidrome/component.jsx
Normal file
|
@ -0,0 +1,56 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
function SinglePlayingEntry({ entry }) {
|
||||
const { username, artist, title, album } = entry;
|
||||
let fullTitle = title;
|
||||
if (artist) fullTitle = `${artist} - ${title}`;
|
||||
if (album) fullTitle += ` — ${album}`;
|
||||
if (username) fullTitle += ` (${username})`;
|
||||
|
||||
return (
|
||||
<div className="text-theme-700 dark:text-theme-200 relative h-5 w-full rounded-md bg-theme-200/50 dark:bg-theme-900/20 mt-1 flex">
|
||||
<div className="text-xs z-10 self-center ml-2 relative w-full h-4 grow mr-2">
|
||||
<div className="absolute w-full whitespace-nowrap text-ellipsis overflow-hidden">{fullTitle}</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: navidromeData, error: navidromeError } = useWidgetAPI(widget, "getNowPlaying");
|
||||
|
||||
if (navidromeError || navidromeData?.["subsonic-response"]?.error) {
|
||||
return <Container error={navidromeError ?? navidromeData?.["subsonic-response"]?.error} />;
|
||||
}
|
||||
|
||||
if (!navidromeData) {
|
||||
return (
|
||||
<SinglePlayingEntry entry={{ title: t("navidrome.please_wait") }} />
|
||||
);
|
||||
}
|
||||
|
||||
const { nowPlaying } = navidromeData["subsonic-response"];
|
||||
if (!nowPlaying.entry) {
|
||||
// nothing playing
|
||||
return (
|
||||
<SinglePlayingEntry entry={{ title: t("navidrome.nothing_streaming") }} />
|
||||
);
|
||||
}
|
||||
|
||||
const nowPlayingEntries = Object.values(nowPlaying.entry);
|
||||
|
||||
return (
|
||||
<div className="flex flex-col pb-1 mx-1">
|
||||
{nowPlayingEntries.map((entry) => (
|
||||
<SinglePlayingEntry key={entry.id} entry={entry} />
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
14
src/widgets/navidrome/widget.js
Normal file
14
src/widgets/navidrome/widget.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/rest/{endpoint}?u={user}&t={token}&s={salt}&v=1.16.1&c=homepage&f=json",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"getNowPlaying": {
|
||||
endpoint: "getNowPlaying",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -1,18 +1,14 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: infoData, error: infoError } = useWidgetAPI(widget, "nginx/proxy-hosts");
|
||||
|
||||
if (infoError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={infoError} />;
|
||||
}
|
||||
|
||||
if (!infoData) {
|
||||
|
|
|
@ -1,6 +1,39 @@
|
|||
import cache from "memory-cache";
|
||||
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import widgets from "widgets/widgets";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const proxyName = "npmProxyHandler";
|
||||
const tokenCacheKey = `${proxyName}__token`;
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
async function login(loginUrl, username, password, service) {
|
||||
const authResponse = await httpProxy(loginUrl, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ identity: username, secret: password }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
const status = authResponse[0];
|
||||
let data = authResponse[2];
|
||||
|
||||
try {
|
||||
data = JSON.parse(Buffer.from(authResponse[2]).toString());
|
||||
|
||||
if (status === 200) {
|
||||
const expiration = new Date(data.expires) - Date.now();
|
||||
cache.put(`${tokenCacheKey}.${service}`, data.token, expiration - (5 * 60 * 1000)); // expiration -5 minutes
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(`Error ${status} logging into npm`, authResponse[2]);
|
||||
}
|
||||
return [status, data.token ?? data];
|
||||
}
|
||||
|
||||
export default async function npmProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
@ -14,27 +47,54 @@ export default async function npmProxyHandler(req, res) {
|
|||
|
||||
if (widget) {
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
|
||||
const loginUrl = `${widget.url}/api/tokens`;
|
||||
const body = { identity: widget.username, secret: widget.password };
|
||||
|
||||
const authResponse = await fetch(loginUrl, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}).then((response) => response.json());
|
||||
let status;
|
||||
let contentType;
|
||||
let data;
|
||||
|
||||
let token = cache.get(`${tokenCacheKey}.${service}`);
|
||||
if (!token) {
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password, service);
|
||||
if (status !== 200) {
|
||||
logger.debug(`HTTTP ${status} logging into npm api: ${token}`);
|
||||
return res.status(status).send(token);
|
||||
}
|
||||
}
|
||||
|
||||
const apiResponse = await fetch(url, {
|
||||
[status, contentType, data] = await httpProxy(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${authResponse.token}`,
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}).then((response) => response.json());
|
||||
});
|
||||
|
||||
return res.send(apiResponse);
|
||||
if (status === 403) {
|
||||
logger.debug(`HTTTP ${status} retrieving data from npm api, logging in and trying again.`);
|
||||
cache.del(`${tokenCacheKey}.${service}`);
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password, service);
|
||||
|
||||
if (status !== 200) {
|
||||
logger.debug(`HTTTP ${status} logging into npm api: ${data}`);
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
[status, contentType, data] = await httpProxy(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
return res.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: statusData, error: statusError } = useWidgetAPI(widget, "status");
|
||||
|
||||
if (statusError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statusError} />;
|
||||
}
|
||||
|
||||
if (!statusData) {
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
import { JSONRPCClient } from "json-rpc-2.0";
|
||||
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
|
||||
export default async function nzbgetProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (widget) {
|
||||
const constructedUrl = new URL(widget.url);
|
||||
constructedUrl.pathname = "jsonrpc";
|
||||
|
||||
const authorization = Buffer.from(`${widget.username}:${widget.password}`).toString("base64");
|
||||
|
||||
const client = new JSONRPCClient((jsonRPCRequest) =>
|
||||
fetch(constructedUrl.toString(), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
authorization: `Basic ${authorization}`,
|
||||
},
|
||||
body: JSON.stringify(jsonRPCRequest),
|
||||
}).then(async (response) => {
|
||||
if (response.status === 200) {
|
||||
const jsonRPCResponse = await response.json();
|
||||
return client.receive(jsonRPCResponse);
|
||||
}
|
||||
|
||||
return Promise.reject(new Error(response.statusText));
|
||||
})
|
||||
);
|
||||
|
||||
return res.send(await client.request(endpoint));
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
|
@ -1,7 +1,8 @@
|
|||
import nzbgetProxyHandler from "./proxy";
|
||||
import jsonrpcProxyHandler from "utils/proxy/handlers/jsonrpc";
|
||||
|
||||
const widget = {
|
||||
proxyHandler: nzbgetProxyHandler,
|
||||
api: "{url}/jsonrpc",
|
||||
proxyHandler: jsonrpcProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
||||
|
|
|
@ -1,18 +1,14 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "Request/count");
|
||||
|
||||
if (statsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData) {
|
||||
|
|
|
@ -1,24 +1,21 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statsData, error: statsError } = useWidgetAPI(widget, "request/count");
|
||||
|
||||
if (statsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statsError} />;
|
||||
}
|
||||
|
||||
if (!statsData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="overseerr.pending" />
|
||||
<Block label="overseerr.processing" />
|
||||
<Block label="overseerr.approved" />
|
||||
<Block label="overseerr.available" />
|
||||
</Container>
|
||||
|
@ -28,6 +25,7 @@ export default function Component({ service }) {
|
|||
return (
|
||||
<Container service={service}>
|
||||
<Block label="overseerr.pending" value={statsData.pending} />
|
||||
<Block label="overseerr.processing" value={statsData.processing} />
|
||||
<Block label="overseerr.approved" value={statsData.approved} />
|
||||
<Block label="overseerr.available" value={statsData.available} />
|
||||
</Container>
|
||||
|
|
|
@ -7,6 +7,12 @@ const widget = {
|
|||
mappings: {
|
||||
"request/count": {
|
||||
endpoint: "request/count",
|
||||
validate: [
|
||||
"pending",
|
||||
"processing",
|
||||
"approved",
|
||||
"available",
|
||||
],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
29
src/widgets/paperlessngx/component.jsx
Normal file
29
src/widgets/paperlessngx/component.jsx
Normal file
|
@ -0,0 +1,29 @@
|
|||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { widget } = service;
|
||||
|
||||
const { data: statisticsData, error: statisticsError } = useWidgetAPI(widget, "statistics");
|
||||
|
||||
if (statisticsError) {
|
||||
return <Container error={statisticsError} />;
|
||||
}
|
||||
|
||||
if (!statisticsData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="paperlessngx.inbox" />
|
||||
<Block label="paperlessngx.total" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{statisticsData.documents_inbox !== undefined && <Block label="paperlessngx.inbox" value={statisticsData.documents_inbox} />}
|
||||
<Block label="paperlessngx.total" value={statisticsData.documents_total} />
|
||||
</Container>
|
||||
);
|
||||
}
|
17
src/widgets/paperlessngx/widget.js
Normal file
17
src/widgets/paperlessngx/widget.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"statistics": {
|
||||
endpoint: "statistics/?format=json",
|
||||
validate: [
|
||||
"documents_total"
|
||||
]
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: piholeData, error: piholeError } = useWidgetAPI(widget, "api.php");
|
||||
|
||||
if (piholeError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={piholeError} />;
|
||||
}
|
||||
|
||||
if (!piholeData) {
|
||||
|
|
|
@ -7,6 +7,11 @@ const widget = {
|
|||
mappings: {
|
||||
"api.php": {
|
||||
endpoint: "api.php",
|
||||
validate: [
|
||||
"dns_queries_today",
|
||||
"ads_blocked_today",
|
||||
"domains_being_blocked"
|
||||
]
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
import useSWR from "swr";
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Block from "components/services/widget/block";
|
||||
import Container from "components/services/widget/container";
|
||||
import { formatProxyUrl } from "utils/proxy/api-helpers";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: plexData, error: plexAPIError } = useSWR(formatProxyUrl(widget, "unified"), {
|
||||
const { data: plexData, error: plexAPIError } = useWidgetAPI(widget, "unified", {
|
||||
refreshInterval: 5000,
|
||||
});
|
||||
|
||||
if (plexAPIError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={plexAPIError} />;
|
||||
}
|
||||
|
||||
if (!plexData) {
|
||||
|
|
|
@ -44,7 +44,7 @@ async function fetchFromPlexAPI(endpoint, widget) {
|
|||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d communicating with Plex. Data: %s", status, data.toString());
|
||||
return [status, data.toString()];
|
||||
return [status, data];
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -58,6 +58,9 @@ async function fetchFromPlexAPI(endpoint, widget) {
|
|||
|
||||
export default async function plexProxyHandler(req, res) {
|
||||
const widget = await getWidget(req);
|
||||
|
||||
const { service } = req.query;
|
||||
|
||||
if (!widget) {
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
@ -65,27 +68,33 @@ export default async function plexProxyHandler(req, res) {
|
|||
logger.debug("Getting streams from Plex API");
|
||||
let streams;
|
||||
let [status, apiData] = await fetchFromPlexAPI("/status/sessions", widget);
|
||||
|
||||
if (status !== 200) {
|
||||
return res.status(status).json({error: {message: "HTTP error communicating with Plex API", data: Buffer.from(apiData).toString()}});
|
||||
}
|
||||
|
||||
if (apiData && apiData.MediaContainer) {
|
||||
streams = apiData.MediaContainer._attributes.size;
|
||||
}
|
||||
|
||||
let libraries = cache.get(librariesCacheKey);
|
||||
let libraries = cache.get(`${librariesCacheKey}.${service}`);
|
||||
if (libraries === null) {
|
||||
logger.debug("Getting libraries from Plex API");
|
||||
[status, apiData] = await fetchFromPlexAPI("/library/sections", widget);
|
||||
if (apiData && apiData.MediaContainer) {
|
||||
libraries = apiData.MediaContainer.Directory;
|
||||
cache.put(librariesCacheKey, libraries, 1000 * 60 * 60 * 6);
|
||||
libraries = [].concat(apiData.MediaContainer.Directory);
|
||||
cache.put(`${librariesCacheKey}.${service}`, libraries, 1000 * 60 * 60 * 6);
|
||||
}
|
||||
}
|
||||
|
||||
let movies = cache.get(moviesCacheKey);
|
||||
let tv = cache.get(tvCacheKey);
|
||||
let movies = cache.get(`${moviesCacheKey}.${service}`);
|
||||
let tv = cache.get(`${tvCacheKey}.${service}`);
|
||||
if (movies === null || tv === null) {
|
||||
movies = 0;
|
||||
tv = 0;
|
||||
logger.debug("Getting movie + tv counts from Plex API");
|
||||
libraries.filter(l => ["movie", "show"].includes(l._attributes.type)).forEach(async (library) => {
|
||||
const movieTVLibraries = libraries.filter(l => ["movie", "show"].includes(l._attributes.type));
|
||||
await Promise.all(movieTVLibraries.map(async (library) => {
|
||||
[status, apiData] = await fetchFromPlexAPI(`/library/sections/${library._attributes.key}/all`, widget);
|
||||
if (apiData && apiData.MediaContainer) {
|
||||
const size = parseInt(apiData.MediaContainer._attributes.size, 10);
|
||||
|
@ -95,9 +104,9 @@ export default async function plexProxyHandler(req, res) {
|
|||
tv += size;
|
||||
}
|
||||
}
|
||||
cache.put(tvCacheKey, tv, 1000 * 60 * 10);
|
||||
cache.put(moviesCacheKey, movies, 1000 * 60 * 10);
|
||||
});
|
||||
}));
|
||||
cache.put(`${tvCacheKey}.${service}`, tv, 1000 * 60 * 10);
|
||||
cache.put(`${moviesCacheKey}.${service}`, movies, 1000 * 60 * 10);
|
||||
}
|
||||
|
||||
const data = {
|
||||
|
|
|
@ -14,7 +14,7 @@ export default function Component({ service }) {
|
|||
});
|
||||
|
||||
if (containersError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={containersError} />;
|
||||
}
|
||||
|
||||
if (!containersData) {
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: indexersData, error: indexersError } = useWidgetAPI(widget, "indexer");
|
||||
const { data: grabsData, error: grabsError } = useWidgetAPI(widget, "indexerstats");
|
||||
|
||||
if (indexersError || grabsError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = indexersError ?? grabsError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!indexersData || !grabsData) {
|
||||
|
|
|
@ -16,7 +16,7 @@ export default function Component({ service }) {
|
|||
const { data: clusterData, error: clusterError } = useWidgetAPI(widget, "cluster/resources");
|
||||
|
||||
if (clusterError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={clusterError} />;
|
||||
}
|
||||
|
||||
if (!clusterData || !clusterData.data) {
|
||||
|
|
35
src/widgets/pyload/component.jsx
Normal file
35
src/widgets/pyload/component.jsx
Normal file
|
@ -0,0 +1,35 @@
|
|||
import { useTranslation } from 'next-i18next'
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
const { data: pyloadData, error: pyloadError } = useWidgetAPI(widget, "status");
|
||||
|
||||
if (pyloadError) {
|
||||
return <Container error={pyloadError} />;
|
||||
}
|
||||
|
||||
if (!pyloadData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="pyload.speed" />
|
||||
<Block label="pyload.active" />
|
||||
<Block label="pyload.queue" />
|
||||
<Block label="pyload.total" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="pyload.speed" value={t("common.bitrate", { value: pyloadData.speed })} />
|
||||
<Block label="pyload.active" value={t("common.number", { value: pyloadData.active })} />
|
||||
<Block label="pyload.queue" value={t("common.number", { value: pyloadData.queue })} />
|
||||
<Block label="pyload.total" value={t("common.number", { value: pyloadData.total })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
102
src/widgets/pyload/proxy.js
Normal file
102
src/widgets/pyload/proxy.js
Normal file
|
@ -0,0 +1,102 @@
|
|||
import cache from 'memory-cache';
|
||||
|
||||
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';
|
||||
|
||||
const proxyName = 'pyloadProxyHandler';
|
||||
const logger = createLogger(proxyName);
|
||||
const sessionCacheKey = `${proxyName}__sessionId`;
|
||||
const isNgCacheKey = `${proxyName}__isNg`;
|
||||
|
||||
async function fetchFromPyloadAPI(url, sessionId, params, service) {
|
||||
const options = {
|
||||
body: params
|
||||
? Object.keys(params)
|
||||
.map((prop) => `${prop}=${params[prop]}`)
|
||||
.join('&')
|
||||
: `session=${sessionId}`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
},
|
||||
};
|
||||
|
||||
// see https://github.com/benphelps/homepage/issues/517
|
||||
const isNg = cache.get(`${isNgCacheKey}.${service}`);
|
||||
if (isNg && !params) {
|
||||
delete options.body;
|
||||
options.headers.Cookie = cache.get(`${sessionCacheKey}.${service}`);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data, responseHeaders] = await httpProxy(url, options);
|
||||
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];
|
||||
}
|
||||
|
||||
async function login(loginUrl, service, username, password = '') {
|
||||
const [status, sessionId, responseHeaders] = await fetchFromPyloadAPI(loginUrl, null, { username, password }, service);
|
||||
|
||||
// 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')) {
|
||||
// Support pyload-ng, see https://github.com/benphelps/homepage/issues/517
|
||||
cache.put(`${isNgCacheKey}.${service}`, true);
|
||||
const sessionCookie = responseHeaders['set-cookie'][0];
|
||||
cache.put(`${sessionCacheKey}.${service}`, sessionCookie, 60 * 60 * 23 * 1000); // cache for 23h
|
||||
} else {
|
||||
cache.put(`${sessionCacheKey}.${service}`, sessionId);
|
||||
}
|
||||
|
||||
return sessionId;
|
||||
}
|
||||
|
||||
export default async function pyloadProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
try {
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (widget) {
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
const loginUrl = `${widget.url}/api/login`;
|
||||
|
||||
let sessionId = cache.get(`${sessionCacheKey}.${service}`) ?? await login(loginUrl, service, widget.username, widget.password);
|
||||
let [status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
||||
|
||||
if (status === 403 || status === 401) {
|
||||
logger.info('Failed to retrieve data from Pyload API, trying to login again...');
|
||||
cache.del(`${sessionCacheKey}.${service}`);
|
||||
sessionId = await login(loginUrl, service, widget.username, widget.password);
|
||||
[status, data] = await fetchFromPyloadAPI(url, sessionId, null, service);
|
||||
}
|
||||
|
||||
if (data?.error || status !== 200) {
|
||||
try {
|
||||
return res.status(status).send({error: {message: "HTTP error communicating with Plex API", data: Buffer.from(data).toString()}});
|
||||
} catch (e) {
|
||||
return res.status(status).send({error: {message: "HTTP error communicating with Plex API", data}});
|
||||
}
|
||||
}
|
||||
|
||||
return res.json(data);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(e);
|
||||
return res.status(500).send({error: {message: `Error communicating with Plex API: ${e.toString()}`}});
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: 'Invalid proxy service type' });
|
||||
}
|
14
src/widgets/pyload/widget.js
Normal file
14
src/widgets/pyload/widget.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
import pyloadProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: pyloadProxyHandler,
|
||||
|
||||
mappings: {
|
||||
"status": {
|
||||
endpoint: "statusServer",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default widget;
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: torrentData, error: torrentError } = useWidgetAPI(widget, "torrents/info");
|
||||
|
||||
if (torrentError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={torrentError} />;
|
||||
}
|
||||
|
||||
if (!torrentData) {
|
||||
|
|
|
@ -1,30 +1,23 @@
|
|||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const logger = createLogger("qbittorrentProxyHandler");
|
||||
|
||||
async function login(widget, params) {
|
||||
async function login(widget) {
|
||||
logger.debug("qBittorrent is rejecting the request, logging in.");
|
||||
const loginUrl = new URL(`${widget.url}/api/v2/auth/login`).toString();
|
||||
const loginBody = `username=${encodeURI(widget.username)}&password=${encodeURI(widget.password)}`;
|
||||
|
||||
// using fetch intentionally, for login only, as the httpProxy method causes qBittorrent to
|
||||
// complain about header encoding
|
||||
return fetch(loginUrl, {
|
||||
const loginParams = {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: loginBody,
|
||||
})
|
||||
.then(async (response) => {
|
||||
addCookieToJar(loginUrl, response.headers);
|
||||
setCookieHeader(loginUrl, params);
|
||||
const data = await response.text();
|
||||
return [response.status, data];
|
||||
})
|
||||
.catch((err) => [500, err]);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const [status, contentType, data] = await httpProxy(loginUrl, loginParams);
|
||||
return [status, data];
|
||||
}
|
||||
|
||||
export default async function qbittorrentProxyHandler(req, res) {
|
||||
|
@ -44,11 +37,10 @@ export default async function qbittorrentProxyHandler(req, res) {
|
|||
|
||||
const url = new URL(formatApiCall("{url}/api/v2/{endpoint}", { endpoint, ...widget }));
|
||||
const params = { method: "GET", headers: {} };
|
||||
setCookieHeader(url, params);
|
||||
|
||||
let [status, contentType, data] = await httpProxy(url, params);
|
||||
if (status === 403) {
|
||||
[status, data] = await login(widget, params);
|
||||
[status, data] = await login(widget);
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d logging in to qBittorrent. Data: %s", status, data);
|
||||
|
@ -59,9 +51,9 @@ export default async function qbittorrentProxyHandler(req, res) {
|
|||
logger.error("Error logging in to qBittorrent: Data: %s", data);
|
||||
return res.status(401).end(data);
|
||||
}
|
||||
}
|
||||
|
||||
[status, contentType, data] = await httpProxy(url, params);
|
||||
[status, contentType, data] = await httpProxy(url, params);
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error("HTTP %d getting data from qBittorrent. Data: %s", status, data);
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: moviesData, error: moviesError } = useWidgetAPI(widget, "movie");
|
||||
const { data: queuedData, error: queuedError } = useWidgetAPI(widget, "queue/status");
|
||||
|
||||
if (moviesError || queuedError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = moviesError ?? queuedError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!moviesData || !queuedData) {
|
||||
|
|
|
@ -16,6 +16,9 @@ const widget = {
|
|||
},
|
||||
"queue/status": {
|
||||
endpoint: "queue/status",
|
||||
validate: [
|
||||
"totalCount"
|
||||
]
|
||||
},
|
||||
},
|
||||
};
|
||||
|
|
|
@ -14,7 +14,8 @@ export default function Component({ service }) {
|
|||
const { data: queueData, error: queueError } = useWidgetAPI(widget, "queue/status");
|
||||
|
||||
if (booksError || wantedError || queueError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
const finalError = booksError ?? wantedError ?? queueError;
|
||||
return <Container error={finalError} />;
|
||||
}
|
||||
|
||||
if (!booksData || !wantedData || !queueData) {
|
||||
|
|
|
@ -12,7 +12,7 @@ export default function Component({ service }) {
|
|||
const { data: statusData, error: statusError } = useWidgetAPI(widget);
|
||||
|
||||
if (statusError) {
|
||||
return <Container error={t("widget.api_error")} />;
|
||||
return <Container error={statusError} />;
|
||||
}
|
||||
|
||||
if (!statusData) {
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue