@stacksjs/dnsx
Version:
A command-line & library DNS client. Like dig & dog, but for TypeScript.
13 lines (11 loc) • 12.7 kB
JavaScript
import{a as K,g as M,p as q,q as Q,r as B}from"../chunk-e3sexww2.js";import g from"node:process";import{EventEmitter as T}from"events";function O(e){return e==null?[]:Array.isArray(e)?e:[e]}function U(e,n,i,t){var s,a=e[n],r=~t.string.indexOf(n)?i==null||i===!0?"":String(i):typeof i==="boolean"?i:~t.boolean.indexOf(n)?i==="false"?!1:i==="true"||(e._.push((s=+i,s*0===0)?s:i),!!i):(s=+i,s*0===0)?s:i;e[n]=a==null?r:Array.isArray(a)?a.concat(r):[a,r]}function G(e,n){e=e||[],n=n||{};var i,t,s,a,r,u={_:[]},o=0,l=0,h=0,c=e.length;let m=n.alias!==void 0,w=n.unknown!==void 0,$=n.default!==void 0;if(n.alias=n.alias||{},n.string=O(n.string),n.boolean=O(n.boolean),m)for(i in n.alias){t=n.alias[i]=O(n.alias[i]);for(o=0;o<t.length;o++)(n.alias[t[o]]=t.concat(i)).splice(o,1)}for(o=n.boolean.length;o-- >0;){t=n.alias[n.boolean[o]]||[];for(l=t.length;l-- >0;)n.boolean.push(t[l])}for(o=n.string.length;o-- >0;){t=n.alias[n.string[o]]||[];for(l=t.length;l-- >0;)n.string.push(t[l])}if($){for(i in n.default)if(a=typeof n.default[i],t=n.alias[i]=n.alias[i]||[],n[a]!==void 0){n[a].push(i);for(o=0;o<t.length;o++)n[a].push(t[o])}}let v=w?Object.keys(n.alias):[];for(o=0;o<c;o++){if(s=e[o],s==="--"){u._=u._.concat(e.slice(++o));break}for(l=0;l<s.length;l++)if(s.charCodeAt(l)!==45)break;if(l===0)u._.push(s);else if(s.substring(l,l+3)==="no-"){if(a=s.substring(l+3),w&&!~v.indexOf(a))return n.unknown(s);u[a]=!1}else{for(h=l+1;h<s.length;h++)if(s.charCodeAt(h)===61)break;a=s.substring(l,h),r=s.substring(++h)||(o+1===c||(""+e[o+1]).charCodeAt(0)===45||e[++o]),t=l===2?[a]:a;for(h=0;h<t.length;h++){if(a=t[h],w&&!~v.indexOf(a))return n.unknown("-".repeat(l)+a);U(u,a,h+1<t.length||r,n)}}}if($){for(i in n.default)if(u[i]===void 0)u[i]=n.default[i]}if(m)for(i in u){t=n.alias[i]||[];while(t.length>0)u[t.shift()]=u[i]}return u}var N=(e)=>e.replace(/[<[].+/,"").trim(),I=(e)=>{let n=/<([^>]+)>/g,i=/\[([^\]]+)\]/g,t=[],s=(u)=>{let o=!1,l=u[1];if(l.startsWith("..."))l=l.slice(3),o=!0;return{required:u[0].startsWith("<"),value:l,variadic:o}},a;while(a=n.exec(e))t.push(s(a));let r;while(r=i.exec(e))t.push(s(r));return t},L=(e)=>{let n={alias:{},boolean:[]};for(let[i,t]of e.entries()){if(t.names.length>1)n.alias[t.names[0]]=t.names.slice(1);if(t.isBoolean)if(t.negated){if(!e.some((a,r)=>{return r!==i&&a.names.some((u)=>t.names.includes(u))&&typeof a.required==="boolean"}))n.boolean.push(t.names[0])}else n.boolean.push(t.names[0])}return n},A=(e)=>{return e.sort((n,i)=>{return n.length>i.length?-1:1})[0]},j=(e,n)=>{return e.length>=n?e:`${e}${" ".repeat(n-e.length)}`},R=(e)=>{return e.replace(/([a-z])-([a-z])/g,(n,i,t)=>{return i+t.toUpperCase()})},S=(e,n,i)=>{let t=0,s=n.length,a=e,r;for(;t<s;++t)r=a[n[t]],a=a[n[t]]=t===s-1?i:r!=null?r:!!~n[t+1].indexOf(".")||!(+n[t+1]>-1)?{}:[]},H=(e,n)=>{for(let i of Object.keys(n)){let t=n[i];if(t.shouldTransform){if(e[i]=Array.prototype.concat.call([],e[i]),typeof t.transformFunction==="function")e[i]=e[i].map(t.transformFunction)}}},F=(e)=>{let n=/([^\\\/]+)$/.exec(e);return n?n[1]:""},E=(e)=>{return e.split(".").map((n,i)=>{return i===0?R(n):n}).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=new Error(e).stack}}class D{constructor(e,n,i){if(this.rawName=e,this.description=n,this.config=Object.assign({},i),e=e.replace(/\.\*/g,""),this.negated=!1,this.names=N(e).split(",").map((t)=>{let s=t.trim().replace(/^-{1,2}/,"");if(s.startsWith("no-"))this.negated=!0,s=s.replace(/^no-/,"");return E(s)}).sort((t,s)=>t.length>s.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 y{constructor(e,n,i={},t){this.rawName=e,this.description=n,this.config=i,this.cli=t,this.options=[],this.aliasNames=[],this.name=N(e),this.args=I(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,n="-v, --version"){return this.versionNumber=e,this.option(n,"Display version number"),this}example(e){return this.examples.push(e),this}option(e,n,i){let t=new D(e,n,i);return this.options.push(t),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((n)=>{return n.names.includes(e)})}outputHelp(){let{name:e,commands:n}=this.cli,{versionNumber:i,options:t,helpCallback:s}=this.cli.globalCommand,a=[{body:`${e}${i?`/${i}`:""}`}];if(a.push({title:"Usage",body:` $ ${e} ${this.usageText||this.rawName}`}),(this.isGlobalCommand||this.isDefaultCommand)&&n.length>0){let o=A(n.map((l)=>l.rawName));a.push({title:"Commands",body:n.map((l)=>{return` ${j(l.rawName,o.length)} ${l.description}`}).join(`
`)}),a.push({title:"For more info, run any command with the `--help` flag",body:n.map((l)=>` $ ${e}${l.name===""?"":` ${l.name}`} --help`).join(`
`)})}let u=this.isGlobalCommand?t:[...this.options,...t||[]];if(!this.isGlobalCommand&&!this.isDefaultCommand)u=u.filter((o)=>o.name!=="version");if(u.length>0){let o=A(u.map((l)=>l.rawName));a.push({title:"Options",body:u.map((l)=>{return` ${j(l.rawName,o.length)} ${l.description} ${l.config.default===void 0?"":`(default: ${l.config.default})`}`}).join(`
`)})}if(this.examples.length>0)a.push({title:"Examples",body:this.examples.map((o)=>{if(typeof o==="function")return o(e);return o}).join(`
`)});if(s)a=s(a)||a;console.log(a.map((o)=>{return o.title?`${o.title}:
${o.body}`:o.body}).join(`
`))}outputVersion(){let{name:e}=this.cli,{versionNumber:n}=this.cli.globalCommand;if(n)console.log(`${e}/${n} ${W}`)}checkRequiredArgs(){let e=this.args.filter((n)=>n.required).length;if(this.cli.args.length<e)throw new p(`missing required args for command \`${this.rawName}\``)}checkUnknownOptions(){let{options:e,globalCommand:n}=this.cli;if(!this.config.allowUnknownOptions){for(let i of Object.keys(e))if(i!=="--"&&!this.hasOption(i)&&!n.hasOption(i))throw new p(`Unknown option \`${i.length>1?`--${i}`:`-${i}`}\``)}}checkOptionValue(){let{options:e,globalCommand:n}=this.cli,i=[...n.options,...this.options];for(let t of i){let s=e[t.name.split(".")[0]];if(t.required){let a=i.some((r)=>r.negated&&r.names.includes(t.name));if(s===!0||s===!1&&!a)throw new p(`option \`${t.rawName}\` value is missing`)}}}}class k extends y{constructor(e){super("@@global@@","",{},e)}}var b=Object.assign;class _ extends T{constructor(e=""){super();this.name=e,this.commands=[],this.rawArgs=[],this.args=[],this.options={},this.globalCommand=new k(this),this.globalCommand.usage("<command> [options]")}usage(e){return this.globalCommand.usage(e),this}command(e,n,i){let t=new y(e,n||"",i,this);return t.globalCommand=this.globalCommand,this.commands.push(t),t}option(e,n,i){return this.globalCommand.option(e,n,i),this}help(e){return this.globalCommand.option("-h, --help","Display this message"),this.globalCommand.helpCallback=e,this.showHelpOnExit=!0,this}version(e,n="-v, --version"){return this.globalCommand.version(e,n),this.showVersionOnExit=!0,this}example(e){return this.globalCommand.example(e),this}outputHelp(){if(this.matchedCommand)this.matchedCommand.outputHelp();else this.globalCommand.outputHelp()}outputVersion(){this.globalCommand.outputVersion()}setParsedInfo({args:e,options:n},i,t){if(this.args=e,this.options=n,i)this.matchedCommand=i;if(t)this.matchedCommandName=t;return this}unsetMatchedCommand(){this.matchedCommand=void 0,this.matchedCommandName=void 0}parse(e=P,{run:n=!0}={}){if(this.rawArgs=e,!this.name)this.name=e[1]?F(e[1]):"cli";let i=!0;for(let s of this.commands){let a=this.mri(e.slice(2),s),r=a.args[0];if(s.isMatched(r)){i=!1;let u=b(b({},a),{args:a.args.slice(1)});this.setParsedInfo(u,s,r),this.emit(`command:${r}`,s)}}if(i){for(let s of this.commands)if(s.name===""){i=!1;let a=this.mri(e.slice(2),s);this.setParsedInfo(a,s),this.emit("command:!",s)}}if(i){let s=this.mri(e.slice(2));this.setParsedInfo(s)}if(this.options.help&&this.showHelpOnExit)this.outputHelp(),n=!1,this.unsetMatchedCommand();if(this.options.version&&this.showVersionOnExit&&this.matchedCommandName==null)this.outputVersion(),n=!1,this.unsetMatchedCommand();let t={args:this.args,options:this.options};if(n)this.runMatchedCommand();if(!this.matchedCommand&&this.args[0])this.emit("command:*");return t}mri(e,n){let i=[...this.globalCommand.options,...n?n.options:[]],t=L(i),s=[],a=e.indexOf("--");if(a>-1)s=e.slice(a+1),e=e.slice(0,a);let r=G(e,t);r=Object.keys(r).reduce((c,m)=>{return b(b({},c),{[E(m)]:r[m]})},{_:[]});let u=r._,o={"--":s},l=n&&n.config.ignoreOptionDefaultValue?n.config.ignoreOptionDefaultValue:this.globalCommand.config.ignoreOptionDefaultValue,h=Object.create(null);for(let c of i){if(!l&&c.config.default!==void 0)for(let m of c.names)o[m]=c.config.default;if(Array.isArray(c.config.type)){if(h[c.name]===void 0)h[c.name]=Object.create(null),h[c.name].shouldTransform=!0,h[c.name].transformFunction=c.config.type[0]}}for(let c of Object.keys(r))if(c!=="_"){let m=c.split(".");S(o,m,r[c]),H(o,h)}return{args:u,options:o}}runMatchedCommand(){let{args:e,options:n,matchedCommand:i}=this;if(!i||!i.commandAction)return;i.checkUnknownOptions(),i.checkOptionValue(),i.checkRequiredArgs();let t=[];return i.args.forEach((s,a)=>{if(s.variadic)t.push(e.slice(a));else t.push(e[a])}),t.push(n),i.commandAction.apply(this,t)}}var V=(e="")=>new _(e);var d=K(Q(),1);var C="0.2.2";var x={error:(e)=>d.default.red(e),warning:(e)=>d.default.yellow(e),success:(e)=>d.default.green(e),info:(e)=>d.default.blue(e),dim:(e)=>d.default.dim(e)},f=V("dnsx");f.command("[...args]","Perform DNS lookup for specified domains").option("-q, --query <HOST>","Host name or domain name to query").option("-t, --type <TYPE>","Type of the DNS record being queried (A, MX, NS...)").option("-n, --nameserver <ADDR>","Address of the nameserver to send packets to").option("--class <CLASS>","Network class of DNS record (IN, CH, HS)").option("--edns <SETTING>","Whether to OPT in to EDNS (disable, hide, show)").option("--txid <NUMBER>","Set transaction ID to specific value").option("-Z <TWEAKS>","Set uncommon protocol tweaks").option("-U, --udp","Use DNS over UDP",{default:!1}).option("-T, --tcp","Use DNS over TCP",{default:!1}).option("-S, --tls","Use DNS-over-TLS",{default:!1}).option("-H, --https","Use DNS-over-HTTPS",{default:!1}).option("-1, --short","Display nothing but first result",{default:!1}).option("-J, --json","Display output as JSON",{default:!1}).option("--color <WHEN>","When to colorize output (always, auto, never)").option("--seconds","Display durations in seconds",{default:!1}).option("--time","Print response time",{default:!1}).option("--verbose","Print additional debugging information",{default:!1}).example("dnsx example.com").example("dnsx example.com MX").example("dnsx example.com A AAAA NS MX").example("dnsx example.com -t MX -n 1.1.1.1 -T").action(async(e,n)=>{try{let i=new Set(["A","AAAA","NS","MX","TXT","SRV","PTR","CNAME","SOA","CAA"]),t=[],s=[];if(e.length>0)t.push(e[0]);for(let h=1;h<e.length;h++){let c=e[h].toUpperCase();if(i.has(c))s.push(c);else t.push(c)}if(n.query)t.push(...Array.isArray(n.query)?n.query:[n.query]);if(n.type)s.push(...Array.isArray(n.type)?n.type:[n.type]);if(t.length===0)console.error(x.error("Error: No domains specified")),g.exit(1);let a=new q({domains:t,nameserver:n.nameserver,type:s.length>0?s:void 0,class:n.class,udp:n.udp,tcp:n.tcp,tls:n.tls,https:n.https,edns:n.edns,txid:n.txid,tweaks:M(n.Z),verbose:n.verbose}),r=Date.now(),u=await a.query(),o=Date.now()-r,l=B(u,{json:Boolean(n.json),short:Boolean(n.short),showDuration:n.time?o:void 0,colors:{enabled:n.color==="always"||n.color!=="never"&&g.stdout.isTTY},rawSeconds:Boolean(n.seconds)});if(console.log(l),u.length===0)g.exit(1)}catch(i){if(console.error(),console.error(x.error(` Error: ${i.message}`)),n.verbose&&i.stack)console.error(x.dim(`
Stack trace:
${i.stack.split(`
`).map((t)=>` ${t}`).join(`
`)}`));console.error(),g.exit(1)}});f.command("version","Show the version of dtsx").action(()=>{console.log(C)});f.help();f.version(C);f.parse();f.on("error",(e)=>{console.error(),console.error(x.error(` Error: ${e.message}`)),console.error(),g.exit(1)});export{x as colors};