ddns: allow overriding domains per configuration (#111)

This commit is contained in:
UUBulb
2025-01-30 12:15:21 +08:00
committed by GitHub
parent 0ba41828dd
commit 42b85f74a9
4 changed files with 65 additions and 17 deletions

View File

@@ -49,6 +49,22 @@ const serverFormSchema = z.object({
enable_ddns: asOptionalField(z.boolean()),
ddns_profiles: asOptionalField(z.array(z.number())),
ddns_profiles_raw: asOptionalField(z.string()),
override_ddns_domains: asOptionalField(z.record(z.coerce.number().int(), z.array(z.string()))),
override_ddns_domains_raw: asOptionalField(
z.string().refine(
(val) => {
try {
JSON.parse(val)
return true
} catch (e) {
return false
}
},
{
message: "Invalid JSON string",
},
),
),
})
export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
@@ -58,6 +74,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
defaultValues: {
...data,
ddns_profiles_raw: data.ddns_profiles ? conv.arrToStr(data.ddns_profiles) : undefined,
override_ddns_domains_raw: data.override_ddns_domains
? JSON.stringify(data.override_ddns_domains)
: undefined,
},
resetOptions: {
keepDefaultValues: false,
@@ -71,6 +90,9 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
values.ddns_profiles = values.ddns_profiles_raw
? conv.strToArr(values.ddns_profiles_raw).map(Number)
: undefined
values.override_ddns_domains = values.override_ddns_domains_raw
? JSON.parse(values.override_ddns_domains_raw)
: undefined
await updateServer(data!.id!, values)
} catch (e) {
console.error(e)
@@ -124,21 +146,43 @@ export const ServerCard: React.FC<ServerCardProps> = ({ data, mutate }) => {
</FormItem>
)}
/>
<FormField
control={form.control}
name="ddns_profiles_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("DDNSProfiles") + t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input placeholder="1,2,3" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
{form.watch("enable_ddns") ? (
<>
<FormField
control={form.control}
name="ddns_profiles_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("DDNSProfiles") + t("SeparateWithComma")}
</FormLabel>
<FormControl>
<Input placeholder="1,2,3" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="override_ddns_domains_raw"
render={({ field }) => (
<FormItem>
<FormLabel>
{t("OverrideDDNSDomains")}
</FormLabel>
<FormControl>
<Textarea className="resize-y" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</>
) : (
<></>
)}
<FormField
control={form.control}
name="enable_ddns"

View File

@@ -17,7 +17,7 @@ export function asOptionalField<T extends z.ZodTypeAny>(schema: T) {
}
export const conv = {
recordToStr: (rec: Record<string, boolean>) => {
recordToStr: <T>(rec: Record<string, T>) => {
const arr: string[] = []
for (const key in rec) {
arr.push(key)

View File

@@ -179,5 +179,6 @@
"ConfirmBlock": "Confirm Block",
"RejectPassword": "Reject Password Login",
"EmptyText": "Text is empty",
"EmptyNote": "You didn't have any note."
"EmptyNote": "You didn't have any note.",
"OverrideDDNSDomains": "Override DDNS Domains (per configuration)"
}

View File

@@ -411,6 +411,7 @@ export interface ModelNAT {
export interface ModelNATForm {
domain: string
enabled: boolean
host: string
/** @minLength 1 */
name: string
@@ -560,6 +561,7 @@ export interface ModelServer {
name: string
/** 管理员可见备注 */
note: string
override_ddns_domains?: Record<string, string[]>
/** 公开备注 */
public_note: string
state: ModelHostState
@@ -582,6 +584,7 @@ export interface ModelServerForm {
name: string
/** 管理员可见备注 */
note?: string
override_ddns_domains?: Record<string, string[]>
/** 公开备注 */
public_note?: string
}