clawvault 2.6.1 → 3.0.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/bin/command-registration.test.js +1 -3
- package/bin/register-core-commands.js +10 -23
- package/bin/register-maintenance-commands.js +3 -20
- package/bin/register-query-commands.js +23 -0
- package/bin/register-task-commands.js +1 -18
- package/bin/register-task-commands.test.js +0 -16
- package/bin/register-vault-operations-commands.js +1 -29
- package/dist/{chunk-QVMXF7FY.js → chunk-3D6BCTP6.js} +39 -1
- package/dist/{chunk-R2MIW5G7.js → chunk-3DHXQHYG.js} +1 -1
- package/dist/{chunk-Q2J5YTUF.js → chunk-3NSBOUT3.js} +73 -36
- package/dist/chunk-3RG5ZIWI.js +10 -0
- package/dist/{chunk-AZYOKJYC.js → chunk-62YTUT6J.js} +2 -2
- package/dist/chunk-6U6MK36V.js +205 -0
- package/dist/{chunk-4QYGFWRM.js → chunk-7R7O6STJ.js} +4 -4
- package/dist/{chunk-VXEOHTSL.js → chunk-C7OK5WKP.js} +4 -4
- package/dist/chunk-CMB7UL7C.js +327 -0
- package/dist/chunk-DEFFDRVP.js +938 -0
- package/dist/{chunk-HIHOUSXS.js → chunk-E7MFQB6D.js} +59 -18
- package/dist/{chunk-ME37YNW3.js → chunk-F2JEUD4J.js} +6 -4
- package/dist/chunk-GAJV4IGR.js +82 -0
- package/dist/chunk-GQSLDZTS.js +560 -0
- package/dist/{chunk-4OXMU5S2.js → chunk-GUKMRGM7.js} +1 -1
- package/dist/{chunk-T76H47ZS.js → chunk-H34S76MB.js} +6 -6
- package/dist/{chunk-R6SXNSFD.js → chunk-JY6FYXIT.js} +10 -5
- package/dist/chunk-K234IDRJ.js +1073 -0
- package/dist/{chunk-IEVLHNLU.js → chunk-LNJA2UGL.js} +86 -9
- package/dist/{chunk-MFAWT5O5.js → chunk-LYHGEHXG.js} +1 -0
- package/dist/chunk-MFM6K7PU.js +374 -0
- package/dist/{chunk-QWQ3TIKS.js → chunk-N2AXRYLC.js} +1 -1
- package/dist/chunk-PAH27GSN.js +108 -0
- package/dist/{chunk-OIWVQYQF.js → chunk-QBLMXKF2.js} +1 -1
- package/dist/{chunk-FHFUXL6G.js → chunk-QK3UCXWL.js} +2 -2
- package/dist/{chunk-3BTHWPMB.js → chunk-SJSFRIYS.js} +1 -1
- package/dist/{chunk-4VRIMU4O.js → chunk-U55BGUAU.js} +2 -2
- package/dist/{chunk-PBEE567J.js → chunk-VGLOTGAS.js} +1 -1
- package/dist/{chunk-F55HGNU4.js → chunk-WAZ3NLWL.js} +47 -0
- package/dist/{chunk-KL4NAOMO.js → chunk-WGRQ6HDV.js} +1 -1
- package/dist/{chunk-UEOUADMO.js → chunk-YKTA5JOJ.js} +13 -10
- package/dist/{chunk-XAVB4GB4.js → chunk-ZVVFWOLW.js} +4 -4
- package/dist/cli/index.cjs +10033 -0
- package/dist/cli/index.d.cts +5 -0
- package/dist/cli/index.js +20 -18
- package/dist/commands/archive.cjs +287 -0
- package/dist/commands/archive.d.cts +11 -0
- package/dist/commands/archive.js +1 -0
- package/dist/commands/backlog.cjs +721 -0
- package/dist/commands/backlog.d.cts +53 -0
- package/dist/commands/backlog.js +3 -2
- package/dist/commands/blocked.cjs +204 -0
- package/dist/commands/blocked.d.cts +26 -0
- package/dist/commands/blocked.js +3 -2
- package/dist/commands/checkpoint.cjs +244 -0
- package/dist/commands/checkpoint.d.cts +41 -0
- package/dist/commands/checkpoint.js +2 -1
- package/dist/commands/compat.cjs +369 -0
- package/dist/commands/compat.d.cts +28 -0
- package/dist/commands/compat.js +2 -1
- package/dist/commands/context.cjs +2989 -0
- package/dist/commands/context.d.cts +2 -0
- package/dist/commands/context.js +5 -4
- package/dist/commands/doctor.cjs +3062 -0
- package/dist/commands/doctor.d.cts +21 -0
- package/dist/commands/doctor.d.ts +6 -1
- package/dist/commands/doctor.js +13 -11
- package/dist/commands/embed.cjs +232 -0
- package/dist/commands/embed.d.cts +17 -0
- package/dist/commands/embed.js +5 -2
- package/dist/commands/entities.cjs +141 -0
- package/dist/commands/entities.d.cts +7 -0
- package/dist/commands/entities.js +1 -0
- package/dist/commands/graph.cjs +501 -0
- package/dist/commands/graph.d.cts +21 -0
- package/dist/commands/graph.js +1 -0
- package/dist/commands/inject.cjs +1636 -0
- package/dist/commands/inject.d.cts +2 -0
- package/dist/commands/inject.d.ts +1 -1
- package/dist/commands/inject.js +4 -2
- package/dist/commands/kanban.cjs +884 -0
- package/dist/commands/kanban.d.cts +63 -0
- package/dist/commands/kanban.js +4 -3
- package/dist/commands/link.cjs +965 -0
- package/dist/commands/link.d.cts +11 -0
- package/dist/commands/link.js +1 -0
- package/dist/commands/migrate-observations.cjs +362 -0
- package/dist/commands/migrate-observations.d.cts +19 -0
- package/dist/commands/migrate-observations.js +3 -2
- package/dist/commands/observe.cjs +4099 -0
- package/dist/commands/observe.d.cts +23 -0
- package/dist/commands/observe.d.ts +1 -0
- package/dist/commands/observe.js +11 -9
- package/dist/commands/project.cjs +1341 -0
- package/dist/commands/project.d.cts +85 -0
- package/dist/commands/project.js +5 -4
- package/dist/commands/rebuild.cjs +3136 -0
- package/dist/commands/rebuild.d.cts +11 -0
- package/dist/commands/rebuild.js +10 -8
- package/dist/commands/recover.cjs +361 -0
- package/dist/commands/recover.d.cts +38 -0
- package/dist/commands/recover.js +3 -2
- package/dist/commands/reflect.cjs +1008 -0
- package/dist/commands/reflect.d.cts +11 -0
- package/dist/commands/reflect.js +6 -4
- package/dist/commands/repair-session.cjs +457 -0
- package/dist/commands/repair-session.d.cts +38 -0
- package/dist/commands/repair-session.js +1 -0
- package/dist/commands/replay.cjs +4103 -0
- package/dist/commands/replay.d.cts +16 -0
- package/dist/commands/replay.js +12 -10
- package/dist/commands/session-recap.cjs +353 -0
- package/dist/commands/session-recap.d.cts +27 -0
- package/dist/commands/session-recap.js +1 -0
- package/dist/commands/setup.cjs +1345 -0
- package/dist/commands/setup.d.cts +100 -0
- package/dist/commands/setup.d.ts +90 -2
- package/dist/commands/setup.js +21 -2
- package/dist/commands/shell-init.cjs +75 -0
- package/dist/commands/shell-init.d.cts +7 -0
- package/dist/commands/shell-init.js +2 -0
- package/dist/commands/sleep.cjs +6028 -0
- package/dist/commands/sleep.d.cts +36 -0
- package/dist/commands/sleep.d.ts +1 -1
- package/dist/commands/sleep.js +17 -15
- package/dist/commands/status.cjs +2736 -0
- package/dist/commands/status.d.cts +52 -0
- package/dist/commands/status.js +12 -10
- package/dist/commands/tailscale.cjs +1532 -0
- package/dist/commands/tailscale.d.cts +52 -0
- package/dist/commands/tailscale.js +1 -0
- package/dist/commands/task.cjs +1236 -0
- package/dist/commands/task.d.cts +97 -0
- package/dist/commands/task.js +4 -3
- package/dist/commands/template.cjs +457 -0
- package/dist/commands/template.d.cts +36 -0
- package/dist/commands/template.js +2 -1
- package/dist/commands/wake.cjs +2626 -0
- package/dist/commands/wake.d.cts +22 -0
- package/dist/commands/wake.d.ts +1 -1
- package/dist/commands/wake.js +10 -9
- package/dist/context-BUGaWpyL.d.cts +46 -0
- package/dist/index.cjs +14526 -0
- package/dist/index.d.cts +858 -0
- package/dist/index.d.ts +192 -7
- package/dist/index.js +101 -75
- package/dist/{inject-x65KXWPk.d.ts → inject-Bzi5E-By.d.cts} +1 -1
- package/dist/inject-Bzi5E-By.d.ts +137 -0
- package/dist/lib/auto-linker.cjs +176 -0
- package/dist/lib/auto-linker.d.cts +26 -0
- package/dist/lib/auto-linker.js +1 -0
- package/dist/lib/canvas-layout.cjs +136 -0
- package/dist/lib/canvas-layout.d.cts +31 -0
- package/dist/lib/canvas-layout.d.ts +16 -100
- package/dist/lib/canvas-layout.js +78 -20
- package/dist/lib/config.cjs +78 -0
- package/dist/lib/config.d.cts +11 -0
- package/dist/lib/config.js +1 -0
- package/dist/lib/entity-index.cjs +84 -0
- package/dist/lib/entity-index.d.cts +26 -0
- package/dist/lib/entity-index.js +1 -0
- package/dist/lib/project-utils.cjs +864 -0
- package/dist/lib/project-utils.d.cts +97 -0
- package/dist/lib/project-utils.js +4 -3
- package/dist/lib/session-repair.cjs +239 -0
- package/dist/lib/session-repair.d.cts +110 -0
- package/dist/lib/session-repair.js +1 -0
- package/dist/lib/session-utils.cjs +209 -0
- package/dist/lib/session-utils.d.cts +63 -0
- package/dist/lib/session-utils.js +1 -0
- package/dist/lib/tailscale.cjs +1183 -0
- package/dist/lib/tailscale.d.cts +225 -0
- package/dist/lib/tailscale.js +1 -0
- package/dist/lib/task-utils.cjs +1137 -0
- package/dist/lib/task-utils.d.cts +208 -0
- package/dist/lib/task-utils.js +3 -2
- package/dist/lib/template-engine.cjs +47 -0
- package/dist/lib/template-engine.d.cts +11 -0
- package/dist/lib/template-engine.js +1 -0
- package/dist/lib/webdav.cjs +568 -0
- package/dist/lib/webdav.d.cts +109 -0
- package/dist/lib/webdav.js +1 -0
- package/dist/plugin/index.cjs +1907 -0
- package/dist/plugin/index.d.cts +36 -0
- package/dist/plugin/index.d.ts +36 -0
- package/dist/plugin/index.js +572 -0
- package/dist/plugin/inject.cjs +356 -0
- package/dist/plugin/inject.d.cts +54 -0
- package/dist/plugin/inject.d.ts +54 -0
- package/dist/plugin/inject.js +17 -0
- package/dist/plugin/observe.cjs +631 -0
- package/dist/plugin/observe.d.cts +39 -0
- package/dist/plugin/observe.d.ts +39 -0
- package/dist/plugin/observe.js +18 -0
- package/dist/plugin/templates.cjs +593 -0
- package/dist/plugin/templates.d.cts +52 -0
- package/dist/plugin/templates.d.ts +52 -0
- package/dist/plugin/templates.js +25 -0
- package/dist/plugin/types.cjs +18 -0
- package/dist/plugin/types.d.cts +209 -0
- package/dist/plugin/types.d.ts +209 -0
- package/dist/plugin/types.js +0 -0
- package/dist/plugin/vault.cjs +927 -0
- package/dist/plugin/vault.d.cts +68 -0
- package/dist/plugin/vault.d.ts +68 -0
- package/dist/plugin/vault.js +22 -0
- package/dist/{types-C74wgGL1.d.ts → types-Y2_Um2Ls.d.cts} +44 -1
- package/dist/types-Y2_Um2Ls.d.ts +205 -0
- package/hooks/clawvault/handler.js +70 -7
- package/hooks/clawvault/handler.test.js +91 -0
- package/openclaw.plugin.json +56 -0
- package/package.json +17 -7
- package/templates/memory-event.md +67 -0
- package/templates/party.md +63 -0
- package/templates/primitive-registry.yaml +551 -0
- package/templates/run.md +68 -0
- package/templates/trigger.md +68 -0
- package/templates/workspace.md +50 -0
- package/dashboard/lib/graph-diff.js +0 -104
- package/dashboard/lib/graph-diff.test.js +0 -75
- package/dashboard/lib/vault-parser.js +0 -556
- package/dashboard/lib/vault-parser.test.js +0 -254
- package/dashboard/public/app.js +0 -796
- package/dashboard/public/index.html +0 -52
- package/dashboard/public/styles.css +0 -221
- package/dashboard/server.js +0 -374
- package/dist/chunk-MAKNAHAW.js +0 -375
- package/dist/chunk-MDIH26GC.js +0 -183
- package/dist/chunk-MGDEINGP.js +0 -99
- package/dist/chunk-RVYA52PY.js +0 -363
- package/dist/chunk-TLGBDTYT.js +0 -33
- package/dist/commands/canvas.d.ts +0 -15
- package/dist/commands/canvas.js +0 -199
- package/dist/commands/sync-bd.d.ts +0 -10
- package/dist/commands/sync-bd.js +0 -9
|
@@ -0,0 +1,1532 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/commands/tailscale.ts
|
|
31
|
+
var tailscale_exports = {};
|
|
32
|
+
__export(tailscale_exports, {
|
|
33
|
+
registerTailscaleCommands: () => registerTailscaleCommands,
|
|
34
|
+
registerTailscaleDiscoverCommand: () => registerTailscaleDiscoverCommand,
|
|
35
|
+
registerTailscaleServeCommand: () => registerTailscaleServeCommand,
|
|
36
|
+
registerTailscaleStatusCommand: () => registerTailscaleStatusCommand,
|
|
37
|
+
registerTailscaleSyncCommand: () => registerTailscaleSyncCommand,
|
|
38
|
+
tailscaleDiscoverCommand: () => tailscaleDiscoverCommand,
|
|
39
|
+
tailscaleServeCommand: () => tailscaleServeCommand,
|
|
40
|
+
tailscaleStatusCommand: () => tailscaleStatusCommand,
|
|
41
|
+
tailscaleSyncCommand: () => tailscaleSyncCommand
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(tailscale_exports);
|
|
44
|
+
var path4 = __toESM(require("path"), 1);
|
|
45
|
+
|
|
46
|
+
// src/lib/config.ts
|
|
47
|
+
var fs = __toESM(require("fs"), 1);
|
|
48
|
+
var path = __toESM(require("path"), 1);
|
|
49
|
+
function findNearestVaultPath(startPath = process.cwd()) {
|
|
50
|
+
let current = path.resolve(startPath);
|
|
51
|
+
while (true) {
|
|
52
|
+
if (fs.existsSync(path.join(current, ".clawvault.json"))) {
|
|
53
|
+
return current;
|
|
54
|
+
}
|
|
55
|
+
const parent = path.dirname(current);
|
|
56
|
+
if (parent === current) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
current = parent;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
function resolveVaultPath(options = {}) {
|
|
63
|
+
if (options.explicitPath) {
|
|
64
|
+
return path.resolve(options.explicitPath);
|
|
65
|
+
}
|
|
66
|
+
if (process.env.CLAWVAULT_PATH) {
|
|
67
|
+
return path.resolve(process.env.CLAWVAULT_PATH);
|
|
68
|
+
}
|
|
69
|
+
const discovered = findNearestVaultPath(options.cwd ?? process.cwd());
|
|
70
|
+
if (discovered) {
|
|
71
|
+
return discovered;
|
|
72
|
+
}
|
|
73
|
+
throw new Error("No vault path found. Set CLAWVAULT_PATH, use --vault, or run inside a vault.");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/lib/tailscale.ts
|
|
77
|
+
var import_child_process = require("child_process");
|
|
78
|
+
var fs3 = __toESM(require("fs"), 1);
|
|
79
|
+
var path3 = __toESM(require("path"), 1);
|
|
80
|
+
var http = __toESM(require("http"), 1);
|
|
81
|
+
var https = __toESM(require("https"), 1);
|
|
82
|
+
|
|
83
|
+
// src/lib/webdav.ts
|
|
84
|
+
var fs2 = __toESM(require("fs"), 1);
|
|
85
|
+
var path2 = __toESM(require("path"), 1);
|
|
86
|
+
var WEBDAV_PREFIX = "/webdav";
|
|
87
|
+
var BLOCKED_PATHS = [
|
|
88
|
+
".clawvault",
|
|
89
|
+
".git",
|
|
90
|
+
".obsidian",
|
|
91
|
+
"node_modules"
|
|
92
|
+
];
|
|
93
|
+
var SUPPORTED_METHODS = ["GET", "PUT", "DELETE", "MKCOL", "PROPFIND", "OPTIONS", "HEAD", "MOVE", "COPY"];
|
|
94
|
+
function toRequestSegments(requestPath) {
|
|
95
|
+
return requestPath.replace(/\\/g, "/").split("/").filter(Boolean);
|
|
96
|
+
}
|
|
97
|
+
function isWithinRoot(fullPath, rootPath) {
|
|
98
|
+
const resolvedRoot = path2.resolve(rootPath);
|
|
99
|
+
const relative2 = path2.relative(resolvedRoot, fullPath);
|
|
100
|
+
return !(relative2.startsWith("..") || path2.isAbsolute(relative2));
|
|
101
|
+
}
|
|
102
|
+
function isPathSafe(requestPath, rootPath) {
|
|
103
|
+
const pathParts = toRequestSegments(requestPath);
|
|
104
|
+
if (pathParts.includes("..")) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
const normalizedRelativePath = path2.normalize(pathParts.join(path2.sep));
|
|
108
|
+
const fullPath = path2.resolve(rootPath, normalizedRelativePath);
|
|
109
|
+
if (!isWithinRoot(fullPath, rootPath)) {
|
|
110
|
+
return false;
|
|
111
|
+
}
|
|
112
|
+
for (const part of pathParts) {
|
|
113
|
+
if (BLOCKED_PATHS.includes(part)) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
function resolveWebDAVPath(requestPath, rootPath) {
|
|
120
|
+
const pathParts = toRequestSegments(requestPath);
|
|
121
|
+
if (pathParts.includes("..")) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
const normalizedRelativePath = path2.normalize(pathParts.join(path2.sep));
|
|
125
|
+
const fullPath = path2.resolve(rootPath, normalizedRelativePath);
|
|
126
|
+
if (!isWithinRoot(fullPath, rootPath)) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
return fullPath;
|
|
130
|
+
}
|
|
131
|
+
function checkAuth(req, auth) {
|
|
132
|
+
if (!auth) {
|
|
133
|
+
return true;
|
|
134
|
+
}
|
|
135
|
+
const authHeader = req.headers.authorization;
|
|
136
|
+
if (!authHeader || !authHeader.startsWith("Basic ")) {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
const base64Credentials = authHeader.slice(6);
|
|
140
|
+
const credentials = Buffer.from(base64Credentials, "base64").toString("utf-8");
|
|
141
|
+
const [username, password] = credentials.split(":");
|
|
142
|
+
return username === auth.username && password === auth.password;
|
|
143
|
+
}
|
|
144
|
+
function escapeXml(str) {
|
|
145
|
+
return str.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
146
|
+
}
|
|
147
|
+
function formatWebDAVDate(date) {
|
|
148
|
+
return date.toUTCString();
|
|
149
|
+
}
|
|
150
|
+
function generatePropfindEntry(href, stats, isCollection) {
|
|
151
|
+
const resourceType = isCollection ? "<D:resourcetype><D:collection/></D:resourcetype>" : "<D:resourcetype/>";
|
|
152
|
+
const contentLength = stats && !isCollection ? `<D:getcontentlength>${stats.size}</D:getcontentlength>` : "";
|
|
153
|
+
const lastModified = stats ? `<D:getlastmodified>${formatWebDAVDate(stats.mtime)}</D:getlastmodified>` : "";
|
|
154
|
+
const etag = stats ? `<D:getetag>"${stats.mtime.getTime().toString(16)}-${stats.size.toString(16)}"</D:getetag>` : "";
|
|
155
|
+
const contentType = !isCollection ? "<D:getcontenttype>application/octet-stream</D:getcontenttype>" : "";
|
|
156
|
+
return ` <D:response>
|
|
157
|
+
<D:href>${escapeXml(href)}</D:href>
|
|
158
|
+
<D:propstat>
|
|
159
|
+
<D:prop>
|
|
160
|
+
${resourceType}
|
|
161
|
+
${contentLength}
|
|
162
|
+
${lastModified}
|
|
163
|
+
${etag}
|
|
164
|
+
${contentType}
|
|
165
|
+
</D:prop>
|
|
166
|
+
<D:status>HTTP/1.1 200 OK</D:status>
|
|
167
|
+
</D:propstat>
|
|
168
|
+
</D:response>`;
|
|
169
|
+
}
|
|
170
|
+
function generatePropfindResponse(entries) {
|
|
171
|
+
const responseEntries = entries.map(
|
|
172
|
+
(e) => generatePropfindEntry(e.href, e.stats, e.isCollection)
|
|
173
|
+
).join("\n");
|
|
174
|
+
return `<?xml version="1.0" encoding="utf-8"?>
|
|
175
|
+
<D:multistatus xmlns:D="DAV:">
|
|
176
|
+
${responseEntries}
|
|
177
|
+
</D:multistatus>`;
|
|
178
|
+
}
|
|
179
|
+
function handleOptions(res, prefix) {
|
|
180
|
+
res.writeHead(200, {
|
|
181
|
+
"Allow": SUPPORTED_METHODS.join(", "),
|
|
182
|
+
"DAV": "1, 2",
|
|
183
|
+
"Content-Length": "0",
|
|
184
|
+
"Access-Control-Allow-Origin": "*",
|
|
185
|
+
"Access-Control-Allow-Methods": SUPPORTED_METHODS.join(", "),
|
|
186
|
+
"Access-Control-Allow-Headers": "Content-Type, Depth, Destination, Overwrite, Authorization",
|
|
187
|
+
"MS-Author-Via": "DAV"
|
|
188
|
+
});
|
|
189
|
+
res.end();
|
|
190
|
+
}
|
|
191
|
+
function handleHead(res, filePath) {
|
|
192
|
+
try {
|
|
193
|
+
const stats = fs2.statSync(filePath);
|
|
194
|
+
if (stats.isDirectory()) {
|
|
195
|
+
res.writeHead(200, {
|
|
196
|
+
"Content-Type": "httpd/unix-directory",
|
|
197
|
+
"Last-Modified": formatWebDAVDate(stats.mtime),
|
|
198
|
+
"ETag": `"${stats.mtime.getTime().toString(16)}"`,
|
|
199
|
+
"Access-Control-Allow-Origin": "*"
|
|
200
|
+
});
|
|
201
|
+
} else {
|
|
202
|
+
res.writeHead(200, {
|
|
203
|
+
"Content-Type": "application/octet-stream",
|
|
204
|
+
"Content-Length": stats.size.toString(),
|
|
205
|
+
"Last-Modified": formatWebDAVDate(stats.mtime),
|
|
206
|
+
"ETag": `"${stats.mtime.getTime().toString(16)}-${stats.size.toString(16)}"`,
|
|
207
|
+
"Access-Control-Allow-Origin": "*"
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
res.end();
|
|
211
|
+
} catch (err) {
|
|
212
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
213
|
+
res.end("Not Found");
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function handleGet(res, filePath) {
|
|
217
|
+
try {
|
|
218
|
+
const stats = fs2.statSync(filePath);
|
|
219
|
+
if (stats.isDirectory()) {
|
|
220
|
+
const entries = fs2.readdirSync(filePath);
|
|
221
|
+
const listing = entries.join("\n");
|
|
222
|
+
res.writeHead(200, {
|
|
223
|
+
"Content-Type": "text/plain",
|
|
224
|
+
"Content-Length": Buffer.byteLength(listing).toString(),
|
|
225
|
+
"Access-Control-Allow-Origin": "*"
|
|
226
|
+
});
|
|
227
|
+
res.end(listing);
|
|
228
|
+
} else {
|
|
229
|
+
const content = fs2.readFileSync(filePath);
|
|
230
|
+
res.writeHead(200, {
|
|
231
|
+
"Content-Type": "application/octet-stream",
|
|
232
|
+
"Content-Length": content.length.toString(),
|
|
233
|
+
"Last-Modified": formatWebDAVDate(stats.mtime),
|
|
234
|
+
"ETag": `"${stats.mtime.getTime().toString(16)}-${stats.size.toString(16)}"`,
|
|
235
|
+
"Access-Control-Allow-Origin": "*"
|
|
236
|
+
});
|
|
237
|
+
res.end(content);
|
|
238
|
+
}
|
|
239
|
+
} catch (err) {
|
|
240
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
241
|
+
res.end("Not Found");
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
function handlePut(res, filePath, body) {
|
|
245
|
+
try {
|
|
246
|
+
const exists = fs2.existsSync(filePath);
|
|
247
|
+
const dir = path2.dirname(filePath);
|
|
248
|
+
if (!fs2.existsSync(dir)) {
|
|
249
|
+
fs2.mkdirSync(dir, { recursive: true });
|
|
250
|
+
}
|
|
251
|
+
fs2.writeFileSync(filePath, body);
|
|
252
|
+
const status = exists ? 204 : 201;
|
|
253
|
+
res.writeHead(status, {
|
|
254
|
+
"Content-Length": "0",
|
|
255
|
+
"Access-Control-Allow-Origin": "*"
|
|
256
|
+
});
|
|
257
|
+
res.end();
|
|
258
|
+
} catch (err) {
|
|
259
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
260
|
+
res.end(`Error: ${err}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
function handleDelete(res, filePath) {
|
|
264
|
+
try {
|
|
265
|
+
if (!fs2.existsSync(filePath)) {
|
|
266
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
267
|
+
res.end("Not Found");
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
const stats = fs2.statSync(filePath);
|
|
271
|
+
if (stats.isDirectory()) {
|
|
272
|
+
fs2.rmSync(filePath, { recursive: true });
|
|
273
|
+
} else {
|
|
274
|
+
fs2.unlinkSync(filePath);
|
|
275
|
+
}
|
|
276
|
+
res.writeHead(204, {
|
|
277
|
+
"Content-Length": "0",
|
|
278
|
+
"Access-Control-Allow-Origin": "*"
|
|
279
|
+
});
|
|
280
|
+
res.end();
|
|
281
|
+
} catch (err) {
|
|
282
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
283
|
+
res.end(`Error: ${err}`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function handleMkcol(res, filePath) {
|
|
287
|
+
try {
|
|
288
|
+
if (fs2.existsSync(filePath)) {
|
|
289
|
+
res.writeHead(405, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
290
|
+
res.end("Resource already exists");
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
const parent = path2.dirname(filePath);
|
|
294
|
+
if (!fs2.existsSync(parent)) {
|
|
295
|
+
res.writeHead(409, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
296
|
+
res.end("Parent directory does not exist");
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
fs2.mkdirSync(filePath);
|
|
300
|
+
res.writeHead(201, {
|
|
301
|
+
"Content-Length": "0",
|
|
302
|
+
"Access-Control-Allow-Origin": "*"
|
|
303
|
+
});
|
|
304
|
+
res.end();
|
|
305
|
+
} catch (err) {
|
|
306
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
307
|
+
res.end(`Error: ${err}`);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
function handlePropfind(res, filePath, webdavPath, prefix, depth) {
|
|
311
|
+
try {
|
|
312
|
+
if (!fs2.existsSync(filePath)) {
|
|
313
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
314
|
+
res.end("Not Found");
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
const stats = fs2.statSync(filePath);
|
|
318
|
+
const entries = [];
|
|
319
|
+
const normalizedWebdavPath = webdavPath.startsWith("/") ? webdavPath : "/" + webdavPath;
|
|
320
|
+
const href = prefix + normalizedWebdavPath;
|
|
321
|
+
entries.push({
|
|
322
|
+
href: href.endsWith("/") || stats.isDirectory() ? href : href,
|
|
323
|
+
stats,
|
|
324
|
+
isCollection: stats.isDirectory()
|
|
325
|
+
});
|
|
326
|
+
if (stats.isDirectory() && depth !== "0") {
|
|
327
|
+
try {
|
|
328
|
+
const children = fs2.readdirSync(filePath);
|
|
329
|
+
for (const child of children) {
|
|
330
|
+
if (BLOCKED_PATHS.includes(child)) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
const childPath = path2.join(filePath, child);
|
|
334
|
+
const childWebdavPath = normalizedWebdavPath.endsWith("/") ? normalizedWebdavPath + child : normalizedWebdavPath + "/" + child;
|
|
335
|
+
try {
|
|
336
|
+
const childStats = fs2.statSync(childPath);
|
|
337
|
+
entries.push({
|
|
338
|
+
href: prefix + childWebdavPath,
|
|
339
|
+
stats: childStats,
|
|
340
|
+
isCollection: childStats.isDirectory()
|
|
341
|
+
});
|
|
342
|
+
} catch {
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
} catch {
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
const xml = generatePropfindResponse(entries);
|
|
349
|
+
res.writeHead(207, {
|
|
350
|
+
"Content-Type": "application/xml; charset=utf-8",
|
|
351
|
+
"Content-Length": Buffer.byteLength(xml).toString(),
|
|
352
|
+
"Access-Control-Allow-Origin": "*"
|
|
353
|
+
});
|
|
354
|
+
res.end(xml);
|
|
355
|
+
} catch (err) {
|
|
356
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
357
|
+
res.end(`Error: ${err}`);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function handleMove(res, sourcePath, destinationPath, overwrite) {
|
|
361
|
+
try {
|
|
362
|
+
if (!fs2.existsSync(sourcePath)) {
|
|
363
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
364
|
+
res.end("Source not found");
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
if (!destinationPath) {
|
|
368
|
+
res.writeHead(400, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
369
|
+
res.end("Destination header required");
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
const destExists = fs2.existsSync(destinationPath);
|
|
373
|
+
if (destExists && !overwrite) {
|
|
374
|
+
res.writeHead(412, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
375
|
+
res.end("Destination exists and Overwrite is F");
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const destDir = path2.dirname(destinationPath);
|
|
379
|
+
if (!fs2.existsSync(destDir)) {
|
|
380
|
+
fs2.mkdirSync(destDir, { recursive: true });
|
|
381
|
+
}
|
|
382
|
+
if (destExists) {
|
|
383
|
+
const destStats = fs2.statSync(destinationPath);
|
|
384
|
+
if (destStats.isDirectory()) {
|
|
385
|
+
fs2.rmSync(destinationPath, { recursive: true });
|
|
386
|
+
} else {
|
|
387
|
+
fs2.unlinkSync(destinationPath);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
fs2.renameSync(sourcePath, destinationPath);
|
|
391
|
+
const status = destExists ? 204 : 201;
|
|
392
|
+
res.writeHead(status, {
|
|
393
|
+
"Content-Length": "0",
|
|
394
|
+
"Access-Control-Allow-Origin": "*"
|
|
395
|
+
});
|
|
396
|
+
res.end();
|
|
397
|
+
} catch (err) {
|
|
398
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
399
|
+
res.end(`Error: ${err}`);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
function handleCopy(res, sourcePath, destinationPath, overwrite) {
|
|
403
|
+
try {
|
|
404
|
+
if (!fs2.existsSync(sourcePath)) {
|
|
405
|
+
res.writeHead(404, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
406
|
+
res.end("Source not found");
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (!destinationPath) {
|
|
410
|
+
res.writeHead(400, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
411
|
+
res.end("Destination header required");
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
const destExists = fs2.existsSync(destinationPath);
|
|
415
|
+
if (destExists && !overwrite) {
|
|
416
|
+
res.writeHead(412, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
417
|
+
res.end("Destination exists and Overwrite is F");
|
|
418
|
+
return;
|
|
419
|
+
}
|
|
420
|
+
const destDir = path2.dirname(destinationPath);
|
|
421
|
+
if (!fs2.existsSync(destDir)) {
|
|
422
|
+
fs2.mkdirSync(destDir, { recursive: true });
|
|
423
|
+
}
|
|
424
|
+
const sourceStats = fs2.statSync(sourcePath);
|
|
425
|
+
if (sourceStats.isDirectory()) {
|
|
426
|
+
copyDirRecursive(sourcePath, destinationPath);
|
|
427
|
+
} else {
|
|
428
|
+
fs2.copyFileSync(sourcePath, destinationPath);
|
|
429
|
+
}
|
|
430
|
+
const status = destExists ? 204 : 201;
|
|
431
|
+
res.writeHead(status, {
|
|
432
|
+
"Content-Length": "0",
|
|
433
|
+
"Access-Control-Allow-Origin": "*"
|
|
434
|
+
});
|
|
435
|
+
res.end();
|
|
436
|
+
} catch (err) {
|
|
437
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
438
|
+
res.end(`Error: ${err}`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function copyDirRecursive(src, dest) {
|
|
442
|
+
if (!fs2.existsSync(dest)) {
|
|
443
|
+
fs2.mkdirSync(dest, { recursive: true });
|
|
444
|
+
}
|
|
445
|
+
const entries = fs2.readdirSync(src, { withFileTypes: true });
|
|
446
|
+
for (const entry of entries) {
|
|
447
|
+
const srcPath = path2.join(src, entry.name);
|
|
448
|
+
const destPath = path2.join(dest, entry.name);
|
|
449
|
+
if (entry.isDirectory()) {
|
|
450
|
+
copyDirRecursive(srcPath, destPath);
|
|
451
|
+
} else {
|
|
452
|
+
fs2.copyFileSync(srcPath, destPath);
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function parseDestinationHeader(destinationHeader, prefix, rootPath) {
|
|
457
|
+
if (!destinationHeader) {
|
|
458
|
+
return null;
|
|
459
|
+
}
|
|
460
|
+
try {
|
|
461
|
+
let destPath;
|
|
462
|
+
if (destinationHeader.startsWith("http://") || destinationHeader.startsWith("https://")) {
|
|
463
|
+
const url = new URL(destinationHeader);
|
|
464
|
+
destPath = decodeURIComponent(url.pathname);
|
|
465
|
+
} else {
|
|
466
|
+
destPath = decodeURIComponent(destinationHeader);
|
|
467
|
+
}
|
|
468
|
+
if (destPath.startsWith(prefix)) {
|
|
469
|
+
destPath = destPath.slice(prefix.length);
|
|
470
|
+
}
|
|
471
|
+
return resolveWebDAVPath(destPath, rootPath);
|
|
472
|
+
} catch {
|
|
473
|
+
return null;
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
function createWebDAVHandler(config) {
|
|
477
|
+
const { rootPath, prefix = WEBDAV_PREFIX, auth } = config;
|
|
478
|
+
return async (req, res) => {
|
|
479
|
+
const rawUrl = req.url || "/";
|
|
480
|
+
if (rawUrl.includes("..")) {
|
|
481
|
+
if (rawUrl.startsWith(prefix)) {
|
|
482
|
+
res.writeHead(403, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
483
|
+
res.end("Forbidden");
|
|
484
|
+
return true;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const url = new URL(rawUrl, `http://${req.headers.host || "localhost"}`);
|
|
488
|
+
const pathname = decodeURIComponent(url.pathname);
|
|
489
|
+
if (!pathname.startsWith(prefix)) {
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
let webdavPath = pathname.slice(prefix.length);
|
|
493
|
+
if (!webdavPath.startsWith("/")) {
|
|
494
|
+
webdavPath = "/" + webdavPath;
|
|
495
|
+
}
|
|
496
|
+
if (req.method === "OPTIONS") {
|
|
497
|
+
handleOptions(res, prefix);
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
if (!checkAuth(req, auth)) {
|
|
501
|
+
res.writeHead(401, {
|
|
502
|
+
"WWW-Authenticate": 'Basic realm="ClawVault WebDAV"',
|
|
503
|
+
"Content-Type": "text/plain",
|
|
504
|
+
"Access-Control-Allow-Origin": "*"
|
|
505
|
+
});
|
|
506
|
+
res.end("Unauthorized");
|
|
507
|
+
return true;
|
|
508
|
+
}
|
|
509
|
+
if (!isPathSafe(webdavPath, rootPath)) {
|
|
510
|
+
res.writeHead(403, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
511
|
+
res.end("Forbidden");
|
|
512
|
+
return true;
|
|
513
|
+
}
|
|
514
|
+
const filePath = resolveWebDAVPath(webdavPath, rootPath);
|
|
515
|
+
if (!filePath) {
|
|
516
|
+
res.writeHead(403, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
517
|
+
res.end("Forbidden");
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
const depth = req.headers.depth || "infinity";
|
|
521
|
+
const overwrite = req.headers.overwrite?.toUpperCase() !== "F";
|
|
522
|
+
const destinationHeader = req.headers.destination;
|
|
523
|
+
switch (req.method) {
|
|
524
|
+
case "HEAD":
|
|
525
|
+
handleHead(res, filePath);
|
|
526
|
+
return true;
|
|
527
|
+
case "GET":
|
|
528
|
+
handleGet(res, filePath);
|
|
529
|
+
return true;
|
|
530
|
+
case "PUT": {
|
|
531
|
+
const chunks = [];
|
|
532
|
+
for await (const chunk of req) {
|
|
533
|
+
chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
|
|
534
|
+
}
|
|
535
|
+
const body = Buffer.concat(chunks);
|
|
536
|
+
handlePut(res, filePath, body);
|
|
537
|
+
return true;
|
|
538
|
+
}
|
|
539
|
+
case "DELETE":
|
|
540
|
+
handleDelete(res, filePath);
|
|
541
|
+
return true;
|
|
542
|
+
case "MKCOL":
|
|
543
|
+
handleMkcol(res, filePath);
|
|
544
|
+
return true;
|
|
545
|
+
case "PROPFIND":
|
|
546
|
+
handlePropfind(res, filePath, webdavPath, prefix, depth);
|
|
547
|
+
return true;
|
|
548
|
+
case "MOVE": {
|
|
549
|
+
const destPath = parseDestinationHeader(destinationHeader, prefix, rootPath);
|
|
550
|
+
if (destPath && destinationHeader) {
|
|
551
|
+
const destWebdavPath = destinationHeader.includes(prefix) ? destinationHeader.slice(destinationHeader.indexOf(prefix) + prefix.length) : destinationHeader;
|
|
552
|
+
if (!isPathSafe(destWebdavPath, rootPath)) {
|
|
553
|
+
res.writeHead(403, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
554
|
+
res.end("Forbidden");
|
|
555
|
+
return true;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
handleMove(res, filePath, destPath, overwrite);
|
|
559
|
+
return true;
|
|
560
|
+
}
|
|
561
|
+
case "COPY": {
|
|
562
|
+
const destPath = parseDestinationHeader(destinationHeader, prefix, rootPath);
|
|
563
|
+
if (destPath && destinationHeader) {
|
|
564
|
+
const destWebdavPath = destinationHeader.includes(prefix) ? destinationHeader.slice(destinationHeader.indexOf(prefix) + prefix.length) : destinationHeader;
|
|
565
|
+
if (!isPathSafe(destWebdavPath, rootPath)) {
|
|
566
|
+
res.writeHead(403, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
567
|
+
res.end("Forbidden");
|
|
568
|
+
return true;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
handleCopy(res, filePath, destPath, overwrite);
|
|
572
|
+
return true;
|
|
573
|
+
}
|
|
574
|
+
default:
|
|
575
|
+
res.writeHead(405, {
|
|
576
|
+
"Allow": SUPPORTED_METHODS.join(", "),
|
|
577
|
+
"Content-Type": "text/plain",
|
|
578
|
+
"Access-Control-Allow-Origin": "*"
|
|
579
|
+
});
|
|
580
|
+
res.end("Method Not Allowed");
|
|
581
|
+
return true;
|
|
582
|
+
}
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// src/lib/tailscale.ts
|
|
587
|
+
var crypto = __toESM(require("crypto"), 1);
|
|
588
|
+
var DEFAULT_SERVE_PORT = 8384;
|
|
589
|
+
var CLAWVAULT_SERVE_PATH = "/.clawvault";
|
|
590
|
+
function hasTailscale() {
|
|
591
|
+
const probe = (0, import_child_process.spawnSync)("tailscale", ["version"], {
|
|
592
|
+
stdio: "pipe",
|
|
593
|
+
encoding: "utf-8",
|
|
594
|
+
timeout: 5e3
|
|
595
|
+
});
|
|
596
|
+
return !probe.error && probe.status === 0;
|
|
597
|
+
}
|
|
598
|
+
function getTailscaleVersion() {
|
|
599
|
+
const result = (0, import_child_process.spawnSync)("tailscale", ["version"], {
|
|
600
|
+
stdio: "pipe",
|
|
601
|
+
encoding: "utf-8",
|
|
602
|
+
timeout: 5e3
|
|
603
|
+
});
|
|
604
|
+
if (result.error || result.status !== 0) {
|
|
605
|
+
return null;
|
|
606
|
+
}
|
|
607
|
+
const lines = result.stdout.trim().split("\n");
|
|
608
|
+
return lines[0] || null;
|
|
609
|
+
}
|
|
610
|
+
function getTailscaleStatus() {
|
|
611
|
+
const status = {
|
|
612
|
+
installed: false,
|
|
613
|
+
running: false,
|
|
614
|
+
connected: false,
|
|
615
|
+
peers: []
|
|
616
|
+
};
|
|
617
|
+
if (!hasTailscale()) {
|
|
618
|
+
status.error = "Tailscale CLI not found. Install from https://tailscale.com/download";
|
|
619
|
+
return status;
|
|
620
|
+
}
|
|
621
|
+
status.installed = true;
|
|
622
|
+
const result = (0, import_child_process.spawnSync)("tailscale", ["status", "--json"], {
|
|
623
|
+
stdio: "pipe",
|
|
624
|
+
encoding: "utf-8",
|
|
625
|
+
timeout: 1e4
|
|
626
|
+
});
|
|
627
|
+
if (result.error) {
|
|
628
|
+
status.error = `Failed to get Tailscale status: ${result.error.message}`;
|
|
629
|
+
return status;
|
|
630
|
+
}
|
|
631
|
+
if (result.status !== 0) {
|
|
632
|
+
status.error = result.stderr?.trim() || "Tailscale daemon not running";
|
|
633
|
+
return status;
|
|
634
|
+
}
|
|
635
|
+
try {
|
|
636
|
+
const data = JSON.parse(result.stdout);
|
|
637
|
+
status.running = true;
|
|
638
|
+
status.backendState = data.BackendState;
|
|
639
|
+
status.connected = data.BackendState === "Running";
|
|
640
|
+
status.tailnetName = data.CurrentTailnet?.Name;
|
|
641
|
+
if (data.Self) {
|
|
642
|
+
status.selfIP = data.Self.TailscaleIPs?.[0];
|
|
643
|
+
status.selfHostname = data.Self.HostName;
|
|
644
|
+
status.selfDNSName = data.Self.DNSName;
|
|
645
|
+
}
|
|
646
|
+
if (data.Peer) {
|
|
647
|
+
for (const [_, peerData] of Object.entries(data.Peer)) {
|
|
648
|
+
const peer = {
|
|
649
|
+
hostname: peerData.HostName || "",
|
|
650
|
+
dnsName: peerData.DNSName || "",
|
|
651
|
+
tailscaleIPs: peerData.TailscaleIPs || [],
|
|
652
|
+
online: peerData.Online || false,
|
|
653
|
+
os: peerData.OS,
|
|
654
|
+
exitNode: peerData.ExitNode,
|
|
655
|
+
tags: peerData.Tags,
|
|
656
|
+
lastSeen: peerData.LastSeen
|
|
657
|
+
};
|
|
658
|
+
status.peers.push(peer);
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
} catch (err) {
|
|
662
|
+
status.error = `Failed to parse Tailscale status: ${err}`;
|
|
663
|
+
}
|
|
664
|
+
return status;
|
|
665
|
+
}
|
|
666
|
+
function findPeer(hostname) {
|
|
667
|
+
const status = getTailscaleStatus();
|
|
668
|
+
if (!status.connected) {
|
|
669
|
+
return null;
|
|
670
|
+
}
|
|
671
|
+
const normalizedSearch = hostname.toLowerCase();
|
|
672
|
+
let peer = status.peers.find(
|
|
673
|
+
(p) => p.hostname.toLowerCase() === normalizedSearch
|
|
674
|
+
);
|
|
675
|
+
if (peer) return peer;
|
|
676
|
+
peer = status.peers.find(
|
|
677
|
+
(p) => p.dnsName.toLowerCase().startsWith(normalizedSearch)
|
|
678
|
+
);
|
|
679
|
+
if (peer) return peer;
|
|
680
|
+
peer = status.peers.find(
|
|
681
|
+
(p) => p.hostname.toLowerCase().includes(normalizedSearch)
|
|
682
|
+
);
|
|
683
|
+
return peer || null;
|
|
684
|
+
}
|
|
685
|
+
function getOnlinePeers() {
|
|
686
|
+
const status = getTailscaleStatus();
|
|
687
|
+
return status.peers.filter((p) => p.online);
|
|
688
|
+
}
|
|
689
|
+
function resolvePeerIP(hostname) {
|
|
690
|
+
const peer = findPeer(hostname);
|
|
691
|
+
return peer?.tailscaleIPs[0] || null;
|
|
692
|
+
}
|
|
693
|
+
function calculateChecksum(filePath) {
|
|
694
|
+
const content = fs3.readFileSync(filePath);
|
|
695
|
+
return crypto.createHash("sha256").update(content).digest("hex");
|
|
696
|
+
}
|
|
697
|
+
function generateVaultManifest(vaultPath) {
|
|
698
|
+
const configPath = path3.join(vaultPath, ".clawvault.json");
|
|
699
|
+
if (!fs3.existsSync(configPath)) {
|
|
700
|
+
throw new Error(`Not a ClawVault: ${vaultPath}`);
|
|
701
|
+
}
|
|
702
|
+
const config = JSON.parse(fs3.readFileSync(configPath, "utf-8"));
|
|
703
|
+
const files = [];
|
|
704
|
+
function walkDir(dir, relativePath = "") {
|
|
705
|
+
const entries = fs3.readdirSync(dir, { withFileTypes: true });
|
|
706
|
+
for (const entry of entries) {
|
|
707
|
+
const fullPath = path3.join(dir, entry.name);
|
|
708
|
+
const relPath = path3.join(relativePath, entry.name);
|
|
709
|
+
if (entry.name.startsWith(".") && entry.name !== ".clawvault.json") {
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
if (entry.name === "node_modules") {
|
|
713
|
+
continue;
|
|
714
|
+
}
|
|
715
|
+
if (entry.isDirectory()) {
|
|
716
|
+
walkDir(fullPath, relPath);
|
|
717
|
+
} else if (entry.isFile() && (entry.name.endsWith(".md") || entry.name === ".clawvault.json")) {
|
|
718
|
+
const stats = fs3.statSync(fullPath);
|
|
719
|
+
const category = relativePath.split(path3.sep)[0] || "root";
|
|
720
|
+
files.push({
|
|
721
|
+
path: relPath,
|
|
722
|
+
size: stats.size,
|
|
723
|
+
modified: stats.mtime.toISOString(),
|
|
724
|
+
checksum: calculateChecksum(fullPath),
|
|
725
|
+
category
|
|
726
|
+
});
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
walkDir(vaultPath);
|
|
731
|
+
return {
|
|
732
|
+
name: config.name,
|
|
733
|
+
version: config.version || "1.0.0",
|
|
734
|
+
lastUpdated: (/* @__PURE__ */ new Date()).toISOString(),
|
|
735
|
+
files
|
|
736
|
+
};
|
|
737
|
+
}
|
|
738
|
+
function compareManifests(local, remote) {
|
|
739
|
+
const localFiles = new Map(local.files.map((f) => [f.path, f]));
|
|
740
|
+
const remoteFiles = new Map(remote.files.map((f) => [f.path, f]));
|
|
741
|
+
const toPush = [];
|
|
742
|
+
const toPull = [];
|
|
743
|
+
const conflicts = [];
|
|
744
|
+
const unchanged = [];
|
|
745
|
+
for (const [filePath, localFile] of localFiles) {
|
|
746
|
+
const remoteFile = remoteFiles.get(filePath);
|
|
747
|
+
if (!remoteFile) {
|
|
748
|
+
toPush.push(localFile);
|
|
749
|
+
} else if (localFile.checksum === remoteFile.checksum) {
|
|
750
|
+
unchanged.push(filePath);
|
|
751
|
+
} else {
|
|
752
|
+
const localTime = new Date(localFile.modified).getTime();
|
|
753
|
+
const remoteTime = new Date(remoteFile.modified).getTime();
|
|
754
|
+
if (localTime > remoteTime) {
|
|
755
|
+
toPush.push(localFile);
|
|
756
|
+
} else if (remoteTime > localTime) {
|
|
757
|
+
toPull.push(remoteFile);
|
|
758
|
+
} else {
|
|
759
|
+
conflicts.push({ path: filePath, local: localFile, remote: remoteFile });
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
for (const [filePath, remoteFile] of remoteFiles) {
|
|
764
|
+
if (!localFiles.has(filePath)) {
|
|
765
|
+
toPull.push(remoteFile);
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
return { toPush, toPull, conflicts, unchanged };
|
|
769
|
+
}
|
|
770
|
+
function serveVault(vaultPath, options = {}) {
|
|
771
|
+
const port = options.port || DEFAULT_SERVE_PORT;
|
|
772
|
+
const pathPrefix = options.pathPrefix || CLAWVAULT_SERVE_PATH;
|
|
773
|
+
if (!fs3.existsSync(path3.join(vaultPath, ".clawvault.json"))) {
|
|
774
|
+
throw new Error(`Not a ClawVault: ${vaultPath}`);
|
|
775
|
+
}
|
|
776
|
+
const webdavHandler = createWebDAVHandler({
|
|
777
|
+
rootPath: vaultPath,
|
|
778
|
+
prefix: WEBDAV_PREFIX,
|
|
779
|
+
auth: options.webdavAuth
|
|
780
|
+
});
|
|
781
|
+
const server = http.createServer(async (req, res) => {
|
|
782
|
+
const url = new URL(req.url || "/", `http://localhost:${port}`);
|
|
783
|
+
const pathname = url.pathname;
|
|
784
|
+
if (pathname.startsWith(WEBDAV_PREFIX)) {
|
|
785
|
+
try {
|
|
786
|
+
const handled = await webdavHandler(req, res);
|
|
787
|
+
if (handled) return;
|
|
788
|
+
} catch (err) {
|
|
789
|
+
res.writeHead(500, { "Content-Type": "text/plain", "Access-Control-Allow-Origin": "*" });
|
|
790
|
+
res.end(`WebDAV Error: ${err}`);
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
795
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
|
796
|
+
res.setHeader("Access-Control-Allow-Headers", "Content-Type");
|
|
797
|
+
if (req.method === "OPTIONS") {
|
|
798
|
+
res.writeHead(200);
|
|
799
|
+
res.end();
|
|
800
|
+
return;
|
|
801
|
+
}
|
|
802
|
+
if (pathname === `${pathPrefix}/health`) {
|
|
803
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
804
|
+
res.end(JSON.stringify({ status: "ok", vault: path3.basename(vaultPath) }));
|
|
805
|
+
return;
|
|
806
|
+
}
|
|
807
|
+
if (pathname === `${pathPrefix}/manifest`) {
|
|
808
|
+
try {
|
|
809
|
+
const manifest = generateVaultManifest(vaultPath);
|
|
810
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
811
|
+
res.end(JSON.stringify(manifest));
|
|
812
|
+
} catch (err) {
|
|
813
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
814
|
+
res.end(JSON.stringify({ error: String(err) }));
|
|
815
|
+
}
|
|
816
|
+
return;
|
|
817
|
+
}
|
|
818
|
+
if (pathname.startsWith(`${pathPrefix}/files/`)) {
|
|
819
|
+
const relativePath = decodeURIComponent(pathname.slice(`${pathPrefix}/files/`.length));
|
|
820
|
+
const filePath = path3.join(vaultPath, relativePath);
|
|
821
|
+
const resolvedPath = path3.resolve(filePath);
|
|
822
|
+
const resolvedVault = path3.resolve(vaultPath);
|
|
823
|
+
if (!resolvedPath.startsWith(resolvedVault)) {
|
|
824
|
+
res.writeHead(403, { "Content-Type": "application/json" });
|
|
825
|
+
res.end(JSON.stringify({ error: "Access denied" }));
|
|
826
|
+
return;
|
|
827
|
+
}
|
|
828
|
+
if (!fs3.existsSync(filePath)) {
|
|
829
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
830
|
+
res.end(JSON.stringify({ error: "File not found" }));
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
try {
|
|
834
|
+
const content = fs3.readFileSync(filePath, "utf-8");
|
|
835
|
+
const stats = fs3.statSync(filePath);
|
|
836
|
+
res.writeHead(200, {
|
|
837
|
+
"Content-Type": "text/markdown",
|
|
838
|
+
"Content-Length": Buffer.byteLength(content),
|
|
839
|
+
"Last-Modified": stats.mtime.toUTCString()
|
|
840
|
+
});
|
|
841
|
+
res.end(content);
|
|
842
|
+
} catch (err) {
|
|
843
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
844
|
+
res.end(JSON.stringify({ error: String(err) }));
|
|
845
|
+
}
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
if (pathname.startsWith(`${pathPrefix}/upload/`) && req.method === "POST") {
|
|
849
|
+
const relativePath = decodeURIComponent(pathname.slice(`${pathPrefix}/upload/`.length));
|
|
850
|
+
const filePath = path3.join(vaultPath, relativePath);
|
|
851
|
+
const resolvedPath = path3.resolve(filePath);
|
|
852
|
+
const resolvedVault = path3.resolve(vaultPath);
|
|
853
|
+
if (!resolvedPath.startsWith(resolvedVault)) {
|
|
854
|
+
res.writeHead(403, { "Content-Type": "application/json" });
|
|
855
|
+
res.end(JSON.stringify({ error: "Access denied" }));
|
|
856
|
+
return;
|
|
857
|
+
}
|
|
858
|
+
let body = "";
|
|
859
|
+
req.on("data", (chunk) => {
|
|
860
|
+
body += chunk;
|
|
861
|
+
});
|
|
862
|
+
req.on("end", () => {
|
|
863
|
+
try {
|
|
864
|
+
const dir = path3.dirname(filePath);
|
|
865
|
+
if (!fs3.existsSync(dir)) {
|
|
866
|
+
fs3.mkdirSync(dir, { recursive: true });
|
|
867
|
+
}
|
|
868
|
+
fs3.writeFileSync(filePath, body, "utf-8");
|
|
869
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
870
|
+
res.end(JSON.stringify({ success: true, path: relativePath }));
|
|
871
|
+
} catch (err) {
|
|
872
|
+
res.writeHead(500, { "Content-Type": "application/json" });
|
|
873
|
+
res.end(JSON.stringify({ error: String(err) }));
|
|
874
|
+
}
|
|
875
|
+
});
|
|
876
|
+
return;
|
|
877
|
+
}
|
|
878
|
+
if (pathname === pathPrefix || pathname === `${pathPrefix}/`) {
|
|
879
|
+
res.writeHead(200, { "Content-Type": "application/json" });
|
|
880
|
+
res.end(JSON.stringify({
|
|
881
|
+
service: "clawvault-sync",
|
|
882
|
+
version: "1.0.0",
|
|
883
|
+
vault: path3.basename(vaultPath),
|
|
884
|
+
endpoints: {
|
|
885
|
+
health: `${pathPrefix}/health`,
|
|
886
|
+
manifest: `${pathPrefix}/manifest`,
|
|
887
|
+
files: `${pathPrefix}/files/<path>`,
|
|
888
|
+
upload: `${pathPrefix}/upload/<path>`,
|
|
889
|
+
webdav: `${WEBDAV_PREFIX}/`
|
|
890
|
+
}
|
|
891
|
+
}));
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
res.writeHead(404, { "Content-Type": "application/json" });
|
|
895
|
+
res.end(JSON.stringify({ error: "Not found" }));
|
|
896
|
+
});
|
|
897
|
+
server.listen(port, "0.0.0.0");
|
|
898
|
+
return {
|
|
899
|
+
server,
|
|
900
|
+
port,
|
|
901
|
+
stop: () => new Promise((resolve4, reject) => {
|
|
902
|
+
server.close((err) => {
|
|
903
|
+
if (err) reject(err);
|
|
904
|
+
else resolve4();
|
|
905
|
+
});
|
|
906
|
+
})
|
|
907
|
+
};
|
|
908
|
+
}
|
|
909
|
+
async function fetchRemoteManifest(host, port = DEFAULT_SERVE_PORT, useHttps = false) {
|
|
910
|
+
return new Promise((resolve4, reject) => {
|
|
911
|
+
const protocol = useHttps ? https : http;
|
|
912
|
+
const url = `${useHttps ? "https" : "http"}://${host}:${port}${CLAWVAULT_SERVE_PATH}/manifest`;
|
|
913
|
+
const req = protocol.get(url, { timeout: 1e4 }, (res) => {
|
|
914
|
+
let data = "";
|
|
915
|
+
res.on("data", (chunk) => {
|
|
916
|
+
data += chunk;
|
|
917
|
+
});
|
|
918
|
+
res.on("end", () => {
|
|
919
|
+
if (res.statusCode !== 200) {
|
|
920
|
+
reject(new Error(`Failed to fetch manifest: HTTP ${res.statusCode}`));
|
|
921
|
+
return;
|
|
922
|
+
}
|
|
923
|
+
try {
|
|
924
|
+
resolve4(JSON.parse(data));
|
|
925
|
+
} catch (err) {
|
|
926
|
+
reject(new Error(`Invalid manifest response: ${err}`));
|
|
927
|
+
}
|
|
928
|
+
});
|
|
929
|
+
});
|
|
930
|
+
req.on("error", reject);
|
|
931
|
+
req.on("timeout", () => {
|
|
932
|
+
req.destroy();
|
|
933
|
+
reject(new Error("Request timed out"));
|
|
934
|
+
});
|
|
935
|
+
});
|
|
936
|
+
}
|
|
937
|
+
async function fetchRemoteFile(host, filePath, port = DEFAULT_SERVE_PORT, useHttps = false) {
|
|
938
|
+
return new Promise((resolve4, reject) => {
|
|
939
|
+
const protocol = useHttps ? https : http;
|
|
940
|
+
const encodedPath = encodeURIComponent(filePath).replace(/%2F/g, "/");
|
|
941
|
+
const url = `${useHttps ? "https" : "http"}://${host}:${port}${CLAWVAULT_SERVE_PATH}/files/${encodedPath}`;
|
|
942
|
+
const req = protocol.get(url, { timeout: 3e4 }, (res) => {
|
|
943
|
+
let data = "";
|
|
944
|
+
res.on("data", (chunk) => {
|
|
945
|
+
data += chunk;
|
|
946
|
+
});
|
|
947
|
+
res.on("end", () => {
|
|
948
|
+
if (res.statusCode !== 200) {
|
|
949
|
+
reject(new Error(`Failed to fetch file: HTTP ${res.statusCode}`));
|
|
950
|
+
return;
|
|
951
|
+
}
|
|
952
|
+
resolve4(data);
|
|
953
|
+
});
|
|
954
|
+
});
|
|
955
|
+
req.on("error", reject);
|
|
956
|
+
req.on("timeout", () => {
|
|
957
|
+
req.destroy();
|
|
958
|
+
reject(new Error("Request timed out"));
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
async function pushFileToRemote(host, filePath, content, port = DEFAULT_SERVE_PORT, useHttps = false) {
|
|
963
|
+
return new Promise((resolve4, reject) => {
|
|
964
|
+
const protocol = useHttps ? https : http;
|
|
965
|
+
const encodedPath = encodeURIComponent(filePath).replace(/%2F/g, "/");
|
|
966
|
+
const url = new URL(`${useHttps ? "https" : "http"}://${host}:${port}${CLAWVAULT_SERVE_PATH}/upload/${encodedPath}`);
|
|
967
|
+
const options = {
|
|
968
|
+
hostname: url.hostname,
|
|
969
|
+
port: url.port,
|
|
970
|
+
path: url.pathname,
|
|
971
|
+
method: "POST",
|
|
972
|
+
headers: {
|
|
973
|
+
"Content-Type": "text/markdown",
|
|
974
|
+
"Content-Length": Buffer.byteLength(content)
|
|
975
|
+
},
|
|
976
|
+
timeout: 3e4
|
|
977
|
+
};
|
|
978
|
+
const req = protocol.request(options, (res) => {
|
|
979
|
+
let data = "";
|
|
980
|
+
res.on("data", (chunk) => {
|
|
981
|
+
data += chunk;
|
|
982
|
+
});
|
|
983
|
+
res.on("end", () => {
|
|
984
|
+
if (res.statusCode !== 200) {
|
|
985
|
+
reject(new Error(`Failed to push file: HTTP ${res.statusCode}`));
|
|
986
|
+
return;
|
|
987
|
+
}
|
|
988
|
+
resolve4();
|
|
989
|
+
});
|
|
990
|
+
});
|
|
991
|
+
req.on("error", reject);
|
|
992
|
+
req.on("timeout", () => {
|
|
993
|
+
req.destroy();
|
|
994
|
+
reject(new Error("Request timed out"));
|
|
995
|
+
});
|
|
996
|
+
req.write(content);
|
|
997
|
+
req.end();
|
|
998
|
+
});
|
|
999
|
+
}
|
|
1000
|
+
async function syncWithPeer(vaultPath, options) {
|
|
1001
|
+
const startTime = Date.now();
|
|
1002
|
+
const result = {
|
|
1003
|
+
pushed: [],
|
|
1004
|
+
pulled: [],
|
|
1005
|
+
deleted: [],
|
|
1006
|
+
unchanged: [],
|
|
1007
|
+
errors: [],
|
|
1008
|
+
stats: {
|
|
1009
|
+
bytesTransferred: 0,
|
|
1010
|
+
filesProcessed: 0,
|
|
1011
|
+
duration: 0
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
const {
|
|
1015
|
+
peer,
|
|
1016
|
+
port = DEFAULT_SERVE_PORT,
|
|
1017
|
+
direction = "bidirectional",
|
|
1018
|
+
dryRun = false,
|
|
1019
|
+
deleteOrphans = false,
|
|
1020
|
+
categories,
|
|
1021
|
+
https: useHttps = false
|
|
1022
|
+
} = options;
|
|
1023
|
+
let host = peer;
|
|
1024
|
+
if (!peer.match(/^\d+\.\d+\.\d+\.\d+$/)) {
|
|
1025
|
+
const resolvedIP = resolvePeerIP(peer);
|
|
1026
|
+
if (!resolvedIP) {
|
|
1027
|
+
result.errors.push(`Could not resolve peer: ${peer}`);
|
|
1028
|
+
result.stats.duration = Date.now() - startTime;
|
|
1029
|
+
return result;
|
|
1030
|
+
}
|
|
1031
|
+
host = resolvedIP;
|
|
1032
|
+
}
|
|
1033
|
+
try {
|
|
1034
|
+
const localManifest = generateVaultManifest(vaultPath);
|
|
1035
|
+
const remoteManifest = await fetchRemoteManifest(host, port, useHttps);
|
|
1036
|
+
let { toPush, toPull, conflicts, unchanged } = compareManifests(localManifest, remoteManifest);
|
|
1037
|
+
if (categories && categories.length > 0) {
|
|
1038
|
+
const categorySet = new Set(categories);
|
|
1039
|
+
toPush = toPush.filter((f) => categorySet.has(f.category));
|
|
1040
|
+
toPull = toPull.filter((f) => categorySet.has(f.category));
|
|
1041
|
+
}
|
|
1042
|
+
result.unchanged = unchanged;
|
|
1043
|
+
for (const conflict of conflicts) {
|
|
1044
|
+
result.errors.push(`Conflict: ${conflict.path} (local and remote have same timestamp but different content)`);
|
|
1045
|
+
}
|
|
1046
|
+
if (direction === "push" || direction === "bidirectional") {
|
|
1047
|
+
for (const file of toPush) {
|
|
1048
|
+
try {
|
|
1049
|
+
if (!dryRun) {
|
|
1050
|
+
const content = fs3.readFileSync(path3.join(vaultPath, file.path), "utf-8");
|
|
1051
|
+
await pushFileToRemote(host, file.path, content, port, useHttps);
|
|
1052
|
+
result.stats.bytesTransferred += file.size;
|
|
1053
|
+
}
|
|
1054
|
+
result.pushed.push(file.path);
|
|
1055
|
+
result.stats.filesProcessed++;
|
|
1056
|
+
} catch (err) {
|
|
1057
|
+
result.errors.push(`Failed to push ${file.path}: ${err}`);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
if (direction === "pull" || direction === "bidirectional") {
|
|
1062
|
+
for (const file of toPull) {
|
|
1063
|
+
try {
|
|
1064
|
+
if (!dryRun) {
|
|
1065
|
+
const content = await fetchRemoteFile(host, file.path, port, useHttps);
|
|
1066
|
+
const filePath = path3.join(vaultPath, file.path);
|
|
1067
|
+
const dir = path3.dirname(filePath);
|
|
1068
|
+
if (!fs3.existsSync(dir)) {
|
|
1069
|
+
fs3.mkdirSync(dir, { recursive: true });
|
|
1070
|
+
}
|
|
1071
|
+
fs3.writeFileSync(filePath, content, "utf-8");
|
|
1072
|
+
result.stats.bytesTransferred += file.size;
|
|
1073
|
+
}
|
|
1074
|
+
result.pulled.push(file.path);
|
|
1075
|
+
result.stats.filesProcessed++;
|
|
1076
|
+
} catch (err) {
|
|
1077
|
+
result.errors.push(`Failed to pull ${file.path}: ${err}`);
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
if (deleteOrphans && direction === "pull") {
|
|
1082
|
+
const remoteFiles = new Set(remoteManifest.files.map((f) => f.path));
|
|
1083
|
+
for (const file of localManifest.files) {
|
|
1084
|
+
if (!remoteFiles.has(file.path)) {
|
|
1085
|
+
if (!categories || categories.includes(file.category)) {
|
|
1086
|
+
try {
|
|
1087
|
+
if (!dryRun) {
|
|
1088
|
+
fs3.unlinkSync(path3.join(vaultPath, file.path));
|
|
1089
|
+
}
|
|
1090
|
+
result.deleted.push(file.path);
|
|
1091
|
+
} catch (err) {
|
|
1092
|
+
result.errors.push(`Failed to delete ${file.path}: ${err}`);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
} catch (err) {
|
|
1099
|
+
result.errors.push(`Sync failed: ${err}`);
|
|
1100
|
+
}
|
|
1101
|
+
result.stats.duration = Date.now() - startTime;
|
|
1102
|
+
return result;
|
|
1103
|
+
}
|
|
1104
|
+
function configureTailscaleServe(localPort, options = {}) {
|
|
1105
|
+
if (!hasTailscale()) {
|
|
1106
|
+
return null;
|
|
1107
|
+
}
|
|
1108
|
+
const args = ["serve"];
|
|
1109
|
+
if (options.funnel) {
|
|
1110
|
+
args.push("--bg");
|
|
1111
|
+
args.push("funnel");
|
|
1112
|
+
} else if (options.background) {
|
|
1113
|
+
args.push("--bg");
|
|
1114
|
+
}
|
|
1115
|
+
args.push(`localhost:${localPort}`);
|
|
1116
|
+
const proc = (0, import_child_process.spawn)("tailscale", args, {
|
|
1117
|
+
stdio: "inherit",
|
|
1118
|
+
detached: options.background
|
|
1119
|
+
});
|
|
1120
|
+
if (options.background) {
|
|
1121
|
+
proc.unref();
|
|
1122
|
+
}
|
|
1123
|
+
return proc;
|
|
1124
|
+
}
|
|
1125
|
+
function stopTailscaleServe() {
|
|
1126
|
+
if (!hasTailscale()) {
|
|
1127
|
+
return false;
|
|
1128
|
+
}
|
|
1129
|
+
const result = (0, import_child_process.spawnSync)("tailscale", ["serve", "off"], {
|
|
1130
|
+
stdio: "pipe",
|
|
1131
|
+
encoding: "utf-8",
|
|
1132
|
+
timeout: 5e3
|
|
1133
|
+
});
|
|
1134
|
+
return result.status === 0;
|
|
1135
|
+
}
|
|
1136
|
+
async function checkPeerClawVault(host, port = DEFAULT_SERVE_PORT) {
|
|
1137
|
+
try {
|
|
1138
|
+
const response = await new Promise((resolve4) => {
|
|
1139
|
+
const req = http.get(
|
|
1140
|
+
`http://${host}:${port}${CLAWVAULT_SERVE_PATH}/health`,
|
|
1141
|
+
{ timeout: 5e3 },
|
|
1142
|
+
(res) => {
|
|
1143
|
+
resolve4(res.statusCode === 200);
|
|
1144
|
+
}
|
|
1145
|
+
);
|
|
1146
|
+
req.on("error", () => resolve4(false));
|
|
1147
|
+
req.on("timeout", () => {
|
|
1148
|
+
req.destroy();
|
|
1149
|
+
resolve4(false);
|
|
1150
|
+
});
|
|
1151
|
+
});
|
|
1152
|
+
return response;
|
|
1153
|
+
} catch {
|
|
1154
|
+
return false;
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
async function discoverClawVaultPeers(port = DEFAULT_SERVE_PORT) {
|
|
1158
|
+
const status = getTailscaleStatus();
|
|
1159
|
+
if (!status.connected) {
|
|
1160
|
+
return [];
|
|
1161
|
+
}
|
|
1162
|
+
const clawvaultPeers = [];
|
|
1163
|
+
const checkPromises = status.peers.filter((p) => p.online).map(async (peer) => {
|
|
1164
|
+
const ip = peer.tailscaleIPs[0];
|
|
1165
|
+
if (!ip) return;
|
|
1166
|
+
const isServing = await checkPeerClawVault(ip, port);
|
|
1167
|
+
if (isServing) {
|
|
1168
|
+
peer.clawvaultServing = true;
|
|
1169
|
+
peer.clawvaultPort = port;
|
|
1170
|
+
clawvaultPeers.push(peer);
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
await Promise.all(checkPromises);
|
|
1174
|
+
return clawvaultPeers;
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
// src/commands/tailscale.ts
|
|
1178
|
+
async function tailscaleStatusCommand(options = {}) {
|
|
1179
|
+
const status = getTailscaleStatus();
|
|
1180
|
+
if (options.json) {
|
|
1181
|
+
console.log(JSON.stringify(status, null, 2));
|
|
1182
|
+
return status;
|
|
1183
|
+
}
|
|
1184
|
+
if (!status.installed) {
|
|
1185
|
+
console.log("Tailscale: Not installed");
|
|
1186
|
+
console.log(" Install from: https://tailscale.com/download");
|
|
1187
|
+
return status;
|
|
1188
|
+
}
|
|
1189
|
+
const version = getTailscaleVersion();
|
|
1190
|
+
console.log(`Tailscale: ${version || "installed"}`);
|
|
1191
|
+
if (!status.running) {
|
|
1192
|
+
console.log(" Status: Daemon not running");
|
|
1193
|
+
if (status.error) {
|
|
1194
|
+
console.log(` Error: ${status.error}`);
|
|
1195
|
+
}
|
|
1196
|
+
return status;
|
|
1197
|
+
}
|
|
1198
|
+
console.log(` Status: ${status.backendState}`);
|
|
1199
|
+
if (status.connected) {
|
|
1200
|
+
console.log(` Tailnet: ${status.tailnetName || "unknown"}`);
|
|
1201
|
+
console.log(` Self IP: ${status.selfIP || "unknown"}`);
|
|
1202
|
+
console.log(` Hostname: ${status.selfHostname || "unknown"}`);
|
|
1203
|
+
if (status.selfDNSName) {
|
|
1204
|
+
console.log(` DNS Name: ${status.selfDNSName}`);
|
|
1205
|
+
}
|
|
1206
|
+
if (options.peers || status.peers.length > 0) {
|
|
1207
|
+
const onlinePeers = status.peers.filter((p) => p.online);
|
|
1208
|
+
const offlinePeers = status.peers.filter((p) => !p.online);
|
|
1209
|
+
console.log(`
|
|
1210
|
+
Peers (${onlinePeers.length} online, ${offlinePeers.length} offline):`);
|
|
1211
|
+
for (const peer of onlinePeers) {
|
|
1212
|
+
const ip = peer.tailscaleIPs[0] || "no-ip";
|
|
1213
|
+
const os = peer.os ? ` (${peer.os})` : "";
|
|
1214
|
+
const clawvault = peer.clawvaultServing ? " [ClawVault]" : "";
|
|
1215
|
+
console.log(` \u25CF ${peer.hostname}${os} - ${ip}${clawvault}`);
|
|
1216
|
+
}
|
|
1217
|
+
if (options.peers) {
|
|
1218
|
+
for (const peer of offlinePeers) {
|
|
1219
|
+
const ip = peer.tailscaleIPs[0] || "no-ip";
|
|
1220
|
+
const os = peer.os ? ` (${peer.os})` : "";
|
|
1221
|
+
console.log(` \u25CB ${peer.hostname}${os} - ${ip} [offline]`);
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
} else {
|
|
1226
|
+
console.log(" Status: Not connected to tailnet");
|
|
1227
|
+
if (status.error) {
|
|
1228
|
+
console.log(` Error: ${status.error}`);
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
return status;
|
|
1232
|
+
}
|
|
1233
|
+
function registerTailscaleStatusCommand(program) {
|
|
1234
|
+
program.command("tailscale-status").alias("ts-status").description("Show Tailscale connection status and peers").option("--json", "Output as JSON").option("--peers", "Show all peers including offline").action(async (rawOptions) => {
|
|
1235
|
+
await tailscaleStatusCommand({
|
|
1236
|
+
json: rawOptions.json,
|
|
1237
|
+
peers: rawOptions.peers
|
|
1238
|
+
});
|
|
1239
|
+
});
|
|
1240
|
+
}
|
|
1241
|
+
async function tailscaleSyncCommand(options) {
|
|
1242
|
+
const vaultPath = resolveVaultPath({ explicitPath: options.vaultPath });
|
|
1243
|
+
const status = getTailscaleStatus();
|
|
1244
|
+
if (!status.installed) {
|
|
1245
|
+
const error = {
|
|
1246
|
+
pushed: [],
|
|
1247
|
+
pulled: [],
|
|
1248
|
+
deleted: [],
|
|
1249
|
+
unchanged: [],
|
|
1250
|
+
errors: ["Tailscale not installed. Install from https://tailscale.com/download"],
|
|
1251
|
+
stats: { bytesTransferred: 0, filesProcessed: 0, duration: 0 }
|
|
1252
|
+
};
|
|
1253
|
+
if (options.json) {
|
|
1254
|
+
console.log(JSON.stringify(error, null, 2));
|
|
1255
|
+
} else {
|
|
1256
|
+
console.error("Error: Tailscale not installed");
|
|
1257
|
+
}
|
|
1258
|
+
return error;
|
|
1259
|
+
}
|
|
1260
|
+
if (!status.connected) {
|
|
1261
|
+
const error = {
|
|
1262
|
+
pushed: [],
|
|
1263
|
+
pulled: [],
|
|
1264
|
+
deleted: [],
|
|
1265
|
+
unchanged: [],
|
|
1266
|
+
errors: ["Not connected to Tailscale. Run `tailscale up` to connect."],
|
|
1267
|
+
stats: { bytesTransferred: 0, filesProcessed: 0, duration: 0 }
|
|
1268
|
+
};
|
|
1269
|
+
if (options.json) {
|
|
1270
|
+
console.log(JSON.stringify(error, null, 2));
|
|
1271
|
+
} else {
|
|
1272
|
+
console.error("Error: Not connected to Tailscale");
|
|
1273
|
+
}
|
|
1274
|
+
return error;
|
|
1275
|
+
}
|
|
1276
|
+
const peer = findPeer(options.peer);
|
|
1277
|
+
if (!peer) {
|
|
1278
|
+
const error = {
|
|
1279
|
+
pushed: [],
|
|
1280
|
+
pulled: [],
|
|
1281
|
+
deleted: [],
|
|
1282
|
+
unchanged: [],
|
|
1283
|
+
errors: [`Peer not found: ${options.peer}`],
|
|
1284
|
+
stats: { bytesTransferred: 0, filesProcessed: 0, duration: 0 }
|
|
1285
|
+
};
|
|
1286
|
+
if (options.json) {
|
|
1287
|
+
console.log(JSON.stringify(error, null, 2));
|
|
1288
|
+
} else {
|
|
1289
|
+
console.error(`Error: Peer not found: ${options.peer}`);
|
|
1290
|
+
console.log("\nAvailable online peers:");
|
|
1291
|
+
for (const p of getOnlinePeers()) {
|
|
1292
|
+
console.log(` - ${p.hostname} (${p.tailscaleIPs[0]})`);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
return error;
|
|
1296
|
+
}
|
|
1297
|
+
if (!peer.online) {
|
|
1298
|
+
const error = {
|
|
1299
|
+
pushed: [],
|
|
1300
|
+
pulled: [],
|
|
1301
|
+
deleted: [],
|
|
1302
|
+
unchanged: [],
|
|
1303
|
+
errors: [`Peer is offline: ${peer.hostname}`],
|
|
1304
|
+
stats: { bytesTransferred: 0, filesProcessed: 0, duration: 0 }
|
|
1305
|
+
};
|
|
1306
|
+
if (options.json) {
|
|
1307
|
+
console.log(JSON.stringify(error, null, 2));
|
|
1308
|
+
} else {
|
|
1309
|
+
console.error(`Error: Peer is offline: ${peer.hostname}`);
|
|
1310
|
+
}
|
|
1311
|
+
return error;
|
|
1312
|
+
}
|
|
1313
|
+
const syncOptions = {
|
|
1314
|
+
peer: peer.tailscaleIPs[0],
|
|
1315
|
+
port: options.port || DEFAULT_SERVE_PORT,
|
|
1316
|
+
direction: options.direction || "bidirectional",
|
|
1317
|
+
dryRun: options.dryRun,
|
|
1318
|
+
deleteOrphans: options.deleteOrphans,
|
|
1319
|
+
categories: options.categories,
|
|
1320
|
+
https: options.https
|
|
1321
|
+
};
|
|
1322
|
+
if (!options.json && !options.dryRun) {
|
|
1323
|
+
console.log(`Syncing with ${peer.hostname} (${peer.tailscaleIPs[0]})...`);
|
|
1324
|
+
}
|
|
1325
|
+
const result = await syncWithPeer(vaultPath, syncOptions);
|
|
1326
|
+
if (options.json) {
|
|
1327
|
+
console.log(JSON.stringify(result, null, 2));
|
|
1328
|
+
} else {
|
|
1329
|
+
const prefix = options.dryRun ? "[dry-run] " : "";
|
|
1330
|
+
if (result.pushed.length > 0) {
|
|
1331
|
+
console.log(`
|
|
1332
|
+
${prefix}Pushed ${result.pushed.length} file(s):`);
|
|
1333
|
+
for (const file of result.pushed.slice(0, 10)) {
|
|
1334
|
+
console.log(` \u2192 ${file}`);
|
|
1335
|
+
}
|
|
1336
|
+
if (result.pushed.length > 10) {
|
|
1337
|
+
console.log(` ... and ${result.pushed.length - 10} more`);
|
|
1338
|
+
}
|
|
1339
|
+
}
|
|
1340
|
+
if (result.pulled.length > 0) {
|
|
1341
|
+
console.log(`
|
|
1342
|
+
${prefix}Pulled ${result.pulled.length} file(s):`);
|
|
1343
|
+
for (const file of result.pulled.slice(0, 10)) {
|
|
1344
|
+
console.log(` \u2190 ${file}`);
|
|
1345
|
+
}
|
|
1346
|
+
if (result.pulled.length > 10) {
|
|
1347
|
+
console.log(` ... and ${result.pulled.length - 10} more`);
|
|
1348
|
+
}
|
|
1349
|
+
}
|
|
1350
|
+
if (result.deleted.length > 0) {
|
|
1351
|
+
console.log(`
|
|
1352
|
+
${prefix}Deleted ${result.deleted.length} file(s):`);
|
|
1353
|
+
for (const file of result.deleted.slice(0, 10)) {
|
|
1354
|
+
console.log(` \u2717 ${file}`);
|
|
1355
|
+
}
|
|
1356
|
+
if (result.deleted.length > 10) {
|
|
1357
|
+
console.log(` ... and ${result.deleted.length - 10} more`);
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
if (result.errors.length > 0) {
|
|
1361
|
+
console.log(`
|
|
1362
|
+
Errors (${result.errors.length}):`);
|
|
1363
|
+
for (const error of result.errors) {
|
|
1364
|
+
console.log(` ! ${error}`);
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
console.log(`
|
|
1368
|
+
Summary:`);
|
|
1369
|
+
console.log(` Pushed: ${result.pushed.length}`);
|
|
1370
|
+
console.log(` Pulled: ${result.pulled.length}`);
|
|
1371
|
+
console.log(` Deleted: ${result.deleted.length}`);
|
|
1372
|
+
console.log(` Unchanged: ${result.unchanged.length}`);
|
|
1373
|
+
console.log(` Errors: ${result.errors.length}`);
|
|
1374
|
+
console.log(` Duration: ${result.stats.duration}ms`);
|
|
1375
|
+
console.log(` Transferred: ${formatBytes(result.stats.bytesTransferred)}`);
|
|
1376
|
+
}
|
|
1377
|
+
return result;
|
|
1378
|
+
}
|
|
1379
|
+
function formatBytes(bytes) {
|
|
1380
|
+
if (bytes === 0) return "0 B";
|
|
1381
|
+
const k = 1024;
|
|
1382
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
1383
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
1384
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
1385
|
+
}
|
|
1386
|
+
function registerTailscaleSyncCommand(program) {
|
|
1387
|
+
program.command("tailscale-sync").alias("ts-sync").description("Sync vault with a peer on the Tailscale network").requiredOption("--peer <hostname>", "Peer hostname or IP to sync with").option("-v, --vault <path>", "Vault path").option("--port <number>", "Port on the peer", parseInt).option("--direction <dir>", "Sync direction: push, pull, or bidirectional", "bidirectional").option("--dry-run", "Show what would be synced without making changes").option("--delete-orphans", "Delete files that exist locally but not on peer (pull only)").option("--categories <list>", "Comma-separated list of categories to sync").option("--https", "Use HTTPS for connection").option("--json", "Output as JSON").action(async (rawOptions) => {
|
|
1388
|
+
await tailscaleSyncCommand({
|
|
1389
|
+
peer: rawOptions.peer,
|
|
1390
|
+
vaultPath: rawOptions.vault,
|
|
1391
|
+
port: rawOptions.port,
|
|
1392
|
+
direction: rawOptions.direction,
|
|
1393
|
+
dryRun: rawOptions.dryRun,
|
|
1394
|
+
deleteOrphans: rawOptions.deleteOrphans,
|
|
1395
|
+
categories: rawOptions.categories?.split(",").map((c) => c.trim()),
|
|
1396
|
+
https: rawOptions.https,
|
|
1397
|
+
json: rawOptions.json
|
|
1398
|
+
});
|
|
1399
|
+
});
|
|
1400
|
+
}
|
|
1401
|
+
var activeServeInstance = null;
|
|
1402
|
+
async function tailscaleServeCommand(options) {
|
|
1403
|
+
if (options.stop) {
|
|
1404
|
+
if (activeServeInstance) {
|
|
1405
|
+
await activeServeInstance.stop();
|
|
1406
|
+
activeServeInstance = null;
|
|
1407
|
+
console.log("ClawVault serve stopped.");
|
|
1408
|
+
}
|
|
1409
|
+
stopTailscaleServe();
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
const vaultPath = resolveVaultPath({ explicitPath: options.vaultPath });
|
|
1413
|
+
const port = options.port || DEFAULT_SERVE_PORT;
|
|
1414
|
+
const status = getTailscaleStatus();
|
|
1415
|
+
console.log(`Starting ClawVault serve...`);
|
|
1416
|
+
console.log(` Vault: ${path4.basename(vaultPath)}`);
|
|
1417
|
+
console.log(` Port: ${port}`);
|
|
1418
|
+
activeServeInstance = serveVault(vaultPath, { port });
|
|
1419
|
+
console.log(` Local URL: http://localhost:${port}/.clawvault`);
|
|
1420
|
+
if (status.connected) {
|
|
1421
|
+
console.log(` Tailscale URL: http://${status.selfIP}:${port}/.clawvault`);
|
|
1422
|
+
if (status.selfDNSName) {
|
|
1423
|
+
const dnsHost = status.selfDNSName.replace(/\.$/, "");
|
|
1424
|
+
console.log(` MagicDNS URL: http://${dnsHost}:${port}/.clawvault`);
|
|
1425
|
+
}
|
|
1426
|
+
if (options.funnel || options.background) {
|
|
1427
|
+
console.log("\nConfiguring Tailscale serve...");
|
|
1428
|
+
configureTailscaleServe(port, {
|
|
1429
|
+
funnel: options.funnel,
|
|
1430
|
+
background: options.background
|
|
1431
|
+
});
|
|
1432
|
+
if (options.funnel) {
|
|
1433
|
+
console.log(" Funnel enabled - vault is accessible from the public internet");
|
|
1434
|
+
}
|
|
1435
|
+
}
|
|
1436
|
+
} else {
|
|
1437
|
+
console.log("\n Note: Not connected to Tailscale. Only local access available.");
|
|
1438
|
+
}
|
|
1439
|
+
console.log("\nEndpoints:");
|
|
1440
|
+
console.log(` Health: /.clawvault/health`);
|
|
1441
|
+
console.log(` Manifest: /.clawvault/manifest`);
|
|
1442
|
+
console.log(` Files: /.clawvault/files/<path>`);
|
|
1443
|
+
if (!options.background) {
|
|
1444
|
+
console.log("\nPress Ctrl+C to stop serving.");
|
|
1445
|
+
process.on("SIGINT", async () => {
|
|
1446
|
+
console.log("\nStopping ClawVault serve...");
|
|
1447
|
+
if (activeServeInstance) {
|
|
1448
|
+
await activeServeInstance.stop();
|
|
1449
|
+
activeServeInstance = null;
|
|
1450
|
+
}
|
|
1451
|
+
stopTailscaleServe();
|
|
1452
|
+
process.exit(0);
|
|
1453
|
+
});
|
|
1454
|
+
await new Promise(() => {
|
|
1455
|
+
});
|
|
1456
|
+
}
|
|
1457
|
+
}
|
|
1458
|
+
function registerTailscaleServeCommand(program) {
|
|
1459
|
+
program.command("tailscale-serve").alias("ts-serve").description("Serve vault for sync over Tailscale").option("-v, --vault <path>", "Vault path").option("--port <number>", `Port to serve on (default: ${DEFAULT_SERVE_PORT})`, parseInt).option("--funnel", "Expose via Tailscale Funnel (public internet)").option("--background", "Run in background").option("--stop", "Stop serving").action(async (rawOptions) => {
|
|
1460
|
+
await tailscaleServeCommand({
|
|
1461
|
+
vaultPath: rawOptions.vault,
|
|
1462
|
+
port: rawOptions.port,
|
|
1463
|
+
funnel: rawOptions.funnel,
|
|
1464
|
+
background: rawOptions.background,
|
|
1465
|
+
stop: rawOptions.stop
|
|
1466
|
+
});
|
|
1467
|
+
});
|
|
1468
|
+
}
|
|
1469
|
+
async function tailscaleDiscoverCommand(options = {}) {
|
|
1470
|
+
const port = options.port || DEFAULT_SERVE_PORT;
|
|
1471
|
+
const status = getTailscaleStatus();
|
|
1472
|
+
if (!status.connected) {
|
|
1473
|
+
if (options.json) {
|
|
1474
|
+
console.log(JSON.stringify({ error: "Not connected to Tailscale", peers: [] }));
|
|
1475
|
+
} else {
|
|
1476
|
+
console.error("Error: Not connected to Tailscale");
|
|
1477
|
+
}
|
|
1478
|
+
return [];
|
|
1479
|
+
}
|
|
1480
|
+
if (!options.json) {
|
|
1481
|
+
console.log("Discovering ClawVault peers on tailnet...");
|
|
1482
|
+
}
|
|
1483
|
+
const peers = await discoverClawVaultPeers(port);
|
|
1484
|
+
if (options.json) {
|
|
1485
|
+
console.log(JSON.stringify({ peers }, null, 2));
|
|
1486
|
+
} else {
|
|
1487
|
+
if (peers.length === 0) {
|
|
1488
|
+
console.log("\nNo ClawVault peers found.");
|
|
1489
|
+
console.log(" Run `clawvault tailscale-serve` on other devices to enable sync.");
|
|
1490
|
+
} else {
|
|
1491
|
+
console.log(`
|
|
1492
|
+
Found ${peers.length} ClawVault peer(s):`);
|
|
1493
|
+
for (const peer of peers) {
|
|
1494
|
+
const ip = peer.tailscaleIPs[0] || "no-ip";
|
|
1495
|
+
const os = peer.os ? ` (${peer.os})` : "";
|
|
1496
|
+
console.log(` \u25CF ${peer.hostname}${os}`);
|
|
1497
|
+
console.log(` IP: ${ip}`);
|
|
1498
|
+
console.log(` Port: ${peer.clawvaultPort}`);
|
|
1499
|
+
if (peer.dnsName) {
|
|
1500
|
+
console.log(` DNS: ${peer.dnsName.replace(/\.$/, "")}`);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
return peers;
|
|
1506
|
+
}
|
|
1507
|
+
function registerTailscaleDiscoverCommand(program) {
|
|
1508
|
+
program.command("tailscale-discover").alias("ts-discover").description("Discover ClawVault peers on the Tailscale network").option("--port <number>", `Port to check (default: ${DEFAULT_SERVE_PORT})`, parseInt).option("--json", "Output as JSON").action(async (rawOptions) => {
|
|
1509
|
+
await tailscaleDiscoverCommand({
|
|
1510
|
+
port: rawOptions.port,
|
|
1511
|
+
json: rawOptions.json
|
|
1512
|
+
});
|
|
1513
|
+
});
|
|
1514
|
+
}
|
|
1515
|
+
function registerTailscaleCommands(program) {
|
|
1516
|
+
registerTailscaleStatusCommand(program);
|
|
1517
|
+
registerTailscaleSyncCommand(program);
|
|
1518
|
+
registerTailscaleServeCommand(program);
|
|
1519
|
+
registerTailscaleDiscoverCommand(program);
|
|
1520
|
+
}
|
|
1521
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1522
|
+
0 && (module.exports = {
|
|
1523
|
+
registerTailscaleCommands,
|
|
1524
|
+
registerTailscaleDiscoverCommand,
|
|
1525
|
+
registerTailscaleServeCommand,
|
|
1526
|
+
registerTailscaleStatusCommand,
|
|
1527
|
+
registerTailscaleSyncCommand,
|
|
1528
|
+
tailscaleDiscoverCommand,
|
|
1529
|
+
tailscaleServeCommand,
|
|
1530
|
+
tailscaleStatusCommand,
|
|
1531
|
+
tailscaleSyncCommand
|
|
1532
|
+
});
|