pal-explorer-cli 0.4.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.
Files changed (156) hide show
  1. package/LICENSE.md +18 -0
  2. package/README.md +314 -0
  3. package/bin/pal.js +230 -0
  4. package/extensions/@palexplorer/analytics/README.md +45 -0
  5. package/extensions/@palexplorer/analytics/docs/MONETIZATION.md +14 -0
  6. package/extensions/@palexplorer/analytics/docs/PLAN.md +23 -0
  7. package/extensions/@palexplorer/analytics/docs/PRIVACY.md +38 -0
  8. package/extensions/@palexplorer/analytics/extension.json +27 -0
  9. package/extensions/@palexplorer/analytics/index.js +186 -0
  10. package/extensions/@palexplorer/analytics/test/analytics.test.js +82 -0
  11. package/extensions/@palexplorer/audit/extension.json +17 -0
  12. package/extensions/@palexplorer/audit/index.js +2 -0
  13. package/extensions/@palexplorer/auth-email/extension.json +17 -0
  14. package/extensions/@palexplorer/auth-email/index.js +102 -0
  15. package/extensions/@palexplorer/auth-oauth/extension.json +16 -0
  16. package/extensions/@palexplorer/auth-oauth/index.js +199 -0
  17. package/extensions/@palexplorer/chat/extension.json +17 -0
  18. package/extensions/@palexplorer/chat/index.js +2 -0
  19. package/extensions/@palexplorer/discovery/extension.json +16 -0
  20. package/extensions/@palexplorer/discovery/index.js +111 -0
  21. package/extensions/@palexplorer/email-notifications/extension.json +23 -0
  22. package/extensions/@palexplorer/email-notifications/index.js +242 -0
  23. package/extensions/@palexplorer/explorer-integration/extension.json +13 -0
  24. package/extensions/@palexplorer/explorer-integration/index.js +122 -0
  25. package/extensions/@palexplorer/groups/extension.json +17 -0
  26. package/extensions/@palexplorer/groups/index.js +2 -0
  27. package/extensions/@palexplorer/networks/extension.json +17 -0
  28. package/extensions/@palexplorer/networks/index.js +2 -0
  29. package/extensions/@palexplorer/share-links/extension.json +17 -0
  30. package/extensions/@palexplorer/share-links/index.js +2 -0
  31. package/extensions/@palexplorer/sync/extension.json +17 -0
  32. package/extensions/@palexplorer/sync/index.js +2 -0
  33. package/extensions/@palexplorer/user-mgmt/extension.json +17 -0
  34. package/extensions/@palexplorer/user-mgmt/index.js +2 -0
  35. package/extensions/@palexplorer/vfs/extension.json +17 -0
  36. package/extensions/@palexplorer/vfs/index.js +167 -0
  37. package/lib/capabilities.js +263 -0
  38. package/lib/commands/analytics.js +175 -0
  39. package/lib/commands/api-keys.js +131 -0
  40. package/lib/commands/audit.js +235 -0
  41. package/lib/commands/auth.js +137 -0
  42. package/lib/commands/backup.js +76 -0
  43. package/lib/commands/billing.js +148 -0
  44. package/lib/commands/chat.js +217 -0
  45. package/lib/commands/cloud-backup.js +231 -0
  46. package/lib/commands/comment.js +99 -0
  47. package/lib/commands/completion.js +203 -0
  48. package/lib/commands/compliance.js +218 -0
  49. package/lib/commands/config.js +136 -0
  50. package/lib/commands/connect.js +44 -0
  51. package/lib/commands/dept.js +294 -0
  52. package/lib/commands/device.js +146 -0
  53. package/lib/commands/download.js +226 -0
  54. package/lib/commands/explorer.js +178 -0
  55. package/lib/commands/extension.js +970 -0
  56. package/lib/commands/favorite.js +90 -0
  57. package/lib/commands/federation.js +270 -0
  58. package/lib/commands/file.js +533 -0
  59. package/lib/commands/group.js +271 -0
  60. package/lib/commands/gui-share.js +29 -0
  61. package/lib/commands/init.js +61 -0
  62. package/lib/commands/invite.js +59 -0
  63. package/lib/commands/list.js +59 -0
  64. package/lib/commands/log.js +116 -0
  65. package/lib/commands/nearby.js +108 -0
  66. package/lib/commands/network.js +251 -0
  67. package/lib/commands/notify.js +198 -0
  68. package/lib/commands/org.js +273 -0
  69. package/lib/commands/pal.js +180 -0
  70. package/lib/commands/permissions.js +216 -0
  71. package/lib/commands/pin.js +97 -0
  72. package/lib/commands/protocol.js +357 -0
  73. package/lib/commands/rbac.js +147 -0
  74. package/lib/commands/recover.js +36 -0
  75. package/lib/commands/register.js +171 -0
  76. package/lib/commands/relay.js +131 -0
  77. package/lib/commands/remote.js +368 -0
  78. package/lib/commands/revoke.js +50 -0
  79. package/lib/commands/scanner.js +280 -0
  80. package/lib/commands/schedule.js +344 -0
  81. package/lib/commands/scim.js +203 -0
  82. package/lib/commands/search.js +181 -0
  83. package/lib/commands/serve.js +438 -0
  84. package/lib/commands/server.js +350 -0
  85. package/lib/commands/share-link.js +199 -0
  86. package/lib/commands/share.js +323 -0
  87. package/lib/commands/sso.js +200 -0
  88. package/lib/commands/status.js +136 -0
  89. package/lib/commands/stream.js +562 -0
  90. package/lib/commands/su.js +187 -0
  91. package/lib/commands/sync.js +827 -0
  92. package/lib/commands/transfers.js +152 -0
  93. package/lib/commands/uninstall.js +188 -0
  94. package/lib/commands/update.js +204 -0
  95. package/lib/commands/user.js +276 -0
  96. package/lib/commands/vfs.js +84 -0
  97. package/lib/commands/web.js +52 -0
  98. package/lib/commands/webhook.js +180 -0
  99. package/lib/commands/whoami.js +59 -0
  100. package/lib/commands/workspace.js +121 -0
  101. package/lib/core/accessLog.js +54 -0
  102. package/lib/core/analytics.js +99 -0
  103. package/lib/core/backup.js +84 -0
  104. package/lib/core/billing.js +336 -0
  105. package/lib/core/bitfieldStore.js +53 -0
  106. package/lib/core/connectionManager.js +182 -0
  107. package/lib/core/dhtDiscovery.js +148 -0
  108. package/lib/core/discoveryClient.js +408 -0
  109. package/lib/core/extensionAnalyzer.js +357 -0
  110. package/lib/core/extensionSandbox.js +250 -0
  111. package/lib/core/extensionWorkerHost.js +166 -0
  112. package/lib/core/extensions.js +1082 -0
  113. package/lib/core/fileDiff.js +69 -0
  114. package/lib/core/groups.js +119 -0
  115. package/lib/core/identity.js +340 -0
  116. package/lib/core/mdnsService.js +126 -0
  117. package/lib/core/networks.js +81 -0
  118. package/lib/core/permissions.js +109 -0
  119. package/lib/core/pro.js +27 -0
  120. package/lib/core/resolver.js +74 -0
  121. package/lib/core/serverList.js +224 -0
  122. package/lib/core/sharePolicy.js +69 -0
  123. package/lib/core/shares.js +325 -0
  124. package/lib/core/signalingServer.js +441 -0
  125. package/lib/core/streamTransport.js +106 -0
  126. package/lib/core/su.js +55 -0
  127. package/lib/core/syncEngine.js +264 -0
  128. package/lib/core/syncState.js +159 -0
  129. package/lib/core/transfers.js +259 -0
  130. package/lib/core/users.js +225 -0
  131. package/lib/core/vfs.js +216 -0
  132. package/lib/core/webServer.js +702 -0
  133. package/lib/core/webrtcStream.js +396 -0
  134. package/lib/crypto/chatEncryption.js +57 -0
  135. package/lib/crypto/shareEncryption.js +195 -0
  136. package/lib/crypto/sharePassword.js +35 -0
  137. package/lib/crypto/streamEncryption.js +189 -0
  138. package/lib/package.json +1 -0
  139. package/lib/protocol/envelope.js +271 -0
  140. package/lib/protocol/handler.js +191 -0
  141. package/lib/protocol/index.js +27 -0
  142. package/lib/protocol/messages.js +247 -0
  143. package/lib/protocol/negotiation.js +127 -0
  144. package/lib/protocol/policy.js +142 -0
  145. package/lib/protocol/router.js +86 -0
  146. package/lib/protocol/sync.js +122 -0
  147. package/lib/utils/cli.js +15 -0
  148. package/lib/utils/config.js +123 -0
  149. package/lib/utils/configIntegrity.js +87 -0
  150. package/lib/utils/downloadDir.js +9 -0
  151. package/lib/utils/explorer.js +83 -0
  152. package/lib/utils/format.js +12 -0
  153. package/lib/utils/help.js +357 -0
  154. package/lib/utils/logger.js +103 -0
  155. package/lib/utils/torrent.js +203 -0
  156. package/package.json +71 -0
