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,131 @@
1
+ import chalk from 'chalk';
2
+ import config from '../utils/config.js';
3
+ import { getIdentity } from '../core/identity.js';
4
+
5
+ const DISCOVERY_URL = process.env.PAL_DISCOVERY_URL || config.get('discoveryUrl') || 'http://localhost:3000';
6
+
7
+ export default function apiKeysCommand(program) {
8
+ const cmd = program
9
+ .command('api-keys')
10
+ .description('manage API keys (Pro)')
11
+ .addHelpText('after', `
12
+ Examples:
13
+ $ pe api-keys List your API keys
14
+ $ pe api-keys create "my-bot" Create a new API key
15
+ $ pe api-keys revoke <keyId> Revoke an API key
16
+ `)
17
+ .action(async () => {
18
+ try {
19
+ const identity = await getIdentity();
20
+ if (!identity?.publicKey) {
21
+ console.log(chalk.red('No identity. Run `pe init` first.'));
22
+ process.exitCode = 1;
23
+ return;
24
+ }
25
+
26
+ const res = await fetch(`${DISCOVERY_URL}/api/v1/keys/${encodeURIComponent(identity.publicKey)}`, {
27
+ signal: AbortSignal.timeout(5000),
28
+ });
29
+
30
+ if (!res.ok) {
31
+ console.log(chalk.red('Failed to fetch API keys.'));
32
+ process.exitCode = 1;
33
+ return;
34
+ }
35
+
36
+ const keys = await res.json();
37
+ if (!Array.isArray(keys) || keys.length === 0) {
38
+ console.log(chalk.gray('No API keys. Use `pe api-keys create <name>` to create one.'));
39
+ return;
40
+ }
41
+
42
+ console.log('');
43
+ console.log(chalk.cyan('API Keys:'));
44
+ for (const k of keys) {
45
+ const status = k.revoked ? chalk.red('revoked') : chalk.green('active');
46
+ console.log(` ${chalk.white(k.id)} [${status}] ${chalk.yellow(k.name || 'unnamed')}`);
47
+ console.log(` Created: ${chalk.gray(k.createdAt || 'unknown')}`);
48
+ if (k.lastUsed) console.log(` Last used: ${chalk.gray(k.lastUsed)}`);
49
+ }
50
+ } catch (err) {
51
+ console.log(chalk.red(`Error: ${err.message}`));
52
+ process.exitCode = 1;
53
+ }
54
+ });
55
+
56
+ cmd
57
+ .command('create <name>')
58
+ .description('create a new API key')
59
+ .action(async (name) => {
60
+ try {
61
+ const identity = await getIdentity();
62
+ if (!identity?.publicKey) {
63
+ console.log(chalk.red('No identity. Run `pe init` first.'));
64
+ process.exitCode = 1;
65
+ return;
66
+ }
67
+
68
+ const res = await fetch(`${DISCOVERY_URL}/api/v1/keys`, {
69
+ method: 'POST',
70
+ headers: { 'Content-Type': 'application/json' },
71
+ body: JSON.stringify({ publicKey: identity.publicKey, name }),
72
+ signal: AbortSignal.timeout(10000),
73
+ });
74
+
75
+ if (!res.ok) {
76
+ const err = await res.json().catch(() => ({}));
77
+ console.log(chalk.red(`Failed: ${err.error || res.statusText}`));
78
+ process.exitCode = 1;
79
+ return;
80
+ }
81
+
82
+ const key = await res.json();
83
+ console.log(chalk.green(`✔ API key created: ${key.id}`));
84
+ const apiKey = key.key || key.secret;
85
+ if (apiKey) {
86
+ console.log(chalk.yellow(' Key (save it — shown only once):'));
87
+ console.log(` ${chalk.white(apiKey)}`);
88
+ try {
89
+ const keytar = (await import('keytar')).default;
90
+ await keytar.setPassword('palexplorer', 'apiKey', apiKey);
91
+ } catch {
92
+ config.set('apiKey', apiKey);
93
+ }
94
+ }
95
+ } catch (err) {
96
+ console.log(chalk.red(`Error: ${err.message}`));
97
+ process.exitCode = 1;
98
+ }
99
+ });
100
+
101
+ cmd
102
+ .command('revoke <keyId>')
103
+ .description('revoke an API key')
104
+ .action(async (keyId) => {
105
+ try {
106
+ const identity = await getIdentity();
107
+ if (!identity?.publicKey) {
108
+ console.log(chalk.red('No identity. Run `pe init` first.'));
109
+ process.exitCode = 1;
110
+ return;
111
+ }
112
+
113
+ const res = await fetch(`${DISCOVERY_URL}/api/v1/keys/${encodeURIComponent(identity.publicKey)}/${encodeURIComponent(keyId)}`, {
114
+ method: 'DELETE',
115
+ signal: AbortSignal.timeout(10000),
116
+ });
117
+
118
+ if (!res.ok) {
119
+ const err = await res.json().catch(() => ({}));
120
+ console.log(chalk.red(`Failed: ${err.error || res.statusText}`));
121
+ process.exitCode = 1;
122
+ return;
123
+ }
124
+
125
+ console.log(chalk.green(`✔ API key ${keyId} revoked.`));
126
+ } catch (err) {
127
+ console.log(chalk.red(`Error: ${err.message}`));
128
+ process.exitCode = 1;
129
+ }
130
+ });
131
+ }
@@ -0,0 +1,235 @@
1
+ import chalk from 'chalk';
2
+ import { createHash } from 'crypto';
3
+ import path from 'path';
4
+ import os from 'os';
5
+
6
+ function isPrivateIP(hostname) {
7
+ if (hostname === 'localhost' || hostname === '127.0.0.1' || hostname === '::1') return true;
8
+ const parts = hostname.split('.').map(Number);
9
+ if (parts.length === 4) {
10
+ if (parts[0] === 10) return true;
11
+ if (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) return true;
12
+ if (parts[0] === 192 && parts[1] === 168) return true;
13
+ if (parts[0] === 169 && parts[1] === 254) return true;
14
+ if (parts[0] === 0) return true;
15
+ }
16
+ return false;
17
+ }
18
+
19
+ function validateWebhookUrl(url) {
20
+ let parsed;
21
+ try { parsed = new URL(url); } catch { throw new Error('Invalid URL'); }
22
+ if (parsed.protocol !== 'https:') throw new Error('Webhook URL must use HTTPS');
23
+ if (isPrivateIP(parsed.hostname)) throw new Error('Webhook URL must not point to private/internal addresses');
24
+ return url;
25
+ }
26
+
27
+ function validateOutputPath(filePath) {
28
+ const resolved = path.resolve(filePath);
29
+ const home = os.homedir();
30
+ const cwd = process.cwd();
31
+ if (!resolved.startsWith(home) && !resolved.startsWith(cwd)) {
32
+ throw new Error('Output path must be within home directory or current working directory');
33
+ }
34
+ if (resolved.includes('..')) {
35
+ throw new Error('Path traversal not allowed');
36
+ }
37
+ return resolved;
38
+ }
39
+
40
+ export default function auditCommand(program) {
41
+ const cmd = program
42
+ .command('audit')
43
+ .description('tamper-proof audit logging (Enterprise)')
44
+ .addHelpText('after', `
45
+ Examples:
46
+ $ pe audit export --format json -o audit.json
47
+ $ pe audit export --from 2026-01-01 --to 2026-03-20 --format csv
48
+ $ pe audit verify Verify hash chain integrity
49
+ $ pe audit alerts --webhook https://hooks.example.com/audit
50
+ $ pe audit siem --endpoint https://siem.example.com/ingest
51
+ `)
52
+ .action(() => { cmd.outputHelp(); });
53
+
54
+ cmd
55
+ .command('export')
56
+ .description('export audit log')
57
+ .option('--format <json|csv>', 'output format', 'json')
58
+ .option('--from <date>', 'start date (ISO format)')
59
+ .option('--to <date>', 'end date (ISO format)')
60
+ .option('-o, --output <path>', 'output file path')
61
+ .action(async (opts) => {
62
+ try {
63
+ const extConfig = (await import('../utils/config.js')).default;
64
+ const store = extConfig.get('ext_store.advanced-audit') || {};
65
+ let entries = store.auditLog || [];
66
+
67
+ if (opts.from) {
68
+ const fromDate = new Date(opts.from).getTime();
69
+ entries = entries.filter(e => new Date(e.timestamp).getTime() >= fromDate);
70
+ }
71
+ if (opts.to) {
72
+ const toDate = new Date(opts.to).getTime();
73
+ entries = entries.filter(e => new Date(e.timestamp).getTime() <= toDate);
74
+ }
75
+
76
+ if (!entries.length) {
77
+ console.log(chalk.gray('No audit log entries found.'));
78
+ return;
79
+ }
80
+
81
+ let output;
82
+ if (opts.format === 'csv') {
83
+ const header = 'id,timestamp,action,user,details,hash';
84
+ const rows = entries.map(e =>
85
+ `${e.id},${e.timestamp},${e.action},${e.user},"${JSON.stringify(e.details).replace(/"/g, '""')}",${e.hash}`
86
+ );
87
+ output = [header, ...rows].join('\n');
88
+ } else {
89
+ output = JSON.stringify(entries, null, 2);
90
+ }
91
+
92
+ if (opts.output) {
93
+ const validatedPath = validateOutputPath(opts.output);
94
+ const fs = await import('fs');
95
+ fs.writeFileSync(validatedPath, output, 'utf8');
96
+ console.log(chalk.green(`✔ Exported ${entries.length} entries to ${validatedPath}`));
97
+ } else {
98
+ console.log(output);
99
+ }
100
+ } catch (err) {
101
+ console.error(chalk.red(`Export failed: ${err.message}`));
102
+ process.exitCode = 1;
103
+ }
104
+ });
105
+
106
+ cmd
107
+ .command('verify')
108
+ .description('verify hash chain integrity of audit log')
109
+ .action(async () => {
110
+ try {
111
+ const extConfig = (await import('../utils/config.js')).default;
112
+ const store = extConfig.get('ext_store.advanced-audit') || {};
113
+ const entries = store.auditLog || [];
114
+
115
+ if (!entries.length) {
116
+ console.log(chalk.gray('No audit log entries to verify.'));
117
+ return;
118
+ }
119
+
120
+ let valid = true;
121
+ let checked = 0;
122
+
123
+ for (let i = 0; i < entries.length; i++) {
124
+ const entry = entries[i];
125
+ const previousHash = entry.previousHash || '';
126
+ const data = `${entry.timestamp}${entry.action}${entry.user}${JSON.stringify(entry.details)}${previousHash}`;
127
+ const expectedHash = createHash('sha256').update(data).digest('hex');
128
+
129
+ if (entry.hash !== expectedHash) {
130
+ console.log(chalk.red(`✘ Entry ${i} (${entry.id}) hash mismatch`));
131
+ console.log(chalk.red(` Expected: ${expectedHash}`));
132
+ console.log(chalk.red(` Got: ${entry.hash}`));
133
+ valid = false;
134
+ }
135
+
136
+ if (i > 0 && entry.previousHash !== entries[i - 1].hash) {
137
+ console.log(chalk.red(`✘ Entry ${i} (${entry.id}) chain broken — previousHash does not match`));
138
+ valid = false;
139
+ }
140
+
141
+ checked++;
142
+ }
143
+
144
+ if (valid) {
145
+ console.log(chalk.green(`✔ Audit log integrity verified (${checked} entries)`));
146
+ } else {
147
+ console.log(chalk.red(`✘ Audit log integrity check FAILED`));
148
+ process.exitCode = 1;
149
+ }
150
+ } catch (err) {
151
+ console.error(chalk.red(`Verify failed: ${err.message}`));
152
+ process.exitCode = 1;
153
+ }
154
+ });
155
+
156
+ cmd
157
+ .command('alerts')
158
+ .description('configure real-time alerts')
159
+ .option('--webhook <url>', 'set alert webhook URL')
160
+ .option('--disable', 'disable alerts')
161
+ .action(async (opts) => {
162
+ try {
163
+ const extConfig = (await import('../utils/config.js')).default;
164
+ const config = extConfig.get('ext.advanced-audit') || {};
165
+
166
+ if (opts.disable) {
167
+ config.realTimeAlerts = false;
168
+ config.alertWebhook = null;
169
+ extConfig.set('ext.advanced-audit', config);
170
+ console.log(chalk.green('✔ Real-time alerts disabled'));
171
+ return;
172
+ }
173
+
174
+ if (opts.webhook) {
175
+ validateWebhookUrl(opts.webhook);
176
+ config.realTimeAlerts = true;
177
+ config.alertWebhook = opts.webhook;
178
+ extConfig.set('ext.advanced-audit', config);
179
+ console.log(chalk.green('✔ Real-time alerts enabled'));
180
+ console.log(` Webhook: ${chalk.white(opts.webhook)}`);
181
+ return;
182
+ }
183
+
184
+ console.log('');
185
+ console.log(chalk.cyan.bold('Alert Configuration'));
186
+ console.log(chalk.gray('─'.repeat(40)));
187
+ console.log(` Enabled: ${config.realTimeAlerts ? chalk.green('yes') : chalk.red('no')}`);
188
+ console.log(` Webhook: ${config.alertWebhook ? chalk.white(config.alertWebhook) : chalk.gray('not set')}`);
189
+ console.log('');
190
+ } catch (err) {
191
+ console.error(chalk.red(`Alerts failed: ${err.message}`));
192
+ process.exitCode = 1;
193
+ }
194
+ });
195
+
196
+ cmd
197
+ .command('siem')
198
+ .description('configure SIEM forwarding')
199
+ .option('--endpoint <url>', 'set SIEM endpoint')
200
+ .option('--disable', 'disable SIEM forwarding')
201
+ .action(async (opts) => {
202
+ try {
203
+ const extConfig = (await import('../utils/config.js')).default;
204
+ const config = extConfig.get('ext.advanced-audit') || {};
205
+
206
+ if (opts.disable) {
207
+ config.siem = false;
208
+ config.siemEndpoint = null;
209
+ extConfig.set('ext.advanced-audit', config);
210
+ console.log(chalk.green('✔ SIEM forwarding disabled'));
211
+ return;
212
+ }
213
+
214
+ if (opts.endpoint) {
215
+ validateWebhookUrl(opts.endpoint);
216
+ config.siem = true;
217
+ config.siemEndpoint = opts.endpoint;
218
+ extConfig.set('ext.advanced-audit', config);
219
+ console.log(chalk.green('✔ SIEM forwarding enabled'));
220
+ console.log(` Endpoint: ${chalk.white(opts.endpoint)}`);
221
+ return;
222
+ }
223
+
224
+ console.log('');
225
+ console.log(chalk.cyan.bold('SIEM Configuration'));
226
+ console.log(chalk.gray('─'.repeat(40)));
227
+ console.log(` Enabled: ${config.siem ? chalk.green('yes') : chalk.red('no')}`);
228
+ console.log(` Endpoint: ${config.siemEndpoint ? chalk.white(config.siemEndpoint) : chalk.gray('not set')}`);
229
+ console.log('');
230
+ } catch (err) {
231
+ console.error(chalk.red(`SIEM failed: ${err.message}`));
232
+ process.exitCode = 1;
233
+ }
234
+ });
235
+ }
@@ -0,0 +1,137 @@
1
+ import chalk from 'chalk';
2
+ import readline from 'readline';
3
+ import { getExtension, loadExtension, deployBundledExtensions, BUNDLED_DIR } from '../core/extensions.js';
4
+ import path from 'path';
5
+ import fs from 'fs';
6
+
7
+ export default function authCommand(program) {
8
+ const auth = program
9
+ .command('auth')
10
+ .description('manage account linking and identity verification');
11
+
12
+ auth
13
+ .command('login [email]')
14
+ .description('login with email OTP to link your account')
15
+ .action(async (email) => {
16
+ // 1. Ensure extension is loaded
17
+ let ext = getExtension('@palexplorer/auth-email');
18
+ if (!ext) {
19
+ console.log(chalk.blue('Loading auth-email extension...'));
20
+ deployBundledExtensions();
21
+ const appDir = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Z]:)/, '$1'));
22
+ const extPath = path.resolve(appDir, '../../extensions/@palexplorer/auth-email');
23
+ const manifest = JSON.parse(fs.readFileSync(path.join(extPath, 'extension.json'), 'utf8'));
24
+ await loadExtension(extPath, { ...manifest, bundled: true });
25
+ ext = getExtension('@palexplorer/auth-email');
26
+ }
27
+
28
+ if (!ext || !ext.ctx?.auth) {
29
+ console.log(chalk.red('Error: auth-email extension not available.'));
30
+ process.exitCode = 1;
31
+ return;
32
+ }
33
+
34
+ const authExt = ext.ctx.auth;
35
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
36
+ const ask = (q) => new Promise(r => rl.question(q, r));
37
+
38
+ try {
39
+ let targetEmail = email;
40
+ if (!targetEmail) {
41
+ targetEmail = await ask('Enter your email address: ');
42
+ }
43
+
44
+ if (!targetEmail || !targetEmail.includes('@')) {
45
+ console.log(chalk.red('Invalid email address.'));
46
+ rl.close();
47
+ process.exitCode = 1;
48
+ return;
49
+ }
50
+
51
+ console.log(chalk.blue(`Sending verification code to ${targetEmail}...`));
52
+ await authExt.requestVerificationCode(targetEmail);
53
+ console.log(chalk.green('✔ Code sent! Please check your inbox.'));
54
+
55
+ const code = await ask('Enter the 6-digit code: ');
56
+ if (!code || code.length !== 6) {
57
+ console.log(chalk.red('Invalid code format.'));
58
+ rl.close();
59
+ process.exitCode = 1;
60
+ return;
61
+ }
62
+
63
+ console.log(chalk.blue('Verifying...'));
64
+ await authExt.confirmCode(targetEmail, code);
65
+ console.log(chalk.green(`✔ Email ${targetEmail} verified successfully!`));
66
+
67
+ console.log(chalk.blue('Linking your Palexplorer identity...'));
68
+ await authExt.linkIdentity();
69
+ console.log(chalk.green('✔ Identity linked! You now have a verified badge.'));
70
+
71
+ rl.close();
72
+ } catch (err) {
73
+ rl.close();
74
+ console.log(chalk.red(`Login failed: ${err.message}`));
75
+ process.exitCode = 1;
76
+ }
77
+ });
78
+
79
+ auth
80
+ .command('google')
81
+ .description('link your account using Google')
82
+ .action(async () => {
83
+ const { getIdentity } = await import('../core/identity.js');
84
+ const { getPrimaryServer } = await import('../core/discoveryClient.js');
85
+ const identity = await getIdentity();
86
+ if (!identity?.publicKey) {
87
+ console.log(chalk.red('No identity found. Run `pe init` first.'));
88
+ process.exitCode = 1; return;
89
+ }
90
+ const baseUrl = getPrimaryServer();
91
+ const returnUrl = encodeURIComponent('palexplorer://auth-callback');
92
+ const url = `${baseUrl}/auth/google?publicKey=${encodeURIComponent(identity.publicKey)}&return_url=${returnUrl}`;
93
+ console.log(chalk.blue('Opening browser for Google login...'));
94
+ console.log(chalk.gray(`URL: ${url}`));
95
+ const { default: open } = await import('open');
96
+ await open(url);
97
+ });
98
+
99
+ auth
100
+ .command('github')
101
+ .description('link your account using GitHub')
102
+ .action(async () => {
103
+ const { getIdentity } = await import('../core/identity.js');
104
+ const { getPrimaryServer } = await import('../core/discoveryClient.js');
105
+ const identity = await getIdentity();
106
+ if (!identity?.publicKey) {
107
+ console.log(chalk.red('No identity found. Run `pe init` first.'));
108
+ process.exitCode = 1; return;
109
+ }
110
+ const baseUrl = getPrimaryServer();
111
+ const returnUrl = encodeURIComponent('palexplorer://auth-callback');
112
+ const url = `${baseUrl}/auth/github?publicKey=${encodeURIComponent(identity.publicKey)}&return_url=${returnUrl}`;
113
+ console.log(chalk.blue('Opening browser for GitHub login...'));
114
+ console.log(chalk.gray(`URL: ${url}`));
115
+ const { default: open } = await import('open');
116
+ await open(url);
117
+ });
118
+
119
+ auth
120
+ .command('status')
121
+ .description('show current authentication status')
122
+ .action(async () => {
123
+ let ext = getExtension('@palexplorer/auth-email');
124
+ if (!ext || !ext.ctx?.config) {
125
+ console.log(chalk.gray('Authentication extension not loaded.'));
126
+ return;
127
+ }
128
+
129
+ const verifiedEmail = ext.ctx.config.get('verifiedEmail');
130
+ if (verifiedEmail) {
131
+ console.log(chalk.green(`✔ Verified: ${verifiedEmail}`));
132
+ console.log(chalk.gray(` Token: ${ext.ctx.config.get('verifiedToken').slice(0, 32)}...`));
133
+ } else {
134
+ console.log(chalk.yellow('! Not verified. Run `pe auth login` to link an email.'));
135
+ }
136
+ });
137
+ }
@@ -0,0 +1,76 @@
1
+ import chalk from 'chalk';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import { createBackup, restoreBackup } from '../core/backup.js';
5
+
6
+ export default function backupCommand(program) {
7
+ const cmd = program
8
+ .command('backup')
9
+ .description('create and restore encrypted backups (Pro)')
10
+ .addHelpText('after', `
11
+ Examples:
12
+ $ pe backup create Create backup (prompted for password)
13
+ $ pe backup create --password "s3cret" Create with password
14
+ $ pe backup create -o ~/backup.json Custom output path
15
+ $ pe backup restore ./palexplorer-backup.json
16
+ `)
17
+ .action(() => {
18
+ cmd.outputHelp();
19
+ });
20
+
21
+ cmd
22
+ .command('create')
23
+ .description('create an encrypted backup of identity, shares, pals, and settings')
24
+ .option('-o, --output <path>', 'Output file path')
25
+ .option('-p, --password <password>', 'Encryption password')
26
+ .action(async (opts) => {
27
+ try {
28
+ const password = opts.password || process.env.PE_BACKUP_PASSWORD;
29
+ if (!password) {
30
+ console.log(chalk.red('Password required. Use --password or PE_BACKUP_PASSWORD env var.'));
31
+ process.exitCode = 1;
32
+ return;
33
+ }
34
+
35
+ const backupData = await createBackup(password);
36
+ const outPath = opts.output || `palexplorer-backup-${new Date().toISOString().slice(0, 10)}.json`;
37
+ const resolved = path.resolve(outPath);
38
+ fs.writeFileSync(resolved, backupData, 'utf8');
39
+ console.log(chalk.green(`✔ Backup created: ${resolved}`));
40
+ } catch (err) {
41
+ console.log(chalk.red(`Backup failed: ${err.message}`));
42
+ process.exitCode = 1;
43
+ }
44
+ });
45
+
46
+ cmd
47
+ .command('restore <file>')
48
+ .description('restore from an encrypted backup file')
49
+ .option('-p, --password <password>', 'Decryption password')
50
+ .action(async (file, opts) => {
51
+ try {
52
+ const resolved = path.resolve(file);
53
+ if (!fs.existsSync(resolved)) {
54
+ console.log(chalk.red(`File not found: ${resolved}`));
55
+ process.exitCode = 1;
56
+ return;
57
+ }
58
+
59
+ const password = opts.password || process.env.PE_BACKUP_PASSWORD;
60
+ if (!password) {
61
+ console.log(chalk.red('Password required. Use --password or PE_BACKUP_PASSWORD env var.'));
62
+ process.exitCode = 1;
63
+ return;
64
+ }
65
+
66
+ const data = fs.readFileSync(resolved, 'utf8');
67
+ const result = await restoreBackup(data, password);
68
+ console.log(chalk.green('✔ Backup restored successfully.'));
69
+ if (result.sharesRestored) console.log(` Shares: ${chalk.white(result.sharesRestored)}`);
70
+ if (result.palsRestored) console.log(` Pals: ${chalk.white(result.palsRestored)}`);
71
+ } catch (err) {
72
+ console.log(chalk.red(`Restore failed: ${err.message}`));
73
+ process.exitCode = 1;
74
+ }
75
+ });
76
+ }