74 lines
2.2 KiB
TypeScript
74 lines
2.2 KiB
TypeScript
import { z } from "zod"
|
|
const FamilySchema = z.enum(["ip", "ip6"])
|
|
const TableSchema = z.enum(["filter", "nat", "mangle", "raw"])
|
|
const NativeIdSchema = z.string().regex(/^\*[A-Za-z0-9]+$/).max(100)
|
|
|
|
export const RuleKeySchema = z.object({
|
|
serverId: z.string().min(1),
|
|
family: FamilySchema,
|
|
table: TableSchema,
|
|
rosId: NativeIdSchema,
|
|
})
|
|
|
|
export const RuleWriteSchema = z.object({
|
|
serverId: z.string().min(1),
|
|
family: FamilySchema,
|
|
table: TableSchema,
|
|
rosId: NativeIdSchema.optional(),
|
|
chain: z.string().min(1),
|
|
action: z.string().min(1),
|
|
protocol: z.string().optional(),
|
|
srcAddress: z.string().optional(),
|
|
dstAddress: z.string().optional(),
|
|
srcAddressList: z.string().optional(),
|
|
dstAddressList: z.string().optional(),
|
|
srcPort: z.string().optional(),
|
|
dstPort: z.string().optional(),
|
|
inInterface: z.string().optional(),
|
|
outInterface: z.string().optional(),
|
|
connectionState: z.string().optional(),
|
|
comment: z.string().optional(),
|
|
disabled: z.boolean().optional(),
|
|
log: z.boolean().optional(),
|
|
logPrefix: z.string().optional(),
|
|
tlsHost: z.string().optional(),
|
|
layer7Proto: z.string().optional(),
|
|
})
|
|
|
|
export const RulePatchSchema = RuleKeySchema.extend({
|
|
disabled: z.boolean(),
|
|
})
|
|
|
|
|
|
function toRosBody(obj: Record<string, string | undefined>): Record<string, string> {
|
|
const out: Record<string, string> = {}
|
|
for (const [k, v] of Object.entries(obj)) {
|
|
if (v !== undefined && v !== "") out[k] = v
|
|
}
|
|
return out
|
|
}
|
|
|
|
export function ruleToRos(d: z.infer<typeof RuleWriteSchema>): Record<string, string> {
|
|
return toRosBody({
|
|
chain: d.chain,
|
|
action: d.action,
|
|
protocol: d.protocol && d.protocol !== "all" ? d.protocol : undefined,
|
|
"src-address": d.srcAddress,
|
|
"dst-address": d.dstAddress,
|
|
"src-address-list": d.srcAddressList,
|
|
"dst-address-list": d.dstAddressList,
|
|
"src-port": d.srcPort,
|
|
"dst-port": d.dstPort,
|
|
"in-interface": d.inInterface,
|
|
"out-interface": d.outInterface,
|
|
"connection-state": d.connectionState,
|
|
comment: d.comment,
|
|
disabled: d.disabled === true ? "yes" : d.disabled === false ? "no" : undefined,
|
|
log: d.log === true ? "yes" : d.log === false ? "no" : undefined,
|
|
"log-prefix": d.logPrefix,
|
|
"tls-host": d.tlsHost,
|
|
"layer7-protocol": d.layer7Proto,
|
|
})
|
|
}
|
|
|