mirror of
https://github.com/fosrl/pangolin.git
synced 2026-03-09 04:06:36 +00:00
Add Smart Host Parsing
This commit is contained in:
@@ -94,6 +94,7 @@ import {
|
|||||||
CommandList
|
CommandList
|
||||||
} from "@app/components/ui/command";
|
} from "@app/components/ui/command";
|
||||||
import { Badge } from "@app/components/ui/badge";
|
import { Badge } from "@app/components/ui/badge";
|
||||||
|
import { parseHostTarget } from "@app/lib/parseHostTarget";
|
||||||
|
|
||||||
const addTargetSchema = z.object({
|
const addTargetSchema = z.object({
|
||||||
ip: z.string().refine(isTargetValid),
|
ip: z.string().refine(isTargetValid),
|
||||||
@@ -647,13 +648,25 @@ export default function ReverseProxyTargets(props: {
|
|||||||
<Input
|
<Input
|
||||||
defaultValue={row.original.ip}
|
defaultValue={row.original.ip}
|
||||||
className="min-w-[150px]"
|
className="min-w-[150px]"
|
||||||
onBlur={(e) =>
|
onBlur={(e) => {
|
||||||
|
const parsed = parseHostTarget(e.target.value);
|
||||||
|
if (parsed) {
|
||||||
|
updateTarget(row.original.targetId, {
|
||||||
|
...row.original,
|
||||||
|
method: parsed.protocol,
|
||||||
|
ip: parsed.host,
|
||||||
|
port: parsed.port
|
||||||
|
});
|
||||||
|
} else {
|
||||||
updateTarget(row.original.targetId, {
|
updateTarget(row.original.targetId, {
|
||||||
...row.original,
|
...row.original,
|
||||||
ip: e.target.value
|
ip: e.target.value
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
}}
|
||||||
|
|
||||||
/>
|
/>
|
||||||
|
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -942,11 +955,22 @@ export default function ReverseProxyTargets(props: {
|
|||||||
name="ip"
|
name="ip"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="relative">
|
<FormItem className="relative">
|
||||||
<FormLabel>
|
<FormLabel>{t("targetAddr")}</FormLabel>
|
||||||
{t("targetAddr")}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input id="ip" {...field} />
|
<Input
|
||||||
|
id="ip"
|
||||||
|
{...field}
|
||||||
|
onBlur={(e) => {
|
||||||
|
const parsed = parseHostTarget(e.target.value);
|
||||||
|
if (parsed) {
|
||||||
|
addTargetForm.setValue("method", parsed.protocol);
|
||||||
|
addTargetForm.setValue("ip", parsed.host);
|
||||||
|
addTargetForm.setValue("port", parsed.port);
|
||||||
|
} else {
|
||||||
|
field.onBlur();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
</FormItem>
|
</FormItem>
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ import { ArrayElement } from "@server/types/ArrayElement";
|
|||||||
import { isTargetValid } from "@server/lib/validators";
|
import { isTargetValid } from "@server/lib/validators";
|
||||||
import { ListTargetsResponse } from "@server/routers/target";
|
import { ListTargetsResponse } from "@server/routers/target";
|
||||||
import { DockerManager, DockerState } from "@app/lib/docker";
|
import { DockerManager, DockerState } from "@app/lib/docker";
|
||||||
|
import { parseHostTarget } from "@app/lib/parseHostTarget";
|
||||||
|
|
||||||
const baseResourceFormSchema = z.object({
|
const baseResourceFormSchema = z.object({
|
||||||
name: z.string().min(1).max(255),
|
name: z.string().min(1).max(255),
|
||||||
@@ -622,12 +623,23 @@ export default function Page() {
|
|||||||
<Input
|
<Input
|
||||||
defaultValue={row.original.ip}
|
defaultValue={row.original.ip}
|
||||||
className="min-w-[150px]"
|
className="min-w-[150px]"
|
||||||
onBlur={(e) =>
|
onBlur={(e) => {
|
||||||
|
const parsed = parseHostTarget(e.target.value);
|
||||||
|
|
||||||
|
if (parsed) {
|
||||||
updateTarget(row.original.targetId, {
|
updateTarget(row.original.targetId, {
|
||||||
...row.original,
|
...row.original,
|
||||||
ip: e.target.value
|
method: parsed.protocol,
|
||||||
})
|
ip: parsed.host,
|
||||||
|
port: parsed.port ? Number(parsed.port) : undefined,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
updateTarget(row.original.targetId, {
|
||||||
|
...row.original,
|
||||||
|
ip: e.target.value,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
@@ -1176,21 +1188,25 @@ export default function Page() {
|
|||||||
)}
|
)}
|
||||||
|
|
||||||
<FormField
|
<FormField
|
||||||
control={
|
control={addTargetForm.control}
|
||||||
addTargetForm.control
|
|
||||||
}
|
|
||||||
name="ip"
|
name="ip"
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="relative">
|
<FormItem className="relative">
|
||||||
<FormLabel>
|
<FormLabel>{t("targetAddr")}</FormLabel>
|
||||||
{t(
|
|
||||||
"targetAddr"
|
|
||||||
)}
|
|
||||||
</FormLabel>
|
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input
|
<Input
|
||||||
id="ip"
|
id="ip"
|
||||||
{...field}
|
{...field}
|
||||||
|
onBlur={(e) => {
|
||||||
|
const parsed = parseHostTarget(e.target.value);
|
||||||
|
if (parsed) {
|
||||||
|
addTargetForm.setValue("method", parsed.protocol);
|
||||||
|
addTargetForm.setValue("ip", parsed.host);
|
||||||
|
addTargetForm.setValue("port", parsed.port);
|
||||||
|
} else {
|
||||||
|
field.onBlur();
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
|
|||||||
15
src/lib/parseHostTarget.ts
Normal file
15
src/lib/parseHostTarget.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
export function parseHostTarget(input: string) {
|
||||||
|
try {
|
||||||
|
const normalized = input.match(/^https?:\/\//) ? input : `http://${input}`;
|
||||||
|
const url = new URL(normalized);
|
||||||
|
|
||||||
|
const protocol = url.protocol.replace(":", ""); // http | https
|
||||||
|
const host = url.hostname;
|
||||||
|
const port = url.port ? parseInt(url.port, 10) : protocol === "https" ? 443 : 80;
|
||||||
|
|
||||||
|
return { protocol, host, port };
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user