@ant0/cli 0.0.0-stage → 0.3.22-beta.3
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 +28 -0
- package/README.md +97 -2
- package/dist/api/generated/schema.d.ts +11600 -0
- package/dist/api/generated/schema.js +6 -0
- package/dist/api/index.d.ts +219 -0
- package/dist/api/index.js +342 -0
- package/dist/bin.d.ts +3 -0
- package/dist/bin.js +12 -0
- package/dist/bulk.d.ts +38 -0
- package/dist/bulk.js +66 -0
- package/dist/commands/account.d.ts +48 -0
- package/dist/commands/account.js +337 -0
- package/dist/commands/api-keys.d.ts +5 -0
- package/dist/commands/api-keys.js +310 -0
- package/dist/commands/browsers.d.ts +4 -0
- package/dist/commands/browsers.js +165 -0
- package/dist/commands/daemon.d.ts +15 -0
- package/dist/commands/daemon.js +202 -0
- package/dist/commands/events.d.ts +8 -0
- package/dist/commands/events.js +81 -0
- package/dist/commands/extensions.d.ts +4 -0
- package/dist/commands/extensions.js +74 -0
- package/dist/commands/profiles.d.ts +36 -0
- package/dist/commands/profiles.js +552 -0
- package/dist/commands/proxies.d.ts +23 -0
- package/dist/commands/proxies.js +170 -0
- package/dist/commands/sync.d.ts +4 -0
- package/dist/commands/sync.js +435 -0
- package/dist/commands/team.d.ts +7 -0
- package/dist/commands/team.js +403 -0
- package/dist/commands/trash.d.ts +10 -0
- package/dist/commands/trash.js +54 -0
- package/dist/commands/workspace.d.ts +10 -0
- package/dist/commands/workspace.js +56 -0
- package/dist/context.d.ts +111 -0
- package/dist/context.js +300 -0
- package/dist/errors.d.ts +27 -0
- package/dist/errors.js +96 -0
- package/dist/format.d.ts +23 -0
- package/dist/format.js +85 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +35 -0
- package/dist/inventory.d.ts +50 -0
- package/dist/inventory.js +202 -0
- package/dist/management-transport.d.ts +3 -0
- package/dist/management-transport.js +90 -0
- package/dist/native-terminal.d.ts +50 -0
- package/dist/native-terminal.js +228 -0
- package/dist/program.d.ts +10 -0
- package/dist/program.js +185 -0
- package/dist/runtime-types.d.ts +20 -0
- package/dist/runtime-types.js +2 -0
- package/dist/set.d.ts +15 -0
- package/dist/set.js +87 -0
- package/dist/shared/parse-error.d.ts +6 -0
- package/dist/shared/parse-error.js +8 -0
- package/dist/shared/proxy-parse.d.ts +42 -0
- package/dist/shared/proxy-parse.js +238 -0
- package/dist/shared/proxy-types.d.ts +21 -0
- package/dist/shared/proxy-types.js +2 -0
- package/dist/shared/recovery-code.d.ts +7 -0
- package/dist/shared/recovery-code.js +53 -0
- package/dist/version.d.ts +8 -0
- package/dist/version.js +18 -0
- package/licenses/README.md +5 -0
- package/licenses/THIRD_PARTY_NOTICES +96 -0
- package/package.json +63 -4
package/dist/program.js
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `ant0` command tree. Commander is configured to never call
|
|
3
|
+
* `process.exit` and to write through the injected streams, so `run()` is testable in-process.
|
|
4
|
+
*/
|
|
5
|
+
import { Command, InvalidArgumentError } from "commander";
|
|
6
|
+
import { Readable, Writable } from "node:stream";
|
|
7
|
+
import { registerAccount } from "./commands/account.js";
|
|
8
|
+
import { registerBrowsers } from "./commands/browsers.js";
|
|
9
|
+
import { registerDaemon } from "./commands/daemon.js";
|
|
10
|
+
import { registerEvents } from "./commands/events.js";
|
|
11
|
+
import { registerExtensions } from "./commands/extensions.js";
|
|
12
|
+
import { registerProfiles } from "./commands/profiles.js";
|
|
13
|
+
import { registerProxies } from "./commands/proxies.js";
|
|
14
|
+
import { registerSync } from "./commands/sync.js";
|
|
15
|
+
import { registerTeam } from "./commands/team.js";
|
|
16
|
+
import { registerTrash } from "./commands/trash.js";
|
|
17
|
+
import { registerWorkspace } from "./commands/workspace.js";
|
|
18
|
+
import { registerApiKeys } from "./commands/api-keys.js";
|
|
19
|
+
import { Context } from "./context.js";
|
|
20
|
+
import { CLI_VERSION } from "./version.js";
|
|
21
|
+
export { CLI_VERSION };
|
|
22
|
+
import { CliError, DaemonNotRunningError, UsageError } from "./errors.js";
|
|
23
|
+
function parsePort(value) {
|
|
24
|
+
const n = Number(value);
|
|
25
|
+
if (!Number.isInteger(n) || n < 1 || n > 65535) {
|
|
26
|
+
throw new InvalidArgumentError("expected a port number (1-65535)");
|
|
27
|
+
}
|
|
28
|
+
return n;
|
|
29
|
+
}
|
|
30
|
+
const API_KEY_ARGV = "API keys are not accepted as arguments; set ANT0_API_KEY or pipe the key with --api-key-stdin";
|
|
31
|
+
function rejectSecretArgv(argv, options) {
|
|
32
|
+
const args = argv.slice(options?.from === "user" ? 0 : 2);
|
|
33
|
+
if (args.some((arg) => arg === "--api-key" || arg.startsWith("--api-key=") || /ant0_sk_/i.test(arg)))
|
|
34
|
+
throw new UsageError(API_KEY_ARGV);
|
|
35
|
+
}
|
|
36
|
+
const FILE_KEY_COMMANDS = [
|
|
37
|
+
"profiles ls",
|
|
38
|
+
"profiles run",
|
|
39
|
+
"profiles start",
|
|
40
|
+
"profiles stop",
|
|
41
|
+
"profiles status",
|
|
42
|
+
"proxies ls",
|
|
43
|
+
"key-status",
|
|
44
|
+
"mcp serve",
|
|
45
|
+
"api-keys create",
|
|
46
|
+
];
|
|
47
|
+
const API_KEY_COMMANDS = FILE_KEY_COMMANDS.filter((name) => name !== "api-keys create");
|
|
48
|
+
class Ant0Command extends Command {
|
|
49
|
+
mcpInvocation = false;
|
|
50
|
+
rememberInvocation(argv = process.argv, options) {
|
|
51
|
+
this.mcpInvocation = false;
|
|
52
|
+
for (let i = options?.from === "user" ? 0 : 2; i < argv.length; i++) {
|
|
53
|
+
const arg = argv[i];
|
|
54
|
+
if (arg === "--home" || arg === "--port" || arg === "--credential-file") {
|
|
55
|
+
i++;
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (arg.startsWith("-"))
|
|
59
|
+
continue;
|
|
60
|
+
this.mcpInvocation = arg === "mcp";
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
parse(argv, options) {
|
|
65
|
+
rejectSecretArgv(argv ?? process.argv, options);
|
|
66
|
+
this.rememberInvocation(argv, options);
|
|
67
|
+
return super.parse(argv, options);
|
|
68
|
+
}
|
|
69
|
+
parseAsync(argv, options) {
|
|
70
|
+
rejectSecretArgv(argv ?? process.argv, options);
|
|
71
|
+
this.rememberInvocation(argv, options);
|
|
72
|
+
return super.parseAsync(argv, options);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function buildProgram(ctx) {
|
|
76
|
+
let mcpParseDiagnostic = false;
|
|
77
|
+
const program = new Ant0Command("ant0")
|
|
78
|
+
.description("Ant0: profile manager for Ant0 Browser (client of the local ant0d daemon)")
|
|
79
|
+
.version(CLI_VERSION, "-V, --version", "print the CLI version")
|
|
80
|
+
.option("--home <dir>", "data directory (default: $ANT0_HOME, else the platform data folder)")
|
|
81
|
+
.option("--port <n>", "daemon port (default: from ant0d.pid, $ANT0D_PORT or 27100)", parsePort)
|
|
82
|
+
.option("--json", "print the API payload verbatim instead of a table")
|
|
83
|
+
.option("--credential-file <path>", "private API-key file for supported reads, profile operations or creation recovery")
|
|
84
|
+
.option("--api-key-stdin", "read an API key from the first line of stdin (or set ANT0_API_KEY)")
|
|
85
|
+
.showHelpAfterError("(run with --help for usage)")
|
|
86
|
+
.exitOverride()
|
|
87
|
+
.configureOutput({
|
|
88
|
+
writeOut: (str) => ctx.io.stdout.write(str),
|
|
89
|
+
writeErr: (str) => {
|
|
90
|
+
// Commander normally echoes invalid option values. In MCP mode keep even pre-start
|
|
91
|
+
// parse failures bounded and independent of user-supplied paths/credentials.
|
|
92
|
+
if (!program.mcpInvocation)
|
|
93
|
+
ctx.io.stderr.write(str);
|
|
94
|
+
else if (!mcpParseDiagnostic) {
|
|
95
|
+
mcpParseDiagnostic = true;
|
|
96
|
+
ctx.io.stderr.write("ant0: mcp: invalid command arguments\n");
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
// Subcommands created with `.command()` inherit exitOverride/configureOutput; the global flags
|
|
101
|
+
// may appear anywhere on the line and are read from the root when a command runs.
|
|
102
|
+
ctx.bindGlobals(() => program.opts());
|
|
103
|
+
program.hook("preAction", async (_root, action) => {
|
|
104
|
+
const globals = ctx.globals;
|
|
105
|
+
const fileKey = globals.credentialFile !== undefined;
|
|
106
|
+
const envKey = ctx.io.env.ANT0_API_KEY !== undefined;
|
|
107
|
+
const stdinKey = globals.apiKeyStdin === true;
|
|
108
|
+
if (!fileKey && !envKey && !stdinKey)
|
|
109
|
+
return;
|
|
110
|
+
if ([fileKey, envKey, stdinKey].filter(Boolean).length > 1)
|
|
111
|
+
throw new UsageError("use only one of --credential-file, ANT0_API_KEY and --api-key-stdin");
|
|
112
|
+
const names = [];
|
|
113
|
+
for (let node = action; node && node !== program; node = node.parent)
|
|
114
|
+
names.unshift(node.name());
|
|
115
|
+
const command = names.join(" ");
|
|
116
|
+
if (!(fileKey ? FILE_KEY_COMMANDS : API_KEY_COMMANDS).includes(command))
|
|
117
|
+
throw new UsageError(fileKey
|
|
118
|
+
? "credential file is not supported for this command"
|
|
119
|
+
: "an API key is not supported for this command; unset ANT0_API_KEY to use the local token");
|
|
120
|
+
if (stdinKey) {
|
|
121
|
+
if (command === "mcp serve")
|
|
122
|
+
throw new UsageError("mcp serve reads its API key from ANT0_API_KEY; stdin carries the MCP protocol");
|
|
123
|
+
ctx.apiKey = await ctx.readStdinLine();
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
// A host may replace the `daemon` group with its own lifecycle commands.
|
|
127
|
+
if (ctx.runtime?.registerLifecycle)
|
|
128
|
+
ctx.runtime.registerLifecycle(program, ctx);
|
|
129
|
+
else
|
|
130
|
+
registerDaemon(program, ctx);
|
|
131
|
+
registerBrowsers(program, ctx);
|
|
132
|
+
registerProfiles(program, ctx);
|
|
133
|
+
registerTrash(program, ctx);
|
|
134
|
+
registerProxies(program, ctx);
|
|
135
|
+
registerExtensions(program, ctx);
|
|
136
|
+
registerEvents(program, ctx);
|
|
137
|
+
registerAccount(program, ctx);
|
|
138
|
+
registerWorkspace(program, ctx);
|
|
139
|
+
registerSync(program, ctx);
|
|
140
|
+
registerTeam(program, ctx);
|
|
141
|
+
registerApiKeys(program, ctx);
|
|
142
|
+
program
|
|
143
|
+
.command("mcp")
|
|
144
|
+
.description("Local MCP server: identity and inventory reads, profile run/stop, cookie and export tools, " +
|
|
145
|
+
"and beta page actions (snapshot, navigate, click, type, fill, select, keys, wait, " +
|
|
146
|
+
"screenshot, text, tabs); local connection only")
|
|
147
|
+
.command("serve")
|
|
148
|
+
.description("Serve MCP over stdio using an already-running local daemon")
|
|
149
|
+
.option("--stdio", "use the stdio MCP transport (required)")
|
|
150
|
+
.action(async (options) => {
|
|
151
|
+
if (!options.stdio)
|
|
152
|
+
throw new UsageError("mcp serve requires --stdio");
|
|
153
|
+
if (ctx.json)
|
|
154
|
+
throw new UsageError("mcp serve does not accept --json");
|
|
155
|
+
const stdin = ctx.io.stdin ?? process.stdin;
|
|
156
|
+
if (!(stdin instanceof Readable) || !(ctx.io.stdout instanceof Writable)) {
|
|
157
|
+
throw new CliError("mcp: stdio streams required");
|
|
158
|
+
}
|
|
159
|
+
let mcp;
|
|
160
|
+
try {
|
|
161
|
+
mcp = await import("@ant0/mcp");
|
|
162
|
+
await mcp.serveStdio({
|
|
163
|
+
...(ctx.globals.home !== undefined ? { home: ctx.globals.home } : {}),
|
|
164
|
+
...(ctx.globals.port !== undefined ? { port: ctx.globals.port } : {}),
|
|
165
|
+
...(ctx.globals.credentialFile !== undefined
|
|
166
|
+
? { credentialFile: ctx.globals.credentialFile }
|
|
167
|
+
: {}),
|
|
168
|
+
env: ctx.io.env,
|
|
169
|
+
stdin,
|
|
170
|
+
stdout: ctx.io.stdout,
|
|
171
|
+
...(ctx.io.signal ? { signal: ctx.io.signal } : {}),
|
|
172
|
+
}, ctx.runtime?.outputWorker);
|
|
173
|
+
}
|
|
174
|
+
catch (error) {
|
|
175
|
+
if (mcp && error instanceof mcp.McpFailure) {
|
|
176
|
+
if (error.code === "unavailable")
|
|
177
|
+
throw new DaemonNotRunningError("mcp: local daemon unavailable");
|
|
178
|
+
throw new CliError(`mcp: ${error.message}`);
|
|
179
|
+
}
|
|
180
|
+
throw new CliError("mcp: adapter failed");
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
return program;
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=program.js.map
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Command } from "commander";
|
|
2
|
+
import type { OutputWorkerCommand } from "@ant0/mcp";
|
|
3
|
+
import type { Context } from "./context.js";
|
|
4
|
+
/** A program and its leading arguments, spawned to start the daemon. */
|
|
5
|
+
export interface DaemonCommand {
|
|
6
|
+
readonly command: string;
|
|
7
|
+
readonly args: readonly string[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Hooks for an embedding host that supplies its own daemon launcher and lifecycle commands. The
|
|
11
|
+
* ordinary CLI passes none: it starts the daemon found by `resolveDaemonCommand` and registers
|
|
12
|
+
* the built-in `daemon` command group.
|
|
13
|
+
*/
|
|
14
|
+
export interface RuntimeCommands {
|
|
15
|
+
readonly daemon: DaemonCommand;
|
|
16
|
+
readonly outputWorker: OutputWorkerCommand;
|
|
17
|
+
/** Registers the host's lifecycle commands in place of the built-in `daemon` group. */
|
|
18
|
+
readonly registerLifecycle?: (program: Command, ctx: Context) => void;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=runtime-types.d.ts.map
|
package/dist/set.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Manifest paths whose value is a list (the profile manifest). */
|
|
2
|
+
export declare const ARRAY_PATHS: ReadonlySet<string>;
|
|
3
|
+
export type Scalar = string | number | boolean | null;
|
|
4
|
+
export declare function parseScalar(raw: string): Scalar;
|
|
5
|
+
export declare function parseSetValue(raw: string, path: string): unknown;
|
|
6
|
+
/** Parse one `key=value` pair. */
|
|
7
|
+
export declare function parseSetPair(item: string): {
|
|
8
|
+
path: string;
|
|
9
|
+
value: unknown;
|
|
10
|
+
};
|
|
11
|
+
/** Assign `value` at the dotted `path` inside `target`, creating intermediate objects. */
|
|
12
|
+
export declare function assignPath(target: Record<string, unknown>, path: string, value: unknown): void;
|
|
13
|
+
/** Build the PATCH body from every `--set` argument. */
|
|
14
|
+
export declare function parseSetArgs(items: readonly string[]): Record<string, unknown>;
|
|
15
|
+
//# sourceMappingURL=set.d.ts.map
|
package/dist/set.js
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `--set key=value` parsing for `ant0 profiles edit`.
|
|
3
|
+
*
|
|
4
|
+
* Keys are dotted paths into the PATCH body (`network.geo.timezone`). Values: `true`/`false`,
|
|
5
|
+
* `null`, numbers, comma lists (`tags=a,b`), and JSON when the value starts with `[`, `{` or `"`
|
|
6
|
+
* (the way to force a string containing commas or digits: `--set 'notes="1,2"'`). Paths known to
|
|
7
|
+
* hold arrays are always sent as arrays, so `--set tags=a` yields `["a"]`.
|
|
8
|
+
*/
|
|
9
|
+
import { UsageError } from "./errors.js";
|
|
10
|
+
const KEY_RE = /^[A-Za-z_][\w-]*(\.[A-Za-z_][\w-]*)*$/;
|
|
11
|
+
/** Manifest paths whose value is a list (the profile manifest). */
|
|
12
|
+
export const ARRAY_PATHS = new Set([
|
|
13
|
+
"tags",
|
|
14
|
+
"network.geo.languages",
|
|
15
|
+
"launch.start_urls",
|
|
16
|
+
"launch.extensions",
|
|
17
|
+
"launch.extra_args",
|
|
18
|
+
]);
|
|
19
|
+
export function parseScalar(raw) {
|
|
20
|
+
const text = raw.trim();
|
|
21
|
+
if (text === "true")
|
|
22
|
+
return true;
|
|
23
|
+
if (text === "false")
|
|
24
|
+
return false;
|
|
25
|
+
if (text === "null")
|
|
26
|
+
return null;
|
|
27
|
+
if (/^-?\d+(\.\d+)?$/.test(text))
|
|
28
|
+
return Number(text);
|
|
29
|
+
return text;
|
|
30
|
+
}
|
|
31
|
+
export function parseSetValue(raw, path) {
|
|
32
|
+
const text = raw.trim();
|
|
33
|
+
if (text.startsWith("[") || text.startsWith("{") || text.startsWith('"')) {
|
|
34
|
+
try {
|
|
35
|
+
return JSON.parse(text);
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
throw new UsageError(`--set ${path}: invalid JSON value (${err.message})`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (ARRAY_PATHS.has(path)) {
|
|
42
|
+
if (text === "")
|
|
43
|
+
return [];
|
|
44
|
+
return text.split(",").map((part) => parseScalar(part));
|
|
45
|
+
}
|
|
46
|
+
if (text.includes(","))
|
|
47
|
+
return text.split(",").map((part) => parseScalar(part));
|
|
48
|
+
return parseScalar(text);
|
|
49
|
+
}
|
|
50
|
+
/** Parse one `key=value` pair. */
|
|
51
|
+
export function parseSetPair(item) {
|
|
52
|
+
const eq = item.indexOf("=");
|
|
53
|
+
if (eq <= 0)
|
|
54
|
+
throw new UsageError(`--set expects key=value, got "${item}"`);
|
|
55
|
+
const path = item.slice(0, eq).trim();
|
|
56
|
+
if (!KEY_RE.test(path))
|
|
57
|
+
throw new UsageError(`--set: invalid key "${path}"`);
|
|
58
|
+
return { path, value: parseSetValue(item.slice(eq + 1), path) };
|
|
59
|
+
}
|
|
60
|
+
/** Assign `value` at the dotted `path` inside `target`, creating intermediate objects. */
|
|
61
|
+
export function assignPath(target, path, value) {
|
|
62
|
+
const parts = path.split(".");
|
|
63
|
+
let node = target;
|
|
64
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
65
|
+
const key = parts[i];
|
|
66
|
+
const next = node[key];
|
|
67
|
+
if (next === null || typeof next !== "object" || Array.isArray(next)) {
|
|
68
|
+
const fresh = {};
|
|
69
|
+
node[key] = fresh;
|
|
70
|
+
node = fresh;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
node = next;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
node[parts[parts.length - 1]] = value;
|
|
77
|
+
}
|
|
78
|
+
/** Build the PATCH body from every `--set` argument. */
|
|
79
|
+
export function parseSetArgs(items) {
|
|
80
|
+
const body = {};
|
|
81
|
+
for (const item of items) {
|
|
82
|
+
const { path, value } = parseSetPair(item);
|
|
83
|
+
assignPath(body, path, value);
|
|
84
|
+
}
|
|
85
|
+
return body;
|
|
86
|
+
}
|
|
87
|
+
//# sourceMappingURL=set.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Thrown by the text parsers (proxy lines/URLs, cookies, CSV) on malformed input. */
|
|
2
|
+
export declare class ParseError extends Error {
|
|
3
|
+
readonly name = "ParseError";
|
|
4
|
+
constructor(message: string, options?: ErrorOptions);
|
|
5
|
+
}
|
|
6
|
+
//# sourceMappingURL=parse-error.d.ts.map
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ProxyCreate } from "./proxy-types.js";
|
|
2
|
+
export interface ParsedHostPort {
|
|
3
|
+
host: string;
|
|
4
|
+
port: number;
|
|
5
|
+
}
|
|
6
|
+
/** Display form used for default names: IPv6 hosts are bracketed. */
|
|
7
|
+
export declare function formatHostPort(host: string, port: number): string;
|
|
8
|
+
/**
|
|
9
|
+
* Parse `scheme://[user[:pass]@]host[:port][/]` for `http`, `https`, `socks5` (and `socks5h`,
|
|
10
|
+
* mapped to `socks5`) and `socks4` (and `socks4a`, mapped to `socks4`; no password). Credentials before `@` are percent-decoded; a raw `@` inside the password
|
|
11
|
+
* is tolerated (the last `@` separates user info from the host). Missing port falls back to
|
|
12
|
+
* 80 / 443 / 1080. The provider export form `scheme://host:port:user:pass` is also accepted;
|
|
13
|
+
* its credentials are taken verbatim (no percent-decoding).
|
|
14
|
+
*
|
|
15
|
+
* @throws {ParseError}
|
|
16
|
+
*/
|
|
17
|
+
export declare function parseProxyUrl(url: string): ProxyCreate;
|
|
18
|
+
/**
|
|
19
|
+
* Parse one provider-style line. Accepted forms (type defaults to `http`):
|
|
20
|
+
*
|
|
21
|
+
* - `host:port`
|
|
22
|
+
* - `host:port:user:pass` (a password may itself contain `:`)
|
|
23
|
+
* - `user:pass@host:port`
|
|
24
|
+
* - `scheme://...` (delegates to `parseProxyUrl`)
|
|
25
|
+
*
|
|
26
|
+
* IPv6 hosts must be bracketed, e.g. `[2001:db8::1]:1080:user:pass`.
|
|
27
|
+
*
|
|
28
|
+
* @throws {ParseError}
|
|
29
|
+
*/
|
|
30
|
+
export declare function parseProxyLine(line: string): ProxyCreate;
|
|
31
|
+
export interface ProxyLineError {
|
|
32
|
+
/** 1-based line number in the input text. */
|
|
33
|
+
line: number;
|
|
34
|
+
message: string;
|
|
35
|
+
}
|
|
36
|
+
export interface ParsedProxyLines {
|
|
37
|
+
proxies: ProxyCreate[];
|
|
38
|
+
errors: ProxyLineError[];
|
|
39
|
+
}
|
|
40
|
+
/** Parse a multi-line provider export; blank lines and `#` comments are skipped. */
|
|
41
|
+
export declare function parseProxyLines(text: string): ParsedProxyLines;
|
|
42
|
+
//# sourceMappingURL=proxy-parse.d.ts.map
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { ParseError } from "./parse-error.js";
|
|
2
|
+
const SCHEME_ALIASES = {
|
|
3
|
+
http: "http",
|
|
4
|
+
https: "https",
|
|
5
|
+
socks5: "socks5",
|
|
6
|
+
socks5h: "socks5",
|
|
7
|
+
socks4: "socks4",
|
|
8
|
+
socks4a: "socks4",
|
|
9
|
+
};
|
|
10
|
+
const DEFAULT_PORTS = {
|
|
11
|
+
http: 80,
|
|
12
|
+
https: 443,
|
|
13
|
+
socks5: 1080,
|
|
14
|
+
socks4: 1080,
|
|
15
|
+
};
|
|
16
|
+
/** Same character rules as `proxy-v1.schema.json#/properties/host` (bare IPv6, no brackets). */
|
|
17
|
+
const HOST_RE = /^[^\s/\\@?#[\]]+$/;
|
|
18
|
+
const PORT_RE = /^\d{1,5}$/;
|
|
19
|
+
const URL_RE = /^([A-Za-z][A-Za-z0-9+.-]*):\/\/(.*)$/;
|
|
20
|
+
/** `host[:port]` (bracketed IPv6 allowed) immediately followed by a path/query/fragment char. */
|
|
21
|
+
const PATH_AFTER_HOST_PORT = /^(?:\[[^\]]*\]|[^:[/?#]+)(?::\d*)?[/?#]/;
|
|
22
|
+
/** Display form used for default names: IPv6 hosts are bracketed. */
|
|
23
|
+
export function formatHostPort(host, port) {
|
|
24
|
+
return host.includes(":") ? `[${host}]:${port}` : `${host}:${port}`;
|
|
25
|
+
}
|
|
26
|
+
function parsePort(text) {
|
|
27
|
+
if (!PORT_RE.test(text))
|
|
28
|
+
throw new ParseError("invalid port: expected 1..65535");
|
|
29
|
+
const port = Number(text);
|
|
30
|
+
if (port < 1 || port > 65535)
|
|
31
|
+
throw new ParseError("port is out of range 1..65535");
|
|
32
|
+
return port;
|
|
33
|
+
}
|
|
34
|
+
function checkHost(host) {
|
|
35
|
+
if (host === "")
|
|
36
|
+
throw new ParseError("missing host");
|
|
37
|
+
if (!HOST_RE.test(host))
|
|
38
|
+
throw new ParseError("invalid host");
|
|
39
|
+
return host;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* An unbracketed `a:b:c...` is an IPv6 literal (not `host:port[:user:pass]`) when it contains
|
|
43
|
+
* `::`, or when it has two or more colons and the segment after the first one is not a port.
|
|
44
|
+
* Only hex digits, dots and colons are considered so `host:8080:user:pass` never matches.
|
|
45
|
+
*/
|
|
46
|
+
function looksLikeBareIpv6(text) {
|
|
47
|
+
if (!/^[0-9A-Fa-f:.]+$/.test(text))
|
|
48
|
+
return false;
|
|
49
|
+
const colons = text.split(":").length - 1;
|
|
50
|
+
if (colons < 2)
|
|
51
|
+
return false;
|
|
52
|
+
if (text.includes("::"))
|
|
53
|
+
return true;
|
|
54
|
+
const second = text.split(":")[1] ?? "";
|
|
55
|
+
return !PORT_RE.test(second);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Parse `host:port`, `[v6]:port`, or a bare host (port optional when `defaultPort` is given).
|
|
59
|
+
* Returns the remaining text after the port (starting with `:`) so callers can consume
|
|
60
|
+
* `:user:pass` suffixes.
|
|
61
|
+
*/
|
|
62
|
+
function splitHostPort(text, defaultPort) {
|
|
63
|
+
let host;
|
|
64
|
+
let after;
|
|
65
|
+
if (text.startsWith("[")) {
|
|
66
|
+
const close = text.indexOf("]");
|
|
67
|
+
if (close < 0)
|
|
68
|
+
throw new ParseError("unterminated IPv6 bracket");
|
|
69
|
+
host = text.slice(1, close);
|
|
70
|
+
after = text.slice(close + 1);
|
|
71
|
+
if (!host.includes(":"))
|
|
72
|
+
throw new ParseError("invalid IPv6 host");
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
if (looksLikeBareIpv6(text)) {
|
|
76
|
+
throw new ParseError("IPv6 hosts must be written in brackets, e.g. [2001:db8::1]:1080");
|
|
77
|
+
}
|
|
78
|
+
const colon = text.indexOf(":");
|
|
79
|
+
host = colon < 0 ? text : text.slice(0, colon);
|
|
80
|
+
after = colon < 0 ? "" : text.slice(colon);
|
|
81
|
+
}
|
|
82
|
+
checkHost(host);
|
|
83
|
+
if (after === "") {
|
|
84
|
+
if (defaultPort === undefined)
|
|
85
|
+
throw new ParseError("missing port");
|
|
86
|
+
return { host, port: defaultPort, rest: "" };
|
|
87
|
+
}
|
|
88
|
+
if (!after.startsWith(":"))
|
|
89
|
+
throw new ParseError("unexpected text after host");
|
|
90
|
+
const next = after.indexOf(":", 1);
|
|
91
|
+
const portText = next < 0 ? after.slice(1) : after.slice(1, next);
|
|
92
|
+
const rest = next < 0 ? "" : after.slice(next);
|
|
93
|
+
if (portText === "") {
|
|
94
|
+
if (defaultPort === undefined)
|
|
95
|
+
throw new ParseError("missing port");
|
|
96
|
+
return { host, port: defaultPort, rest };
|
|
97
|
+
}
|
|
98
|
+
return { host, port: parsePort(portText), rest };
|
|
99
|
+
}
|
|
100
|
+
function decodeComponent(text, what) {
|
|
101
|
+
try {
|
|
102
|
+
return decodeURIComponent(text);
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
throw new ParseError(`invalid percent-encoding in ${what}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function splitUserInfo(userInfo, decode) {
|
|
109
|
+
const colon = userInfo.indexOf(":");
|
|
110
|
+
let username = colon < 0 ? userInfo : userInfo.slice(0, colon);
|
|
111
|
+
let password = colon < 0 ? undefined : userInfo.slice(colon + 1);
|
|
112
|
+
if (decode) {
|
|
113
|
+
username = decodeComponent(username, "username");
|
|
114
|
+
if (password !== undefined)
|
|
115
|
+
password = decodeComponent(password, "password");
|
|
116
|
+
}
|
|
117
|
+
const out = {};
|
|
118
|
+
if (username !== "")
|
|
119
|
+
out.username = username;
|
|
120
|
+
if (password !== undefined && password !== "")
|
|
121
|
+
out.password = password;
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
function build(type, host, port, creds) {
|
|
125
|
+
if (type === "socks4" && creds.password !== undefined) {
|
|
126
|
+
throw new ParseError("SOCKS4 proxies have a user ID but no password; remove the password or use socks5");
|
|
127
|
+
}
|
|
128
|
+
const out = { name: formatHostPort(host, port).slice(0, 120), type, host, port };
|
|
129
|
+
if (creds.username !== undefined)
|
|
130
|
+
out.username = creds.username;
|
|
131
|
+
if (creds.password !== undefined)
|
|
132
|
+
out.password = creds.password;
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Parse `scheme://[user[:pass]@]host[:port][/]` for `http`, `https`, `socks5` (and `socks5h`,
|
|
137
|
+
* mapped to `socks5`) and `socks4` (and `socks4a`, mapped to `socks4`; no password). Credentials before `@` are percent-decoded; a raw `@` inside the password
|
|
138
|
+
* is tolerated (the last `@` separates user info from the host). Missing port falls back to
|
|
139
|
+
* 80 / 443 / 1080. The provider export form `scheme://host:port:user:pass` is also accepted;
|
|
140
|
+
* its credentials are taken verbatim (no percent-decoding).
|
|
141
|
+
*
|
|
142
|
+
* @throws {ParseError}
|
|
143
|
+
*/
|
|
144
|
+
export function parseProxyUrl(url) {
|
|
145
|
+
const m = URL_RE.exec(url.trim());
|
|
146
|
+
if (!m)
|
|
147
|
+
throw new ParseError("not a proxy URL: expected scheme://host:port");
|
|
148
|
+
const scheme = (m[1] ?? "").toLowerCase();
|
|
149
|
+
const type = SCHEME_ALIASES[scheme];
|
|
150
|
+
if (!type)
|
|
151
|
+
throw new ParseError("unsupported proxy type: expected http, https, socks5 or socks4");
|
|
152
|
+
let rest = m[2] ?? "";
|
|
153
|
+
// Strip an optional trailing "/" (providers often paste "http://host:port/").
|
|
154
|
+
if (rest.endsWith("/"))
|
|
155
|
+
rest = rest.slice(0, -1);
|
|
156
|
+
const at = rest.lastIndexOf("@");
|
|
157
|
+
const userInfo = at < 0 ? "" : rest.slice(0, at);
|
|
158
|
+
const hostPort = at < 0 ? rest : rest.slice(at + 1);
|
|
159
|
+
// With "@" the host:port part must be clean; without it only the host/port segment is checked
|
|
160
|
+
// so a "/" inside a verbatim ":user:pass" suffix is not mistaken for a path.
|
|
161
|
+
if (at >= 0 ? /[/?#]/.test(hostPort) : PATH_AFTER_HOST_PORT.test(hostPort)) {
|
|
162
|
+
throw new ParseError("proxy URL must not contain a path, query or fragment");
|
|
163
|
+
}
|
|
164
|
+
const { host, port, rest: tail } = splitHostPort(hostPort, DEFAULT_PORTS[type]);
|
|
165
|
+
if (at >= 0) {
|
|
166
|
+
if (tail !== "")
|
|
167
|
+
throw new ParseError("unexpected text after port");
|
|
168
|
+
return build(type, host, port, splitUserInfo(userInfo, true));
|
|
169
|
+
}
|
|
170
|
+
if (tail === "")
|
|
171
|
+
return build(type, host, port, {});
|
|
172
|
+
// scheme://host:port:user:pass (a common provider export format); credentials verbatim.
|
|
173
|
+
const creds = splitUserInfo(tail.slice(1), false);
|
|
174
|
+
if (creds.username === undefined && creds.password === undefined) {
|
|
175
|
+
throw new ParseError("unexpected text after port");
|
|
176
|
+
}
|
|
177
|
+
return build(type, host, port, creds);
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Parse one provider-style line. Accepted forms (type defaults to `http`):
|
|
181
|
+
*
|
|
182
|
+
* - `host:port`
|
|
183
|
+
* - `host:port:user:pass` (a password may itself contain `:`)
|
|
184
|
+
* - `user:pass@host:port`
|
|
185
|
+
* - `scheme://...` (delegates to `parseProxyUrl`)
|
|
186
|
+
*
|
|
187
|
+
* IPv6 hosts must be bracketed, e.g. `[2001:db8::1]:1080:user:pass`.
|
|
188
|
+
*
|
|
189
|
+
* @throws {ParseError}
|
|
190
|
+
*/
|
|
191
|
+
export function parseProxyLine(line) {
|
|
192
|
+
const text = line.trim();
|
|
193
|
+
if (text === "")
|
|
194
|
+
throw new ParseError("empty proxy line");
|
|
195
|
+
if (/^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(text))
|
|
196
|
+
return parseProxyUrl(text);
|
|
197
|
+
if (/\s/.test(text))
|
|
198
|
+
throw new ParseError("proxy line must not contain whitespace");
|
|
199
|
+
const at = text.lastIndexOf("@");
|
|
200
|
+
if (at >= 0) {
|
|
201
|
+
const creds = splitUserInfo(text.slice(0, at), false);
|
|
202
|
+
const { host, port, rest } = splitHostPort(text.slice(at + 1));
|
|
203
|
+
if (rest !== "")
|
|
204
|
+
throw new ParseError("unexpected text after port");
|
|
205
|
+
return build("http", host, port, creds);
|
|
206
|
+
}
|
|
207
|
+
const { host, port, rest } = splitHostPort(text);
|
|
208
|
+
if (rest === "")
|
|
209
|
+
return build("http", host, port, {});
|
|
210
|
+
// rest is ":user[:pass[:more...]]"; everything after the user's colon is the password.
|
|
211
|
+
const creds = splitUserInfo(rest.slice(1), false);
|
|
212
|
+
if (creds.username === undefined && creds.password === undefined) {
|
|
213
|
+
throw new ParseError("expected host:port or host:port:user:pass");
|
|
214
|
+
}
|
|
215
|
+
return build("http", host, port, creds);
|
|
216
|
+
}
|
|
217
|
+
/** Parse a multi-line provider export; blank lines and `#` comments are skipped. */
|
|
218
|
+
export function parseProxyLines(text) {
|
|
219
|
+
const proxies = [];
|
|
220
|
+
const errors = [];
|
|
221
|
+
const lines = text.split(/\r?\n/);
|
|
222
|
+
lines.forEach((raw, index) => {
|
|
223
|
+
const line = raw.trim();
|
|
224
|
+
if (line === "" || line.startsWith("#"))
|
|
225
|
+
return;
|
|
226
|
+
try {
|
|
227
|
+
proxies.push(parseProxyLine(line));
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
errors.push({
|
|
231
|
+
line: index + 1,
|
|
232
|
+
message: error instanceof Error ? error.message : String(error),
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
return { proxies, errors };
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=proxy-parse.js.map
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type ProxyType = "http" | "https" | "socks5" | "socks4";
|
|
2
|
+
/**
|
|
3
|
+
* Input for `POST /proxies` (object form). `password` is PLAINTEXT on input; the daemon stores it
|
|
4
|
+
* in the secrets table and writes `password_ref` on the stored document.
|
|
5
|
+
*/
|
|
6
|
+
export interface ProxyCreate {
|
|
7
|
+
name: string;
|
|
8
|
+
type: ProxyType;
|
|
9
|
+
host: string;
|
|
10
|
+
port: number;
|
|
11
|
+
username?: string;
|
|
12
|
+
password?: string;
|
|
13
|
+
change_ip_url?: string;
|
|
14
|
+
session_pattern?: string;
|
|
15
|
+
provider?: string;
|
|
16
|
+
/** socks5 only; defaults to `false`. */
|
|
17
|
+
udp?: boolean;
|
|
18
|
+
tags?: string[];
|
|
19
|
+
notes?: string;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=proxy-types.d.ts.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** A recovery code encodes exactly this many raw bytes. */
|
|
2
|
+
export declare const RECOVERY_CODE_BYTES = 32;
|
|
3
|
+
/** Format 32 raw bytes as 13 dash-separated groups of four base32 characters. */
|
|
4
|
+
export declare function formatRecoveryCode(raw: Uint8Array): string;
|
|
5
|
+
/** Parse a recovery code (dashes and case ignored) back to its 32 raw bytes. */
|
|
6
|
+
export declare function parseRecoveryCode(text: string): Buffer;
|
|
7
|
+
//# sourceMappingURL=recovery-code.d.ts.map
|