Skip to content

Commit

Permalink
Enforce method
Browse files Browse the repository at this point in the history
  • Loading branch information
shamoon committed Jun 3, 2024
1 parent 67a9f49 commit 19c3ac0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 15 deletions.
5 changes: 5 additions & 0 deletions src/pages/api/services/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export default async function handler(req, res) {
const endpoint = mapping?.endpoint;
const endpointProxy = mapping?.proxyHandler || serviceProxyHandler;

if (mapping.method && mapping.method !== req.method) {
logger.debug("Unsupported method: %s", req.method);
return res.status(403).json({ error: "Unsupported method" });
}

if (!endpoint) {
logger.debug("Unsupported service endpoint: %s", type);
return res.status(403).json({ error: "Unsupported service endpoint" });
Expand Down
4 changes: 3 additions & 1 deletion src/widgets/emby/component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ export default function Component({ service }) {
}),
);
const url = `/api/services/proxy?${params.toString()}`;
await fetch(url).then(() => {
await fetch(url, {
method: "POST",
}).then(() => {
sessionMutate();
});
}
Expand Down
18 changes: 13 additions & 5 deletions src/widgets/stash/component.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";

import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
import { formatProxyUrl } from "utils/proxy/api-helpers";

export default function Component({ service }) {
const { t } = useTranslation();

const { widget } = service;
const { data: stats, error: stashError } = useWidgetAPI(widget, "stats");
const [stats, setStats] = useState(null);

if (stashError) {
return <Container service={service} error={stashError} />;
}
useEffect(() => {
async function fetchStats() {
const url = formatProxyUrl(widget, "stats");
const res = await fetch(url, { method: "POST" });
setStats(await res.json());
}
if (!stats) {
fetchStats();
}
}, [widget, stats]);

if (!stats) {
return (
Expand Down
22 changes: 18 additions & 4 deletions src/widgets/unmanic/component.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { useEffect, useState } from "react";

import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
import { formatProxyUrl } from "utils/proxy/api-helpers";

export default function Component({ service }) {
const { widget } = service;

const { data: workersData, error: workersError } = useWidgetAPI(widget, "workers");
const { data: pendingData, error: pendingError } = useWidgetAPI(widget, "pending");

if (workersError || pendingError) {
const finalError = workersError ?? pendingError;
return <Container service={service} error={finalError} />;
const [pendingData, setPendingData] = useState(null);

useEffect(() => {
async function fetchPending() {
const url = formatProxyUrl(widget, "pending");
const res = await fetch(url, { method: "POST" });
setPendingData(await res.json());
}
if (!pendingData) {
fetchPending();
}
}, [widget, pendingData]);

if (workersError) {
return <Container service={service} error={workersError} />;
}

if (!workersData || !pendingData) {
Expand Down
18 changes: 13 additions & 5 deletions src/widgets/uptimerobot/component.jsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { useTranslation } from "next-i18next";
import { useEffect, useState } from "react";

import Container from "components/services/widget/container";
import Block from "components/services/widget/block";
import useWidgetAPI from "utils/proxy/use-widget-api";
import { formatProxyUrl } from "utils/proxy/api-helpers";

export default function Component({ service }) {
const { widget } = service;
const { t } = useTranslation();

const { data: uptimerobotData, error: uptimerobotError } = useWidgetAPI(widget, "getmonitors");
const [uptimerobotData, setUptimerobotData] = useState(null);

if (uptimerobotError) {
return <Container service={service} error={uptimerobotError} />;
}
useEffect(() => {
async function fetchData() {
const url = formatProxyUrl(widget, "getmonitors");
const res = await fetch(url, { method: "POST" });
setUptimerobotData(await res.json());
}
if (!uptimerobotData) {
fetchData();
}
}, [widget, uptimerobotData]);

if (!uptimerobotData) {
return (
Expand Down

0 comments on commit 19c3ac0

Please sign in to comment.