diff --git a/server/routers/auth/verifyTargetAccess.ts b/server/routers/auth/verifyTargetAccess.ts index 86d63e0d..2f6a0658 100644 --- a/server/routers/auth/verifyTargetAccess.ts +++ b/server/routers/auth/verifyTargetAccess.ts @@ -33,7 +33,7 @@ export async function verifyTargetAccess(req: Request, res: Response, next: Next const resourceId = target[0].resourceId; - if (resourceId) { + if (!resourceId) { return next( createHttpError( HttpCode.INTERNAL_SERVER_ERROR, diff --git a/src/app/[orgId]/resources/[resourceId]/components/CreateResource.tsx b/src/app/[orgId]/resources/[resourceId]/components/CreateResource.tsx index 5b3c4cfa..1066a93b 100644 --- a/src/app/[orgId]/resources/[resourceId]/components/CreateResource.tsx +++ b/src/app/[orgId]/resources/[resourceId]/components/CreateResource.tsx @@ -25,18 +25,18 @@ import { useRouter } from "next/navigation"; import { Checkbox } from "@app/components/ui/checkbox" import { - Command, - CommandEmpty, - CommandGroup, - CommandInput, - CommandItem, - CommandList, + Command, + CommandEmpty, + CommandGroup, + CommandInput, + CommandItem, + CommandList, } from "@/components/ui/command" import { - Popover, - PopoverContent, - PopoverTrigger, + Popover, + PopoverContent, + PopoverTrigger, } from "@/components/ui/popover" import { ListSitesResponse } from "@server/routers/site" import { AxiosResponse } from "axios" @@ -92,7 +92,7 @@ export function CreateResourceForm() { async function onSubmit(data: AccountFormValues) { console.log(data); - + const res = await api .put(`/org/${orgId}/site/${data.siteId}/resource/`, { name: data.name, @@ -141,10 +141,10 @@ export function CreateResourceForm() { {/* */} + /> This is the fully qualified domain name that will be used to access the resource. @@ -169,69 +169,69 @@ export function CreateResourceForm() { )} /> */} - ( - - Site - - - - - {field.value - ? sites.find( - (site) => site.siteId === field.value - )?.name - : "Select site"} - - - - - - - - - No site found. - - {sites.map((site) => ( - { - form.setValue("siteId", site.siteId) - }} - > - - {site.name} - - ))} - - - - - - - This is the site that will be used in the dashboard. - - - - )} - /> + ( + + Site + + + + + {field.value + ? sites.find( + (site) => site.siteId === field.value + )?.name + : "Select site"} + + + + + + + + + No site found. + + {sites.map((site) => ( + { + form.setValue("siteId", site.siteId) + }} + > + + {site.name} + + ))} + + + + + + + This is the site that will be used in the dashboard. + + + + )} + /> Create Resource diff --git a/src/app/[orgId]/resources/[resourceId]/targets/page.tsx b/src/app/[orgId]/resources/[resourceId]/targets/page.tsx index 0d6689f5..b237bcdd 100644 --- a/src/app/[orgId]/resources/[resourceId]/targets/page.tsx +++ b/src/app/[orgId]/resources/[resourceId]/targets/page.tsx @@ -13,9 +13,15 @@ import api from "@app/api" import { AxiosResponse } from "axios" import { ListTargetsResponse } from "@server/routers/target/listTargets" +const isValidIPAddress = (ip: string) => { + const ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/; + return ipv4Regex.test(ip); +} + export default function ReverseProxyTargets({ params }: { params: { resourceId: string } }) { const [targets, setTargets] = useState([]) const [nextId, setNextId] = useState(1) + const [ipError, setIpError] = useState("") useEffect(() => { if (typeof window !== "undefined") { @@ -30,35 +36,47 @@ export default function ReverseProxyTargets({ params }: { params: { resourceId: const [newTarget, setNewTarget] = useState({ resourceId: params.resourceId, ip: "", - method: "GET", + method: "http", port: 80, - protocol: "http", + protocol: "TCP", }) - const addTarget = async () => { - const res = await api.put(`/resource/${params.resourceId}/target`, { + const addTarget = () => { + if (!isValidIPAddress(newTarget.ip)) { + setIpError("Invalid IP address format"); + return; + } + setIpError(""); + + api.put(`/resource/${params.resourceId}/target`, { ...newTarget, resourceId: undefined }) - .catch((err) => { - console.error(err) - - }); + .catch((err) => { + console.error(err) + + }).then((res) => { + // console.log(res) + setTargets([...targets, { ...newTarget, targetId: nextId, enabled: true }]) + setNextId(nextId + 1) + setNewTarget({ + resourceId: params.resourceId, + ip: "", + method: "GET", + port: 80, + protocol: "http", + }) + }); - setTargets([...targets, { ...newTarget, targetId: nextId, enabled: true }]) - setNextId(nextId + 1) - setNewTarget({ - resourceId: params.resourceId, - ip: "", - method: "GET", - port: 80, - protocol: "http", - }) } - const removeTarget = async (targetId: number) => { - setTargets(targets.filter((target) => target.targetId !== targetId)) - const res = await api.delete(`/target/${targetId}`) + const removeTarget = (targetId: number) => { + api.delete(`/target/${targetId}`) + .catch((err) => { + console.error(err) + }).then((res) => { + setTargets(targets.filter((target) => target.targetId !== targetId)); + }); } const toggleTarget = (targetId: number) => { @@ -67,7 +85,12 @@ export default function ReverseProxyTargets({ params }: { params: { resourceId: target.targetId === targetId ? { ...target, enabled: !target.enabled } : target ) ) - const res = api.post(`/target/${targetId}`, { enabled: !targets.find((target) => target.targetId === targetId)?.enabled }) + api.post(`/target/${targetId}`, { enabled: !targets.find((target) => target.targetId === targetId)?.enabled }) + .catch((err) => { + console.error(err) + }).then((res) => { + console.log(res) + }); // Add a visual feedback const targetElement = document.getElementById(`target-${targetId}`) @@ -81,93 +104,98 @@ export default function ReverseProxyTargets({ params }: { params: { resourceId: return ( - {/* - */} - {/* Add New Target - - */} - { - e.preventDefault() - addTarget() - }} - className="space-y-4" - > - - - IP Address - setNewTarget({ ...newTarget, ip: e.target.value })} - required - /> - - - Method - setNewTarget({ ...newTarget, method: value })} - > - - - - - GET - POST - PUT - DELETE - - - - - Port - setNewTarget({ ...newTarget, port: parseInt(e.target.value) })} - required - /> - - - Protocol - setNewTarget({ ...newTarget, protocol: value })} - > - - - - - HTTP - HTTPS - - - - - - Add Target - - - {/* - */} + { + e.preventDefault() + addTarget() + }} + className="space-y-4" + > + + + IP Address + { + setNewTarget({ ...newTarget, ip: e.target.value }) + setIpError("") + }} + required + /> + {ipError && {ipError}} + + + Method + setNewTarget({ ...newTarget, method: value })} + > + + + + + HTTP + HTTPS + + + + + Port + setNewTarget({ ...newTarget, port: parseInt(e.target.value) })} + required + /> + + + Protocol + setNewTarget({ ...newTarget, protocol: value })} + > + + + + + UDP + TCP + + + + + + Add Target + + - + {targets.map((target) => ( - - - - + + + + Target {target.targetId} - toggleTarget(target.targetId)} - /> + + toggleTarget(target.targetId)} + /> + removeTarget(target.targetId)} + > + + + - - + + {target.ip}:{target.port} @@ -181,14 +209,6 @@ export default function ReverseProxyTargets({ params }: { params: { resourceId: {target.protocol?.toUpperCase()} - removeTarget(target.targetId)} - > - Remove - ))}
{ipError}