mirror of
https://github.com/fosrl/pangolin.git
synced 2025-08-18 08:18:43 +02:00
Complex regex for domains/ips
This commit is contained in:
parent
fb754bc4e0
commit
dc7bd41eb9
3 changed files with 87 additions and 3 deletions
|
@ -12,6 +12,34 @@ import { isIpInCidr } from "@server/lib/ip";
|
||||||
import { fromError } from "zod-validation-error";
|
import { fromError } from "zod-validation-error";
|
||||||
import { addTargets } from "../newt/targets";
|
import { addTargets } from "../newt/targets";
|
||||||
|
|
||||||
|
// Regular expressions for validation
|
||||||
|
const DOMAIN_REGEX =
|
||||||
|
/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
||||||
|
const IPV4_REGEX =
|
||||||
|
/^(?:(?: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]?)$/;
|
||||||
|
const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
||||||
|
|
||||||
|
// Schema for domain names and IP addresses
|
||||||
|
const domainSchema = z
|
||||||
|
.string()
|
||||||
|
.min(1, "Domain cannot be empty")
|
||||||
|
.max(255, "Domain name too long")
|
||||||
|
.refine(
|
||||||
|
(value) => {
|
||||||
|
// Check if it's a valid IP address (v4 or v6)
|
||||||
|
if (IPV4_REGEX.test(value) || IPV6_REGEX.test(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's a valid domain name
|
||||||
|
return DOMAIN_REGEX.test(value);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Invalid domain name or IP address format",
|
||||||
|
path: ["domain"]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const createTargetParamsSchema = z
|
const createTargetParamsSchema = z
|
||||||
.object({
|
.object({
|
||||||
resourceId: z
|
resourceId: z
|
||||||
|
@ -23,7 +51,7 @@ const createTargetParamsSchema = z
|
||||||
|
|
||||||
const createTargetSchema = z
|
const createTargetSchema = z
|
||||||
.object({
|
.object({
|
||||||
ip: z.string(),
|
ip: domainSchema,
|
||||||
method: z.string().min(1).max(10),
|
method: z.string().min(1).max(10),
|
||||||
port: z.number().int().min(1).max(65535),
|
port: z.number().int().min(1).max(65535),
|
||||||
protocol: z.string().optional(),
|
protocol: z.string().optional(),
|
||||||
|
|
|
@ -11,6 +11,34 @@ import { fromError } from "zod-validation-error";
|
||||||
import { addPeer } from "../gerbil/peers";
|
import { addPeer } from "../gerbil/peers";
|
||||||
import { addTargets } from "../newt/targets";
|
import { addTargets } from "../newt/targets";
|
||||||
|
|
||||||
|
// Regular expressions for validation
|
||||||
|
const DOMAIN_REGEX =
|
||||||
|
/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
||||||
|
const IPV4_REGEX =
|
||||||
|
/^(?:(?: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]?)$/;
|
||||||
|
const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
||||||
|
|
||||||
|
// Schema for domain names and IP addresses
|
||||||
|
const domainSchema = z
|
||||||
|
.string()
|
||||||
|
.min(1, "Domain cannot be empty")
|
||||||
|
.max(255, "Domain name too long")
|
||||||
|
.refine(
|
||||||
|
(value) => {
|
||||||
|
// Check if it's a valid IP address (v4 or v6)
|
||||||
|
if (IPV4_REGEX.test(value) || IPV6_REGEX.test(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's a valid domain name
|
||||||
|
return DOMAIN_REGEX.test(value);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Invalid domain name or IP address format",
|
||||||
|
path: ["domain"]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const updateTargetParamsSchema = z
|
const updateTargetParamsSchema = z
|
||||||
.object({
|
.object({
|
||||||
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
targetId: z.string().transform(Number).pipe(z.number().int().positive())
|
||||||
|
@ -19,7 +47,7 @@ const updateTargetParamsSchema = z
|
||||||
|
|
||||||
const updateTargetBodySchema = z
|
const updateTargetBodySchema = z
|
||||||
.object({
|
.object({
|
||||||
ip: z.string().optional(),
|
ip: domainSchema.optional(),
|
||||||
method: z.string().min(1).max(10).optional(),
|
method: z.string().min(1).max(10).optional(),
|
||||||
port: z.number().int().min(1).max(65535).optional(),
|
port: z.number().int().min(1).max(65535).optional(),
|
||||||
enabled: z.boolean().optional()
|
enabled: z.boolean().optional()
|
||||||
|
|
|
@ -63,8 +63,36 @@ import {
|
||||||
} from "@app/components/Settings";
|
} from "@app/components/Settings";
|
||||||
import { SwitchInput } from "@app/components/SwitchInput";
|
import { SwitchInput } from "@app/components/SwitchInput";
|
||||||
|
|
||||||
|
// Regular expressions for validation
|
||||||
|
const DOMAIN_REGEX =
|
||||||
|
/^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
||||||
|
const IPV4_REGEX =
|
||||||
|
/^(?:(?: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]?)$/;
|
||||||
|
const IPV6_REGEX = /^(?:[A-F0-9]{1,4}:){7}[A-F0-9]{1,4}$/i;
|
||||||
|
|
||||||
|
// Schema for domain names and IP addresses
|
||||||
|
const domainSchema = z
|
||||||
|
.string()
|
||||||
|
.min(1, "Domain cannot be empty")
|
||||||
|
.max(255, "Domain name too long")
|
||||||
|
.refine(
|
||||||
|
(value) => {
|
||||||
|
// Check if it's a valid IP address (v4 or v6)
|
||||||
|
if (IPV4_REGEX.test(value) || IPV6_REGEX.test(value)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it's a valid domain name
|
||||||
|
return DOMAIN_REGEX.test(value);
|
||||||
|
},
|
||||||
|
{
|
||||||
|
message: "Invalid domain name or IP address format",
|
||||||
|
path: ["domain"]
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const addTargetSchema = z.object({
|
const addTargetSchema = z.object({
|
||||||
ip: z.string(),
|
ip: domainSchema,
|
||||||
method: z.string(),
|
method: z.string(),
|
||||||
port: z.coerce.number().int().positive()
|
port: z.coerce.number().int().positive()
|
||||||
// protocol: z.string(),
|
// protocol: z.string(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue