@mark1russell7/netdiag-cli 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +115 -2
- package/build/bin.d.ts +1 -0
- package/build/bin.js +7 -0
- package/build/bin.js.map +1 -0
- package/build/cli-IhViZxm9.js +468 -0
- package/build/cli-IhViZxm9.js.map +1 -0
- package/build/index.d.ts +213 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1 -0
- package/package.json +74 -3
- package/src/bin.ts +5 -0
- package/src/check.ts +50 -0
- package/src/cli.ts +81 -0
- package/src/index.ts +11 -0
- package/src/next.ts +197 -0
- package/src/plan.ts +180 -0
- package/src/report.ts +149 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli-IhViZxm9.js","names":[],"sources":["../src/plan.ts","../src/next.ts","../src/report.ts","../src/check.ts","../src/cli.ts"],"sourcesContent":["import {\n canonicalHost,\n grantProblems,\n hostKind,\n namedGrant,\n parseTarget,\n type Grant,\n type Target,\n type VisitHost,\n type VisitInput,\n type VisitResolver,\n} from \"@mark1russell7/netdiag\";\nimport { DEFAULT_PING_COUNT, MAX_PING_COUNT } from \"@mark1russell7/netdiag/tools\";\nimport { err, ok, type Result } from \"@mark1russell7/optional\";\n\n// The plan of a check: the options of the command line, changed into the input of a visit. The plan is the scope rule\n// of the command: it grants only the hosts that the user names, the DNS resolver and the ranges of `--grant`.\n\n/** The schemes of the HTTP request of a check. */\nexport const SCHEMES: readonly [\"http\", \"https\"] = [\"http\", \"https\"];\n\n/** A scheme of the HTTP request of a check. */\nexport type Scheme = (typeof SCHEMES)[number];\n\n/** The options of the command `check`. Each field is a flag of the command line. */\nexport interface CheckOptions {\n /** The host to check: a DNS name, an IP address, `host:port` or a URL. */\n readonly target: string;\n /** The port of the HTTP request. The default is the port in the target, then the port of the scheme. */\n readonly port?: number | undefined;\n /** The scheme of the HTTP request. The default is the scheme of a URL target, then `https`. */\n readonly scheme?: Scheme | undefined;\n /** The path of the HTTP request. The default is the path of a URL target, then `/`. */\n readonly path?: string | undefined;\n /** The hosts that work, for a comparison with the target. */\n readonly reference?: readonly string[] | undefined;\n /** The address of the DNS resolver, with an optional port. The default is the first resolver of the computer. */\n readonly resolver?: string | undefined;\n /** The IP address of the default gateway. Without it, the check does not ping a gateway. */\n readonly gateway?: string | undefined;\n /** The CIDR ranges that the user owns. The check can probe each address in them. */\n readonly grant?: readonly string[] | undefined;\n /** The number of echo requests to each address. The default is `DEFAULT_PING_COUNT`. */\n readonly pingCount?: number | undefined;\n}\n\n/** The parts of the computer that a plan reads. */\nexport interface PlanSystem {\n /** The DNS resolvers of the computer, in the form of `getServers()` of `node:dns`. */\n readonly resolvers: readonly string[];\n}\n\n/** The plan of a check: the target, the HTTP request, the number of echo requests, and the input of the visit. */\nexport interface CheckPlan {\n /** The target, with the port of the HTTP request when the user gives one. */\n readonly target: Target;\n readonly scheme: Scheme;\n readonly path: string;\n readonly pingCount: number;\n /** The input of `visitTarget`, with the grants of the scope. */\n readonly visit: VisitInput;\n}\n\ninterface Located {\n readonly target: Target;\n readonly scheme?: Scheme | undefined;\n readonly path?: string | undefined;\n}\n\nfunction isScheme(text: string): text is Scheme {\n return (SCHEMES as readonly string[]).includes(text);\n}\n\n/** This function reads a host of the user. A URL also gives a scheme and a path. Only a port in the text stays in the target. */\nfunction locate(text: string, what: string): Result<Located, string> {\n const parsed = parseTarget(text);\n if (parsed.tag === \"err\") return err(`The ${what} is not valid. ${parsed.error}`);\n if (!text.includes(\"://\")) return ok({ target: parsed.value });\n const url = new URL(text.trim());\n const scheme = url.protocol.slice(0, -1);\n if (!isScheme(scheme)) return err(`The ${what} \"${text}\" has the scheme ${scheme}. A check sends HTTP and HTTPS requests only.`);\n // `parseTarget` gives the port of the scheme to a URL with no port. The check keeps only a port that the URL names.\n const host = { host: parsed.value.host, kind: parsed.value.kind };\n const target = url.port === \"\" ? host : { ...host, port: Number(url.port) };\n return ok({ target, scheme, path: `${url.pathname}${url.search}` });\n}\n\n/** This function gives a grant of one IP address. */\nfunction addressGrant(address: Target, basis: Grant[\"basis\"], note: string): Grant {\n return { ...namedGrant(address, note), basis };\n}\n\nfunction visitHost(target: Target): VisitHost {\n return target.port === undefined ? { host: target.host } : { host: target.host, port: target.port };\n}\n\nfunction whole(value: number, low: number, high: number): boolean {\n return Number.isInteger(value) && value >= low && value <= high;\n}\n\n/**\n * This function gives the plan of a check from the options of the command line, or each problem of the options.\n *\n * The grants of the plan are the scope rule of the command. The plan grants only these hosts:\n *\n * - the target, and each reference host, with the basis `named`\n * - the DNS resolver, with the basis `named` from `--resolver`, or `configured` from the computer\n * - the gateway, when the user gives it, with the basis `named`\n * - each range of `--grant`, with the basis `owned`\n *\n * The visit also puts in the scope the addresses that the resolver gives for a named host.\n */\nexport function planCheck(options: CheckOptions, system: PlanSystem): Result<CheckPlan, readonly string[]> {\n const problems: string[] = [];\n const grants: Grant[] = [];\n\n const located = locate(options.target, \"target\");\n if (located.tag === \"err\") return err([located.error]);\n const scheme = options.scheme ?? located.value.scheme ?? \"https\";\n const path = options.path ?? located.value.path ?? \"/\";\n if (!isScheme(scheme)) problems.push(`The scheme ${String(scheme)} is not http or https.`);\n if (!path.startsWith(\"/\")) problems.push(`The path \"${path}\" does not start with \"/\".`);\n const port = options.port ?? located.value.target.port;\n if (port !== undefined && !whole(port, 1, 65535)) problems.push(`The port ${port} is not an integer from 1 to 65535.`);\n const pingCount = options.pingCount ?? DEFAULT_PING_COUNT;\n if (!whole(pingCount, 0, MAX_PING_COUNT)) problems.push(`The ping count ${pingCount} is not an integer from 0 to ${MAX_PING_COUNT}.`);\n const target: Target = port === undefined ? { host: located.value.target.host, kind: located.value.target.kind } : { ...located.value.target, port };\n grants.push(namedGrant(target, \"the target\"));\n\n const references: VisitHost[] = [];\n for (const text of options.reference ?? []) {\n const reference = locate(text, \"reference\");\n if (reference.tag === \"err\") problems.push(reference.error);\n else {\n references.push(visitHost(reference.value.target));\n grants.push(namedGrant(reference.value.target, \"a reference host\"));\n }\n }\n\n let resolver: VisitResolver | undefined;\n const resolverText = options.resolver ?? system.resolvers[0];\n if (resolverText !== undefined) {\n const from = options.resolver === undefined ? \"The DNS resolver of the computer\" : \"The DNS resolver\";\n const parsed = parseTarget(resolverText);\n if (parsed.tag === \"err\") problems.push(`${from} is not valid. ${parsed.error}`);\n else if (parsed.value.kind === \"name\") problems.push(`${from} \"${resolverText}\" is not an IP address.`);\n else {\n resolver = parsed.value.port === undefined ? { address: parsed.value.host } : { address: parsed.value.host, port: parsed.value.port };\n grants.push(addressGrant(parsed.value, options.resolver === undefined ? \"configured\" : \"named\", \"the DNS resolver\"));\n }\n }\n\n let gateway: string | undefined;\n if (options.gateway !== undefined) {\n const host = canonicalHost(options.gateway);\n const kind = hostKind(host);\n if (kind === \"name\") problems.push(`The gateway \"${options.gateway}\" is not an IP address.`);\n else {\n gateway = host;\n grants.push(addressGrant({ host, kind }, \"named\", \"the default gateway\"));\n }\n }\n\n for (const cidr of options.grant ?? []) grants.push({ kind: \"range\", cidr: cidr.trim(), basis: \"owned\", note: \"a range of --grant\" });\n\n // A grant with a problem stops the plan, so a typing error cannot widen the scope.\n for (const grant of grants) {\n for (const problem of grantProblems(grant)) problems.push(grant.basis === \"configured\" ? `${problem} Give a resolver with --resolver <address>.` : problem);\n }\n if (problems.length > 0) return err(problems);\n\n const visit: VisitInput = {\n target: visitHost(target),\n references,\n ...(gateway === undefined ? {} : { gateway }),\n ...(resolver === undefined ? {} : { resolver }),\n grants,\n };\n return ok({ target, scheme, path, pingCount, visit });\n}\n","import {\n DNS_ANSWER,\n GAIN_RESOLUTION,\n HTTP_RESPONSE,\n ICMP_ECHO,\n LOSS_MIN_SENT,\n STANDARD_CATALOG,\n TCP_CONNECT,\n TLS_HANDSHAKE,\n featuresOf,\n formatTarget,\n hostKind,\n rankProbes,\n type Candidate,\n type Diagnosis,\n type Evidence,\n type Observable,\n type VisitRole,\n} from \"@mark1russell7/netdiag\";\nimport type { CheckPlan } from \"./plan.ts\";\n\n// The choice of the next step. Each candidate is a probe of the visit for one role. Its features are the features that\n// the facts of the probe give, and that the evidence does not have yet. `rankProbes` orders the candidates by the\n// information that they can give about the most probable causes.\n\n/** A probe of a visit: the DNS query, the ICMP echo requests, the TLS handshake, or the HTTP request. */\nexport type NextProbeKind = \"dns\" | \"ping\" | \"tls\" | \"http\";\n\n/** A probe that the next step can start, for one role of the visit. */\nexport interface NextProbe {\n /** The id of the candidate: the role and the probe, for example `gateway.ping`. */\n readonly id: string;\n readonly role: VisitRole;\n readonly probe: NextProbeKind;\n /** The fact kinds that the probe gives. A TLS handshake and an HTTP request also give the TCP connect before them. */\n readonly kinds: readonly string[];\n /** An approximate time of the probe, in seconds. When two gains are equal, the lower cost comes first. */\n readonly cost: number;\n}\n\n/** The next step of a check: the instruction in plain words, its information gain, and the features that it observes. */\nexport interface NextStep {\n /** The id of the probe of the step, for example `gateway.ping`. */\n readonly id: string;\n /** The instruction to the user. */\n readonly step: string;\n /** The expected information gain over the most probable causes, in bits. */\n readonly gain: number;\n /** The features that the step observes and that the evidence does not have. */\n readonly observes: readonly Observable[];\n}\n\n/** The number of the most probable causes that the report shows, and that the gain of a next step tells apart. */\nexport const TOP_CAUSES = 3;\n\nconst PROBE_KINDS: Readonly<Record<NextProbeKind, readonly string[]>> = {\n dns: [DNS_ANSWER.kind],\n ping: [ICMP_ECHO.kind],\n tls: [TCP_CONNECT.kind, TLS_HANDSHAKE.kind],\n http: [TCP_CONNECT.kind, HTTP_RESPONSE.kind],\n};\n\nconst PROBE_COSTS: Readonly<Record<NextProbeKind, number>> = { dns: 1, tls: 2, http: 3, ping: 5 };\n\nconst PROBE_NAMES: Readonly<Record<NextProbeKind, string>> = { dns: \"DNS query\", ping: \"ping\", tls: \"TLS handshake\", http: \"HTTP request\" };\n\nfunction nextProbe(role: VisitRole, probe: NextProbeKind): NextProbe {\n return { id: `${role}.${probe}`, role, probe, kinds: PROBE_KINDS[probe], cost: PROBE_COSTS[probe] };\n}\n\n/**\n * The candidates of the next step: each probe of the target and of the reference hosts, and the ping of the gateway.\n * The TLS handshake and the HTTP request are two candidates, one for each stage. Each stage has its own conditions in\n * `ready`. One candidate for the whole HTTPS request is the next change of this list.\n */\nexport const NEXT_PROBES: readonly NextProbe[] = [\n ...([\"target\", \"reference\"] as const).flatMap((role) => ([\"dns\", \"ping\", \"tls\", \"http\"] as const).map((probe) => nextProbe(role, probe))),\n nextProbe(\"gateway\", \"ping\"),\n];\n\n/** The instruction to add a reference host. */\nexport const ADD_REFERENCE = \"Add a host that works for you with --reference <host>, for example a large web site.\";\n\n/** The instruction to add the gateway. */\nexport const ADD_GATEWAY = \"Give the address of your router with --gateway <address>.\";\n\nconst keyOf = (item: Observable): string => `${item.feature}\\u0000${item.primary ? 1 : 0}`;\n\n/** This function tells if the evidence of a role has no value of a feature, or has the value `value`. */\nfunction openOrIs(evidence: readonly Evidence[], primary: boolean, feature: string, value: string): boolean {\n const values = evidence.filter((item) => item.feature === feature && item.primary === primary).map((item) => item.value);\n return values.length === 0 || values.includes(value);\n}\n\n/**\n * The features that a probe gives only after a stage with one value. For example, a DNS answer gives addresses only\n * when its outcome is `ok`. A new check cannot give such a feature when the evidence has another value of the stage.\n */\nconst AFTER_STAGE: Readonly<Record<string, readonly [feature: string, value: string]>> = {\n \"dns.address\": [\"dns.answer\", \"ok\"],\n};\n\n/** This function tells if a new check can give a feature, from the stage before the feature. */\nfunction observable(item: Observable, evidence: readonly Evidence[]): boolean {\n const stage = AFTER_STAGE[item.feature];\n return stage === undefined || openOrIs(evidence, item.primary, stage[0], stage[1]);\n}\n\n/** This function tells if the user can add the host of a role with an option. The step of such a role tells how. */\nfunction absent(role: VisitRole, plan: CheckPlan): boolean {\n if (role === \"reference\") return (plan.visit.references ?? []).length === 0;\n return role === \"gateway\" && plan.visit.gateway === undefined;\n}\n\n/**\n * This function tells if a probe can give its facts in a new check. A probe after a stage that failed cannot: for\n * example, no TLS handshake follows a TCP connect that the server refused.\n */\nfunction ready(probe: NextProbe, plan: CheckPlan, addressed: ReadonlySet<VisitRole>, evidence: readonly Evidence[]): boolean {\n const primary = probe.role === \"target\";\n if (absent(probe.role, plan)) return probe.probe !== \"tls\" || plan.scheme === \"https\";\n switch (probe.probe) {\n case \"dns\":\n return probe.role === \"target\" ? plan.target.kind === \"name\" : (plan.visit.references ?? []).some((host) => hostKind(host.host) === \"name\");\n case \"ping\":\n return probe.role === \"gateway\" || addressed.has(probe.role);\n case \"tls\":\n return addressed.has(probe.role) && openOrIs(evidence, primary, \"tcp.connect\", \"connected\");\n case \"http\":\n return (\n addressed.has(probe.role) &&\n openOrIs(evidence, primary, \"tcp.connect\", \"connected\") &&\n (plan.scheme === \"http\" || openOrIs(evidence, primary, \"tls.handshake\", \"ok\"))\n );\n }\n}\n\n/** This function gives the instruction of a next step in plain words. */\nexport function stepText(probe: NextProbe, observes: readonly Observable[], plan: CheckPlan): string {\n const morePings = `Run the check again with --ping-count ${LOSS_MIN_SENT}.`;\n if (probe.role === \"reference\" && absent(\"reference\", plan)) return ADD_REFERENCE;\n if (probe.role === \"gateway\" && absent(\"gateway\", plan)) return plan.pingCount === 0 ? `${ADD_GATEWAY} ${morePings}` : ADD_GATEWAY;\n const who = probe.role === \"target\" ? formatTarget(plan.target) : probe.role === \"gateway\" ? `the gateway ${plan.visit.gateway ?? \"\"}` : \"the reference hosts\";\n switch (probe.probe) {\n case \"dns\":\n if (plan.visit.resolver === undefined) return \"Give the address of a DNS resolver with --resolver <address>.\";\n break;\n case \"ping\":\n if (plan.pingCount === 0 || (plan.pingCount < LOSS_MIN_SENT && observes.every((item) => item.feature === \"path.loss\"))) return morePings;\n break;\n case \"tls\":\n if (plan.scheme === \"http\") return \"Run the check again with --scheme https.\";\n break;\n case \"http\":\n break;\n }\n const features = [...new Set(observes.map((item) => item.feature))].join(\", \");\n return `The check has no value of ${features} from the ${PROBE_NAMES[probe.probe]} of ${who}. Look at the problems of the visit, then run the check again.`;\n}\n\n/**\n * This function gives the next steps of a check, from the largest information gain. The first step is the next step.\n *\n * - A candidate observes the features of its fact kinds that the evidence does not have, with the role of the probe.\n * A feature after a stage that gave another value is not observable: no address follows an answer `nxdomain`.\n * - A probe that cannot give its facts is not a candidate. `addressed` has the roles with an address to probe: an IP\n * address, or an address of a DNS answer.\n * - A step with a gain of `GAIN_RESOLUTION` or less tells nothing, so the list does not have it.\n * - Two probes with the same instruction give one step, with the larger gain. For example, each probe of a reference\n * host that the user did not give has the instruction `ADD_REFERENCE`.\n */\nexport function nextSteps(diagnosis: Diagnosis, plan: CheckPlan, addressed: ReadonlySet<VisitRole>): NextStep[] {\n if (diagnosis.causes.length === 0) return [];\n const seen = new Set(diagnosis.used.map(keyOf));\n const probes = new Map(NEXT_PROBES.filter((probe) => ready(probe, plan, addressed, diagnosis.used)).map((probe) => [probe.id, probe]));\n const candidates: Candidate[] = [...probes.values()]\n .map((probe) => ({\n id: probe.id,\n cost: probe.cost,\n observes: featuresOf(probe.kinds, { gateway: probe.role === \"gateway\" })\n .map((feature) => ({ feature, primary: probe.role === \"target\" }))\n .filter((item) => !seen.has(keyOf(item)) && observable(item, diagnosis.used)),\n }))\n .filter((candidate) => candidate.observes.length > 0);\n if (candidates.length === 0) return [];\n const steps: NextStep[] = [];\n const said = new Set<string>();\n for (const choice of rankProbes(STANDARD_CATALOG, diagnosis, candidates, { top: TOP_CAUSES })) {\n const probe = probes.get(choice.candidate.id);\n if (probe === undefined || choice.gain <= GAIN_RESOLUTION) continue;\n const step = stepText(probe, choice.counted, plan);\n if (said.has(step)) continue;\n said.add(step);\n steps.push({ id: probe.id, step, gain: choice.gain, observes: choice.counted });\n }\n return steps;\n}\n","import {\n DNS_ANSWER,\n STANDARD_CATALOG,\n diagnose,\n formatTarget,\n hostKind,\n isFactOf,\n readEvidence,\n type Cause,\n type Diagnosis,\n type Evidence,\n type Fact,\n type Grant,\n type Skipped,\n type VisitRole,\n type VisitStep,\n} from \"@mark1russell7/netdiag\";\nimport { countOf } from \"@mark1russell7/text\";\nimport { pad } from \"@mark1russell7/text/length\";\nimport { TOP_CAUSES, nextSteps, type NextStep } from \"./next.ts\";\nimport type { CheckPlan, Scheme } from \"./plan.ts\";\n\n// The report of a check: the steps of the visit, changed into evidence, the causes and the next step. Each function of\n// this module is pure: the same plan and the same steps give the same report.\n\n/** The format of the JSON report of a check. A change to the shape of `CheckReport` changes the format. */\nexport const CHECK_FORMAT = \"netdiag-check/1\";\n\n/** A fact of a visit, with the role and the host of the step that gave it. */\nexport interface CheckFact {\n readonly role: VisitRole;\n readonly host: string;\n readonly fact: Fact;\n}\n\n/** A problem of a visit: a probe that did not end, with the role and the host of the step. */\nexport interface CheckProblem {\n readonly role: VisitRole;\n readonly host: string;\n readonly problem: string;\n}\n\n/** The report of a check. `--json` writes this object. */\nexport interface CheckReport {\n readonly format: typeof CHECK_FORMAT;\n /** The target, as `host` or `host:port`. */\n readonly target: string;\n readonly scheme: Scheme;\n readonly path: string;\n /** The grants of the scope of the check. */\n readonly grants: readonly Grant[];\n /** Each cause, from the most probable. */\n readonly causes: readonly Cause[];\n /** The next step, or `null` when no probe can tell more about the most probable causes. */\n readonly next: NextStep | null;\n /** Each next step with a gain, from the largest gain. The first one is `next`. */\n readonly choices: readonly NextStep[];\n readonly problems: readonly CheckProblem[];\n /** The evidence that the diagnosis used. A fact of the role `target` gives primary evidence. */\n readonly evidence: readonly Evidence[];\n /** The evidence that the diagnosis did not use, with the reason. */\n readonly ignored: Diagnosis[\"ignored\"];\n /** The facts that gave no evidence, with the reason. */\n readonly skipped: readonly Skipped[];\n readonly facts: readonly CheckFact[];\n}\n\n/** This function gives the roles with an address to probe: an IP address of the plan, or an address of a DNS answer. */\nfunction addressedRoles(plan: CheckPlan, facts: readonly CheckFact[]): Set<VisitRole> {\n const roles = new Set<VisitRole>();\n if (plan.target.kind !== \"name\") roles.add(\"target\");\n if ((plan.visit.references ?? []).some((host) => hostKind(host.host) !== \"name\")) roles.add(\"reference\");\n if (plan.visit.gateway !== undefined) roles.add(\"gateway\");\n for (const { role, fact } of facts) if (isFactOf(DNS_ANSWER, fact) && fact.value.addresses.length > 0) roles.add(role);\n return roles;\n}\n\n/**\n * This function gives the report of the steps of a visit.\n *\n * - A fact of the role `target` gives primary evidence. A fact of each other role gives reference evidence.\n * - The report finds the facts of the target by their role, not by their position. Thus a first step of the role\n * `local` is not a problem.\n * - The causes come from `diagnose` with `STANDARD_CATALOG`. The next steps come from `nextSteps`.\n */\nexport function reportOf(plan: CheckPlan, steps: readonly VisitStep[]): CheckReport {\n const facts: CheckFact[] = steps.flatMap((step) => (step.kind === \"fact\" ? [{ role: step.role, host: step.host, fact: step.fact }] : []));\n const problems: CheckProblem[] = steps.flatMap((step) => (step.kind === \"problem\" ? [{ role: step.role, host: step.host, problem: step.problem }] : []));\n const primary = new Set(facts.filter((item) => item.role === \"target\").map((item) => item.fact.id));\n const reading = readEvidence(\n facts.map((item) => item.fact),\n { primary: (fact) => primary.has(fact.id), gateway: plan.visit.gateway },\n );\n const diagnosis = diagnose(STANDARD_CATALOG, reading.evidence);\n const choices = nextSteps(diagnosis, plan, addressedRoles(plan, facts));\n return {\n format: CHECK_FORMAT,\n target: formatTarget(plan.target),\n scheme: plan.scheme,\n path: plan.path,\n grants: plan.visit.grants,\n causes: diagnosis.causes,\n next: choices[0] ?? null,\n choices,\n problems,\n evidence: diagnosis.used,\n ignored: diagnosis.ignored,\n skipped: reading.skipped,\n facts,\n };\n}\n\n/** This function gives a probability as a percent for a person: `<1%` for a probability above 0 that rounds to 0. */\nexport function percentOf(probability: number): string {\n const value = Math.round(probability * 100);\n return value === 0 && probability > 0 ? \"<1%\" : `${value}%`;\n}\n\n/**\n * This function gives the role and the host of a step for a person. A step of the role `local` is a passive read of\n * this computer, so its text is `this computer`.\n */\nexport function subjectText(role: string, host: string): string {\n return role === \"local\" ? \"this computer\" : `${role} ${host}`;\n}\n\n/** The indent of the lines under a cause. */\nconst UNDER = \" \".repeat(8);\n\n/**\n * This function gives the lines of the text report of a check. The lines show the most probable causes with their\n * probabilities and the next step with its gain. They also show the problems of the visit and the count of the evidence.\n */\nexport function formatReport(report: CheckReport): string[] {\n const lines = [`netdiag check ${report.target} (${report.scheme}, path ${report.path})`, \"\", \"The probable causes\"];\n for (const cause of report.causes.slice(0, TOP_CAUSES)) {\n lines.push(` ${pad(percentOf(cause.probability), 4, { side: \"start\" })} ${cause.summary}`, `${UNDER}To do: ${cause.step}`);\n }\n if (report.evidence.length === 0) lines.push(\" The visit gave no evidence. These probabilities are the probabilities before a probe.\");\n lines.push(\"\");\n if (report.next === null) lines.push(\"The next step\", \" netdiag has no probe that can tell more about these causes.\");\n else lines.push(`The next step (it can give ${report.next.gain.toFixed(2)} bits of information about the causes)`, ` ${report.next.step}`);\n if (report.problems.length > 0) {\n lines.push(\"\", `The problems of the visit (${report.problems.length})`);\n for (const item of report.problems) lines.push(` ${subjectText(item.role, item.host)}: ${item.problem}`);\n }\n lines.push(\"\", `${countOf(report.evidence.length, \"observation\")} from ${countOf(report.facts.length, \"fact\")}. The option --json gives each fact.`);\n return lines;\n}\n","import { getServers } from \"node:dns\";\nimport { realClock, type Clock } from \"@mark1russell7/algebra/clock\";\nimport { visitTarget, type TlsProbeOptions, type VisitStep } from \"@mark1russell7/netdiag\";\nimport { commandHost, type ToolHost } from \"@mark1russell7/netdiag/tools\";\nimport { runCommand } from \"@procedures/shell/command\";\nimport { planCheck, type CheckOptions } from \"./plan.ts\";\nimport { reportOf, type CheckReport } from \"./report.ts\";\n\n// The edge of a check: the visit sends the packets, and starts the outside tools. A test gives a fake tool host and\n// servers on the loopback addresses.\n\n/** The parts of the computer that a check uses. A test gives its own parts. */\nexport interface CheckDeps {\n readonly clock: Clock;\n /** The host of the outside tools. Without it, the check starts no `ping`. */\n readonly tools?: ToolHost | undefined;\n /** The DNS resolvers of the computer, in the form of `getServers()` of `node:dns`. */\n readonly resolvers: readonly string[];\n /** The trust stores of the TLS handshake. The default is the roots of Node.js. */\n readonly tls?: TlsProbeOptions | undefined;\n readonly signal?: AbortSignal | undefined;\n}\n\n/** This function gives the parts of this computer: the monotonic clock, `runCommand` of `@procedures/shell` and the DNS resolvers. */\nexport function systemDeps(): CheckDeps {\n return { clock: realClock, tools: commandHost(runCommand, process.platform), resolvers: getServers() };\n}\n\n/**\n * This function plans a check, starts its visit, and gives the report.\n *\n * It throws a `RangeError` with each problem of the options before the first probe.\n */\nexport async function check(options: CheckOptions, deps: CheckDeps): Promise<CheckReport> {\n const planned = planCheck(options, { resolvers: deps.resolvers });\n if (planned.tag === \"err\") throw new RangeError(planned.error.join(\"\\n\"));\n const plan = planned.value;\n const steps: VisitStep[] = [];\n const visit = visitTarget(plan.visit, {\n clock: deps.clock,\n tools: deps.tools,\n pingCount: plan.pingCount,\n scheme: plan.scheme,\n path: plan.path,\n tls: deps.tls,\n signal: deps.signal,\n });\n for await (const step of visit) steps.push(step);\n return reportOf(plan, steps);\n}\n","import { readFileSync } from \"node:fs\";\nimport { DEFAULT_PING_COUNT, MAX_PING_COUNT } from \"@mark1russell7/netdiag/tools\";\nimport { createProcedureCli, type Print, type ProcedureCli, type Terminal } from \"@procedures/cli-kit\";\nimport { defineProcedure } from \"@procedures/client\";\nimport { z } from \"zod\";\nimport { check, systemDeps } from \"./check.ts\";\nimport { SCHEMES, type CheckOptions } from \"./plan.ts\";\nimport { formatReport, type CheckReport } from \"./report.ts\";\n\n// The command line of netdiag. `@procedures/cli-kit` makes the flags from the input schema of the command `check`, and\n// gives the help, `--json` and the exit code.\n\n/** The name of the program. */\nexport const PROGRAM = \"netdiag\";\n\n/** The options of `createNetdiagCli`. */\nexport interface NetdiagCliOptions {\n /** The function that operates one check. A test gives a fake. */\n readonly check: (options: CheckOptions, signal: AbortSignal | undefined) => Promise<CheckReport>;\n /** The version that `--version` shows. */\n readonly version?: string | undefined;\n /** The output of the program. The default writes plain text to the console. */\n readonly print?: Print | undefined;\n /** The terminal state. The program asks for a missing target only in a terminal. */\n readonly terminal?: Terminal | undefined;\n}\n\nconst CHECK_INPUT = z.object({\n target: z.string().min(1).describe(\"The host to check: a DNS name, an IP address, host:port or a URL\"),\n port: z.number().int().min(1).max(65535).optional().describe(\"The port of the HTTP request. The default is the port of the scheme\"),\n scheme: z.enum(SCHEMES).optional().describe(\"The scheme of the HTTP request: http or https. The default is https\"),\n path: z.string().optional().describe(\"The path of the HTTP request. The default is /\"),\n reference: z.array(z.string()).optional().describe(\"A host that works, for a comparison. Give the flag again for more hosts\"),\n resolver: z.string().optional().describe(\"The address of the DNS resolver. The default is the first resolver of this computer\"),\n gateway: z.string().optional().describe(\"The IP address of the default gateway. The check pings it\"),\n grant: z.array(z.string()).optional().describe(\"A CIDR range that you own. The check can probe each address in it\"),\n pingCount: z\n .number()\n .int()\n .min(0)\n .max(MAX_PING_COUNT)\n .optional()\n .describe(`The number of echo requests to each address. The default is ${DEFAULT_PING_COUNT}`),\n});\n\n/** This function makes the command line of netdiag with the command `check`. */\nexport function createNetdiagCli(options: NetdiagCliOptions): ProcedureCli {\n const procedure = defineProcedure({\n path: [\"check\"],\n input: CHECK_INPUT,\n output: z.custom<CheckReport>(),\n metadata: {\n description: \"Check a target, and show the probable causes of a fault and the next step\",\n args: [\"target\"],\n effects: { reads: [\"network\"], writes: [\"process\"], destructive: false },\n },\n handler: (input, context) => options.check(input, context.signal),\n });\n return createProcedureCli({\n name: PROGRAM,\n version: options.version,\n description: \"the probable causes of a network fault, and the next step\",\n procedures: [procedure],\n format: { check: (result) => formatReport(result as CheckReport) },\n print: options.print,\n terminal: options.terminal,\n });\n}\n\n/** This function gives the version in `package.json` of this package. */\nexport function packageVersion(): string {\n const manifest: unknown = JSON.parse(readFileSync(new URL(\"../package.json\", import.meta.url), \"utf8\"));\n const version = typeof manifest === \"object\" && manifest !== null && \"version\" in manifest ? manifest.version : undefined;\n return typeof version === \"string\" ? version : \"0.0.0\";\n}\n\n/** This function operates the command line on this computer, and gives the exit code. */\nexport async function main(argv: readonly string[]): Promise<number> {\n const cli = createNetdiagCli({ version: packageVersion(), check: (options, signal) => check(options, { ...systemDeps(), signal }) });\n return cli.run(argv);\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,MAAa,UAAsC,CAAC,QAAQ,OAAO;AAkDnE,SAAS,SAAS,MAA8B;CAC9C,OAAQ,QAA8B,SAAS,IAAI;AACrD;;AAGA,SAAS,OAAO,MAAc,MAAuC;CACnE,MAAM,SAAS,YAAY,IAAI;CAC/B,IAAI,OAAO,QAAQ,OAAO,OAAO,IAAI,OAAO,KAAK,iBAAiB,OAAO,OAAO;CAChF,IAAI,CAAC,KAAK,SAAS,KAAK,GAAG,OAAO,GAAG,EAAE,QAAQ,OAAO,MAAM,CAAC;CAC7D,MAAM,MAAM,IAAI,IAAI,KAAK,KAAK,CAAC;CAC/B,MAAM,SAAS,IAAI,SAAS,MAAM,GAAG,EAAE;CACvC,IAAI,CAAC,SAAS,MAAM,GAAG,OAAO,IAAI,OAAO,KAAK,IAAI,KAAK,mBAAmB,OAAO,8CAA8C;CAE/H,MAAM,OAAO;EAAE,MAAM,OAAO,MAAM;EAAM,MAAM,OAAO,MAAM;CAAK;CAChE,MAAM,SAAS,IAAI,SAAS,KAAK,OAAO;EAAE,GAAG;EAAM,MAAM,OAAO,IAAI,IAAI;CAAE;CAC1E,OAAO,GAAG;EAAE;EAAQ;EAAQ,MAAM,GAAG,IAAI,WAAW,IAAI;CAAS,CAAC;AACpE;;AAGA,SAAS,aAAa,SAAiB,OAAuB,MAAqB;CACjF,OAAO;EAAE,GAAG,WAAW,SAAS,IAAI;EAAG;CAAM;AAC/C;AAEA,SAAS,UAAU,QAA2B;CAC5C,OAAO,OAAO,SAAS,KAAA,IAAY,EAAE,MAAM,OAAO,KAAK,IAAI;EAAE,MAAM,OAAO;EAAM,MAAM,OAAO;CAAK;AACpG;AAEA,SAAS,MAAM,OAAe,KAAa,MAAuB;CAChE,OAAO,OAAO,UAAU,KAAK,KAAK,SAAS,OAAO,SA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,aAAa;EACb,YAAY,CAAC,SAAS;EACtB,QAAQ,EAAE,QAAQ,WAAW,aAAa,MAAqB,EAAE;EACjE,OAAO,QAAQ;EACf,UAAU,QAAQ;CACpB,CAAC;AACH;;AAGA,SAAgB,iBAAyB;CACvC,MAAM,WAAoB,KAAK,MAAM,aAAa,IAAI,IAAI,mBAAmB,YAAY,GAAG,GAAG,MAAM,CAAC;CACtG,MAAM,UAAU,OAAO,aAAa,YAAY,aAAa,QAAQ,aAAa,WAAW,SAAS,UAAU,KAAA;CAChH,OAAO,OAAO,YAAY,WAAW,UAAU;AACjD;;AAGA,eAAsB,KAAK,MAA0C;CAEnE,OADY,iBAAiB;EAAE,SAAS,eAAe;EAAG,QAAQ,SAAS,WAAW,MAAM,SAAS;GAAE,GAAG,WAAW;GAAG;EAAO,CAAC;CAAE,CACzH,CAAC,CAAC,IAAI,IAAI;AACrB"}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry of `@mark1russell7/netdiag-cli`: the command line of netdiag.
|
|
3
|
+
*
|
|
4
|
+
* The program `netdiag` has the command `check`. The pure parts are also exports. They are the plan of a check with
|
|
5
|
+
* its scope rule, the report, the text of the report and the choice of the next step.
|
|
6
|
+
*/
|
|
7
|
+
import { Cause, Diagnosis, Evidence, Fact, Grant, Observable, Skipped, Target, TlsProbeOptions, VisitInput, VisitRole, VisitStep } from "@mark1russell7/netdiag";
|
|
8
|
+
import { ToolHost } from "@mark1russell7/netdiag/tools";
|
|
9
|
+
import { Result } from "@mark1russell7/optional";
|
|
10
|
+
import { Clock } from "@mark1russell7/algebra/clock";
|
|
11
|
+
import { Print, ProcedureCli, Terminal } from "@procedures/cli-kit";
|
|
12
|
+
/** The schemes of the HTTP request of a check. */
|
|
13
|
+
export declare const SCHEMES: readonly ["http", "https"];
|
|
14
|
+
/** A scheme of the HTTP request of a check. */
|
|
15
|
+
type Scheme = (typeof SCHEMES)[number];
|
|
16
|
+
/** The options of the command `check`. Each field is a flag of the command line. */
|
|
17
|
+
interface CheckOptions {
|
|
18
|
+
/** The host to check: a DNS name, an IP address, `host:port` or a URL. */
|
|
19
|
+
readonly target: string;
|
|
20
|
+
/** The port of the HTTP request. The default is the port in the target, then the port of the scheme. */
|
|
21
|
+
readonly port?: number | undefined;
|
|
22
|
+
/** The scheme of the HTTP request. The default is the scheme of a URL target, then `https`. */
|
|
23
|
+
readonly scheme?: Scheme | undefined;
|
|
24
|
+
/** The path of the HTTP request. The default is the path of a URL target, then `/`. */
|
|
25
|
+
readonly path?: string | undefined;
|
|
26
|
+
/** The hosts that work, for a comparison with the target. */
|
|
27
|
+
readonly reference?: readonly string[] | undefined;
|
|
28
|
+
/** The address of the DNS resolver, with an optional port. The default is the first resolver of the computer. */
|
|
29
|
+
readonly resolver?: string | undefined;
|
|
30
|
+
/** The IP address of the default gateway. Without it, the check does not ping a gateway. */
|
|
31
|
+
readonly gateway?: string | undefined;
|
|
32
|
+
/** The CIDR ranges that the user owns. The check can probe each address in them. */
|
|
33
|
+
readonly grant?: readonly string[] | undefined;
|
|
34
|
+
/** The number of echo requests to each address. The default is `DEFAULT_PING_COUNT`. */
|
|
35
|
+
readonly pingCount?: number | undefined;
|
|
36
|
+
}
|
|
37
|
+
/** The parts of the computer that a plan reads. */
|
|
38
|
+
interface PlanSystem {
|
|
39
|
+
/** The DNS resolvers of the computer, in the form of `getServers()` of `node:dns`. */
|
|
40
|
+
readonly resolvers: readonly string[];
|
|
41
|
+
}
|
|
42
|
+
/** The plan of a check: the target, the HTTP request, the number of echo requests, and the input of the visit. */
|
|
43
|
+
interface CheckPlan {
|
|
44
|
+
/** The target, with the port of the HTTP request when the user gives one. */
|
|
45
|
+
readonly target: Target;
|
|
46
|
+
readonly scheme: Scheme;
|
|
47
|
+
readonly path: string;
|
|
48
|
+
readonly pingCount: number;
|
|
49
|
+
/** The input of `visitTarget`, with the grants of the scope. */
|
|
50
|
+
readonly visit: VisitInput;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* This function gives the plan of a check from the options of the command line, or each problem of the options.
|
|
54
|
+
*
|
|
55
|
+
* The grants of the plan are the scope rule of the command. The plan grants only these hosts:
|
|
56
|
+
*
|
|
57
|
+
* - the target, and each reference host, with the basis `named`
|
|
58
|
+
* - the DNS resolver, with the basis `named` from `--resolver`, or `configured` from the computer
|
|
59
|
+
* - the gateway, when the user gives it, with the basis `named`
|
|
60
|
+
* - each range of `--grant`, with the basis `owned`
|
|
61
|
+
*
|
|
62
|
+
* The visit also puts in the scope the addresses that the resolver gives for a named host.
|
|
63
|
+
*/
|
|
64
|
+
export declare function planCheck(options: CheckOptions, system: PlanSystem): Result<CheckPlan, readonly string[]>;
|
|
65
|
+
/** A probe of a visit: the DNS query, the ICMP echo requests, the TLS handshake, or the HTTP request. */
|
|
66
|
+
type NextProbeKind = "dns" | "ping" | "tls" | "http";
|
|
67
|
+
/** A probe that the next step can start, for one role of the visit. */
|
|
68
|
+
interface NextProbe {
|
|
69
|
+
/** The id of the candidate: the role and the probe, for example `gateway.ping`. */
|
|
70
|
+
readonly id: string;
|
|
71
|
+
readonly role: VisitRole;
|
|
72
|
+
readonly probe: NextProbeKind;
|
|
73
|
+
/** The fact kinds that the probe gives. A TLS handshake and an HTTP request also give the TCP connect before them. */
|
|
74
|
+
readonly kinds: readonly string[];
|
|
75
|
+
/** An approximate time of the probe, in seconds. When two gains are equal, the lower cost comes first. */
|
|
76
|
+
readonly cost: number;
|
|
77
|
+
}
|
|
78
|
+
/** The next step of a check: the instruction in plain words, its information gain, and the features that it observes. */
|
|
79
|
+
interface NextStep {
|
|
80
|
+
/** The id of the probe of the step, for example `gateway.ping`. */
|
|
81
|
+
readonly id: string;
|
|
82
|
+
/** The instruction to the user. */
|
|
83
|
+
readonly step: string;
|
|
84
|
+
/** The expected information gain over the most probable causes, in bits. */
|
|
85
|
+
readonly gain: number;
|
|
86
|
+
/** The features that the step observes and that the evidence does not have. */
|
|
87
|
+
readonly observes: readonly Observable[];
|
|
88
|
+
}
|
|
89
|
+
/** The number of the most probable causes that the report shows, and that the gain of a next step tells apart. */
|
|
90
|
+
export declare const TOP_CAUSES = 3;
|
|
91
|
+
/**
|
|
92
|
+
* The candidates of the next step: each probe of the target and of the reference hosts, and the ping of the gateway.
|
|
93
|
+
* The TLS handshake and the HTTP request are two candidates, one for each stage. Each stage has its own conditions in
|
|
94
|
+
* `ready`. One candidate for the whole HTTPS request is the next change of this list.
|
|
95
|
+
*/
|
|
96
|
+
export declare const NEXT_PROBES: readonly NextProbe[];
|
|
97
|
+
/** The instruction to add a reference host. */
|
|
98
|
+
export declare const ADD_REFERENCE = "Add a host that works for you with --reference <host>, for example a large web site.";
|
|
99
|
+
/** The instruction to add the gateway. */
|
|
100
|
+
export declare const ADD_GATEWAY = "Give the address of your router with --gateway <address>.";
|
|
101
|
+
/** This function gives the instruction of a next step in plain words. */
|
|
102
|
+
export declare function stepText(probe: NextProbe, observes: readonly Observable[], plan: CheckPlan): string;
|
|
103
|
+
/**
|
|
104
|
+
* This function gives the next steps of a check, from the largest information gain. The first step is the next step.
|
|
105
|
+
*
|
|
106
|
+
* - A candidate observes the features of its fact kinds that the evidence does not have, with the role of the probe.
|
|
107
|
+
* A feature after a stage that gave another value is not observable: no address follows an answer `nxdomain`.
|
|
108
|
+
* - A probe that cannot give its facts is not a candidate. `addressed` has the roles with an address to probe: an IP
|
|
109
|
+
* address, or an address of a DNS answer.
|
|
110
|
+
* - A step with a gain of `GAIN_RESOLUTION` or less tells nothing, so the list does not have it.
|
|
111
|
+
* - Two probes with the same instruction give one step, with the larger gain. For example, each probe of a reference
|
|
112
|
+
* host that the user did not give has the instruction `ADD_REFERENCE`.
|
|
113
|
+
*/
|
|
114
|
+
export declare function nextSteps(diagnosis: Diagnosis, plan: CheckPlan, addressed: ReadonlySet<VisitRole>): NextStep[];
|
|
115
|
+
/** The format of the JSON report of a check. A change to the shape of `CheckReport` changes the format. */
|
|
116
|
+
export declare const CHECK_FORMAT = "netdiag-check/1";
|
|
117
|
+
/** A fact of a visit, with the role and the host of the step that gave it. */
|
|
118
|
+
interface CheckFact {
|
|
119
|
+
readonly role: VisitRole;
|
|
120
|
+
readonly host: string;
|
|
121
|
+
readonly fact: Fact;
|
|
122
|
+
}
|
|
123
|
+
/** A problem of a visit: a probe that did not end, with the role and the host of the step. */
|
|
124
|
+
interface CheckProblem {
|
|
125
|
+
readonly role: VisitRole;
|
|
126
|
+
readonly host: string;
|
|
127
|
+
readonly problem: string;
|
|
128
|
+
}
|
|
129
|
+
/** The report of a check. `--json` writes this object. */
|
|
130
|
+
interface CheckReport {
|
|
131
|
+
readonly format: typeof CHECK_FORMAT;
|
|
132
|
+
/** The target, as `host` or `host:port`. */
|
|
133
|
+
readonly target: string;
|
|
134
|
+
readonly scheme: Scheme;
|
|
135
|
+
readonly path: string;
|
|
136
|
+
/** The grants of the scope of the check. */
|
|
137
|
+
readonly grants: readonly Grant[];
|
|
138
|
+
/** Each cause, from the most probable. */
|
|
139
|
+
readonly causes: readonly Cause[];
|
|
140
|
+
/** The next step, or `null` when no probe can tell more about the most probable causes. */
|
|
141
|
+
readonly next: NextStep | null;
|
|
142
|
+
/** Each next step with a gain, from the largest gain. The first one is `next`. */
|
|
143
|
+
readonly choices: readonly NextStep[];
|
|
144
|
+
readonly problems: readonly CheckProblem[];
|
|
145
|
+
/** The evidence that the diagnosis used. A fact of the role `target` gives primary evidence. */
|
|
146
|
+
readonly evidence: readonly Evidence[];
|
|
147
|
+
/** The evidence that the diagnosis did not use, with the reason. */
|
|
148
|
+
readonly ignored: Diagnosis["ignored"];
|
|
149
|
+
/** The facts that gave no evidence, with the reason. */
|
|
150
|
+
readonly skipped: readonly Skipped[];
|
|
151
|
+
readonly facts: readonly CheckFact[];
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* This function gives the report of the steps of a visit.
|
|
155
|
+
*
|
|
156
|
+
* - A fact of the role `target` gives primary evidence. A fact of each other role gives reference evidence.
|
|
157
|
+
* - The report finds the facts of the target by their role, not by their position. Thus a first step of the role
|
|
158
|
+
* `local` is not a problem.
|
|
159
|
+
* - The causes come from `diagnose` with `STANDARD_CATALOG`. The next steps come from `nextSteps`.
|
|
160
|
+
*/
|
|
161
|
+
export declare function reportOf(plan: CheckPlan, steps: readonly VisitStep[]): CheckReport;
|
|
162
|
+
/** This function gives a probability as a percent for a person: `<1%` for a probability above 0 that rounds to 0. */
|
|
163
|
+
export declare function percentOf(probability: number): string;
|
|
164
|
+
/**
|
|
165
|
+
* This function gives the role and the host of a step for a person. A step of the role `local` is a passive read of
|
|
166
|
+
* this computer, so its text is `this computer`.
|
|
167
|
+
*/
|
|
168
|
+
export declare function subjectText(role: string, host: string): string;
|
|
169
|
+
/**
|
|
170
|
+
* This function gives the lines of the text report of a check. The lines show the most probable causes with their
|
|
171
|
+
* probabilities and the next step with its gain. They also show the problems of the visit and the count of the evidence.
|
|
172
|
+
*/
|
|
173
|
+
export declare function formatReport(report: CheckReport): string[];
|
|
174
|
+
/** The parts of the computer that a check uses. A test gives its own parts. */
|
|
175
|
+
interface CheckDeps {
|
|
176
|
+
readonly clock: Clock;
|
|
177
|
+
/** The host of the outside tools. Without it, the check starts no `ping`. */
|
|
178
|
+
readonly tools?: ToolHost | undefined;
|
|
179
|
+
/** The DNS resolvers of the computer, in the form of `getServers()` of `node:dns`. */
|
|
180
|
+
readonly resolvers: readonly string[];
|
|
181
|
+
/** The trust stores of the TLS handshake. The default is the roots of Node.js. */
|
|
182
|
+
readonly tls?: TlsProbeOptions | undefined;
|
|
183
|
+
readonly signal?: AbortSignal | undefined;
|
|
184
|
+
}
|
|
185
|
+
/** This function gives the parts of this computer: the monotonic clock, `runCommand` of `@procedures/shell` and the DNS resolvers. */
|
|
186
|
+
export declare function systemDeps(): CheckDeps;
|
|
187
|
+
/**
|
|
188
|
+
* This function plans a check, starts its visit, and gives the report.
|
|
189
|
+
*
|
|
190
|
+
* It throws a `RangeError` with each problem of the options before the first probe.
|
|
191
|
+
*/
|
|
192
|
+
export declare function check(options: CheckOptions, deps: CheckDeps): Promise<CheckReport>;
|
|
193
|
+
/** The name of the program. */
|
|
194
|
+
export declare const PROGRAM = "netdiag";
|
|
195
|
+
/** The options of `createNetdiagCli`. */
|
|
196
|
+
interface NetdiagCliOptions {
|
|
197
|
+
/** The function that operates one check. A test gives a fake. */
|
|
198
|
+
readonly check: (options: CheckOptions, signal: AbortSignal | undefined) => Promise<CheckReport>;
|
|
199
|
+
/** The version that `--version` shows. */
|
|
200
|
+
readonly version?: string | undefined;
|
|
201
|
+
/** The output of the program. The default writes plain text to the console. */
|
|
202
|
+
readonly print?: Print | undefined;
|
|
203
|
+
/** The terminal state. The program asks for a missing target only in a terminal. */
|
|
204
|
+
readonly terminal?: Terminal | undefined;
|
|
205
|
+
}
|
|
206
|
+
/** This function makes the command line of netdiag with the command `check`. */
|
|
207
|
+
export declare function createNetdiagCli(options: NetdiagCliOptions): ProcedureCli;
|
|
208
|
+
/** This function gives the version in `package.json` of this package. */
|
|
209
|
+
export declare function packageVersion(): string;
|
|
210
|
+
/** This function operates the command line on this computer, and gives the exit code. */
|
|
211
|
+
export declare function main(argv: readonly string[]): Promise<number>;
|
|
212
|
+
export type { CheckDeps, CheckFact, CheckOptions, CheckPlan, CheckProblem, CheckReport, NetdiagCliOptions, NextProbe, NextProbeKind, NextStep, PlanSystem, Scheme };
|
|
213
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/plan.ts","../src/next.ts","../src/report.ts","../src/check.ts","../src/cli.ts"],"mappings":";;;;;;;;;;;;qBAmBa;;KAGD,iBAAiB;;UAGZ;;WAEN;;WAEA;;WAEA,SAAS;;WAET;;WAEA;;WAEA;;WAEA;;WAEA;;WAEA;;;UAIM;;WAEN;;;UAIM;;WAEN,QAAQ;WACR,QAAQ;WACR;WACA;;WAEA,OAAO;;;;;;;;;;;;;;wBAoDF,UAAU,SAAS,cAAc,QAAQ,aAAa,OAAO;;KCtFjE;;UAGK;;WAEN;WACA,MAAM;WACN,OAAO;;WAEP;;WAEA;;;UAIM;;WAEN;;WAEA;;WAEA;;WAEA,mBAAmB;;;qBAIjB;;;;;;qBAsBA,sBAAsB;;qBAMtB;;qBAGA;;wBAsDG,SAAS,OAAO,WAAW,mBAAmB,cAAc,MAAM;;;;;;;;;;;;wBAiClE,UAAU,WAAW,WAAW,MAAM,WAAW,WAAW,YAAY,aAAa;;qBCjJxF;;UAGI;WACN,MAAM;WACN;WACA,MAAM;;;UAIA;WACN,MAAM;WACN;WACA;;;UAIM;WACN,eAAe;;WAEf;WACA,QAAQ;WACR;;WAEA,iBAAiB;;WAEjB,iBAAiB;;WAEjB,MAAM;;WAEN,kBAAkB;WAClB,mBAAmB;;WAEnB,mBAAmB;;WAEnB,SAAS;;WAET,kBAAkB;WAClB,gBAAgB;;;;;;;;;;wBAqBX,SAAS,MAAM,WAAW,gBAAgB,cAAc;;wBA4BxD,UAAU;;;;;wBASV,YAAY,cAAc;;;;;wBAW1B,aAAa,QAAQ;;UCzHpB;WACN,OAAO;;WAEP,QAAQ;;WAER;;WAEA,MAAM;WACN,SAAS;;;wBAIJ,cAAc;;;;;;wBASR,MAAM,SAAS,cAAc,MAAM,YAAY,QAAQ;;qBCpBhE;;UAGI;;WAEN,QAAQ,SAAS,cAAc,QAAQ,4BAA4B,QAAQ;;WAE3E;;WAEA,QAAQ;;WAER,WAAW;;;wBAsBN,iBAAiB,SAAS,oBAAoB;;wBAwB9C;;wBAOM,KAAK,0BAA0B"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { f as ADD_GATEWAY, p as ADD_REFERENCE, s as CHECK_FORMAT, m as NEXT_PROBES, t as PROGRAM, v as SCHEMES, h as TOP_CAUSES, a as check, n as createNetdiagCli, c as formatReport, r as main, g as nextSteps, i as packageVersion, l as percentOf, y as planCheck, u as reportOf, _ as stepText, d as subjectText, o as systemDeps } from "./cli-IhViZxm9.js";
|
package/package.json
CHANGED
|
@@ -1,6 +1,77 @@
|
|
|
1
1
|
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/package",
|
|
2
3
|
"name": "@mark1russell7/netdiag-cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
4
|
+
"version": "0.1.1",
|
|
5
|
+
"description": "The command line of netdiag: one command takes a target and gives the probable causes of a fault and the next step.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"network",
|
|
8
|
+
"diagnosis",
|
|
9
|
+
"cli",
|
|
10
|
+
"dns",
|
|
11
|
+
"tls",
|
|
12
|
+
"http",
|
|
13
|
+
"ping"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/mark1russell7/netdiag#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/mark1russell7/netdiag/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "git+https://github.com/mark1russell7/netdiag.git",
|
|
22
|
+
"directory": "packages/netdiag-cli"
|
|
23
|
+
},
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Mark Russell (https://github.com/mark1russell7)",
|
|
26
|
+
"type": "module",
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"main": "./build/index.js",
|
|
29
|
+
"types": "./build/index.d.ts",
|
|
30
|
+
"bin": {
|
|
31
|
+
"netdiag": "./build/bin.js"
|
|
32
|
+
},
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./build/index.d.ts",
|
|
36
|
+
"default": "./build/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./package.json": "./package.json"
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"build",
|
|
42
|
+
"src",
|
|
43
|
+
"!src/**/*.test.ts"
|
|
44
|
+
],
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=22.12"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"devDependencies": {
|
|
52
|
+
"@mark1russell7/cue": "^2.0.0",
|
|
53
|
+
"@netdiag/fixtures": "0.1.0",
|
|
54
|
+
"@types/node": "^26.6.5",
|
|
55
|
+
"@vitest/coverage-v8": "^5.0.3",
|
|
56
|
+
"fast-check": "^4.10.2",
|
|
57
|
+
"tsdown": "^0.23.7",
|
|
58
|
+
"typescript": "^7.0.2",
|
|
59
|
+
"vitest": "^5.0.3"
|
|
60
|
+
},
|
|
61
|
+
"dependencies": {
|
|
62
|
+
"@mark1russell7/algebra": "^0.8.10",
|
|
63
|
+
"@mark1russell7/netdiag": "0.1.6",
|
|
64
|
+
"@mark1russell7/optional": "^1.2.0",
|
|
65
|
+
"@mark1russell7/text": "^0.0.6",
|
|
66
|
+
"@procedures/cli-kit": "^0.57.0",
|
|
67
|
+
"@procedures/client": "^0.57.0",
|
|
68
|
+
"@procedures/shell": "^0.57.0",
|
|
69
|
+
"zod": "^4.6.7"
|
|
70
|
+
},
|
|
71
|
+
"scripts": {
|
|
72
|
+
"build": "tsdown",
|
|
73
|
+
"test": "vitest run",
|
|
74
|
+
"test:watch": "vitest",
|
|
75
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tsconfig.test.json"
|
|
76
|
+
}
|
|
6
77
|
}
|
package/src/bin.ts
ADDED
package/src/check.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { getServers } from "node:dns";
|
|
2
|
+
import { realClock, type Clock } from "@mark1russell7/algebra/clock";
|
|
3
|
+
import { visitTarget, type TlsProbeOptions, type VisitStep } from "@mark1russell7/netdiag";
|
|
4
|
+
import { commandHost, type ToolHost } from "@mark1russell7/netdiag/tools";
|
|
5
|
+
import { runCommand } from "@procedures/shell/command";
|
|
6
|
+
import { planCheck, type CheckOptions } from "./plan.ts";
|
|
7
|
+
import { reportOf, type CheckReport } from "./report.ts";
|
|
8
|
+
|
|
9
|
+
// The edge of a check: the visit sends the packets, and starts the outside tools. A test gives a fake tool host and
|
|
10
|
+
// servers on the loopback addresses.
|
|
11
|
+
|
|
12
|
+
/** The parts of the computer that a check uses. A test gives its own parts. */
|
|
13
|
+
export interface CheckDeps {
|
|
14
|
+
readonly clock: Clock;
|
|
15
|
+
/** The host of the outside tools. Without it, the check starts no `ping`. */
|
|
16
|
+
readonly tools?: ToolHost | undefined;
|
|
17
|
+
/** The DNS resolvers of the computer, in the form of `getServers()` of `node:dns`. */
|
|
18
|
+
readonly resolvers: readonly string[];
|
|
19
|
+
/** The trust stores of the TLS handshake. The default is the roots of Node.js. */
|
|
20
|
+
readonly tls?: TlsProbeOptions | undefined;
|
|
21
|
+
readonly signal?: AbortSignal | undefined;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** This function gives the parts of this computer: the monotonic clock, `runCommand` of `@procedures/shell` and the DNS resolvers. */
|
|
25
|
+
export function systemDeps(): CheckDeps {
|
|
26
|
+
return { clock: realClock, tools: commandHost(runCommand, process.platform), resolvers: getServers() };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* This function plans a check, starts its visit, and gives the report.
|
|
31
|
+
*
|
|
32
|
+
* It throws a `RangeError` with each problem of the options before the first probe.
|
|
33
|
+
*/
|
|
34
|
+
export async function check(options: CheckOptions, deps: CheckDeps): Promise<CheckReport> {
|
|
35
|
+
const planned = planCheck(options, { resolvers: deps.resolvers });
|
|
36
|
+
if (planned.tag === "err") throw new RangeError(planned.error.join("\n"));
|
|
37
|
+
const plan = planned.value;
|
|
38
|
+
const steps: VisitStep[] = [];
|
|
39
|
+
const visit = visitTarget(plan.visit, {
|
|
40
|
+
clock: deps.clock,
|
|
41
|
+
tools: deps.tools,
|
|
42
|
+
pingCount: plan.pingCount,
|
|
43
|
+
scheme: plan.scheme,
|
|
44
|
+
path: plan.path,
|
|
45
|
+
tls: deps.tls,
|
|
46
|
+
signal: deps.signal,
|
|
47
|
+
});
|
|
48
|
+
for await (const step of visit) steps.push(step);
|
|
49
|
+
return reportOf(plan, steps);
|
|
50
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { DEFAULT_PING_COUNT, MAX_PING_COUNT } from "@mark1russell7/netdiag/tools";
|
|
3
|
+
import { createProcedureCli, type Print, type ProcedureCli, type Terminal } from "@procedures/cli-kit";
|
|
4
|
+
import { defineProcedure } from "@procedures/client";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
import { check, systemDeps } from "./check.ts";
|
|
7
|
+
import { SCHEMES, type CheckOptions } from "./plan.ts";
|
|
8
|
+
import { formatReport, type CheckReport } from "./report.ts";
|
|
9
|
+
|
|
10
|
+
// The command line of netdiag. `@procedures/cli-kit` makes the flags from the input schema of the command `check`, and
|
|
11
|
+
// gives the help, `--json` and the exit code.
|
|
12
|
+
|
|
13
|
+
/** The name of the program. */
|
|
14
|
+
export const PROGRAM = "netdiag";
|
|
15
|
+
|
|
16
|
+
/** The options of `createNetdiagCli`. */
|
|
17
|
+
export interface NetdiagCliOptions {
|
|
18
|
+
/** The function that operates one check. A test gives a fake. */
|
|
19
|
+
readonly check: (options: CheckOptions, signal: AbortSignal | undefined) => Promise<CheckReport>;
|
|
20
|
+
/** The version that `--version` shows. */
|
|
21
|
+
readonly version?: string | undefined;
|
|
22
|
+
/** The output of the program. The default writes plain text to the console. */
|
|
23
|
+
readonly print?: Print | undefined;
|
|
24
|
+
/** The terminal state. The program asks for a missing target only in a terminal. */
|
|
25
|
+
readonly terminal?: Terminal | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const CHECK_INPUT = z.object({
|
|
29
|
+
target: z.string().min(1).describe("The host to check: a DNS name, an IP address, host:port or a URL"),
|
|
30
|
+
port: z.number().int().min(1).max(65535).optional().describe("The port of the HTTP request. The default is the port of the scheme"),
|
|
31
|
+
scheme: z.enum(SCHEMES).optional().describe("The scheme of the HTTP request: http or https. The default is https"),
|
|
32
|
+
path: z.string().optional().describe("The path of the HTTP request. The default is /"),
|
|
33
|
+
reference: z.array(z.string()).optional().describe("A host that works, for a comparison. Give the flag again for more hosts"),
|
|
34
|
+
resolver: z.string().optional().describe("The address of the DNS resolver. The default is the first resolver of this computer"),
|
|
35
|
+
gateway: z.string().optional().describe("The IP address of the default gateway. The check pings it"),
|
|
36
|
+
grant: z.array(z.string()).optional().describe("A CIDR range that you own. The check can probe each address in it"),
|
|
37
|
+
pingCount: z
|
|
38
|
+
.number()
|
|
39
|
+
.int()
|
|
40
|
+
.min(0)
|
|
41
|
+
.max(MAX_PING_COUNT)
|
|
42
|
+
.optional()
|
|
43
|
+
.describe(`The number of echo requests to each address. The default is ${DEFAULT_PING_COUNT}`),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
/** This function makes the command line of netdiag with the command `check`. */
|
|
47
|
+
export function createNetdiagCli(options: NetdiagCliOptions): ProcedureCli {
|
|
48
|
+
const procedure = defineProcedure({
|
|
49
|
+
path: ["check"],
|
|
50
|
+
input: CHECK_INPUT,
|
|
51
|
+
output: z.custom<CheckReport>(),
|
|
52
|
+
metadata: {
|
|
53
|
+
description: "Check a target, and show the probable causes of a fault and the next step",
|
|
54
|
+
args: ["target"],
|
|
55
|
+
effects: { reads: ["network"], writes: ["process"], destructive: false },
|
|
56
|
+
},
|
|
57
|
+
handler: (input, context) => options.check(input, context.signal),
|
|
58
|
+
});
|
|
59
|
+
return createProcedureCli({
|
|
60
|
+
name: PROGRAM,
|
|
61
|
+
version: options.version,
|
|
62
|
+
description: "the probable causes of a network fault, and the next step",
|
|
63
|
+
procedures: [procedure],
|
|
64
|
+
format: { check: (result) => formatReport(result as CheckReport) },
|
|
65
|
+
print: options.print,
|
|
66
|
+
terminal: options.terminal,
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** This function gives the version in `package.json` of this package. */
|
|
71
|
+
export function packageVersion(): string {
|
|
72
|
+
const manifest: unknown = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
73
|
+
const version = typeof manifest === "object" && manifest !== null && "version" in manifest ? manifest.version : undefined;
|
|
74
|
+
return typeof version === "string" ? version : "0.0.0";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** This function operates the command line on this computer, and gives the exit code. */
|
|
78
|
+
export async function main(argv: readonly string[]): Promise<number> {
|
|
79
|
+
const cli = createNetdiagCli({ version: packageVersion(), check: (options, signal) => check(options, { ...systemDeps(), signal }) });
|
|
80
|
+
return cli.run(argv);
|
|
81
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The entry of `@mark1russell7/netdiag-cli`: the command line of netdiag.
|
|
3
|
+
*
|
|
4
|
+
* The program `netdiag` has the command `check`. The pure parts are also exports. They are the plan of a check with
|
|
5
|
+
* its scope rule, the report, the text of the report and the choice of the next step.
|
|
6
|
+
*/
|
|
7
|
+
export { SCHEMES, planCheck, type CheckOptions, type CheckPlan, type PlanSystem, type Scheme } from "./plan.ts";
|
|
8
|
+
export { ADD_GATEWAY, ADD_REFERENCE, NEXT_PROBES, TOP_CAUSES, nextSteps, stepText, type NextProbe, type NextProbeKind, type NextStep } from "./next.ts";
|
|
9
|
+
export { CHECK_FORMAT, formatReport, percentOf, reportOf, subjectText, type CheckFact, type CheckProblem, type CheckReport } from "./report.ts";
|
|
10
|
+
export { check, systemDeps, type CheckDeps } from "./check.ts";
|
|
11
|
+
export { PROGRAM, createNetdiagCli, main, packageVersion, type NetdiagCliOptions } from "./cli.ts";
|