Skip to content

Commit 94cd503

Browse files
committed
fix(dns): extract and normalize prio for porkbun MX/SRV records to prevent duplicates
1 parent ea1c6fc commit 94cd503

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

‎packages/server/src/utils/dns/porkbun.ts‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@ interface PorkbunRecord {
5656
notes: string;
5757
}
5858

59+
const inlinePriority = (record: {
60+
type: string;
61+
content: string;
62+
prio?: string | null;
63+
}) =>
64+
(record.type === "MX" || record.type === "SRV") && record.prio && record.prio !== "0"
65+
? `${record.prio} ${record.content}`
66+
: record.content;
67+
68+
const buildValue = (record: { type: string; content: string }) => {
69+
const value = record.content.trim();
70+
if (record.type === "MX" || record.type === "SRV") {
71+
const match = /^(\d+)\s+(\S.*)$/.exec(value);
72+
if (match) {
73+
return { content: match[2] as string, prio: match[1] as string };
74+
}
75+
}
76+
return { content: value };
77+
};
78+
5979
export const porkbunClient: DnsClient<PorkbunConfig> = {
6080
async listZones(config) {
6181
const result = await pbFetch<{ domains: { domain: string }[] }>(
@@ -77,7 +97,7 @@ export const porkbunClient: DnsClient<PorkbunConfig> = {
7797
id: record.id,
7898
type: record.type,
7999
name: record.name,
80-
content: record.content,
100+
content: inlinePriority(record),
81101
ttl: Number(record.ttl),
82102
}));
83103
},
@@ -89,15 +109,23 @@ export const porkbunClient: DnsClient<PorkbunConfig> = {
89109
`/dns/retrieveByNameType/${record.zoneId}/${record.type}/${subdomain}`,
90110
);
91111

112+
const built = buildValue(record);
92113
const payload = {
93114
name: subdomain,
94115
type: record.type,
95-
content: record.content,
116+
content: built.content,
117+
...(built.prio ? { prio: built.prio } : {}),
96118
ttl: record.ttl ?? 600,
97119
};
98120

121+
const expectedContent = inlinePriority({
122+
type: record.type,
123+
content: built.content,
124+
prio: built.prio,
125+
});
126+
99127
const existingRecord = existing.records.find(
100-
(r) => r.content === record.content,
128+
(r) => inlinePriority(r) === expectedContent,
101129
);
102130
if (existingRecord) {
103131
await pbFetch(
@@ -117,10 +145,12 @@ export const porkbunClient: DnsClient<PorkbunConfig> = {
117145
},
118146

119147
async updateRecord(config, zoneId, recordId, record) {
148+
const built = buildValue(record);
120149
await pbFetch(config, `/dns/edit/${zoneId}/${recordId}`, {
121150
name: toSubdomain(record.name, zoneId),
122151
type: record.type,
123-
content: record.content,
152+
content: built.content,
153+
...(built.prio ? { prio: built.prio } : {}),
124154
ttl: record.ttl ?? 600,
125155
});
126156
return { id: recordId };

0 commit comments

Comments
 (0)