pal-explorer-cli 0.4.11 → 0.4.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +149 -149
- package/bin/pal.js +63 -2
- package/extensions/@palexplorer/analytics/extension.json +20 -1
- package/extensions/@palexplorer/analytics/index.js +19 -9
- package/extensions/@palexplorer/audit/extension.json +14 -0
- package/extensions/@palexplorer/auth-email/extension.json +15 -0
- package/extensions/@palexplorer/auth-oauth/extension.json +15 -0
- package/extensions/@palexplorer/chat/extension.json +14 -0
- package/extensions/@palexplorer/discovery/extension.json +17 -0
- package/extensions/@palexplorer/discovery/index.js +1 -1
- package/extensions/@palexplorer/email-notifications/extension.json +23 -0
- package/extensions/@palexplorer/groups/extension.json +15 -0
- package/extensions/@palexplorer/share-links/extension.json +15 -0
- package/extensions/@palexplorer/sync/extension.json +16 -0
- package/extensions/@palexplorer/user-mgmt/extension.json +15 -0
- package/lib/capabilities.js +24 -24
- package/lib/commands/analytics.js +175 -175
- package/lib/commands/api-keys.js +131 -131
- package/lib/commands/audit.js +235 -235
- package/lib/commands/auth.js +137 -137
- package/lib/commands/backup.js +76 -76
- package/lib/commands/billing.js +148 -148
- package/lib/commands/chat.js +217 -217
- package/lib/commands/cloud-backup.js +231 -231
- package/lib/commands/comment.js +99 -99
- package/lib/commands/completion.js +203 -203
- package/lib/commands/compliance.js +218 -218
- package/lib/commands/config.js +136 -136
- package/lib/commands/connect.js +44 -44
- package/lib/commands/dept.js +294 -294
- package/lib/commands/device.js +146 -146
- package/lib/commands/download.js +240 -226
- package/lib/commands/explorer.js +178 -178
- package/lib/commands/extension.js +1060 -970
- package/lib/commands/favorite.js +90 -90
- package/lib/commands/federation.js +270 -270
- package/lib/commands/file.js +533 -533
- package/lib/commands/group.js +271 -271
- package/lib/commands/gui-share.js +29 -29
- package/lib/commands/init.js +61 -61
- package/lib/commands/invite.js +59 -59
- package/lib/commands/list.js +58 -58
- package/lib/commands/log.js +116 -116
- package/lib/commands/nearby.js +108 -108
- package/lib/commands/network.js +251 -251
- package/lib/commands/notify.js +198 -198
- package/lib/commands/org.js +273 -273
- package/lib/commands/pal.js +403 -180
- package/lib/commands/permissions.js +216 -216
- package/lib/commands/pin.js +97 -97
- package/lib/commands/protocol.js +357 -357
- package/lib/commands/rbac.js +147 -147
- package/lib/commands/recover.js +36 -36
- package/lib/commands/register.js +171 -171
- package/lib/commands/relay.js +131 -131
- package/lib/commands/remote.js +368 -368
- package/lib/commands/revoke.js +50 -50
- package/lib/commands/scanner.js +280 -280
- package/lib/commands/schedule.js +344 -344
- package/lib/commands/scim.js +203 -203
- package/lib/commands/search.js +181 -181
- package/lib/commands/serve.js +438 -438
- package/lib/commands/server.js +350 -350
- package/lib/commands/share-link.js +199 -199
- package/lib/commands/share.js +336 -323
- package/lib/commands/sso.js +200 -200
- package/lib/commands/status.js +145 -145
- package/lib/commands/stream.js +562 -562
- package/lib/commands/su.js +187 -187
- package/lib/commands/sync.js +979 -979
- package/lib/commands/transfers.js +152 -152
- package/lib/commands/uninstall.js +188 -188
- package/lib/commands/update.js +204 -204
- package/lib/commands/user.js +276 -276
- package/lib/commands/vfs.js +84 -84
- package/lib/commands/web-login.js +79 -79
- package/lib/commands/web.js +52 -52
- package/lib/commands/webhook.js +180 -180
- package/lib/commands/whoami.js +59 -59
- package/lib/commands/workspace.js +121 -121
- package/lib/core/billing.js +16 -5
- package/lib/core/dhtDiscovery.js +9 -2
- package/lib/core/discoveryClient.js +13 -7
- package/lib/core/extensions.js +142 -1
- package/lib/core/identity.js +33 -2
- package/lib/core/imageProcessor.js +109 -0
- package/lib/core/imageTorrent.js +167 -0
- package/lib/core/permissions.js +1 -1
- package/lib/core/pro.js +11 -4
- package/lib/core/serverList.js +4 -1
- package/lib/core/shares.js +12 -1
- package/lib/core/signalingServer.js +14 -2
- package/lib/core/su.js +1 -1
- package/lib/core/users.js +1 -1
- package/lib/protocol/messages.js +12 -3
- package/lib/utils/explorer.js +1 -1
- package/lib/utils/help.js +357 -357
- package/lib/utils/torrent.js +1 -0
- package/package.json +4 -3
package/lib/commands/remote.js
CHANGED
|
@@ -1,368 +1,368 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import fs from 'fs';
|
|
4
|
-
import { requireIdentity, auditLog } from '../core/permissions.js';
|
|
5
|
-
import { getFriends } from '../core/users.js';
|
|
6
|
-
import { formatSize } from '../utils/format.js';
|
|
7
|
-
import { printJson, parseCommaList } from '../utils/cli.js';
|
|
8
|
-
import { getTorrentMetadata, destroyClient, TORRENT_TIMEOUT_LONG } from '../utils/torrent.js';
|
|
9
|
-
import { sendRequest, sendAuthenticatedRequest } from '../core/signalingServer.js';
|
|
10
|
-
import { getNearbyPeers, getCachedPeerAddress } from '../core/mdnsService.js';
|
|
11
|
-
import { getIdentity } from '../core/identity.js';
|
|
12
|
-
|
|
13
|
-
// Verify a cached address is still alive via quick TCP probe
|
|
14
|
-
async function verifyPeerAlive(ip, port = 7474) {
|
|
15
|
-
try {
|
|
16
|
-
const r = await sendRequest(ip, port, { type: 'status' }, 3000);
|
|
17
|
-
return r.ok && r.protocol === 'PAL/1.0';
|
|
18
|
-
} catch { return false; }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Resolve a pal's IP and signaling port via mDNS (LAN) or discovery server (device registration)
|
|
22
|
-
async function resolvePeerAddress(pal) {
|
|
23
|
-
// Strategy 1: mDNS — check if peer is on LAN (only works when
|
|
24
|
-
const nearby = getNearbyPeers();
|
|
25
|
-
const mdnsPeer = nearby.find(p =>
|
|
26
|
-
p.publicKey === pal.publicKey || p.publicKey === pal.id ||
|
|
27
|
-
p.handle === pal.handle || p.name === pal.name
|
|
28
|
-
);
|
|
29
|
-
if (mdnsPeer && mdnsPeer.ip && mdnsPeer.ip !== 'unknown') {
|
|
30
|
-
return { ip: mdnsPeer.ip, port: 7474, source: 'mDNS' };
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// Strategy 2: Cached peer address (persisted from previous mDNS/discovery)
|
|
34
|
-
// MEDIUM-3 fix: only look up by public key identifiers
|
|
35
|
-
const cached = getCachedPeerAddress([pal.publicKey, pal.id]);
|
|
36
|
-
if (cached && cached.ip) {
|
|
37
|
-
const alive = await verifyPeerAlive(cached.ip, cached.port || 7474);
|
|
38
|
-
if (alive) {
|
|
39
|
-
return { ip: cached.ip, port: cached.port || 7474, source: 'cache' };
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// Strategy 3: Discovery server — get device IP (NOT share lists)
|
|
44
|
-
const handle = pal.handle || pal.name;
|
|
45
|
-
if (handle) {
|
|
46
|
-
try {
|
|
47
|
-
const { getPrimaryServer } = await import('../core/discoveryClient.js');
|
|
48
|
-
const serverUrl = getPrimaryServer();
|
|
49
|
-
const devicesRes = await fetch(`${serverUrl}/devices/${encodeURIComponent(handle)}`, {
|
|
50
|
-
signal: AbortSignal.timeout(5000)
|
|
51
|
-
});
|
|
52
|
-
if (devicesRes.ok) {
|
|
53
|
-
const { devices = [] } = await devicesRes.json();
|
|
54
|
-
for (const device of devices) {
|
|
55
|
-
if (device.ip) {
|
|
56
|
-
return { ip: device.ip, port: 7474, source: 'discovery', device };
|
|
57
|
-
}
|
|
58
|
-
if (device.serveUrl) {
|
|
59
|
-
try {
|
|
60
|
-
const url = new URL(device.serveUrl);
|
|
61
|
-
return { ip: url.hostname, port: 7474, source: 'discovery', device };
|
|
62
|
-
} catch {}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
} catch {}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Strategy 4: Check if pal has a known IP in their friend record
|
|
70
|
-
if (pal.ip) {
|
|
71
|
-
return { ip: pal.ip, port: 7474, source: 'friend-record' };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return null;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// Query a peer's share list via TCP signaling (PAL/1.0) with authentication
|
|
78
|
-
async function queryPeerShares(ip, port = 7474, timeout = 5000) {
|
|
79
|
-
// Try authenticated request first for full share list access
|
|
80
|
-
try {
|
|
81
|
-
const identity = await getIdentity();
|
|
82
|
-
if (identity?.privateKey && identity?.publicKey) {
|
|
83
|
-
const response = await sendAuthenticatedRequest(
|
|
84
|
-
ip, port, { type: 'share_list' }, identity.privateKey, identity.publicKey, timeout
|
|
85
|
-
);
|
|
86
|
-
if (response.ok) return response;
|
|
87
|
-
}
|
|
88
|
-
} catch {}
|
|
89
|
-
|
|
90
|
-
// Fallback to unauthenticated (will only see public shares)
|
|
91
|
-
const response = await sendRequest(ip, port, { type: 'share_list' }, timeout);
|
|
92
|
-
if (!response.ok) {
|
|
93
|
-
throw new Error(response.error || 'Peer refused share list request');
|
|
94
|
-
}
|
|
95
|
-
return response;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export default function remoteCommand(program) {
|
|
99
|
-
const remote = program
|
|
100
|
-
.command('remote')
|
|
101
|
-
.description('browse and download from pals\' shares')
|
|
102
|
-
.addHelpText('after', `
|
|
103
|
-
Examples:
|
|
104
|
-
$
|
|
105
|
-
$
|
|
106
|
-
$
|
|
107
|
-
$
|
|
108
|
-
$
|
|
109
|
-
`);
|
|
110
|
-
|
|
111
|
-
// ── browse ──────────────────────────────────────────────────────────────
|
|
112
|
-
remote
|
|
113
|
-
.command('browse <handle>')
|
|
114
|
-
.description('list shares from a pal')
|
|
115
|
-
.option('--device <deviceName>', 'Filter by device name')
|
|
116
|
-
.option('--json', 'Output as JSON')
|
|
117
|
-
.action(async (handle, opts) => {
|
|
118
|
-
try {
|
|
119
|
-
requireIdentity();
|
|
120
|
-
handle = handle.replace(/^@/, '');
|
|
121
|
-
|
|
122
|
-
const friends = getFriends();
|
|
123
|
-
const pal = friends.find(f => f.handle === handle || f.name === handle || f.id === handle || f.publicKey === handle);
|
|
124
|
-
if (!pal) {
|
|
125
|
-
console.error(chalk.red(`@${handle} is not in your pals. Run \`
|
|
126
|
-
process.exitCode = 1;
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
console.log(chalk.dim(`Looking for @${handle}...`));
|
|
131
|
-
const addr = await resolvePeerAddress(pal);
|
|
132
|
-
if (!addr) {
|
|
133
|
-
console.log(chalk.yellow(`Could not find @${handle}. The pal might be offline.`));
|
|
134
|
-
console.log(chalk.dim(` Tried: mDNS (LAN), discovery server (device lookup)`));
|
|
135
|
-
process.exitCode = 1;
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
console.log(chalk.dim(`Found @${handle} via ${addr.source} (${addr.ip}:${addr.port})`));
|
|
140
|
-
|
|
141
|
-
let peerData;
|
|
142
|
-
try {
|
|
143
|
-
peerData = await queryPeerShares(addr.ip, addr.port);
|
|
144
|
-
} catch (err) {
|
|
145
|
-
console.log(chalk.yellow(`Could not connect to @${handle} at ${addr.ip}:${addr.port}`));
|
|
146
|
-
console.log(chalk.dim(` Error: ${err.message}`));
|
|
147
|
-
console.log(chalk.dim(` The pal might not be running \`
|
|
148
|
-
process.exitCode = 1;
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const allShares = (peerData.shares || []).map(s => ({
|
|
153
|
-
...s,
|
|
154
|
-
device: peerData.deviceName,
|
|
155
|
-
deviceId: peerData.deviceId,
|
|
156
|
-
}));
|
|
157
|
-
|
|
158
|
-
if (opts.device) {
|
|
159
|
-
const filtered = allShares.filter(s => s.device === opts.device);
|
|
160
|
-
if (filtered.length === 0) {
|
|
161
|
-
console.log(chalk.dim(`No shares from device "${opts.device}".`));
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (opts.json || program.opts().json) { printJson(allShares); return; }
|
|
167
|
-
|
|
168
|
-
if (allShares.length === 0) {
|
|
169
|
-
console.log(chalk.dim(`No shares available from @${handle}.`));
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
console.log(chalk.bold(`\n Shares from @${chalk.cyan(handle)}:\n`));
|
|
174
|
-
for (const share of allShares) {
|
|
175
|
-
const vis = share.visibility === 'private' ? chalk.yellow('private') : chalk.green('public');
|
|
176
|
-
const dev = chalk.dim(`[${share.device}]`);
|
|
177
|
-
console.log(` ${chalk.bold(share.name)} ${vis} ${dev}`);
|
|
178
|
-
if (share.magnet) console.log(chalk.dim(` ${share.magnet.slice(0, 60)}...`));
|
|
179
|
-
}
|
|
180
|
-
console.log(chalk.dim(`\n ${allShares.length} share${allShares.length !== 1 ? 's' : ''} total\n`));
|
|
181
|
-
|
|
182
|
-
auditLog('remote.browse', { handle, shareCount: allShares.length });
|
|
183
|
-
} catch (err) {
|
|
184
|
-
console.error(chalk.red(err.message));
|
|
185
|
-
process.exitCode = 1;
|
|
186
|
-
}
|
|
187
|
-
});
|
|
188
|
-
|
|
189
|
-
// ── files ───────────────────────────────────────────────────────────────
|
|
190
|
-
remote
|
|
191
|
-
.command('files <handle> <shareName>')
|
|
192
|
-
.description('list files in a remote share')
|
|
193
|
-
.option('--depth <n>', 'Recursive depth for folder listing (1-5, default: 2)', parseInt)
|
|
194
|
-
.option('--json', 'Output as JSON')
|
|
195
|
-
.action(async (handle, shareName, opts) => {
|
|
196
|
-
try {
|
|
197
|
-
requireIdentity();
|
|
198
|
-
handle = handle.replace(/^@/, '');
|
|
199
|
-
|
|
200
|
-
const share = await findShareFromPeer(handle, shareName);
|
|
201
|
-
if (!share) {
|
|
202
|
-
console.error(chalk.red(`Share "${shareName}" not found from @${handle}.`));
|
|
203
|
-
process.exitCode = 1;
|
|
204
|
-
return;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
if (!share.magnet) {
|
|
208
|
-
console.error(chalk.red('Share not seeded yet. The pal needs to run `
|
|
209
|
-
console.log(chalk.gray(' Once seeded, retry:
|
|
210
|
-
process.exitCode = 1;
|
|
211
|
-
return;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Fast path: get file list directly from peer via TCP signaling
|
|
215
|
-
let files;
|
|
216
|
-
if (share.peerIp) {
|
|
217
|
-
try {
|
|
218
|
-
console.log(chalk.dim('Querying file list from peer...'));
|
|
219
|
-
const fileResp = await sendRequest(share.peerIp, 7474, {
|
|
220
|
-
type: 'share_files', shareId: share.id
|
|
221
|
-
}, 5000);
|
|
222
|
-
if (fileResp.ok && fileResp.files?.length > 0) {
|
|
223
|
-
files = fileResp.files;
|
|
224
|
-
}
|
|
225
|
-
} catch { /* fall through to BitTorrent */ }
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
// Fallback: get file list from BitTorrent metadata
|
|
229
|
-
if (!files) {
|
|
230
|
-
const directPeers = share.peerIp && share.peerTorrentPort
|
|
231
|
-
? [{ ip: share.peerIp, torrentPort: share.peerTorrentPort }] : [];
|
|
232
|
-
console.log(chalk.dim('Connecting to swarm to get file list...'));
|
|
233
|
-
let torrent, client;
|
|
234
|
-
try {
|
|
235
|
-
({ torrent, client } = await getTorrentMetadata(share.magnet, { lanPeers: directPeers }));
|
|
236
|
-
files = torrent.files.map(f => ({ name: f.name, path: f.path, size: f.length }));
|
|
237
|
-
torrent.destroy();
|
|
238
|
-
await destroyClient(client);
|
|
239
|
-
} catch (err) {
|
|
240
|
-
console.error(chalk.red(`Failed to get file list: ${err.message}`));
|
|
241
|
-
process.exitCode = 1;
|
|
242
|
-
return;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
if (opts.json || program.opts().json) { printJson(files); return; }
|
|
247
|
-
|
|
248
|
-
console.log(chalk.bold(`\n Files in "${shareName}" from @${chalk.cyan(handle)}:\n`));
|
|
249
|
-
for (const f of files) {
|
|
250
|
-
const type = f.isDir ? chalk.blue('DIR ') : chalk.gray('FILE');
|
|
251
|
-
const indent = ' '.repeat(((f.path || f.name).split('/').length - 1));
|
|
252
|
-
console.log(` ${indent}${type} ${f.path || f.name} ${chalk.dim(formatSize(f.size || 0))}`);
|
|
253
|
-
}
|
|
254
|
-
console.log(chalk.dim(`\n ${files.length} file${files.length !== 1 ? 's' : ''}\n`));
|
|
255
|
-
|
|
256
|
-
auditLog('remote.files', { handle, shareName, fileCount: files.length });
|
|
257
|
-
} catch (err) {
|
|
258
|
-
console.error(chalk.red(err.message));
|
|
259
|
-
process.exitCode = 1;
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
// ── download ────────────────────────────────────────────────────────────
|
|
264
|
-
remote
|
|
265
|
-
.command('download <handle> <shareName>')
|
|
266
|
-
.description('download a share from a pal')
|
|
267
|
-
.option('-o, --output <dir>', 'Output directory', '.')
|
|
268
|
-
.option('--files <files>', 'Comma-separated file names to download (selective)')
|
|
269
|
-
.option('--json', 'Output as JSON')
|
|
270
|
-
.action(async (handle, shareName, opts) => {
|
|
271
|
-
try {
|
|
272
|
-
requireIdentity();
|
|
273
|
-
handle = handle.replace(/^@/, '');
|
|
274
|
-
|
|
275
|
-
const share = await findShareFromPeer(handle, shareName);
|
|
276
|
-
if (!share) {
|
|
277
|
-
console.error(chalk.red(`Share "${shareName}" not found from @${handle}.`));
|
|
278
|
-
process.exitCode = 1;
|
|
279
|
-
return;
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
if (!share.magnet) {
|
|
283
|
-
console.error(chalk.red('Share has no magnet link — the pal needs to run `
|
|
284
|
-
process.exitCode = 1;
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const outputDir = path.resolve(opts.output);
|
|
289
|
-
fs.mkdirSync(outputDir, { recursive: true });
|
|
290
|
-
|
|
291
|
-
const selectedFiles = parseCommaList(opts.files).length > 0 ? parseCommaList(opts.files) : null;
|
|
292
|
-
|
|
293
|
-
// P2P download via WebTorrent (BitTorrent swarm) — inject direct peer for fast discovery
|
|
294
|
-
const directPeers = share.peerIp && share.peerTorrentPort
|
|
295
|
-
? [{ ip: share.peerIp, torrentPort: share.peerTorrentPort }] : [];
|
|
296
|
-
if (directPeers.length) console.log(chalk.dim(`Direct peer: ${share.peerIp}:${share.peerTorrentPort}`));
|
|
297
|
-
console.log(chalk.dim('Connecting to swarm and downloading...'));
|
|
298
|
-
const { torrent, client } = await getTorrentMetadata(share.magnet, { path: outputDir, timeout: TORRENT_TIMEOUT_LONG, lanPeers: directPeers });
|
|
299
|
-
|
|
300
|
-
if (selectedFiles) {
|
|
301
|
-
for (const file of torrent.files) {
|
|
302
|
-
if (!selectedFiles.some(sf => file.name === sf || file.path.includes(sf))) {
|
|
303
|
-
file.deselect();
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
await new Promise((resolve) => {
|
|
309
|
-
torrent.on('done', resolve);
|
|
310
|
-
const interval = setInterval(() => {
|
|
311
|
-
const pct = (torrent.progress * 100).toFixed(1);
|
|
312
|
-
const speed = (torrent.downloadSpeed / 1048576).toFixed(1);
|
|
313
|
-
process.stdout.write(`\r ${chalk.cyan(pct + '%')} ${chalk.dim(speed + ' MB/s')} ${chalk.dim(torrent.numPeers + ' peers')}`);
|
|
314
|
-
}, 500);
|
|
315
|
-
torrent.on('done', () => { clearInterval(interval); process.stdout.write('\n'); });
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const downloaded = torrent.files
|
|
319
|
-
.filter(f => !selectedFiles || selectedFiles.some(sf => f.name === sf || f.path.includes(sf)))
|
|
320
|
-
.map(f => ({ name: f.name, path: f.path, size: f.length }));
|
|
321
|
-
|
|
322
|
-
torrent.destroy();
|
|
323
|
-
await destroyClient(client);
|
|
324
|
-
|
|
325
|
-
if (opts.json || program.opts().json) { printJson({ success: true, files: downloaded, outputDir }); return; }
|
|
326
|
-
|
|
327
|
-
console.log(chalk.green(`\n Downloaded ${downloaded.length} file${downloaded.length !== 1 ? 's' : ''} to ${outputDir}\n`));
|
|
328
|
-
for (const f of downloaded) {
|
|
329
|
-
console.log(` ${chalk.gray('✓')} ${f.path} ${chalk.dim(`(${formatSize(f.size)})`)}`);
|
|
330
|
-
}
|
|
331
|
-
console.log();
|
|
332
|
-
|
|
333
|
-
auditLog('remote.download', { handle, shareName, fileCount: downloaded.length, outputDir });
|
|
334
|
-
} catch (err) {
|
|
335
|
-
console.error(chalk.red(err.message));
|
|
336
|
-
process.exitCode = 1;
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
// Find a specific share from a peer via P2P signaling
|
|
342
|
-
async function findShareFromPeer(handle, shareName) {
|
|
343
|
-
const friends = getFriends();
|
|
344
|
-
const pal = friends.find(f => f.handle === handle || f.name === handle || f.id === handle || f.publicKey === handle);
|
|
345
|
-
if (!pal) return null;
|
|
346
|
-
|
|
347
|
-
const addr = await resolvePeerAddress(pal);
|
|
348
|
-
if (!addr) return null;
|
|
349
|
-
|
|
350
|
-
try {
|
|
351
|
-
const peerData = await queryPeerShares(addr.ip, addr.port);
|
|
352
|
-
const match = (peerData.shares || []).find(s => s.name === shareName);
|
|
353
|
-
if (match) {
|
|
354
|
-
// HIGH-2 fix: only trust torrentPort from authenticated responses
|
|
355
|
-
// (queryPeerShares already attempts auth — torrentPort is only present
|
|
356
|
-
// in authenticated friend responses from the server)
|
|
357
|
-
return {
|
|
358
|
-
...match,
|
|
359
|
-
device: peerData.deviceName,
|
|
360
|
-
deviceId: peerData.deviceId,
|
|
361
|
-
peerIp: addr.ip,
|
|
362
|
-
peerTorrentPort: peerData.torrentPort || null,
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
} catch {}
|
|
366
|
-
|
|
367
|
-
return null;
|
|
368
|
-
}
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
import { requireIdentity, auditLog } from '../core/permissions.js';
|
|
5
|
+
import { getFriends } from '../core/users.js';
|
|
6
|
+
import { formatSize } from '../utils/format.js';
|
|
7
|
+
import { printJson, parseCommaList } from '../utils/cli.js';
|
|
8
|
+
import { getTorrentMetadata, destroyClient, TORRENT_TIMEOUT_LONG } from '../utils/torrent.js';
|
|
9
|
+
import { sendRequest, sendAuthenticatedRequest } from '../core/signalingServer.js';
|
|
10
|
+
import { getNearbyPeers, getCachedPeerAddress } from '../core/mdnsService.js';
|
|
11
|
+
import { getIdentity } from '../core/identity.js';
|
|
12
|
+
|
|
13
|
+
// Verify a cached address is still alive via quick TCP probe
|
|
14
|
+
async function verifyPeerAlive(ip, port = 7474) {
|
|
15
|
+
try {
|
|
16
|
+
const r = await sendRequest(ip, port, { type: 'status' }, 3000);
|
|
17
|
+
return r.ok && r.protocol === 'PAL/1.0';
|
|
18
|
+
} catch { return false; }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Resolve a pal's IP and signaling port via mDNS (LAN) or discovery server (device registration)
|
|
22
|
+
async function resolvePeerAddress(pal) {
|
|
23
|
+
// Strategy 1: mDNS — check if peer is on LAN (only works when pal serve is running in same process)
|
|
24
|
+
const nearby = getNearbyPeers();
|
|
25
|
+
const mdnsPeer = nearby.find(p =>
|
|
26
|
+
p.publicKey === pal.publicKey || p.publicKey === pal.id ||
|
|
27
|
+
p.handle === pal.handle || p.name === pal.name
|
|
28
|
+
);
|
|
29
|
+
if (mdnsPeer && mdnsPeer.ip && mdnsPeer.ip !== 'unknown') {
|
|
30
|
+
return { ip: mdnsPeer.ip, port: 7474, source: 'mDNS' };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Strategy 2: Cached peer address (persisted from previous mDNS/discovery)
|
|
34
|
+
// MEDIUM-3 fix: only look up by public key identifiers
|
|
35
|
+
const cached = getCachedPeerAddress([pal.publicKey, pal.id]);
|
|
36
|
+
if (cached && cached.ip) {
|
|
37
|
+
const alive = await verifyPeerAlive(cached.ip, cached.port || 7474);
|
|
38
|
+
if (alive) {
|
|
39
|
+
return { ip: cached.ip, port: cached.port || 7474, source: 'cache' };
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Strategy 3: Discovery server — get device IP (NOT share lists)
|
|
44
|
+
const handle = pal.handle || pal.name;
|
|
45
|
+
if (handle) {
|
|
46
|
+
try {
|
|
47
|
+
const { getPrimaryServer } = await import('../core/discoveryClient.js');
|
|
48
|
+
const serverUrl = getPrimaryServer();
|
|
49
|
+
const devicesRes = await fetch(`${serverUrl}/devices/${encodeURIComponent(handle)}`, {
|
|
50
|
+
signal: AbortSignal.timeout(5000)
|
|
51
|
+
});
|
|
52
|
+
if (devicesRes.ok) {
|
|
53
|
+
const { devices = [] } = await devicesRes.json();
|
|
54
|
+
for (const device of devices) {
|
|
55
|
+
if (device.ip) {
|
|
56
|
+
return { ip: device.ip, port: 7474, source: 'discovery', device };
|
|
57
|
+
}
|
|
58
|
+
if (device.serveUrl) {
|
|
59
|
+
try {
|
|
60
|
+
const url = new URL(device.serveUrl);
|
|
61
|
+
return { ip: url.hostname, port: 7474, source: 'discovery', device };
|
|
62
|
+
} catch {}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
} catch {}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Strategy 4: Check if pal has a known IP in their friend record
|
|
70
|
+
if (pal.ip) {
|
|
71
|
+
return { ip: pal.ip, port: 7474, source: 'friend-record' };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Query a peer's share list via TCP signaling (PAL/1.0) with authentication
|
|
78
|
+
async function queryPeerShares(ip, port = 7474, timeout = 5000) {
|
|
79
|
+
// Try authenticated request first for full share list access
|
|
80
|
+
try {
|
|
81
|
+
const identity = await getIdentity();
|
|
82
|
+
if (identity?.privateKey && identity?.publicKey) {
|
|
83
|
+
const response = await sendAuthenticatedRequest(
|
|
84
|
+
ip, port, { type: 'share_list' }, identity.privateKey, identity.publicKey, timeout
|
|
85
|
+
);
|
|
86
|
+
if (response.ok) return response;
|
|
87
|
+
}
|
|
88
|
+
} catch {}
|
|
89
|
+
|
|
90
|
+
// Fallback to unauthenticated (will only see public shares)
|
|
91
|
+
const response = await sendRequest(ip, port, { type: 'share_list' }, timeout);
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw new Error(response.error || 'Peer refused share list request');
|
|
94
|
+
}
|
|
95
|
+
return response;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export default function remoteCommand(program) {
|
|
99
|
+
const remote = program
|
|
100
|
+
.command('remote')
|
|
101
|
+
.description('browse and download from pals\' shares')
|
|
102
|
+
.addHelpText('after', `
|
|
103
|
+
Examples:
|
|
104
|
+
$ pal remote browse @alice List Alice's shares
|
|
105
|
+
$ pal remote browse @alice --device laptop List shares from specific device
|
|
106
|
+
$ pal remote files @alice "Photos" List files in Alice's "Photos" share
|
|
107
|
+
$ pal remote download @alice "Photos" Download Alice's "Photos" share
|
|
108
|
+
$ pal remote download @alice "Photos" --files "pic1.jpg,pic2.jpg"
|
|
109
|
+
`);
|
|
110
|
+
|
|
111
|
+
// ── browse ──────────────────────────────────────────────────────────────
|
|
112
|
+
remote
|
|
113
|
+
.command('browse <handle>')
|
|
114
|
+
.description('list shares from a pal')
|
|
115
|
+
.option('--device <deviceName>', 'Filter by device name')
|
|
116
|
+
.option('--json', 'Output as JSON')
|
|
117
|
+
.action(async (handle, opts) => {
|
|
118
|
+
try {
|
|
119
|
+
requireIdentity();
|
|
120
|
+
handle = handle.replace(/^@/, '');
|
|
121
|
+
|
|
122
|
+
const friends = getFriends();
|
|
123
|
+
const pal = friends.find(f => f.handle === handle || f.name === handle || f.id === handle || f.publicKey === handle);
|
|
124
|
+
if (!pal) {
|
|
125
|
+
console.error(chalk.red(`@${handle} is not in your pals. Run \`pal pal add\` first.`));
|
|
126
|
+
process.exitCode = 1;
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
console.log(chalk.dim(`Looking for @${handle}...`));
|
|
131
|
+
const addr = await resolvePeerAddress(pal);
|
|
132
|
+
if (!addr) {
|
|
133
|
+
console.log(chalk.yellow(`Could not find @${handle}. The pal might be offline.`));
|
|
134
|
+
console.log(chalk.dim(` Tried: mDNS (LAN), discovery server (device lookup)`));
|
|
135
|
+
process.exitCode = 1;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
console.log(chalk.dim(`Found @${handle} via ${addr.source} (${addr.ip}:${addr.port})`));
|
|
140
|
+
|
|
141
|
+
let peerData;
|
|
142
|
+
try {
|
|
143
|
+
peerData = await queryPeerShares(addr.ip, addr.port);
|
|
144
|
+
} catch (err) {
|
|
145
|
+
console.log(chalk.yellow(`Could not connect to @${handle} at ${addr.ip}:${addr.port}`));
|
|
146
|
+
console.log(chalk.dim(` Error: ${err.message}`));
|
|
147
|
+
console.log(chalk.dim(` The pal might not be running \`pal serve\``));
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const allShares = (peerData.shares || []).map(s => ({
|
|
153
|
+
...s,
|
|
154
|
+
device: peerData.deviceName,
|
|
155
|
+
deviceId: peerData.deviceId,
|
|
156
|
+
}));
|
|
157
|
+
|
|
158
|
+
if (opts.device) {
|
|
159
|
+
const filtered = allShares.filter(s => s.device === opts.device);
|
|
160
|
+
if (filtered.length === 0) {
|
|
161
|
+
console.log(chalk.dim(`No shares from device "${opts.device}".`));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (opts.json || program.opts().json) { printJson(allShares); return; }
|
|
167
|
+
|
|
168
|
+
if (allShares.length === 0) {
|
|
169
|
+
console.log(chalk.dim(`No shares available from @${handle}.`));
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
console.log(chalk.bold(`\n Shares from @${chalk.cyan(handle)}:\n`));
|
|
174
|
+
for (const share of allShares) {
|
|
175
|
+
const vis = share.visibility === 'private' ? chalk.yellow('private') : chalk.green('public');
|
|
176
|
+
const dev = chalk.dim(`[${share.device}]`);
|
|
177
|
+
console.log(` ${chalk.bold(share.name)} ${vis} ${dev}`);
|
|
178
|
+
if (share.magnet) console.log(chalk.dim(` ${share.magnet.slice(0, 60)}...`));
|
|
179
|
+
}
|
|
180
|
+
console.log(chalk.dim(`\n ${allShares.length} share${allShares.length !== 1 ? 's' : ''} total\n`));
|
|
181
|
+
|
|
182
|
+
auditLog('remote.browse', { handle, shareCount: allShares.length });
|
|
183
|
+
} catch (err) {
|
|
184
|
+
console.error(chalk.red(err.message));
|
|
185
|
+
process.exitCode = 1;
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// ── files ───────────────────────────────────────────────────────────────
|
|
190
|
+
remote
|
|
191
|
+
.command('files <handle> <shareName>')
|
|
192
|
+
.description('list files in a remote share')
|
|
193
|
+
.option('--depth <n>', 'Recursive depth for folder listing (1-5, default: 2)', parseInt)
|
|
194
|
+
.option('--json', 'Output as JSON')
|
|
195
|
+
.action(async (handle, shareName, opts) => {
|
|
196
|
+
try {
|
|
197
|
+
requireIdentity();
|
|
198
|
+
handle = handle.replace(/^@/, '');
|
|
199
|
+
|
|
200
|
+
const share = await findShareFromPeer(handle, shareName);
|
|
201
|
+
if (!share) {
|
|
202
|
+
console.error(chalk.red(`Share "${shareName}" not found from @${handle}.`));
|
|
203
|
+
process.exitCode = 1;
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!share.magnet) {
|
|
208
|
+
console.error(chalk.red('Share not seeded yet. The pal needs to run `pal serve` to start seeding and generate magnet links.'));
|
|
209
|
+
console.log(chalk.gray(' Once seeded, retry: pal remote files @' + handle + ' "' + shareName + '"'));
|
|
210
|
+
process.exitCode = 1;
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Fast path: get file list directly from peer via TCP signaling
|
|
215
|
+
let files;
|
|
216
|
+
if (share.peerIp) {
|
|
217
|
+
try {
|
|
218
|
+
console.log(chalk.dim('Querying file list from peer...'));
|
|
219
|
+
const fileResp = await sendRequest(share.peerIp, 7474, {
|
|
220
|
+
type: 'share_files', shareId: share.id
|
|
221
|
+
}, 5000);
|
|
222
|
+
if (fileResp.ok && fileResp.files?.length > 0) {
|
|
223
|
+
files = fileResp.files;
|
|
224
|
+
}
|
|
225
|
+
} catch { /* fall through to BitTorrent */ }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Fallback: get file list from BitTorrent metadata
|
|
229
|
+
if (!files) {
|
|
230
|
+
const directPeers = share.peerIp && share.peerTorrentPort
|
|
231
|
+
? [{ ip: share.peerIp, torrentPort: share.peerTorrentPort }] : [];
|
|
232
|
+
console.log(chalk.dim('Connecting to swarm to get file list...'));
|
|
233
|
+
let torrent, client;
|
|
234
|
+
try {
|
|
235
|
+
({ torrent, client } = await getTorrentMetadata(share.magnet, { lanPeers: directPeers }));
|
|
236
|
+
files = torrent.files.map(f => ({ name: f.name, path: f.path, size: f.length }));
|
|
237
|
+
torrent.destroy();
|
|
238
|
+
await destroyClient(client);
|
|
239
|
+
} catch (err) {
|
|
240
|
+
console.error(chalk.red(`Failed to get file list: ${err.message}`));
|
|
241
|
+
process.exitCode = 1;
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (opts.json || program.opts().json) { printJson(files); return; }
|
|
247
|
+
|
|
248
|
+
console.log(chalk.bold(`\n Files in "${shareName}" from @${chalk.cyan(handle)}:\n`));
|
|
249
|
+
for (const f of files) {
|
|
250
|
+
const type = f.isDir ? chalk.blue('DIR ') : chalk.gray('FILE');
|
|
251
|
+
const indent = ' '.repeat(((f.path || f.name).split('/').length - 1));
|
|
252
|
+
console.log(` ${indent}${type} ${f.path || f.name} ${chalk.dim(formatSize(f.size || 0))}`);
|
|
253
|
+
}
|
|
254
|
+
console.log(chalk.dim(`\n ${files.length} file${files.length !== 1 ? 's' : ''}\n`));
|
|
255
|
+
|
|
256
|
+
auditLog('remote.files', { handle, shareName, fileCount: files.length });
|
|
257
|
+
} catch (err) {
|
|
258
|
+
console.error(chalk.red(err.message));
|
|
259
|
+
process.exitCode = 1;
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
// ── download ────────────────────────────────────────────────────────────
|
|
264
|
+
remote
|
|
265
|
+
.command('download <handle> <shareName>')
|
|
266
|
+
.description('download a share from a pal')
|
|
267
|
+
.option('-o, --output <dir>', 'Output directory', '.')
|
|
268
|
+
.option('--files <files>', 'Comma-separated file names to download (selective)')
|
|
269
|
+
.option('--json', 'Output as JSON')
|
|
270
|
+
.action(async (handle, shareName, opts) => {
|
|
271
|
+
try {
|
|
272
|
+
requireIdentity();
|
|
273
|
+
handle = handle.replace(/^@/, '');
|
|
274
|
+
|
|
275
|
+
const share = await findShareFromPeer(handle, shareName);
|
|
276
|
+
if (!share) {
|
|
277
|
+
console.error(chalk.red(`Share "${shareName}" not found from @${handle}.`));
|
|
278
|
+
process.exitCode = 1;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!share.magnet) {
|
|
283
|
+
console.error(chalk.red('Share has no magnet link — the pal needs to run `pal serve` to generate magnets.'));
|
|
284
|
+
process.exitCode = 1;
|
|
285
|
+
return;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
const outputDir = path.resolve(opts.output);
|
|
289
|
+
fs.mkdirSync(outputDir, { recursive: true });
|
|
290
|
+
|
|
291
|
+
const selectedFiles = parseCommaList(opts.files).length > 0 ? parseCommaList(opts.files) : null;
|
|
292
|
+
|
|
293
|
+
// P2P download via WebTorrent (BitTorrent swarm) — inject direct peer for fast discovery
|
|
294
|
+
const directPeers = share.peerIp && share.peerTorrentPort
|
|
295
|
+
? [{ ip: share.peerIp, torrentPort: share.peerTorrentPort }] : [];
|
|
296
|
+
if (directPeers.length) console.log(chalk.dim(`Direct peer: ${share.peerIp}:${share.peerTorrentPort}`));
|
|
297
|
+
console.log(chalk.dim('Connecting to swarm and downloading...'));
|
|
298
|
+
const { torrent, client } = await getTorrentMetadata(share.magnet, { path: outputDir, timeout: TORRENT_TIMEOUT_LONG, lanPeers: directPeers });
|
|
299
|
+
|
|
300
|
+
if (selectedFiles) {
|
|
301
|
+
for (const file of torrent.files) {
|
|
302
|
+
if (!selectedFiles.some(sf => file.name === sf || file.path.includes(sf))) {
|
|
303
|
+
file.deselect();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
await new Promise((resolve) => {
|
|
309
|
+
torrent.on('done', resolve);
|
|
310
|
+
const interval = setInterval(() => {
|
|
311
|
+
const pct = (torrent.progress * 100).toFixed(1);
|
|
312
|
+
const speed = (torrent.downloadSpeed / 1048576).toFixed(1);
|
|
313
|
+
process.stdout.write(`\r ${chalk.cyan(pct + '%')} ${chalk.dim(speed + ' MB/s')} ${chalk.dim(torrent.numPeers + ' peers')}`);
|
|
314
|
+
}, 500);
|
|
315
|
+
torrent.on('done', () => { clearInterval(interval); process.stdout.write('\n'); });
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
const downloaded = torrent.files
|
|
319
|
+
.filter(f => !selectedFiles || selectedFiles.some(sf => f.name === sf || f.path.includes(sf)))
|
|
320
|
+
.map(f => ({ name: f.name, path: f.path, size: f.length }));
|
|
321
|
+
|
|
322
|
+
torrent.destroy();
|
|
323
|
+
await destroyClient(client);
|
|
324
|
+
|
|
325
|
+
if (opts.json || program.opts().json) { printJson({ success: true, files: downloaded, outputDir }); return; }
|
|
326
|
+
|
|
327
|
+
console.log(chalk.green(`\n Downloaded ${downloaded.length} file${downloaded.length !== 1 ? 's' : ''} to ${outputDir}\n`));
|
|
328
|
+
for (const f of downloaded) {
|
|
329
|
+
console.log(` ${chalk.gray('✓')} ${f.path} ${chalk.dim(`(${formatSize(f.size)})`)}`);
|
|
330
|
+
}
|
|
331
|
+
console.log();
|
|
332
|
+
|
|
333
|
+
auditLog('remote.download', { handle, shareName, fileCount: downloaded.length, outputDir });
|
|
334
|
+
} catch (err) {
|
|
335
|
+
console.error(chalk.red(err.message));
|
|
336
|
+
process.exitCode = 1;
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Find a specific share from a peer via P2P signaling
|
|
342
|
+
async function findShareFromPeer(handle, shareName) {
|
|
343
|
+
const friends = getFriends();
|
|
344
|
+
const pal = friends.find(f => f.handle === handle || f.name === handle || f.id === handle || f.publicKey === handle);
|
|
345
|
+
if (!pal) return null;
|
|
346
|
+
|
|
347
|
+
const addr = await resolvePeerAddress(pal);
|
|
348
|
+
if (!addr) return null;
|
|
349
|
+
|
|
350
|
+
try {
|
|
351
|
+
const peerData = await queryPeerShares(addr.ip, addr.port);
|
|
352
|
+
const match = (peerData.shares || []).find(s => s.name === shareName);
|
|
353
|
+
if (match) {
|
|
354
|
+
// HIGH-2 fix: only trust torrentPort from authenticated responses
|
|
355
|
+
// (queryPeerShares already attempts auth — torrentPort is only present
|
|
356
|
+
// in authenticated friend responses from the server)
|
|
357
|
+
return {
|
|
358
|
+
...match,
|
|
359
|
+
device: peerData.deviceName,
|
|
360
|
+
deviceId: peerData.deviceId,
|
|
361
|
+
peerIp: addr.ip,
|
|
362
|
+
peerTorrentPort: peerData.torrentPort || null,
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
} catch {}
|
|
366
|
+
|
|
367
|
+
return null;
|
|
368
|
+
}
|