import{s as e,t}from"./utils-7Gdj77QB.js";import n from"node:os";import r from"node:path";import i from"node:fs";function a(e){let t=e?.trim();return t?t.endsWith(`.`)?t:`${t}.`:null}function o(e){let t=e?.env??process.env;return a(e?.configDomain??t.OPENCLAW_WIDE_AREA_DOMAIN??null)}function s(e){return`${e.replace(/\.$/,``)}.db`}function c(e){return r.join(t,`dns`,s(e))}function l(e,t){let n=e.trim().toLowerCase().replace(/[^a-z0-9-]+/g,`-`).replace(/^-+/,``).replace(/-+$/,``),r=n.length>0?n:t;return r.length<=63?r:r.slice(0,63)}function u(e){return`"${e.replaceAll(`\\`,`\\\\`).replaceAll(`"`,`\\"`).replaceAll(`
`,`\\n`)}"`}function d(e){return`${e.getUTCFullYear()}${String(e.getUTCMonth()+1).padStart(2,`0`)}${String(e.getUTCDate()).padStart(2,`0`)}`}function f(e,t){let n=d(t),r=Number.parseInt(`${n}01`,10);return!e||!Number.isFinite(e)?r:String(e).startsWith(n)?e+1:r}function p(e){let t=e.match(/^\s*@\s+IN\s+SOA\s+\S+\s+\S+\s+(\d+)\s+/m);if(!t)return null;let n=Number.parseInt(t[1],10);return Number.isFinite(n)?n:null}function m(e){return e.match(/^\s*;\s*openclaw-content-hash:\s*(\S+)\s*$/m)?.[1]??null}function h(e){let t=2166136261;for(let n=0;n<e.length;n++)t^=e.charCodeAt(n),t=Math.imul(t,16777619);return(t>>>0).toString(16).padStart(8,`0`)}function g(e){let t=n.hostname().split(`.`)[0]??`openclaw`,r=l(e.hostLabel??t,`openclaw`),i=l(e.instanceLabel??`${t}-gateway`,`openclaw-gw`),o=a(e.domain)??`local.`,s=[`displayName=${e.displayName.trim()||t}`,`role=gateway`,`transport=gateway`,`gatewayPort=${e.gatewayPort}`];e.gatewayTlsEnabled&&(s.push(`gatewayTls=1`),e.gatewayTlsFingerprintSha256&&s.push(`gatewayTlsSha256=${e.gatewayTlsFingerprintSha256}`)),e.tailnetDns?.trim()&&s.push(`tailnetDns=${e.tailnetDns.trim()}`),typeof e.sshPort==`number`&&e.sshPort>0&&s.push(`sshPort=${e.sshPort}`),e.cliPath?.trim()&&s.push(`cliPath=${e.cliPath.trim()}`);let c=[];c.push(`$ORIGIN ${o}`),c.push(`$TTL 60`);let d=`@ IN SOA ns1 hostmaster ${e.serial} 7200 3600 1209600 60`;c.push(d),c.push(`@ IN NS ns1`),c.push(`ns1 IN A ${e.tailnetIPv4}`),c.push(`${r} IN A ${e.tailnetIPv4}`),e.tailnetIPv6&&c.push(`${r} IN AAAA ${e.tailnetIPv6}`),c.push(`_openclaw-gw._tcp IN PTR ${i}._openclaw-gw._tcp`),c.push(`${i}._openclaw-gw._tcp IN SRV 0 0 ${e.gatewayPort} ${r}`),c.push(`${i}._openclaw-gw._tcp IN TXT ${s.map(u).join(` `)}`);let f=`${c.join(`
`)}\n`;return`; openclaw-content-hash: ${h(`${c.map(e=>e===d?`@ IN SOA ns1 hostmaster SERIAL 7200 3600 1209600 60`:e).join(`
`)}\n`)}\n${f}`}function _(e){return g(e)}async function v(t){let n=a(t.domain);if(!n)throw Error(`wide-area discovery domain is required`);let o=c(n);await e(r.dirname(o));let s=(()=>{try{return i.readFileSync(o,`utf-8`)}catch{return null}})(),l=m(_({...t,serial:0})),u=s?m(s):null;if(s&&l&&u===l)return{zonePath:o,changed:!1};let d=f(s?p(s):null,new Date),h=_({...t,serial:d});return i.writeFileSync(o,h,`utf-8`),{zonePath:o,changed:!0}}export{o as n,v as r,c as t};
