peerpigeon 0.0.8 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/examples/browser/index.html +16 -16
- package/examples/debug-configuration.js +171 -0
- package/examples/node/storage-spaces-test.js +329 -0
- package/index.js +3 -0
- package/package.json +13 -8
- package/server/index.js +12 -12
- package/src/CleanupManager.js +20 -18
- package/src/ConnectionManager.js +407 -102
- package/src/CryptoManager.js +28 -26
- package/src/DebugLogger.js +211 -0
- package/src/DistributedStorageManager.js +469 -102
- package/src/EnvironmentDetector.js +3 -1
- package/src/EventEmitter.js +1 -0
- package/src/EvictionManager.js +26 -24
- package/src/GossipManager.js +69 -33
- package/src/MediaManager.js +12 -10
- package/src/MeshOptimizer.js +30 -28
- package/src/PeerConnection.js +621 -159
- package/src/PeerDiscovery.js +19 -17
- package/src/PeerPigeonMesh.js +241 -33
- package/src/SignalingClient.js +59 -44
- package/src/SignalingHandler.js +280 -51
- package/src/StorageManager.js +7 -5
- package/src/TimerUtils.js +1 -0
- package/src/WebDHT.js +125 -123
- package/websocket-server/server.js +31 -103
package/src/CryptoManager.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { EventEmitter } from './EventEmitter.js';
|
|
2
|
+
import DebugLogger from './DebugLogger.js';
|
|
2
3
|
|
|
3
4
|
// Dynamic import for unsea to handle both Node.js and browser environments
|
|
4
5
|
let unsea = null;
|
|
@@ -49,6 +50,7 @@ async function initializeUnsea() {
|
|
|
49
50
|
export class CryptoManager extends EventEmitter {
|
|
50
51
|
constructor() {
|
|
51
52
|
super();
|
|
53
|
+
this.debug = DebugLogger.create('CryptoManager');
|
|
52
54
|
this.unsea = null;
|
|
53
55
|
this.keypair = null;
|
|
54
56
|
this.peerKeys = new Map(); // Store peer public keys
|
|
@@ -99,7 +101,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
99
101
|
|
|
100
102
|
return this.keypair;
|
|
101
103
|
} catch (error) {
|
|
102
|
-
|
|
104
|
+
this.debug.error('CryptoManager initialization failed:', error);
|
|
103
105
|
this.emit('cryptoError', { error: error.message });
|
|
104
106
|
throw error;
|
|
105
107
|
}
|
|
@@ -128,7 +130,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
128
130
|
}
|
|
129
131
|
} catch (error) {
|
|
130
132
|
// Fallback to generating ephemeral keys
|
|
131
|
-
|
|
133
|
+
this.debug.warn('Failed to use persistent storage, generating ephemeral keys:', error);
|
|
132
134
|
this.keypair = await this.unsea.generateRandomPair();
|
|
133
135
|
}
|
|
134
136
|
} else {
|
|
@@ -138,7 +140,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
138
140
|
|
|
139
141
|
this.emit('userAuthenticated', { alias, publicKey: this.getPublicKey() });
|
|
140
142
|
} catch (error) {
|
|
141
|
-
|
|
143
|
+
this.debug.error('User authentication failed:', error);
|
|
142
144
|
throw error;
|
|
143
145
|
}
|
|
144
146
|
}
|
|
@@ -260,7 +262,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
260
262
|
|
|
261
263
|
return result;
|
|
262
264
|
} catch (error) {
|
|
263
|
-
|
|
265
|
+
this.debug.error('Peer encryption failed:', error);
|
|
264
266
|
throw error;
|
|
265
267
|
}
|
|
266
268
|
}
|
|
@@ -296,7 +298,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
296
298
|
|
|
297
299
|
return parsed;
|
|
298
300
|
} catch (error) {
|
|
299
|
-
|
|
301
|
+
this.debug.error('Peer decryption failed:', error);
|
|
300
302
|
throw error;
|
|
301
303
|
}
|
|
302
304
|
}
|
|
@@ -313,7 +315,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
313
315
|
const serialized = typeof data === 'string' ? data : JSON.stringify(data);
|
|
314
316
|
return await this.unsea.signMessage(serialized, this.keypair.priv);
|
|
315
317
|
} catch (error) {
|
|
316
|
-
|
|
318
|
+
this.debug.error('Signing failed:', error);
|
|
317
319
|
throw error;
|
|
318
320
|
}
|
|
319
321
|
}
|
|
@@ -332,7 +334,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
332
334
|
const serialized = typeof data === 'string' ? data : JSON.stringify(data);
|
|
333
335
|
return await this.unsea.verifyMessage(serialized, signature, publicKey);
|
|
334
336
|
} catch (error) {
|
|
335
|
-
|
|
337
|
+
this.debug.error('Signature verification failed:', error);
|
|
336
338
|
return false;
|
|
337
339
|
}
|
|
338
340
|
}
|
|
@@ -355,7 +357,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
355
357
|
this.emit('groupKeyGenerated', { groupId, publicKey: groupKey.pub });
|
|
356
358
|
return groupKeyWithMeta;
|
|
357
359
|
} catch (error) {
|
|
358
|
-
|
|
360
|
+
this.debug.error('Group key generation failed:', error);
|
|
359
361
|
throw error;
|
|
360
362
|
}
|
|
361
363
|
}
|
|
@@ -407,7 +409,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
407
409
|
|
|
408
410
|
return result;
|
|
409
411
|
} catch (error) {
|
|
410
|
-
|
|
412
|
+
this.debug.error('Group encryption failed:', error);
|
|
411
413
|
throw error;
|
|
412
414
|
}
|
|
413
415
|
}
|
|
@@ -447,7 +449,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
447
449
|
|
|
448
450
|
return parsed;
|
|
449
451
|
} catch (error) {
|
|
450
|
-
|
|
452
|
+
this.debug.error('Group decryption failed:', error);
|
|
451
453
|
throw error;
|
|
452
454
|
}
|
|
453
455
|
}
|
|
@@ -471,7 +473,7 @@ export class CryptoManager extends EventEmitter {
|
|
|
471
473
|
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
472
474
|
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('').substring(0, 16);
|
|
473
475
|
} catch (error) {
|
|
474
|
-
|
|
476
|
+
this.debug.warn('Could not use crypto.subtle for nonce generation:', error);
|
|
475
477
|
}
|
|
476
478
|
}
|
|
477
479
|
|
|
@@ -556,18 +558,18 @@ export class CryptoManager extends EventEmitter {
|
|
|
556
558
|
return results;
|
|
557
559
|
}
|
|
558
560
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
+
this.debug.log('🔍 Debug: unsea object:', this.unsea);
|
|
562
|
+
this.debug.log('🔍 Debug: available methods:', Object.keys(this.unsea));
|
|
561
563
|
|
|
562
564
|
try {
|
|
563
565
|
// Test keypair generation
|
|
564
|
-
|
|
566
|
+
this.debug.log('🧪 Testing keypair generation...');
|
|
565
567
|
const testKeypair = await this.unsea.generateRandomPair();
|
|
566
|
-
|
|
568
|
+
this.debug.log('🔍 Generated keypair:', testKeypair);
|
|
567
569
|
results.keypairGeneration = !!(testKeypair && (testKeypair.pub || testKeypair.publicKey));
|
|
568
570
|
|
|
569
571
|
// Test encryption/decryption - create two keypairs to simulate peer-to-peer encryption
|
|
570
|
-
|
|
572
|
+
this.debug.log('🧪 Testing encryption...');
|
|
571
573
|
const testMessage = 'Hello, crypto world!';
|
|
572
574
|
|
|
573
575
|
// Create a second keypair to simulate a peer
|
|
@@ -575,38 +577,38 @@ export class CryptoManager extends EventEmitter {
|
|
|
575
577
|
|
|
576
578
|
// Encrypt from our keypair TO the peer keypair
|
|
577
579
|
const encrypted = await this.unsea.encryptMessageWithMeta(testMessage, peerKeypair);
|
|
578
|
-
|
|
580
|
+
this.debug.log('🔍 Encrypted result:', encrypted);
|
|
579
581
|
results.encryption = !!encrypted;
|
|
580
582
|
|
|
581
|
-
|
|
583
|
+
this.debug.log('🧪 Testing decryption...');
|
|
582
584
|
// Use the ephemeral private key (epriv) for decryption as shown in the example
|
|
583
585
|
const decrypted = await this.unsea.decryptMessageWithMeta(encrypted, peerKeypair.epriv);
|
|
584
|
-
|
|
586
|
+
this.debug.log('🔍 Decrypted result:', decrypted);
|
|
585
587
|
results.decryption = decrypted === testMessage;
|
|
586
588
|
|
|
587
589
|
// Test signing/verification
|
|
588
|
-
|
|
590
|
+
this.debug.log('🧪 Testing signing...');
|
|
589
591
|
const signature = await this.unsea.signMessage(testMessage, this.keypair.priv);
|
|
590
|
-
|
|
592
|
+
this.debug.log('🔍 Signature:', signature);
|
|
591
593
|
results.signing = !!signature;
|
|
592
594
|
|
|
593
|
-
|
|
595
|
+
this.debug.log('🧪 Testing verification...');
|
|
594
596
|
const verified = await this.unsea.verifyMessage(testMessage, signature, this.keypair.pub);
|
|
595
|
-
|
|
597
|
+
this.debug.log('🔍 Verification result:', verified);
|
|
596
598
|
results.verification = verified === true;
|
|
597
599
|
|
|
598
600
|
// Test group encryption (same as regular encryption with different key)
|
|
599
|
-
|
|
601
|
+
this.debug.log('🧪 Testing group encryption...');
|
|
600
602
|
const groupKey = await this.unsea.generateRandomPair();
|
|
601
603
|
const groupEncrypted = await this.unsea.encryptMessageWithMeta(testMessage, groupKey);
|
|
602
604
|
const groupDecrypted = await this.unsea.decryptMessageWithMeta(groupEncrypted, groupKey.epriv);
|
|
603
605
|
results.groupEncryption = groupDecrypted === testMessage;
|
|
604
606
|
} catch (error) {
|
|
605
|
-
|
|
607
|
+
this.debug.error('🔍 Self-test error:', error);
|
|
606
608
|
results.errors.push(error.message);
|
|
607
609
|
}
|
|
608
610
|
|
|
609
|
-
|
|
611
|
+
this.debug.log('🔍 Final test results:', results);
|
|
610
612
|
return results;
|
|
611
613
|
}
|
|
612
614
|
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Debug Logger - Configurable debugging system for PeerPigeon
|
|
3
|
+
*
|
|
4
|
+
* Provides centralized control over console logging with module-specific enable/disable
|
|
5
|
+
* functionality. Default is disabled to prevent console noise in production.
|
|
6
|
+
*
|
|
7
|
+
* Usage:
|
|
8
|
+
* const debug = DebugLogger.create('ModuleName');
|
|
9
|
+
* debug.log('This will only show if enabled');
|
|
10
|
+
* debug.warn('Warnings can be controlled separately');
|
|
11
|
+
* debug.error('Errors can be controlled separately');
|
|
12
|
+
*
|
|
13
|
+
* Configuration:
|
|
14
|
+
* DebugLogger.enable('ModuleName'); // Enable specific module
|
|
15
|
+
* DebugLogger.enableAll(); // Enable all modules
|
|
16
|
+
* DebugLogger.disable('ModuleName'); // Disable specific module
|
|
17
|
+
* DebugLogger.disableAll(); // Disable all modules (default)
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
class DebugLogger {
|
|
21
|
+
static moduleStates = new Map();
|
|
22
|
+
static globalEnabled = false;
|
|
23
|
+
static defaultEnabled = false; // Default is disabled
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Create a debug logger for a specific module
|
|
27
|
+
* @param {string} moduleName - Name of the module (e.g., 'GossipManager', 'PeerConnection')
|
|
28
|
+
* @returns {Object} Debug logger with log/warn/error methods
|
|
29
|
+
*/
|
|
30
|
+
static create(moduleName) {
|
|
31
|
+
// Initialize module state if not exists
|
|
32
|
+
if (!this.moduleStates.has(moduleName)) {
|
|
33
|
+
this.moduleStates.set(moduleName, this.defaultEnabled);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
/**
|
|
38
|
+
* Debug log - for general debugging information
|
|
39
|
+
* @param {...any} args - Arguments to log
|
|
40
|
+
*/
|
|
41
|
+
log: (...args) => {
|
|
42
|
+
if (this.isEnabled(moduleName)) {
|
|
43
|
+
console.log(`[${moduleName}]`, ...args);
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Debug warn - for warnings
|
|
49
|
+
* @param {...any} args - Arguments to log
|
|
50
|
+
*/
|
|
51
|
+
warn: (...args) => {
|
|
52
|
+
if (this.isEnabled(moduleName)) {
|
|
53
|
+
console.warn(`[${moduleName}]`, ...args);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Debug error - for errors
|
|
59
|
+
* @param {...any} args - Arguments to log
|
|
60
|
+
*/
|
|
61
|
+
error: (...args) => {
|
|
62
|
+
if (this.isEnabled(moduleName)) {
|
|
63
|
+
console.error(`[${moduleName}]`, ...args);
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Debug info - alias for log
|
|
69
|
+
* @param {...any} args - Arguments to log
|
|
70
|
+
*/
|
|
71
|
+
info: (...args) => {
|
|
72
|
+
if (this.isEnabled(moduleName)) {
|
|
73
|
+
console.info(`[${moduleName}]`, ...args);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Debug debug - alias for log
|
|
79
|
+
* @param {...any} args - Arguments to log
|
|
80
|
+
*/
|
|
81
|
+
debug: (...args) => {
|
|
82
|
+
if (this.isEnabled(moduleName)) {
|
|
83
|
+
console.debug(`[${moduleName}]`, ...args);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Check if debugging is enabled for a module
|
|
91
|
+
* @param {string} moduleName - Module name to check
|
|
92
|
+
* @returns {boolean} True if enabled
|
|
93
|
+
*/
|
|
94
|
+
static isEnabled(moduleName) {
|
|
95
|
+
if (this.globalEnabled) return true;
|
|
96
|
+
return this.moduleStates.get(moduleName) || false;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Enable debugging for a specific module
|
|
101
|
+
* @param {string} moduleName - Module name to enable
|
|
102
|
+
*/
|
|
103
|
+
static enable(moduleName) {
|
|
104
|
+
this.moduleStates.set(moduleName, true);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Disable debugging for a specific module
|
|
109
|
+
* @param {string} moduleName - Module name to disable
|
|
110
|
+
*/
|
|
111
|
+
static disable(moduleName) {
|
|
112
|
+
this.moduleStates.set(moduleName, false);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Enable debugging for all modules
|
|
117
|
+
*/
|
|
118
|
+
static enableAll() {
|
|
119
|
+
this.globalEnabled = true;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Disable debugging for all modules
|
|
124
|
+
*/
|
|
125
|
+
static disableAll() {
|
|
126
|
+
this.globalEnabled = false;
|
|
127
|
+
// Reset all module states to false
|
|
128
|
+
for (const [moduleName] of this.moduleStates) {
|
|
129
|
+
this.moduleStates.set(moduleName, false);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Enable debugging for multiple modules
|
|
135
|
+
* @param {string[]} moduleNames - Array of module names to enable
|
|
136
|
+
*/
|
|
137
|
+
static enableModules(moduleNames) {
|
|
138
|
+
for (const moduleName of moduleNames) {
|
|
139
|
+
this.enable(moduleName);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Disable debugging for multiple modules
|
|
145
|
+
* @param {string[]} moduleNames - Array of module names to disable
|
|
146
|
+
*/
|
|
147
|
+
static disableModules(moduleNames) {
|
|
148
|
+
for (const moduleName of moduleNames) {
|
|
149
|
+
this.disable(moduleName);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Get the current state of all modules
|
|
155
|
+
* @returns {Object} Object with module names as keys and enabled state as values
|
|
156
|
+
*/
|
|
157
|
+
static getState() {
|
|
158
|
+
const state = {};
|
|
159
|
+
for (const [moduleName, enabled] of this.moduleStates) {
|
|
160
|
+
state[moduleName] = enabled;
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
globalEnabled: this.globalEnabled,
|
|
164
|
+
modules: state
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Configure debugging from an options object
|
|
170
|
+
* @param {Object} options - Configuration options
|
|
171
|
+
* @param {boolean} [options.enableAll] - Enable all modules
|
|
172
|
+
* @param {boolean} [options.disableAll] - Disable all modules
|
|
173
|
+
* @param {string[]} [options.enable] - Array of module names to enable
|
|
174
|
+
* @param {string[]} [options.disable] - Array of module names to disable
|
|
175
|
+
*/
|
|
176
|
+
static configure(options = {}) {
|
|
177
|
+
if (options.disableAll) {
|
|
178
|
+
this.disableAll();
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (options.enableAll) {
|
|
182
|
+
this.enableAll();
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
if (options.enable && Array.isArray(options.enable)) {
|
|
186
|
+
this.enableModules(options.enable);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
if (options.disable && Array.isArray(options.disable)) {
|
|
190
|
+
this.disableModules(options.disable);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Get list of all known modules
|
|
196
|
+
* @returns {string[]} Array of module names
|
|
197
|
+
*/
|
|
198
|
+
static getModules() {
|
|
199
|
+
return Array.from(this.moduleStates.keys()).sort();
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Export for Node.js and browser environments
|
|
204
|
+
if (typeof module !== 'undefined' && module.exports) {
|
|
205
|
+
module.exports = DebugLogger;
|
|
206
|
+
} else if (typeof window !== 'undefined') {
|
|
207
|
+
window.DebugLogger = DebugLogger;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// ES6 export
|
|
211
|
+
export default DebugLogger;
|