@stacksjs/rpx 0.11.0 → 0.11.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -167,7 +167,7 @@ const config: ProxyOptions = {
167
167
  export default config
168
168
  ```
169
169
 
170
- _Then run:_
170
+ Then run:
171
171
 
172
172
  ```bash
173
173
  ./rpx start
package/dist/bin/cli.js CHANGED
@@ -1,4 +1,4 @@
1
- import{a as N,b as d,q as O,t as B,u as U}from"../chunk-szmp12hh.js";import"../chunk-b6654twv.js";import{I as q}from"../chunk-4csm61sj.js";import"../chunk-4960052z.js";import*as g from"node:process";import{EventEmitter as R}from"events";function C(e){return e==null?[]:Array.isArray(e)?e:[e]}function H(e,t,n,s){var i,a=e[t],r=~s.string.indexOf(t)?n==null||n===!0?"":String(n):typeof n==="boolean"?n:~s.boolean.indexOf(t)?n==="false"?!1:n==="true"||(e._.push((i=+n,i*0===0)?i:n),!!n):(i=+n,i*0===0)?i:n;e[t]=a==null?r:Array.isArray(a)?a.concat(r):[a,r]}function G(e,t){e=e||[],t=t||{};var n,s,i,a,r,h={_:[]},o=0,l=0,m=0,c=e.length;let u=t.alias!==void 0,w=t.unknown!==void 0,_=t.default!==void 0;if(t.alias=t.alias||{},t.string=C(t.string),t.boolean=C(t.boolean),u)for(n in t.alias){s=t.alias[n]=C(t.alias[n]);for(o=0;o<s.length;o++)(t.alias[s[o]]=s.concat(n)).splice(o,1)}for(o=t.boolean.length;o-- >0;){s=t.alias[t.boolean[o]]||[];for(l=s.length;l-- >0;)t.boolean.push(s[l])}for(o=t.string.length;o-- >0;){s=t.alias[t.string[o]]||[];for(l=s.length;l-- >0;)t.string.push(s[l])}if(_){for(n in t.default)if(a=typeof t.default[n],s=t.alias[n]=t.alias[n]||[],t[a]!==void 0){t[a].push(n);for(o=0;o<s.length;o++)t[a].push(s[o])}}let D=w?Object.keys(t.alias):[];for(o=0;o<c;o++){if(i=e[o],i==="--"){h._=h._.concat(e.slice(++o));break}for(l=0;l<i.length;l++)if(i.charCodeAt(l)!==45)break;if(l===0)h._.push(i);else if(i.substring(l,l+3)==="no-"){if(a=i.substring(l+3),w&&!~D.indexOf(a))return t.unknown(i);h[a]=!1}else{for(m=l+1;m<i.length;m++)if(i.charCodeAt(m)===61)break;a=i.substring(l,m),r=i.substring(++m)||(o+1===c||(""+e[o+1]).charCodeAt(0)===45||e[++o]),s=l===2?[a]:a;for(m=0;m<s.length;m++){if(a=s[m],w&&!~D.indexOf(a))return t.unknown("-".repeat(l)+a);H(h,a,m+1<s.length||r,t)}}}if(_){for(n in t.default)if(h[n]===void 0)h[n]=t.default[n]}if(u)for(n in h){s=t.alias[n]||[];while(s.length>0)h[s.shift()]=h[n]}return h}var v=(e)=>e.replace(/[<[].+/,"").trim(),L=(e)=>{let t=/<([^>]+)>/g,n=/\[([^\]]+)\]/g,s=[],i=(h)=>{let o=!1,l=h[1];if(l.startsWith("..."))l=l.slice(3),o=!0;return{required:h[0].startsWith("<"),value:l,variadic:o}},a;while(a=t.exec(e))s.push(i(a));let r;while(r=n.exec(e))s.push(i(r));return s},M=(e)=>{let t={alias:{},boolean:[]};for(let[n,s]of e.entries()){if(s.names.length>1)t.alias[s.names[0]]=s.names.slice(1);if(s.isBoolean)if(s.negated){if(!e.some((a,r)=>{return r!==n&&a.names.some((h)=>s.names.includes(h))&&typeof a.required==="boolean"}))t.boolean.push(s.names[0])}else t.boolean.push(s.names[0])}return t},E=(e)=>{return e.sort((t,n)=>{return t.length>n.length?-1:1})[0]},V=(e,t)=>{return e.length>=t?e:`${e}${" ".repeat(t-e.length)}`},S=(e)=>{return e.replace(/([a-z])-([a-z])/g,(t,n,s)=>{return n+s.toUpperCase()})},y=(e,t,n)=>{let s=0,i=t.length,a=e,r;for(;s<i;++s)r=a[t[s]],a=a[t[s]]=s===i-1?n:r!=null?r:!!~t[s+1].indexOf(".")||!(+t[s+1]>-1)?{}:[]},F=(e,t)=>{for(let n of Object.keys(t)){let s=t[n];if(s.shouldTransform){if(e[n]=Array.prototype.concat.call([],e[n]),typeof s.transformFunction==="function")e[n]=e[n].map(s.transformFunction)}}},x=(e)=>{let t=/([^\\\/]+)$/.exec(e);return t?t[1]:""},j=(e)=>{return e.split(".").map((t,n)=>{return n===0?S(t):t}).join(".")};class p extends Error{constructor(e){super(e);if(this.name=this.constructor.name,typeof Error.captureStackTrace==="function")Error.captureStackTrace(this,this.constructor);else this.stack=Error(e).stack}}class T{constructor(e,t,n){if(this.rawName=e,this.description=t,this.config=Object.assign({},n),e=e.replace(/\.\*/g,""),this.negated=!1,this.names=v(e).split(",").map((s)=>{let i=s.trim().replace(/^-{1,2}/,"");if(i.startsWith("no-"))this.negated=!0,i=i.replace(/^no-/,"");return j(i)}).sort((s,i)=>s.length>i.length?1:-1),this.name=this.names[this.names.length-1],this.negated&&this.config.default==null)this.config.default=!0;if(e.includes("<"))this.required=!0;else if(e.includes("["))this.required=!1;else this.isBoolean=!0}}var P=process.argv,W=`${process.platform}-${process.arch} node-${process.version}`;class ${constructor(e,t,n={},s){this.rawName=e,this.description=t,this.config=n,this.cli=s,this.options=[],this.aliasNames=[],this.name=v(e),this.args=L(e),this.examples=[]}usage(e){return this.usageText=e,this}allowUnknownOptions(){return this.config.allowUnknownOptions=!0,this}ignoreOptionDefaultValue(){return this.config.ignoreOptionDefaultValue=!0,this}version(e,t="-v, --version"){return this.versionNumber=e,this.option(t,"Display version number"),this}example(e){return this.examples.push(e),this}option(e,t,n){let s=new T(e,t,n);return this.options.push(s),this}alias(e){return this.aliasNames.push(e),this}action(e){return this.commandAction=e,this}isMatched(e){return this.name===e||this.aliasNames.includes(e)}get isDefaultCommand(){return this.name===""||this.aliasNames.includes("!")}get isGlobalCommand(){return this instanceof k}hasOption(e){return e=e.split(".")[0],this.options.find((t)=>{return t.names.includes(e)})}outputHelp(){let{name:e,commands:t}=this.cli,{versionNumber:n,options:s,helpCallback:i}=this.cli.globalCommand,a=[{body:`${e}${n?`/${n}`:""}`}];if(a.push({title:"Usage",body:` $ ${e} ${this.usageText||this.rawName}`}),(this.isGlobalCommand||this.isDefaultCommand)&&t.length>0){let o=E(t.map((l)=>l.rawName));a.push({title:"Commands",body:t.map((l)=>{return` ${V(l.rawName,o.length)} ${l.description}`}).join(`
1
+ import{b as N,c as d,r as O,u as B,v as U}from"../chunk-8mnzvjyr.js";import"../chunk-3y886wa5.js";import{C as q}from"../chunk-g5db14m7.js";import"../chunk-gbny098p.js";import*as g from"node:process";import{EventEmitter as R}from"events";function C(e){return e==null?[]:Array.isArray(e)?e:[e]}function H(e,t,n,s){var i,a=e[t],r=~s.string.indexOf(t)?n==null||n===!0?"":String(n):typeof n==="boolean"?n:~s.boolean.indexOf(t)?n==="false"?!1:n==="true"||(e._.push((i=+n,i*0===0)?i:n),!!n):(i=+n,i*0===0)?i:n;e[t]=a==null?r:Array.isArray(a)?a.concat(r):[a,r]}function G(e,t){e=e||[],t=t||{};var n,s,i,a,r,h={_:[]},o=0,l=0,m=0,c=e.length;let u=t.alias!==void 0,w=t.unknown!==void 0,_=t.default!==void 0;if(t.alias=t.alias||{},t.string=C(t.string),t.boolean=C(t.boolean),u)for(n in t.alias){s=t.alias[n]=C(t.alias[n]);for(o=0;o<s.length;o++)(t.alias[s[o]]=s.concat(n)).splice(o,1)}for(o=t.boolean.length;o-- >0;){s=t.alias[t.boolean[o]]||[];for(l=s.length;l-- >0;)t.boolean.push(s[l])}for(o=t.string.length;o-- >0;){s=t.alias[t.string[o]]||[];for(l=s.length;l-- >0;)t.string.push(s[l])}if(_){for(n in t.default)if(a=typeof t.default[n],s=t.alias[n]=t.alias[n]||[],t[a]!==void 0){t[a].push(n);for(o=0;o<s.length;o++)t[a].push(s[o])}}let D=w?Object.keys(t.alias):[];for(o=0;o<c;o++){if(i=e[o],i==="--"){h._=h._.concat(e.slice(++o));break}for(l=0;l<i.length;l++)if(i.charCodeAt(l)!==45)break;if(l===0)h._.push(i);else if(i.substring(l,l+3)==="no-"){if(a=i.substring(l+3),w&&!~D.indexOf(a))return t.unknown(i);h[a]=!1}else{for(m=l+1;m<i.length;m++)if(i.charCodeAt(m)===61)break;a=i.substring(l,m),r=i.substring(++m)||(o+1===c||(""+e[o+1]).charCodeAt(0)===45||e[++o]),s=l===2?[a]:a;for(m=0;m<s.length;m++){if(a=s[m],w&&!~D.indexOf(a))return t.unknown("-".repeat(l)+a);H(h,a,m+1<s.length||r,t)}}}if(_){for(n in t.default)if(h[n]===void 0)h[n]=t.default[n]}if(u)for(n in h){s=t.alias[n]||[];while(s.length>0)h[s.shift()]=h[n]}return h}var v=(e)=>e.replace(/[<[].+/,"").trim(),L=(e)=>{let t=/<([^>]+)>/g,n=/\[([^\]]+)\]/g,s=[],i=(h)=>{let o=!1,l=h[1];if(l.startsWith("..."))l=l.slice(3),o=!0;return{required:h[0].startsWith("<"),value:l,variadic:o}},a;while(a=t.exec(e))s.push(i(a));let r;while(r=n.exec(e))s.push(i(r));return s},M=(e)=>{let t={alias:{},boolean:[]};for(let[n,s]of e.entries()){if(s.names.length>1)t.alias[s.names[0]]=s.names.slice(1);if(s.isBoolean)if(s.negated){if(!e.some((a,r)=>{return r!==n&&a.names.some((h)=>s.names.includes(h))&&typeof a.required==="boolean"}))t.boolean.push(s.names[0])}else t.boolean.push(s.names[0])}return t},E=(e)=>{return e.sort((t,n)=>{return t.length>n.length?-1:1})[0]},V=(e,t)=>{return e.length>=t?e:`${e}${" ".repeat(t-e.length)}`},S=(e)=>{return e.replace(/([a-z])-([a-z])/g,(t,n,s)=>{return n+s.toUpperCase()})},y=(e,t,n)=>{let s=0,i=t.length,a=e,r;for(;s<i;++s)r=a[t[s]],a=a[t[s]]=s===i-1?n:r!=null?r:!!~t[s+1].indexOf(".")||!(+t[s+1]>-1)?{}:[]},F=(e,t)=>{for(let n of Object.keys(t)){let s=t[n];if(s.shouldTransform){if(e[n]=Array.prototype.concat.call([],e[n]),typeof s.transformFunction==="function")e[n]=e[n].map(s.transformFunction)}}},x=(e)=>{let t=/([^\\\/]+)$/.exec(e);return t?t[1]:""},j=(e)=>{return e.split(".").map((t,n)=>{return n===0?S(t):t}).join(".")};class p extends Error{constructor(e){super(e);if(this.name=this.constructor.name,typeof Error.captureStackTrace==="function")Error.captureStackTrace(this,this.constructor);else this.stack=Error(e).stack}}class T{constructor(e,t,n){if(this.rawName=e,this.description=t,this.config=Object.assign({},n),e=e.replace(/\.\*/g,""),this.negated=!1,this.names=v(e).split(",").map((s)=>{let i=s.trim().replace(/^-{1,2}/,"");if(i.startsWith("no-"))this.negated=!0,i=i.replace(/^no-/,"");return j(i)}).sort((s,i)=>s.length>i.length?1:-1),this.name=this.names[this.names.length-1],this.negated&&this.config.default==null)this.config.default=!0;if(e.includes("<"))this.required=!0;else if(e.includes("["))this.required=!1;else this.isBoolean=!0}}var P=process.argv,W=`${process.platform}-${process.arch} node-${process.version}`;class ${constructor(e,t,n={},s){this.rawName=e,this.description=t,this.config=n,this.cli=s,this.options=[],this.aliasNames=[],this.name=v(e),this.args=L(e),this.examples=[]}usage(e){return this.usageText=e,this}allowUnknownOptions(){return this.config.allowUnknownOptions=!0,this}ignoreOptionDefaultValue(){return this.config.ignoreOptionDefaultValue=!0,this}version(e,t="-v, --version"){return this.versionNumber=e,this.option(t,"Display version number"),this}example(e){return this.examples.push(e),this}option(e,t,n){let s=new T(e,t,n);return this.options.push(s),this}alias(e){return this.aliasNames.push(e),this}action(e){return this.commandAction=e,this}isMatched(e){return this.name===e||this.aliasNames.includes(e)}get isDefaultCommand(){return this.name===""||this.aliasNames.includes("!")}get isGlobalCommand(){return this instanceof k}hasOption(e){return e=e.split(".")[0],this.options.find((t)=>{return t.names.includes(e)})}outputHelp(){let{name:e,commands:t}=this.cli,{versionNumber:n,options:s,helpCallback:i}=this.cli.globalCommand,a=[{body:`${e}${n?`/${n}`:""}`}];if(a.push({title:"Usage",body:` $ ${e} ${this.usageText||this.rawName}`}),(this.isGlobalCommand||this.isDefaultCommand)&&t.length>0){let o=E(t.map((l)=>l.rawName));a.push({title:"Commands",body:t.map((l)=>{return` ${V(l.rawName,o.length)} ${l.description}`}).join(`
2
2
  `)}),a.push({title:"For more info, run any command with the `--help` flag",body:t.map((l)=>` $ ${e}${l.name===""?"":` ${l.name}`} --help`).join(`
3
3
  `)})}let h=this.isGlobalCommand?s:[...this.options,...s||[]];if(!this.isGlobalCommand&&!this.isDefaultCommand)h=h.filter((o)=>o.name!=="version");if(h.length>0){let o=E(h.map((l)=>l.rawName));a.push({title:"Options",body:h.map((l)=>{return` ${V(l.rawName,o.length)} ${l.description} ${l.config.default===void 0?"":`(default: ${l.config.default})`}`}).join(`
4
4
  `)})}if(this.examples.length>0)a.push({title:"Examples",body:this.examples.map((o)=>{if(typeof o==="function")return o(e);return o}).join(`
@@ -0,0 +1 @@
1
+ import{createRequire as r}from"node:module";var e=r(import.meta.url);
@@ -1 +1 @@
1
- import{E as F}from"./chunk-4csm61sj.js";import{P as X}from"./chunk-4960052z.js";import Q from"node:dgram";var $=15353;function _(k){return{id:k.readUInt16BE(0),flags:k.readUInt16BE(2),qdcount:k.readUInt16BE(4),ancount:k.readUInt16BE(6),nscount:k.readUInt16BE(8),arcount:k.readUInt16BE(10)}}function C(k,z){let A=[],j=z;while(!0){let B=k[j];if(B===0){j++;break}if((B&192)===192){let G=k.readUInt16BE(j)&16383,{name:E}=C(k,G);A.push(E),j+=2;break}j++,A.push(k.subarray(j,j+B).toString("ascii")),j+=B}return{name:A.join("."),newOffset:j}}function H(k,z){let{name:A,newOffset:j}=C(k,z),B=k.readUInt16BE(j),G=k.readUInt16BE(j+2);return{question:{name:A,type:B,class:G},newOffset:j+4}}function U(k){let z=k.split("."),A=[];for(let j of z)A.push(Buffer.from([j.length])),A.push(Buffer.from(j,"ascii"));return A.push(Buffer.from([0])),Buffer.concat(A)}function T(k,z,A){let j=[],B=Buffer.alloc(12);B.writeUInt16BE(k,0),B.writeUInt16BE(33152,2),B.writeUInt16BE(1,4),B.writeUInt16BE(1,6),B.writeUInt16BE(0,8),B.writeUInt16BE(0,10),j.push(B),j.push(U(z.name));let G=Buffer.alloc(4);G.writeUInt16BE(z.type,0),G.writeUInt16BE(z.class,2),j.push(G),j.push(U(z.name));let E=Buffer.alloc(10);if(E.writeUInt16BE(z.type,0),E.writeUInt16BE(1,2),E.writeUInt32BE(300,4),z.type===1){E.writeUInt16BE(4,8),j.push(E);let K=A.split(".").map((M)=>Number.parseInt(M,10));j.push(Buffer.from(K))}else if(z.type===28)E.writeUInt16BE(16,8),j.push(E),j.push(Buffer.from([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]));else return B.writeUInt16BE(33155,2),B.writeUInt16BE(0,6),Buffer.concat([B,U(z.name),G]);return Buffer.concat(j)}function I(k,z){let A=[],j=Buffer.alloc(12);j.writeUInt16BE(k,0),j.writeUInt16BE(33155,2),j.writeUInt16BE(1,4),j.writeUInt16BE(0,6),j.writeUInt16BE(0,8),j.writeUInt16BE(0,10),A.push(j),A.push(U(z.name));let B=Buffer.alloc(4);return B.writeUInt16BE(z.type,0),B.writeUInt16BE(z.class,2),A.push(B),Buffer.concat(A)}var J=null,Z=new Set;async function R(k,z){if(J)return F("dns","DNS server already running",z),!0;return Z=new Set(k.map((A)=>A.toLowerCase())),new Promise((A)=>{J=Q.createSocket("udp4"),J.on("error",(j)=>{if(F("dns",`DNS server error: ${j.message}`,z),j.message.includes("EACCES")||j.message.includes("permission"))F("dns","DNS server requires root privileges to bind to port 53",z);J?.close(),J=null,A(!1)}),J.on("message",(j,B)=>{try{let G=_(j),{question:E}=H(j,12);F("dns",`Query for ${E.name} type ${E.type} from ${B.address}`,z);let K=E.name.toLowerCase(),M=!1;for(let Y of Z)if(K===Y||K.endsWith(`.${Y}`)){M=!0;break}let W;if(M&&(E.type===1||E.type===28))W=T(G.id,E,"127.0.0.1"),F("dns",`Responding with localhost for ${E.name}`,z);else W=I(G.id,E),F("dns",`NXDOMAIN for ${E.name}`,z);J?.send(W,B.port,B.address)}catch(G){F("dns",`Error processing DNS query: ${G}`,z)}}),J.on("listening",()=>{let j=J?.address();F("dns",`DNS server listening on ${j?.address}:${j?.port}`,z),A(!0)});try{J.bind($,"127.0.0.1")}catch(j){F("dns",`Failed to bind DNS server: ${j}`,z),A(!1)}})}function O(k){if(J)F("dns","Stopping DNS server",k),J.close(),J=null}function w(){return J!==null}function x(k){let z=new Set;for(let A of k){let j=A.split(".");if(j.length>=2)z.add(j[j.length-1])}return Array.from(z)}var V=new Set;async function D(k){if(process.platform!=="darwin")return;let{execSudoSync:z,getSudoPassword:A}=await import("./chunk-q7w0fwv3.js");if(!A()){F("dns","Cannot flush DNS cache without SUDO_PASSWORD",k);return}try{z("dscacheutil -flushcache"),z("killall -HUP mDNSResponder 2>/dev/null || true"),F("dns","DNS cache flushed",k)}catch(B){F("dns",`Could not flush DNS cache: ${B}`,k)}}async function y(k,z){if(process.platform!=="darwin")return F("dns","Resolver setup only needed on macOS",k),!0;let{execSudoSync:A,getSudoPassword:j}=await import("./chunk-q7w0fwv3.js");if(!j())return F("dns","SUDO_PASSWORD not set, cannot create resolver files",k),!1;let G=z?x(z):["test"];try{for(let E of G){if(V.has(E))continue;let K=`bash -c 'mkdir -p /etc/resolver && echo -e "nameserver 127.0.0.1\\nport ${$}" > /etc/resolver/${E}'`;A(K),V.add(E),F("dns",`Created /etc/resolver/${E} for .${E} TLD`,k)}return await D(k),!0}catch(E){return F("dns",`Failed to create resolver file: ${E}`,k),!1}}async function L(k){if(process.platform!=="darwin")return;let{execSudoSync:z,getSudoPassword:A}=await import("./chunk-q7w0fwv3.js");try{if(A()){for(let B of V)z(`rm -f /etc/resolver/${B}`),F("dns",`Removed /etc/resolver/${B}`,k);V.clear()}}catch(j){F("dns",`Failed to remove resolver files: ${j}`,k)}}export{O as stopDnsServer,R as startDnsServer,y as setupResolver,L as removeResolver,w as isDnsServerRunning};
1
+ import{y as F}from"./chunk-g5db14m7.js";import{H as X}from"./chunk-gbny098p.js";import Q from"node:dgram";var $=15353;function _(k){return{id:k.readUInt16BE(0),flags:k.readUInt16BE(2),qdcount:k.readUInt16BE(4),ancount:k.readUInt16BE(6),nscount:k.readUInt16BE(8),arcount:k.readUInt16BE(10)}}function C(k,z){let A=[],j=z;while(!0){let B=k[j];if(B===0){j++;break}if((B&192)===192){let G=k.readUInt16BE(j)&16383,{name:E}=C(k,G);A.push(E),j+=2;break}j++,A.push(k.subarray(j,j+B).toString("ascii")),j+=B}return{name:A.join("."),newOffset:j}}function H(k,z){let{name:A,newOffset:j}=C(k,z),B=k.readUInt16BE(j),G=k.readUInt16BE(j+2);return{question:{name:A,type:B,class:G},newOffset:j+4}}function U(k){let z=k.split("."),A=[];for(let j of z)A.push(Buffer.from([j.length])),A.push(Buffer.from(j,"ascii"));return A.push(Buffer.from([0])),Buffer.concat(A)}function T(k,z,A){let j=[],B=Buffer.alloc(12);B.writeUInt16BE(k,0),B.writeUInt16BE(33152,2),B.writeUInt16BE(1,4),B.writeUInt16BE(1,6),B.writeUInt16BE(0,8),B.writeUInt16BE(0,10),j.push(B),j.push(U(z.name));let G=Buffer.alloc(4);G.writeUInt16BE(z.type,0),G.writeUInt16BE(z.class,2),j.push(G),j.push(U(z.name));let E=Buffer.alloc(10);if(E.writeUInt16BE(z.type,0),E.writeUInt16BE(1,2),E.writeUInt32BE(300,4),z.type===1){E.writeUInt16BE(4,8),j.push(E);let K=A.split(".").map((M)=>Number.parseInt(M,10));j.push(Buffer.from(K))}else if(z.type===28)E.writeUInt16BE(16,8),j.push(E),j.push(Buffer.from([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]));else return B.writeUInt16BE(33155,2),B.writeUInt16BE(0,6),Buffer.concat([B,U(z.name),G]);return Buffer.concat(j)}function I(k,z){let A=[],j=Buffer.alloc(12);j.writeUInt16BE(k,0),j.writeUInt16BE(33155,2),j.writeUInt16BE(1,4),j.writeUInt16BE(0,6),j.writeUInt16BE(0,8),j.writeUInt16BE(0,10),A.push(j),A.push(U(z.name));let B=Buffer.alloc(4);return B.writeUInt16BE(z.type,0),B.writeUInt16BE(z.class,2),A.push(B),Buffer.concat(A)}var J=null,Z=new Set;async function R(k,z){if(J)return F("dns","DNS server already running",z),!0;return Z=new Set(k.map((A)=>A.toLowerCase())),new Promise((A)=>{J=Q.createSocket("udp4"),J.on("error",(j)=>{if(F("dns",`DNS server error: ${j.message}`,z),j.message.includes("EACCES")||j.message.includes("permission"))F("dns","DNS server requires root privileges to bind to port 53",z);J?.close(),J=null,A(!1)}),J.on("message",(j,B)=>{try{let G=_(j),{question:E}=H(j,12);F("dns",`Query for ${E.name} type ${E.type} from ${B.address}`,z);let K=E.name.toLowerCase(),M=!1;for(let Y of Z)if(K===Y||K.endsWith(`.${Y}`)){M=!0;break}let W;if(M&&(E.type===1||E.type===28))W=T(G.id,E,"127.0.0.1"),F("dns",`Responding with localhost for ${E.name}`,z);else W=I(G.id,E),F("dns",`NXDOMAIN for ${E.name}`,z);J?.send(W,B.port,B.address)}catch(G){F("dns",`Error processing DNS query: ${G}`,z)}}),J.on("listening",()=>{let j=J?.address();F("dns",`DNS server listening on ${j?.address}:${j?.port}`,z),A(!0)});try{J.bind($,"127.0.0.1")}catch(j){F("dns",`Failed to bind DNS server: ${j}`,z),A(!1)}})}function O(k){if(J)F("dns","Stopping DNS server",k),J.close(),J=null}function w(){return J!==null}function x(k){let z=new Set;for(let A of k){let j=A.split(".");if(j.length>=2)z.add(j[j.length-1])}return Array.from(z)}var V=new Set;async function D(k){if(process.platform!=="darwin")return;let{execSudoSync:z,getSudoPassword:A}=await import("./chunk-94pvxvt5.js");if(!A()){F("dns","Cannot flush DNS cache without SUDO_PASSWORD",k);return}try{z("dscacheutil -flushcache"),z("killall -HUP mDNSResponder 2>/dev/null || true"),F("dns","DNS cache flushed",k)}catch(B){F("dns",`Could not flush DNS cache: ${B}`,k)}}async function y(k,z){if(process.platform!=="darwin")return F("dns","Resolver setup only needed on macOS",k),!0;let{execSudoSync:A,getSudoPassword:j}=await import("./chunk-94pvxvt5.js");if(!j())return F("dns","SUDO_PASSWORD not set, cannot create resolver files",k),!1;let G=z?x(z):["test"];try{for(let E of G){if(V.has(E))continue;let K=`bash -c 'mkdir -p /etc/resolver && echo -e "nameserver 127.0.0.1\\nport ${$}" > /etc/resolver/${E}'`;A(K),V.add(E),F("dns",`Created /etc/resolver/${E} for .${E} TLD`,k)}return await D(k),!0}catch(E){return F("dns",`Failed to create resolver file: ${E}`,k),!1}}async function L(k){if(process.platform!=="darwin")return;let{execSudoSync:z,getSudoPassword:A}=await import("./chunk-94pvxvt5.js");try{if(A()){for(let B of V)z(`rm -f /etc/resolver/${B}`),F("dns",`Removed /etc/resolver/${B}`,k);V.clear()}}catch(j){F("dns",`Failed to remove resolver files: ${j}`,k)}}export{O as stopDnsServer,R as startDnsServer,y as setupResolver,L as removeResolver,w as isDnsServerRunning};