@@ -0,0 +1,171 @@
1
+ import chalk from 'chalk';
2
+ import config from '../utils/config.js';
3
+ import { setIdentityHandle, getIdentity } from '../core/identity.js';
4
+ import { getPrimaryServer, postTo, parseHandle } from '../core/discoveryClient.js';
5
+ import { updateProfileHandle } from '../core/users.js';
6
+
7
+ export default function registerCommand(program) {
8
+ program
9
+ .command('register <handle>')
10
+ .description('register your identity with a handle on the discovery network')
11
+ .option('--email <email>', 'Verify ownership via email OTP (required for first registration)')
12
+ .option('--server <url>', 'Register on a specific discovery server')
13
+ .addHelpText('after', `
14
+ Examples:
15
+ $ pe register alice Register handle @alice (direct signature)
16
+ $ pe register alice --email a@b Register with email OTP verification
17
+ $ pe register alice --server https://custom.com Register on specific server
18
+ `)
19
+ .action(async (handle, opts) => {
20
+ handle = handle.replace(/^@/, '');
21
+ const identity = await getIdentity();
22
+ if (!identity) {
23
+ console.log(chalk.red('No identity found. Run `pe init <name>` first.'));
24
+ process.exitCode = 1; return;
25
+ }
26
+
27
+ if (!/^[a-zA-Z0-9_-]{3,32}$/.test(handle)) {
28
+ console.log(chalk.red('Handle must be 3-32 characters: letters, numbers, hyphens, underscores only.'));
29
+ process.exitCode = 1; return;
30
+ }
31
+
32
+ const serverUrl = opts.server || getPrimaryServer();
33
+
34
+ if (opts.email) {
35
+ // OTP flow: request OTP sent to email
36
+ console.log(chalk.blue(`Sending OTP to ${opts.email}...`));
37
+ try {
38
+ const res = await postTo(serverUrl, '/otp/send', { handle, publicKey: identity.publicKey });
39
+ const data = await res.json();
40
+ if (!res.ok) {
41
+ console.log(chalk.red(`Failed: ${data.error}`));
42
+ process.exitCode = 1; return;
43
+ }
44
+ console.log(chalk.green('OTP sent. Run `pe verify <code>` with the code from your email.'));
45
+ config.set('_pendingRegistration', { handle, publicKey: identity.publicKey, email: opts.email, serverUrl });
46
+ } catch {
47
+ console.log(chalk.red(`Could not reach discovery server at ${serverUrl}`));
48
+ process.exitCode = 1;
49
+ }
50
+ return;
51
+ }
52
+
53
+ // Challenge-response registration
54
+ const sodium = (await import('sodium-native')).default;
55
+ const privateKey = Buffer.from(identity.privateKey, 'hex');
56
+
57
+ try {
58
+ process.stdout.write(chalk.gray(`Registering @${handle} on ${serverUrl}... `));
59
+
60
+ // Step 1: Request challenge from server
61
+ const challengeRes = await postTo(serverUrl, '/register/challenge', { handle, publicKey: identity.publicKey });
62
+ const challengeData = await challengeRes.json();
63
+ if (!challengeRes.ok || !challengeData.challengeId) {
64
+ console.log(chalk.red(`\nFailed: ${challengeData.error || 'Could not get challenge'}`));
65
+ if (challengeData.suggestions?.length > 0) {
66
+ console.log(chalk.yellow('Available alternatives:'));
67
+ for (const s of challengeData.suggestions) {
68
+ console.log(chalk.white(` @${s}`));
69
+ }
70
+ }
71
+ process.exitCode = 1; return;
72
+ }
73
+
74
+ // Step 2: Sign challenge and complete registration
75
+ const message = `register:${handle}:${challengeData.nonce}`;
76
+ const sig = Buffer.alloc(sodium.crypto_sign_BYTES);
77
+ sodium.crypto_sign_detached(sig, Buffer.from(message), privateKey);
78
+
79
+ const res = await postTo(serverUrl, '/register', { challengeId: challengeData.challengeId, signature: sig.toString('hex') });
80
+ const result = await res.json();
81
+ if (res.status === 403 && result.code === 'VANITY_HANDLE_PRO_REQUIRED') {
82
+ console.log(chalk.red(`\nVanity handle "@${handle}" requires Pro.`));
83
+ console.log(chalk.yellow('Short custom handles (3-15 chars) are a Pro feature.'));
84
+ console.log(chalk.gray('Upgrade: pe billing upgrade'));
85
+ console.log(chalk.gray('Or use an auto-generated handle instead.'));
86
+ process.exitCode = 1; return;
87
+ }
88
+ if (result.success) {
89
+ console.log(chalk.green('done.'));
90
+ // Store full federated handle if registered on non-primary server
91
+ const primary = getPrimaryServer();
92
+ const fullHandle = (opts.server && opts.server !== primary)
93
+ ? `${handle}@${serverUrl.replace(/^https?:\/\//, '')}`
94
+ : handle;
95
+ console.log(chalk.green(`Registered as @${fullHandle}`));
96
+ setIdentityHandle(fullHandle);
97
+ updateProfileHandle(identity.publicKey, fullHandle);
98
+ config.delete('_pendingRegistration');
99
+
100
+ // Publish to DHT for decentralized fallback
101
+ try {
102
+ const { DHTDiscovery } = await import('../core/dhtDiscovery.js');
103
+ const dht = new DHTDiscovery();
104
+ const dhtHash = await dht.publish(handle, identity.publicKey, identity.privateKey);
105
+ const cache = config.get('handleCache') || {};
106
+ if (!cache[handle]) cache[handle] = {};
107
+ cache[handle].dhtHash = dhtHash;
108
+ config.set('handleCache', cache);
109
+ console.log(chalk.gray(`Published to DHT (hash: ${dhtHash.slice(0, 12)}...)`));
110
+ dht.destroy();
111
+ } catch (err) {
112
+ console.log(chalk.yellow(`DHT publish skipped: ${err.message}`));
113
+ }
114
+ } else {
115
+ console.log(chalk.red(`\nFailed: ${result.error}`));
116
+ process.exitCode = 1;
117
+ }
118
+ } catch {
119
+ console.log(chalk.red(`\nCould not reach discovery server at ${serverUrl}`));
120
+ process.exitCode = 1;
121
+ }
122
+ });
123
+
124
+ program
125
+ .command('verify <code>')
126
+ .description('complete OTP email verification for handle registration')
127
+ .addHelpText('after', `
128
+ Examples:
129
+ $ pe verify 123456 Complete OTP verification
130
+ `)
131
+ .action(async (code) => {
132
+ const pending = config.get('_pendingRegistration');
133
+ if (!pending?.handle) {
134
+ console.log(chalk.red('No pending registration. Run `pe register <handle> --email <email>` first.'));
135
+ process.exitCode = 1; return;
136
+ }
137
+
138
+ const identity = await getIdentity();
139
+ if (!identity?.privateKey) {
140
+ console.log(chalk.red('No identity found.'));
141
+ process.exitCode = 1; return;
142
+ }
143
+
144
+ const serverUrl = pending.serverUrl || getPrimaryServer();
145
+ const sodium = (await import('sodium-native')).default;
146
+ const privateKey = Buffer.from(identity.privateKey, 'hex');
147
+ const now = Date.now();
148
+ const message = `${pending.handle}:${code}:${now}`;
149
+ const sig = Buffer.alloc(sodium.crypto_sign_BYTES);
150
+ sodium.crypto_sign_detached(sig, Buffer.from(message), privateKey);
151
+
152
+ try {
153
+ process.stdout.write(chalk.gray(`Verifying @${pending.handle}... `));
154
+ const res = await postTo(serverUrl, '/otp/verify', { handle: pending.handle, code, signature: sig.toString('hex'), timestamp: now });
155
+ const result = await res.json();
156
+ if (result.success) {
157
+ console.log(chalk.green('done.'));
158
+ console.log(chalk.green(`Handle @${pending.handle} verified and registered.`));
159
+ setIdentityHandle(pending.handle);
160
+ updateProfileHandle(identity.publicKey, pending.handle);
161
+ config.delete('_pendingRegistration');
162
+ } else {
163
+ console.log(chalk.red(`\nVerification failed: ${result.error}`));
164
+ process.exitCode = 1;
165
+ }
166
+ } catch {
167
+ console.log(chalk.red(`\nCould not reach discovery server at ${serverUrl}`));
168
+ process.exitCode = 1;
169
+ }
170
+ });
171
+ }
@@ -0,0 +1,131 @@
1
+ import chalk from 'chalk';
2
+ import crypto from 'crypto';
3
+
4
+ export default function relayCommand(program) {
5
+ const cmd = program
6
+ .command('relay')
7
+ .description('manage the TURN relay server extension')
8
+ .addHelpText('after', `
9
+ Examples:
10
+ $ pe relay start Start the relay server
11
+ $ pe relay stop Stop the relay server
12
+ $ pe relay status Show relay status
13
+ $ pe relay credentials Generate TURN credentials (default 1h TTL)
14
+ $ pe relay credentials 3600 Generate credentials with custom TTL (seconds)
15
+ `)
16
+ .action(() => {
17
+ cmd.outputHelp();
18
+ });
19
+
20
+ cmd
21
+ .command('start')
22
+ .description('start the TURN relay server')
23
+ .action(async () => {
24
+ try {
25
+ const extConfig = (await import('../utils/config.js')).default;
26
+ const cfg = extConfig.get('ext.relay') || {};
27
+
28
+ if (!cfg.secret) {
29
+ cfg.secret = crypto.randomBytes(32).toString('hex');
30
+ extConfig.set('ext.relay', { ...cfg });
31
+ }
32
+
33
+ const port = cfg.port || 3478;
34
+ const realm = cfg.realm || 'palexplorer';
35
+
36
+ const store = extConfig.get('ext_store.relay') || {};
37
+ store.running = true;
38
+ store.activeAllocations = store.activeAllocations || {};
39
+ extConfig.set('ext_store.relay', store);
40
+
41
+ console.log(chalk.green(`✔ Relay server started`));
42
+ console.log(` Port: ${chalk.white(port)}`);
43
+ console.log(` Realm: ${chalk.white(realm)}`);
44
+ } catch (err) {
45
+ console.log(chalk.red(`Failed to start relay: ${err.message}`));
46
+ process.exitCode = 1;
47
+ }
48
+ });
49
+
50
+ cmd
51
+ .command('stop')
52
+ .description('stop the TURN relay server')
53
+ .action(async () => {
54
+ try {
55
+ const extConfig = (await import('../utils/config.js')).default;
56
+ const store = extConfig.get('ext_store.relay') || {};
57
+ store.running = false;
58
+ store.activeAllocations = {};
59
+ extConfig.set('ext_store.relay', store);
60
+
61
+ console.log(chalk.green(`✔ Relay server stopped`));
62
+ } catch (err) {
63
+ console.log(chalk.red(`Failed to stop relay: ${err.message}`));
64
+ process.exitCode = 1;
65
+ }
66
+ });
67
+
68
+ cmd
69
+ .command('status')
70
+ .description('show relay server status')
71
+ .action(async () => {
72
+ try {
73
+ const extConfig = (await import('../utils/config.js')).default;
74
+ const cfg = extConfig.get('ext.relay') || {};
75
+ const store = extConfig.get('ext_store.relay') || {};
76
+
77
+ const running = store.running || false;
78
+ const port = cfg.port || 3478;
79
+ const realm = cfg.realm || 'palexplorer';
80
+ const maxBandwidth = cfg.maxBandwidth || 'unlimited';
81
+ const allocations = Object.keys(store.activeAllocations || {}).length;
82
+
83
+ console.log('');
84
+ console.log(chalk.cyan.bold('TURN Relay Status'));
85
+ console.log(` Status: ${running ? chalk.green('running') : chalk.red('stopped')}`);
86
+ console.log(` Port: ${chalk.white(port)}`);
87
+ console.log(` Realm: ${chalk.white(realm)}`);
88
+ console.log(` Bandwidth: ${chalk.white(maxBandwidth)}`);
89
+ console.log(` Allocations: ${chalk.white(allocations)}`);
90
+ console.log(` Secret: ${cfg.secret ? chalk.gray(cfg.secret.slice(0, 8) + '...') : chalk.yellow('not set')}`);
91
+ console.log('');
92
+ } catch (err) {
93
+ console.log(chalk.red(`Failed to get status: ${err.message}`));
94
+ process.exitCode = 1;
95
+ }
96
+ });
97
+
98
+ cmd
99
+ .command('credentials [ttl]')
100
+ .description('generate TURN credentials (HMAC-SHA1 based)')
101
+ .action(async (ttl) => {
102
+ try {
103
+ const extConfig = (await import('../utils/config.js')).default;
104
+ const cfg = extConfig.get('ext.relay') || {};
105
+
106
+ if (!cfg.secret) {
107
+ cfg.secret = crypto.randomBytes(32).toString('hex');
108
+ extConfig.set('ext.relay', { ...cfg });
109
+ }
110
+
111
+ const credentialTTL = parseInt(ttl, 10) || cfg.credentialTTL || 3600;
112
+ const timestamp = Math.floor(Date.now() / 1000) + credentialTTL;
113
+ const username = `${timestamp}:palexplorer`;
114
+ const password = crypto
115
+ .createHmac('sha1', cfg.secret)
116
+ .update(username)
117
+ .digest('base64');
118
+
119
+ console.log('');
120
+ console.log(chalk.cyan.bold('TURN Credentials'));
121
+ console.log(` Username: ${chalk.white(username)}`);
122
+ console.log(` Password: ${chalk.white(password)}`);
123
+ console.log(` TTL: ${chalk.white(credentialTTL + 's')}`);
124
+ console.log(` Expires: ${chalk.gray(new Date(timestamp * 1000).toISOString())}`);
125
+ console.log('');
126
+ } catch (err) {
127
+ console.log(chalk.red(`Failed to generate credentials: ${err.message}`));
128
+ process.exitCode = 1;
129
+ }
130
+ });
131
+ }
@@ -0,0 +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 pe 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
+ $ pe remote browse @alice List Alice's shares
105
+ $ pe remote browse @alice --device laptop List shares from specific device
106
+ $ pe remote files @alice "Photos" List files in Alice's "Photos" share
107
+ $ pe remote download @alice "Photos" Download Alice's "Photos" share
108
+ $ pe 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 \`pe 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 \`pe 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 `pe serve` to start seeding and generate magnet links.'));
209
+ console.log(chalk.gray(' Once seeded, retry: pe 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 `pe 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
+ }