@capybara-hq/device 0.1.0-rc.7 → 0.1.0-rc.9
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 +106 -189
- package/dist/api.d.ts +5 -0
- package/dist/args.d.ts +8 -3
- package/dist/args.js +38 -19
- package/dist/cli.d.ts +7 -5
- package/dist/cli.js +20 -31
- package/dist/client.d.ts +0 -2
- package/dist/client.js +23 -12
- package/dist/command-log.d.ts +3 -5
- package/dist/commands/login.js +55 -1
- package/dist/commands/mode.d.ts +52 -0
- package/dist/commands/mode.js +108 -0
- package/dist/commands/policy.d.ts +11 -65
- package/dist/commands/policy.js +62 -249
- package/dist/commands/service.d.ts +4 -3
- package/dist/commands/service.js +30 -2
- package/dist/commands/start.d.ts +2 -2
- package/dist/commands/start.js +14 -6
- package/dist/commands/status.js +11 -3
- package/dist/exec/docker.d.ts +22 -30
- package/dist/exec/docker.js +81 -69
- package/dist/exec/fs-helper.d.ts +13 -8
- package/dist/exec/fs-helper.js +30 -12
- package/dist/exec/host.d.ts +8 -8
- package/dist/exec/host.js +9 -9
- package/dist/exec/os.d.ts +36 -69
- package/dist/exec/os.js +65 -159
- package/dist/exec/sandbox-messages.d.ts +14 -0
- package/dist/exec/sandbox-messages.js +21 -0
- package/dist/exec/shared.d.ts +21 -36
- package/dist/exec/shared.js +30 -164
- package/dist/exec/switch.d.ts +38 -55
- package/dist/exec/switch.js +58 -108
- package/dist/exec/types.d.ts +8 -18
- package/dist/index.d.ts +1 -1
- package/dist/network.d.ts +7 -7
- package/dist/network.js +11 -18
- package/dist/policy-store.d.ts +7 -8
- package/dist/policy-store.js +11 -15
- package/dist/policy.d.ts +25 -100
- package/dist/policy.js +44 -146
- package/dist/server-ops.d.ts +15 -35
- package/dist/server-ops.js +36 -117
- package/dist/service/launchd.d.ts +11 -1
- package/dist/service/launchd.js +14 -5
- package/dist/state.d.ts +15 -16
- package/dist/state.js +38 -36
- package/package.json +2 -2
- package/dist/proxy-guard.d.ts +0 -58
- package/dist/proxy-guard.js +0 -137
package/dist/proxy-guard.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { execFile } from "node:child_process";
|
|
2
|
-
import { connect } from "node:net";
|
|
3
|
-
/**
|
|
4
|
-
* The macOS localhost proxy guard (spec §5.2.3). `dev` lets sandboxed commands reach any port on
|
|
5
|
-
* this machine's localhost, which includes a local proxy that forwards past the network allowlist
|
|
6
|
-
* (Clash and the like) or Docker's TCP API. The device looks for one at start, whenever the
|
|
7
|
-
* policy is reloaded and again before a command once the last result is older than
|
|
8
|
-
* {@link PROXY_GUARD_TTL_MS}; when it finds one, `dev` / `open` run without local binding unless the
|
|
9
|
-
* owner set `localhost: allow`. A check that cannot run on macOS counts as a finding (fail safe).
|
|
10
|
-
*/
|
|
11
|
-
/** How long a proxy guard result is used before the next command runs the guard again. */
|
|
12
|
-
export const PROXY_GUARD_TTL_MS = 30_000;
|
|
13
|
-
/** Common local proxy ports (Clash, V2Ray, Surge, Privoxy, Tor) and Docker's TCP API ports. */
|
|
14
|
-
export const PROXY_PORTS = Object.freeze([
|
|
15
|
-
7890, 7891, 7897, 1080, 1087, 8118, 9050, 9150, 2375, 2376,
|
|
16
|
-
]);
|
|
17
|
-
/** How long one port probe waits for a connection. */
|
|
18
|
-
const PROBE_TIMEOUT_MS = 150;
|
|
19
|
-
/** The proxy variables of a program's environment, checked in any case. */
|
|
20
|
-
const PROXY_ENV = ["http_proxy", "https_proxy", "all_proxy"];
|
|
21
|
-
/** Whether `host` names this machine's loopback. */
|
|
22
|
-
export function isLoopbackHost(host) {
|
|
23
|
-
const h = host
|
|
24
|
-
.trim()
|
|
25
|
-
.toLowerCase()
|
|
26
|
-
.replace(/^\[|\]$/g, "");
|
|
27
|
-
return h === "localhost" || h === "::1" || h.startsWith("127.") || h === "0.0.0.0";
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* The loopback proxies `scutil --proxy` reports as enabled: `HTTPEnable`, `HTTPSEnable`,
|
|
31
|
-
* `SOCKSEnable` set to 1 with their `*Proxy` host on loopback. Returns `"HTTP 127.0.0.1:7890"`
|
|
32
|
-
* style entries.
|
|
33
|
-
*/
|
|
34
|
-
export function parseScutilProxy(text) {
|
|
35
|
-
const fields = new Map();
|
|
36
|
-
for (const line of text.split("\n")) {
|
|
37
|
-
const m = /^\s*([A-Za-z]+)\s*:\s*(.*?)\s*$/.exec(line);
|
|
38
|
-
if (m !== null)
|
|
39
|
-
fields.set(m[1], m[2]);
|
|
40
|
-
}
|
|
41
|
-
const out = [];
|
|
42
|
-
for (const kind of ["HTTP", "HTTPS", "SOCKS"]) {
|
|
43
|
-
if (fields.get(`${kind}Enable`) !== "1")
|
|
44
|
-
continue;
|
|
45
|
-
const host = fields.get(`${kind}Proxy`);
|
|
46
|
-
if (host === undefined || !isLoopbackHost(host))
|
|
47
|
-
continue;
|
|
48
|
-
const port = fields.get(`${kind}Port`);
|
|
49
|
-
out.push(`${kind} ${host}${port !== undefined ? `:${port}` : ""}`);
|
|
50
|
-
}
|
|
51
|
-
return out;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* `value` with the user and password of a proxy URL (`user:pass@`) replaced by `***@`. The
|
|
55
|
-
* userinfo ends at the last `@` before the host, so a password with an unencoded `@`, `/` or `:`
|
|
56
|
-
* is hidden whole.
|
|
57
|
-
*/
|
|
58
|
-
export function redactProxyUserinfo(value) {
|
|
59
|
-
const scheme = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.exec(value)?.[0] ?? "";
|
|
60
|
-
const rest = value.slice(scheme.length);
|
|
61
|
-
// the host part never holds an `@`: everything before the last one is userinfo
|
|
62
|
-
const at = rest.lastIndexOf("@");
|
|
63
|
-
return at === -1 ? value : `${scheme}***@${rest.slice(at + 1)}`;
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* The proxy variables of `env` (any case) whose host is loopback, as `NAME=value` (credentials in
|
|
67
|
-
* the value redacted: the result is printed and logged).
|
|
68
|
-
*/
|
|
69
|
-
export function loopbackProxyEnv(env) {
|
|
70
|
-
const out = [];
|
|
71
|
-
for (const [name, value] of Object.entries(env)) {
|
|
72
|
-
if (!PROXY_ENV.includes(name.toLowerCase()) || value === undefined || value === "")
|
|
73
|
-
continue;
|
|
74
|
-
let host;
|
|
75
|
-
try {
|
|
76
|
-
// `127.0.0.1:7890` without a scheme is common too.
|
|
77
|
-
host = new URL(value.includes("://") ? value : `http://${value}`).hostname;
|
|
78
|
-
}
|
|
79
|
-
catch {
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (isLoopbackHost(host))
|
|
83
|
-
out.push(`${name}=${redactProxyUserinfo(value)}`);
|
|
84
|
-
}
|
|
85
|
-
return out;
|
|
86
|
-
}
|
|
87
|
-
function runScutil() {
|
|
88
|
-
return new Promise((resolve) => {
|
|
89
|
-
execFile("/usr/sbin/scutil", ["--proxy"], { timeout: 2_000 }, (e, stdout) => resolve(e === null ? stdout : undefined));
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/** The loopback addresses probed: a proxy may listen on either. */
|
|
93
|
-
export const PROBE_HOSTS = Object.freeze(["127.0.0.1", "::1"]);
|
|
94
|
-
/** Whether something accepts a TCP connection on `host`:`port` within a short time. */
|
|
95
|
-
export function probeLoopbackPort(port, host = "127.0.0.1", timeoutMs = PROBE_TIMEOUT_MS) {
|
|
96
|
-
return new Promise((resolve) => {
|
|
97
|
-
const socket = connect({ host, port });
|
|
98
|
-
const done = (open) => {
|
|
99
|
-
socket.destroy();
|
|
100
|
-
resolve(open);
|
|
101
|
-
};
|
|
102
|
-
socket.setTimeout(timeoutMs, () => done(false));
|
|
103
|
-
socket.once("connect", () => done(true));
|
|
104
|
-
socket.once("error", () => done(false));
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Looks for a local forwarding proxy: the system proxy (macOS `scutil --proxy`), loopback proxy
|
|
109
|
-
* variables in the program's environment, and a listener on {@link PROXY_PORTS} of 127.0.0.1 or
|
|
110
|
-
* ::1. A proxy configured only through a PAC file (automatic proxy configuration) is not read.
|
|
111
|
-
* Never throws. On macOS a system proxy setting that cannot be read counts as found (fail safe);
|
|
112
|
-
* a port probe that cannot run finds nothing.
|
|
113
|
-
*/
|
|
114
|
-
export async function detectLocalProxy(deps = {}) {
|
|
115
|
-
const platform = deps.platform ?? process.platform;
|
|
116
|
-
const reasons = [];
|
|
117
|
-
if (platform === "darwin") {
|
|
118
|
-
const text = await (deps.scutil ?? runScutil)().catch(() => undefined);
|
|
119
|
-
if (text === undefined)
|
|
120
|
-
reasons.push("the system proxy settings could not be read");
|
|
121
|
-
else
|
|
122
|
-
for (const p of parseScutilProxy(text))
|
|
123
|
-
reasons.push(`system proxy ${p}`);
|
|
124
|
-
}
|
|
125
|
-
for (const v of loopbackProxyEnv(deps.env ?? process.env))
|
|
126
|
-
reasons.push(`environment ${v}`);
|
|
127
|
-
const probe = deps.probe ?? ((port, host) => probeLoopbackPort(port, host));
|
|
128
|
-
const ports = deps.ports ?? PROXY_PORTS;
|
|
129
|
-
const targets = PROBE_HOSTS.flatMap((host) => ports.map((port) => ({ host, port })));
|
|
130
|
-
const open = await Promise.all(targets.map((t) => probe(t.port, t.host).catch(() => false)));
|
|
131
|
-
targets.forEach((t, i) => {
|
|
132
|
-
const where = t.host.includes(":") ? `[${t.host}]` : t.host;
|
|
133
|
-
if (open[i])
|
|
134
|
-
reasons.push(`a listener on ${where}:${t.port}`);
|
|
135
|
-
});
|
|
136
|
-
return { detected: reasons.length > 0, reasons };
|
|
137
|
-
}
|