| 1 | |
| 2 | |
| 3 | const fs = require("node:fs"); |
| 4 | const net = require("node:net"); |
| 5 | const dgram = require("node:dgram"); |
| 6 | const dns = require("node:dns").promises; |
| 7 | const http = require("node:http"); |
| 8 | const { execFileSync, execFile, spawnSync } = require("node:child_process"); |
| 9 | const runFile = require("node:util").promisify(execFile); |
| 10 | const input = JSON.parse(Buffer.from(process.argv[1], "base64").toString()); |
| 11 | function read(command, args) { |
| 12 | try { |
| 13 | return execFileSync(command, args, { |
| 14 | timeout: 2500, |
| 15 | encoding: "utf8", |
| 16 | stdio: ["ignore", "pipe", "ignore"], |
| 17 | }).trim(); |
| 18 | } catch { |
| 19 | return ""; |
| 20 | } |
| 21 | } |
| 22 | function tcp(host, port) { |
| 23 | return new Promise((resolve) => { |
| 24 | const socket = net.connect({ host, port }); |
| 25 | const finish = (value) => { |
| 26 | socket.destroy(); |
| 27 | resolve(value); |
| 28 | }; |
| 29 | socket.setTimeout(1800, () => finish(false)); |
| 30 | socket.once("connect", () => finish(true)); |
| 31 | socket.once("error", () => finish(false)); |
| 32 | }); |
| 33 | } |
| 34 | function udp() { |
| 35 | return new Promise((resolve) => { |
| 36 | const socket = dgram.createSocket("udp4"); |
| 37 | socket.send( |
| 38 | Buffer.from("Cage network boundary probe"), |
| 39 | 3478, |
| 40 | "1.1.1.1", |
| 41 | (err) => { |
| 42 | socket.close(); |
| 43 | resolve(!err); |
| 44 | }, |
| 45 | ); |
| 46 | }); |
| 47 | } |
| 48 | function probe(path) { |
| 49 | return new Promise((resolve) => { |
| 50 | const req = http.get( |
| 51 | { host: "198.18.0.254", port: 80, path, agent: false }, |
| 52 | (res) => { |
| 53 | let data = ""; |
| 54 | res.on("data", (chunk) => { |
| 55 | data += chunk; |
| 56 | if (data.length > 4096) req.destroy(new Error("oversized probe")); |
| 57 | }); |
| 58 | res.on("error", () => resolve({ status: 0 })); |
| 59 | res.on("end", () => { |
| 60 | try { |
| 61 | const body = JSON.parse(data); |
| 62 | resolve({ status: res.statusCode, ip: body.ip || "" }); |
| 63 | } catch { |
| 64 | resolve({ status: res.statusCode }); |
| 65 | } |
| 66 | }); |
| 67 | }, |
| 68 | ); |
| 69 | req.setTimeout(16000, () => req.destroy(new Error("probe timeout"))); |
| 70 | req.once("error", () => resolve({ status: 0 })); |
| 71 | }); |
| 72 | } |
| 73 | (async () => { |
| 74 | const result = {}; |
| 75 | if (input.stage === "boundary" || input.stage === "environment") { |
| 76 | result.tz = Intl.DateTimeFormat().resolvedOptions().timeZone; |
| 77 | result.process_locale = Intl.DateTimeFormat().resolvedOptions().locale; |
| 78 | result.system_locale = read("/usr/bin/defaults", [ |
| 79 | "read", |
| 80 | "-g", |
| 81 | "AppleLocale", |
| 82 | ]).split("@")[0]; |
| 83 | const languages = read("/usr/bin/defaults", ["read", "-g", "AppleLanguages"]); |
| 84 | if (languages) { |
| 85 | const primary = spawnSync("/usr/bin/plutil", ["-extract", "0", "raw", "-o", "-", "-"], { |
| 86 | input: languages, encoding: "utf8", timeout: 2500, |
| 87 | }); |
| 88 | if (primary.status === 0) result.system_language = primary.stdout.trim(); |
| 89 | } |
| 90 | try { |
| 91 | const zone = fs.readlinkSync("/etc/localtime").split("/zoneinfo/")[1]; |
| 92 | if (zone) result.system_timezone = zone; |
| 93 | } catch {} |
| 94 | } |
| 95 | if (input.stage === "boundary") { |
| 96 | [ |
| 97 | result.public_connected, |
| 98 | result.management_connected, |
| 99 | result.ipv6_connected, |
| 100 | result.udp_attempted, |
| 101 | ] = await Promise.all([ |
| 102 | tcp("1.1.1.1", 443), |
| 103 | tcp(input.management_host, input.control_port), |
| 104 | tcp("2606:4700:4700::1111", 443), |
| 105 | udp(), |
| 106 | ]); |
| 107 | const routes = read("/usr/sbin/netstat", ["-rn", "-f", "inet6"]); |
| 108 | result.ipv6_default_routes = /Internet6:/.test(routes) |
| 109 | ? (routes.match(/^default\s+/gm) || []).length |
| 110 | : -1; |
| 111 | const helper = "/usr/local/libexec/cage-macos-ipv6-check"; |
| 112 | const lookup = spawnSync( |
| 113 | "/sbin/route", |
| 114 | ["-n", "get", "-inet6", "2606:4700:4700::1111"], |
| 115 | { encoding: "utf8", timeout: 2000 }, |
| 116 | ); |
| 117 | result.ipv6_unscoped_absent = |
| 118 | !lookup.error && |
| 119 | /not in table|network is unreachable/i.test( |
| 120 | lookup.stderr + " " + lookup.stdout, |
| 121 | ); |
| 122 | const rows = routes.split("\n").filter((row) => /^default\s/.test(row)); |
| 123 | result.ipv6_scoped = await Promise.all( |
| 124 | rows.slice(0, 16).map(async (row) => { |
| 125 | const [, gateway, flags, name] = row.trim().split(/\s+/); |
| 126 | if ( |
| 127 | !/^utun[0-9]+$/.test(name) || |
| 128 | !flags.includes("I") || |
| 129 | !gateway.startsWith("fe80:") |
| 130 | ) |
| 131 | return { name }; |
| 132 | const detail = read("/sbin/route", [ |
| 133 | "-n", |
| 134 | "get", |
| 135 | "-inet6", |
| 136 | "-ifscope", |
| 137 | name, |
| 138 | "2606:4700:4700::1111", |
| 139 | ]); |
| 140 | try { |
| 141 | const { stdout } = await runFile(helper, [name], { timeout: 2500 }); |
| 142 | const data = JSON.parse(stdout); |
| 143 | return { |
| 144 | name, |
| 145 | routeVerified: new RegExp( |
| 146 | "interface:\\s*" + name + "\\s*$", |
| 147 | "m", |
| 148 | ).test(detail), |
| 149 | result: data.result, |
| 150 | globalSources: data.globalSources, |
| 151 | boundIndex: data.boundIndex, |
| 152 | }; |
| 153 | } catch { |
| 154 | return { name }; |
| 155 | } |
| 156 | }), |
| 157 | ); |
| 158 | try { |
| 159 | result.ipv6_unbound = JSON.parse(read(helper, ["default"])); |
| 160 | } catch {} |
| 161 | const iface = read("/sbin/route", [ |
| 162 | "-n", |
| 163 | "get", |
| 164 | "-inet", |
| 165 | "198.18.0.254", |
| 166 | ]).match(/interface:\s*(en[0-9]+)\s*$/m)?.[1]; |
| 167 | if (iface && input.run_id && input.check_id) { |
| 168 | result.ipv6_link_interface = iface; |
| 169 | result.ipv6_link_sent = |
| 170 | read(helper, ["link", iface, input.run_id, input.check_id]) === "sent"; |
| 171 | } |
| 172 | try { |
| 173 | await dns.lookup(input.nonce + ".example.com"); |
| 174 | result.public_dns_answered = true; |
| 175 | } catch { |
| 176 | result.public_dns_answered = false; |
| 177 | } |
| 178 | } |
| 179 | if (input.stage === "network") { |
| 180 | [result.egress, result.dns] = await Promise.all([ |
| 181 | probe("/egress"), |
| 182 | probe("/dns"), |
| 183 | ]); |
| 184 | } |
| 185 | console.log(JSON.stringify(result)); |
| 186 | })().catch(() => process.exit(1)); |