@takodotid/circuit 0.9.0 → 0.11.0
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 +3 -2
- package/dist/chunks/index-ex1wcst7.js +109 -0
- package/dist/chunks/index-fq57hya4.js +20 -0
- package/dist/chunks/new-7n8qvtjd.js +168 -0
- package/dist/cli/index.js +4079 -0
- package/dist/index.js +48 -0
- package/dist/presets/index.js +269 -0
- package/dist/types/adapters/devices/catalog.d.ts +58 -0
- package/dist/types/adapters/devices/derive.d.ts +5 -0
- package/dist/types/adapters/devices/index.d.ts +3 -0
- package/dist/types/adapters/devices/lines.d.ts +56 -0
- package/dist/types/adapters/devices/raisecom-ros/index.d.ts +23 -0
- package/dist/types/adapters/devices/raisecom-ros/models.d.ts +14 -0
- package/dist/types/adapters/devices/routeros/export.d.ts +39 -0
- package/dist/types/adapters/devices/routeros/index.d.ts +17 -0
- package/dist/types/adapters/devices/routeros/models.d.ts +14 -0
- package/dist/types/adapters/devices/routeros/plan.d.ts +4 -0
- package/dist/types/adapters/devices/routeros/render.d.ts +96 -0
- package/dist/types/adapters/devices/types.d.ts +34 -0
- package/dist/types/adapters/devices/vrp/index.d.ts +24 -0
- package/dist/types/adapters/devices/vrp/models.d.ts +12 -0
- package/dist/types/adapters/registries/index.d.ts +3 -0
- package/dist/types/adapters/registries/peeringdb.d.ts +16 -0
- package/dist/types/core/addr.d.ts +24 -0
- package/dist/types/core/define.d.ts +50 -0
- package/dist/types/core/merge.d.ts +4 -0
- package/dist/types/core/secrets.d.ts +21 -0
- package/dist/types/core/time.d.ts +2 -0
- package/dist/types/core/validate.d.ts +2 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/presets/index.d.ts +101 -0
- package/dist/types/schema/common.d.ts +24 -0
- package/dist/types/schema/device.d.ts +80 -0
- package/dist/types/schema/firewall.d.ts +118 -0
- package/dist/types/schema/index.d.ts +7 -0
- package/dist/types/schema/interfaces.d.ts +181 -0
- package/dist/types/schema/policy.d.ts +88 -0
- package/dist/types/schema/routing.d.ts +115 -0
- package/dist/types/schema/system.d.ts +186 -0
- package/dist/types/transport/ssh.d.ts +22 -0
- package/package.json +19 -5
- package/src/cli/index.ts +10 -2
- package/src/cli/new.ts +60 -12
- package/src/core/secrets.ts +7 -9
- package/src/presets/index.ts +17 -3
- package/templates/project/AGENTS.md +10 -10
- package/templates/project/README.md +7 -7
- package/templates/project/tsconfig.json +1 -1
package/README.md
CHANGED
|
@@ -9,10 +9,10 @@ Documentation: [circuit.tako.id](https://circuit.tako.id)
|
|
|
9
9
|
|
|
10
10
|
## Start
|
|
11
11
|
|
|
12
|
-
You need [Bun](https://bun.sh). Then:
|
|
12
|
+
You need [Node.js](https://nodejs.org) 20.12 or newer, or [Bun](https://bun.sh). Then:
|
|
13
13
|
|
|
14
14
|
```bash
|
|
15
|
-
bunx @takodotid/circuit new
|
|
15
|
+
npx @takodotid/circuit new # or: pnpm dlx @takodotid/circuit new, bunx @takodotid/circuit new
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
It asks which kind of network you have, creates a project with example devices, and installs Circuit. [Getting started](https://circuit.tako.id/guide/getting-started) walks through the first run.
|
|
@@ -25,6 +25,7 @@ Circuit supports MikroTik RouterOS 7, Huawei VRP and Raisecom ROS. Releases are
|
|
|
25
25
|
bun install
|
|
26
26
|
bun run typecheck
|
|
27
27
|
bun run test # every example and template validates, and plans nothing against its own render
|
|
28
|
+
bun run build # dist/, what Node, npm and pnpm run
|
|
28
29
|
bun run docs:dev # the documentation site, from docs/
|
|
29
30
|
```
|
|
30
31
|
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// src/core/addr.ts
|
|
2
|
+
var familyOf = (address) => address.includes(":") ? "ipv6" : "ipv4";
|
|
3
|
+
var bitsOf = (family) => family === "ipv4" ? 32 : 128;
|
|
4
|
+
function parse(address) {
|
|
5
|
+
if (!address.includes(":")) {
|
|
6
|
+
const octets = address.split(".");
|
|
7
|
+
const valid2 = octets.length === 4 && octets.every((octet) => /^\d{1,3}$/.test(octet) && Number(octet) <= 255);
|
|
8
|
+
if (!valid2)
|
|
9
|
+
return null;
|
|
10
|
+
return octets.reduce((value, octet) => value << 8n | BigInt(octet), 0n);
|
|
11
|
+
}
|
|
12
|
+
const [before, after, extra] = address.split("::");
|
|
13
|
+
if (extra !== undefined)
|
|
14
|
+
return null;
|
|
15
|
+
const groupsOf = (text) => text ? text.split(":") : [];
|
|
16
|
+
const leading = groupsOf(before);
|
|
17
|
+
const trailing = groupsOf(after);
|
|
18
|
+
const zeros = after === undefined ? 0 : 8 - leading.length - trailing.length;
|
|
19
|
+
const groups = [...leading, ...Array(zeros).fill("0"), ...trailing];
|
|
20
|
+
const valid = groups.length === 8 && groups.every((group) => /^[0-9a-fA-F]{1,4}$/.test(group));
|
|
21
|
+
if (!valid)
|
|
22
|
+
return null;
|
|
23
|
+
return groups.reduce((value, group) => value << 16n | BigInt(parseInt(group, 16)), 0n);
|
|
24
|
+
}
|
|
25
|
+
function split(cidr) {
|
|
26
|
+
const [address, lengthText] = cidr.split("/");
|
|
27
|
+
const family = familyOf(address);
|
|
28
|
+
const length = Number(lengthText);
|
|
29
|
+
const valid = parse(address) !== null && lengthText !== undefined && Number.isInteger(length) && length >= 0 && length <= bitsOf(family);
|
|
30
|
+
if (!valid)
|
|
31
|
+
throw new Error(`'${cidr}' is not an address with a length`);
|
|
32
|
+
return { addr: address, len: length, family };
|
|
33
|
+
}
|
|
34
|
+
var isAddress = (text) => parse(text) !== null;
|
|
35
|
+
function isCidr(text) {
|
|
36
|
+
try {
|
|
37
|
+
split(text);
|
|
38
|
+
return true;
|
|
39
|
+
} catch {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
var mask = (length, family) => {
|
|
44
|
+
const all = (1n << BigInt(bitsOf(family))) - 1n;
|
|
45
|
+
const hostBits = (1n << BigInt(bitsOf(family) - length)) - 1n;
|
|
46
|
+
return all ^ hostBits;
|
|
47
|
+
};
|
|
48
|
+
function format(value, family) {
|
|
49
|
+
if (family === "ipv4") {
|
|
50
|
+
return [24n, 16n, 8n, 0n].map((shift) => String(value >> shift & 255n)).join(".");
|
|
51
|
+
}
|
|
52
|
+
const groups = Array.from({ length: 8 }, (_, index) => (value >> BigInt(112 - index * 16) & 0xffffn).toString(16));
|
|
53
|
+
let longestStart = -1;
|
|
54
|
+
let longestLength = 0;
|
|
55
|
+
for (let start = 0;start < 8; ) {
|
|
56
|
+
let end = start;
|
|
57
|
+
while (end < 8 && groups[end] === "0")
|
|
58
|
+
end++;
|
|
59
|
+
const runLength = end - start;
|
|
60
|
+
if (runLength > longestLength && runLength > 1) {
|
|
61
|
+
longestStart = start;
|
|
62
|
+
longestLength = runLength;
|
|
63
|
+
}
|
|
64
|
+
start = end === start ? start + 1 : end;
|
|
65
|
+
}
|
|
66
|
+
if (longestStart < 0)
|
|
67
|
+
return groups.join(":");
|
|
68
|
+
const head = groups.slice(0, longestStart).join(":");
|
|
69
|
+
const tail = groups.slice(longestStart + longestLength).join(":");
|
|
70
|
+
return `${head}::${tail}`;
|
|
71
|
+
}
|
|
72
|
+
function network(cidr) {
|
|
73
|
+
const { addr, len, family } = split(cidr);
|
|
74
|
+
return `${format(parse(addr) & mask(len, family), family)}/${len}`;
|
|
75
|
+
}
|
|
76
|
+
function contains(outer, inner) {
|
|
77
|
+
const outerParts = split(outer);
|
|
78
|
+
const [innerAddress, innerLengthText] = inner.split("/");
|
|
79
|
+
if (familyOf(innerAddress) !== outerParts.family)
|
|
80
|
+
return false;
|
|
81
|
+
const innerLength = innerLengthText === undefined ? bitsOf(outerParts.family) : Number(innerLengthText);
|
|
82
|
+
if (innerLength < outerParts.len)
|
|
83
|
+
return false;
|
|
84
|
+
const outerMask = mask(outerParts.len, outerParts.family);
|
|
85
|
+
return (parse(innerAddress) & outerMask) === (parse(outerParts.addr) & outerMask);
|
|
86
|
+
}
|
|
87
|
+
var netmask = (length) => format(mask(length, "ipv4"), "ipv4");
|
|
88
|
+
var wildcard = (length) => format(mask(length, "ipv4") ^ 0xffffffffn, "ipv4");
|
|
89
|
+
var host = (cidr) => cidr.split("/")[0];
|
|
90
|
+
var NOT_GLOBAL_V4 = [
|
|
91
|
+
"0.0.0.0/8",
|
|
92
|
+
"10.0.0.0/8",
|
|
93
|
+
"100.64.0.0/10",
|
|
94
|
+
"127.0.0.0/8",
|
|
95
|
+
"169.254.0.0/16",
|
|
96
|
+
"172.16.0.0/12",
|
|
97
|
+
"192.0.2.0/24",
|
|
98
|
+
"192.168.0.0/16",
|
|
99
|
+
"198.18.0.0/15",
|
|
100
|
+
"198.51.100.0/24",
|
|
101
|
+
"203.0.113.0/24"
|
|
102
|
+
];
|
|
103
|
+
function isGlobal(address) {
|
|
104
|
+
if (familyOf(address) === "ipv6")
|
|
105
|
+
return contains("2000::/3", address) && !contains("2001:db8::/32", address);
|
|
106
|
+
return !NOT_GLOBAL_V4.some((prefix) => contains(prefix, address));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export { familyOf, split, isAddress, isCidr, network, contains, netmask, wildcard, host, isGlobal };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
19
|
+
|
|
20
|
+
export { __toESM, __require };
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import"./index-fq57hya4.js";
|
|
2
|
+
|
|
3
|
+
// src/cli/new.ts
|
|
4
|
+
import { spawnSync } from "node:child_process";
|
|
5
|
+
import { cpSync, existsSync, readdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
6
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
7
|
+
import { createInterface } from "node:readline/promises";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
function packageRoot() {
|
|
10
|
+
let directory = dirname(fileURLToPath(import.meta.url));
|
|
11
|
+
while (!existsSync(join(directory, "templates"))) {
|
|
12
|
+
const parent = dirname(directory);
|
|
13
|
+
if (parent === directory)
|
|
14
|
+
throw new Error("Circuit's templates are missing from its package");
|
|
15
|
+
directory = parent;
|
|
16
|
+
}
|
|
17
|
+
return directory;
|
|
18
|
+
}
|
|
19
|
+
var ROOT = packageRoot();
|
|
20
|
+
var TEMPLATES = join(ROOT, "templates");
|
|
21
|
+
var circuit = JSON.parse(readFileSync(join(ROOT, "package.json"), "utf8"));
|
|
22
|
+
var MANAGERS = {
|
|
23
|
+
npm: { install: "npm install", run: "npx circuit", types: "node" },
|
|
24
|
+
pnpm: { install: "pnpm install", run: "pnpm circuit", types: "node" },
|
|
25
|
+
bun: { install: "bun install", run: "bun circuit", types: "bun" }
|
|
26
|
+
};
|
|
27
|
+
function detectManager() {
|
|
28
|
+
const agent = process.env.npm_config_user_agent ?? "";
|
|
29
|
+
if (agent.startsWith("pnpm/"))
|
|
30
|
+
return "pnpm";
|
|
31
|
+
if (agent.startsWith("bun/") || process.versions.bun)
|
|
32
|
+
return "bun";
|
|
33
|
+
return "npm";
|
|
34
|
+
}
|
|
35
|
+
var PATTERNS = {
|
|
36
|
+
"single-site": { title: "A single site", fits: "a home, an office or an internal network: a router and a switch, no BGP", asn: false },
|
|
37
|
+
"edge-router": { title: "An edge router", fits: "your own AS number, with an IP transit and an internet exchange", asn: true },
|
|
38
|
+
colocation: {
|
|
39
|
+
title: "Colocation with tenants",
|
|
40
|
+
fits: "a provider whose customers each get their own VLANs, addresses and ports",
|
|
41
|
+
asn: true
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
var EXAMPLE_ASN = "64500";
|
|
45
|
+
var USAGE = `usage: circuit new [directory] [options]
|
|
46
|
+
|
|
47
|
+
--pattern <name> single-site, edge-router or colocation
|
|
48
|
+
--asn <number> your AS number, for edge-router and colocation
|
|
49
|
+
--1password write .env with 1Password references instead of empty values
|
|
50
|
+
--use <manager> npm, pnpm or bun; the one that ran this by default
|
|
51
|
+
--no-install do not install the dependencies`;
|
|
52
|
+
function fail(message) {
|
|
53
|
+
console.error(message);
|
|
54
|
+
process.exit(1);
|
|
55
|
+
}
|
|
56
|
+
function option(args, name) {
|
|
57
|
+
const at = args.indexOf(`--${name}`);
|
|
58
|
+
if (at >= 0)
|
|
59
|
+
return args[at + 1];
|
|
60
|
+
return args.find((arg) => arg.startsWith(`--${name}=`))?.slice(name.length + 3);
|
|
61
|
+
}
|
|
62
|
+
function filesIn(directory) {
|
|
63
|
+
return readdirSync(directory, { recursive: true, withFileTypes: true }).filter((entry) => entry.isFile()).map((entry) => relative(directory, join(entry.parentPath, entry.name)));
|
|
64
|
+
}
|
|
65
|
+
async function create(args) {
|
|
66
|
+
if (args.includes("--help") || args.includes("-h")) {
|
|
67
|
+
console.log(USAGE);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
const valued = new Set(["--pattern", "--asn", "--use"]);
|
|
71
|
+
const positional = args.filter((arg, index) => !arg.startsWith("--") && !valued.has(args[index - 1] ?? ""));
|
|
72
|
+
const interactive = process.stdin.isTTY === true;
|
|
73
|
+
const terminal = interactive ? createInterface({ input: process.stdin, output: process.stdout }) : undefined;
|
|
74
|
+
const ask = async (question, fallback) => {
|
|
75
|
+
if (!terminal)
|
|
76
|
+
return fallback;
|
|
77
|
+
const answer = (await terminal.question(`${question} (${fallback}): `)).trim();
|
|
78
|
+
return answer || fallback;
|
|
79
|
+
};
|
|
80
|
+
const directory = positional[0] ?? await ask("Directory for the project", "my-network");
|
|
81
|
+
const target = resolve(process.cwd(), directory);
|
|
82
|
+
if (existsSync(target) && readdirSync(target).length)
|
|
83
|
+
fail(`${target} is not empty`);
|
|
84
|
+
let pattern = option(args, "pattern");
|
|
85
|
+
if (!pattern) {
|
|
86
|
+
const names = Object.keys(PATTERNS);
|
|
87
|
+
if (terminal) {
|
|
88
|
+
console.log(`
|
|
89
|
+
Which is closest to your network?`);
|
|
90
|
+
names.forEach((name2, index) => console.log(` ${index + 1}. ${PATTERNS[name2].title}: ${PATTERNS[name2].fits}`));
|
|
91
|
+
}
|
|
92
|
+
pattern = names[Number(await ask("Number", "1")) - 1];
|
|
93
|
+
}
|
|
94
|
+
if (!pattern || !(pattern in PATTERNS))
|
|
95
|
+
fail(`no pattern ${pattern}; there are ${Object.keys(PATTERNS).join(", ")}`);
|
|
96
|
+
let asn = option(args, "asn");
|
|
97
|
+
if (!asn && PATTERNS[pattern].asn) {
|
|
98
|
+
asn = await ask("Your AS number, or press enter to keep the documentation example", EXAMPLE_ASN);
|
|
99
|
+
}
|
|
100
|
+
if (asn && !/^\d+$/.test(asn))
|
|
101
|
+
fail(`${asn} is not an AS number`);
|
|
102
|
+
const manager = option(args, "use") ?? detectManager();
|
|
103
|
+
if (!(manager in MANAGERS))
|
|
104
|
+
fail(`no package manager ${manager}; there are ${Object.keys(MANAGERS).join(", ")}`);
|
|
105
|
+
const { install, run, types } = MANAGERS[manager];
|
|
106
|
+
const onePassword = args.includes("--1password") || (await ask("Keep the secrets in 1Password? y or n", "n")).toLowerCase().startsWith("y");
|
|
107
|
+
terminal?.close();
|
|
108
|
+
cpSync(join(TEMPLATES, pattern), target, { recursive: true });
|
|
109
|
+
cpSync(join(TEMPLATES, "project"), target, { recursive: true });
|
|
110
|
+
renameSync(join(target, "gitignore"), join(target, ".gitignore"));
|
|
111
|
+
for (const file of ["AGENTS.md", "README.md", "tsconfig.json"]) {
|
|
112
|
+
const path = join(target, file);
|
|
113
|
+
const text = readFileSync(path, "utf8").replaceAll("npx circuit", run).replaceAll("npm install", install).replaceAll('"types": ["node"]', `"types": ["${types}"]`);
|
|
114
|
+
writeFileSync(path, text);
|
|
115
|
+
}
|
|
116
|
+
const sources = filesIn(target).filter((file) => file.endsWith(".ts"));
|
|
117
|
+
const secrets = new Set;
|
|
118
|
+
for (const file of sources) {
|
|
119
|
+
const path = join(target, file);
|
|
120
|
+
let text = readFileSync(path, "utf8");
|
|
121
|
+
if (asn && asn !== EXAMPLE_ASN)
|
|
122
|
+
text = text.replaceAll(new RegExp(`\\b${EXAMPLE_ASN}\\b`, "g"), asn);
|
|
123
|
+
for (const [, name2] of text.matchAll(/secret\("([A-Z0-9_]+)"\)/g))
|
|
124
|
+
secrets.add(name2);
|
|
125
|
+
writeFileSync(path, text);
|
|
126
|
+
}
|
|
127
|
+
const env = [
|
|
128
|
+
"# Secrets for Circuit. Never commit this file.",
|
|
129
|
+
"# A value is the secret itself, or a 1Password reference, op://vault/item/field, read with the 1Password CLI.",
|
|
130
|
+
...[...secrets].sort().map((name2) => onePassword ? `${name2}=op://Network/${name2}/password` : `${name2}=`),
|
|
131
|
+
""
|
|
132
|
+
];
|
|
133
|
+
writeFileSync(join(target, ".env"), env.join(`
|
|
134
|
+
`));
|
|
135
|
+
const name = basename(target).toLowerCase().replace(/[^a-z0-9-]+/g, "-");
|
|
136
|
+
const packageJson = {
|
|
137
|
+
name,
|
|
138
|
+
private: true,
|
|
139
|
+
type: "module",
|
|
140
|
+
dependencies: { "@takodotid/circuit": circuit.version },
|
|
141
|
+
devDependencies: { [`@types/${types}`]: circuit.devDependencies[`@types/${types}`] }
|
|
142
|
+
};
|
|
143
|
+
writeFileSync(join(target, "package.json"), JSON.stringify(packageJson, null, 4) + `
|
|
144
|
+
`);
|
|
145
|
+
if (!args.includes("--no-install")) {
|
|
146
|
+
const result = spawnSync(install, { cwd: target, stdio: "inherit", shell: true });
|
|
147
|
+
if (result.status !== 0)
|
|
148
|
+
console.log(`${install} failed; run it again in the project.`);
|
|
149
|
+
}
|
|
150
|
+
const steps = [
|
|
151
|
+
`cd ${directory}`,
|
|
152
|
+
onePassword ? `Point each line of .env at the item that holds it in 1Password, then run: ${run} secrets` : `Fill in each password in .env, then run: ${run} secrets`,
|
|
153
|
+
"Change the names, addresses and ports in the device files to your own.",
|
|
154
|
+
...PATTERNS[pattern].asn && (!asn || asn === EXAMPLE_ASN) ? ["Put your own AS number in routing.ts."] : [],
|
|
155
|
+
`${run} validate`,
|
|
156
|
+
`${run} snapshot, then ${run} diff, to see what would change on each device`
|
|
157
|
+
];
|
|
158
|
+
console.log(`
|
|
159
|
+
Created ${directory} from the pattern "${PATTERNS[pattern].title}".
|
|
160
|
+
|
|
161
|
+
Next:`);
|
|
162
|
+
steps.forEach((step, index) => console.log(` ${index + 1}. ${step}`));
|
|
163
|
+
console.log(`
|
|
164
|
+
What each file does: https://circuit.tako.id/patterns/${pattern}`);
|
|
165
|
+
}
|
|
166
|
+
export {
|
|
167
|
+
create
|
|
168
|
+
};
|