peerpigeon 0.0.5 → 0.0.7
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 +190 -12
- package/examples/browser/app.js +40 -40
- package/examples/browser/crypto-test.html +427 -0
- package/examples/browser/index.html +161 -0
- package/examples/browser/styles.css +216 -0
- package/examples/browser/ui-old.js +1704 -1708
- package/examples/browser/ui.js +2226 -1692
- package/examples/nativescript/README.md +73 -0
- package/examples/nativescript/app.js +152 -0
- package/examples/nativescript/package.json +27 -0
- package/examples/node/test-crypto-media.js +137 -0
- package/examples/node/test-crypto.js +146 -0
- package/examples/node/test-node.js +114 -115
- package/package.json +16 -13
- package/src/CleanupManager.js +154 -154
- package/src/ConnectionManager.js +772 -753
- package/src/CryptoManager.js +612 -0
- package/src/EnvironmentDetector.js +457 -384
- package/src/EventEmitter.js +19 -19
- package/src/EvictionManager.js +211 -211
- package/src/GossipManager.js +447 -366
- package/src/MediaManager.js +216 -214
- package/src/MeshOptimizer.js +165 -166
- package/src/PeerConnection.js +847 -941
- package/src/PeerDiscovery.js +255 -259
- package/src/PeerPigeonMesh.js +1100 -721
- package/src/SignalingClient.js +517 -518
- package/src/SignalingHandler.js +295 -295
- package/src/StorageManager.js +118 -104
- package/src/TimerUtils.js +29 -29
- package/src/WebDHT.js +1123 -948
- package/websocket-server/server.js +360 -362
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# NativeScript Example
|
|
2
|
+
|
|
3
|
+
This example demonstrates how to use PeerPigeon in a NativeScript application.
|
|
4
|
+
|
|
5
|
+
## Prerequisites
|
|
6
|
+
|
|
7
|
+
- NativeScript CLI installed (`npm install -g @nativescript/cli`)
|
|
8
|
+
- NativeScript development environment set up for your target platform (iOS/Android)
|
|
9
|
+
- WebRTC plugin for NativeScript (if using media features)
|
|
10
|
+
|
|
11
|
+
## Setup
|
|
12
|
+
|
|
13
|
+
1. Create a new NativeScript project:
|
|
14
|
+
```bash
|
|
15
|
+
ns create MyPeerPigeonApp --js
|
|
16
|
+
cd MyPeerPigeonApp
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
2. Install PeerPigeon:
|
|
20
|
+
```bash
|
|
21
|
+
npm install peerpigeon
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
3. For WebRTC support, install a WebRTC plugin:
|
|
25
|
+
```bash
|
|
26
|
+
# Example plugin (check for latest NativeScript WebRTC plugins)
|
|
27
|
+
ns plugin add @nativescript/webrtc
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
4. Copy the example code from `app.js` to your project's main file.
|
|
31
|
+
|
|
32
|
+
## Features Supported
|
|
33
|
+
|
|
34
|
+
- ✅ Environment detection
|
|
35
|
+
- ✅ WebSocket signaling (with appropriate plugin)
|
|
36
|
+
- ✅ Basic mesh networking
|
|
37
|
+
- ✅ Storage management (using NativeScript's application-settings)
|
|
38
|
+
- ⚠️ WebRTC (requires additional plugin)
|
|
39
|
+
- ⚠️ Media features (requires camera/microphone permissions)
|
|
40
|
+
|
|
41
|
+
## Platform Considerations
|
|
42
|
+
|
|
43
|
+
### Android
|
|
44
|
+
- Requires network permissions in `app/App_Resources/Android/src/main/AndroidManifest.xml`:
|
|
45
|
+
```xml
|
|
46
|
+
<uses-permission android:name="android.permission.INTERNET" />
|
|
47
|
+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### iOS
|
|
51
|
+
- May require camera/microphone permissions if using media features
|
|
52
|
+
- Add appropriate usage descriptions in `app/App_Resources/iOS/Info.plist`
|
|
53
|
+
|
|
54
|
+
## Running the Example
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
# Run on iOS simulator
|
|
58
|
+
ns run ios
|
|
59
|
+
|
|
60
|
+
# Run on Android emulator
|
|
61
|
+
ns run android
|
|
62
|
+
|
|
63
|
+
# Run on physical device
|
|
64
|
+
ns run ios --device
|
|
65
|
+
ns run android --device
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Notes
|
|
69
|
+
|
|
70
|
+
- This example uses basic mesh networking without WebRTC
|
|
71
|
+
- For full WebRTC support, additional native plugins are required
|
|
72
|
+
- Storage uses NativeScript's application-settings module
|
|
73
|
+
- Network connectivity is automatically detected
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { Application } from '@nativescript/core';
|
|
2
|
+
import { PeerPigeonMesh } from 'peerpigeon';
|
|
3
|
+
|
|
4
|
+
// NativeScript PeerPigeon Example
|
|
5
|
+
console.log('🔮 Starting NativeScript PeerPigeon Example');
|
|
6
|
+
|
|
7
|
+
let mesh;
|
|
8
|
+
let peerId;
|
|
9
|
+
|
|
10
|
+
// Initialize PeerPigeon when app launches
|
|
11
|
+
Application.on(Application.launchEvent, (_args) => {
|
|
12
|
+
console.log('📱 NativeScript app launched');
|
|
13
|
+
initializePeerPigeon();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
// Cleanup when app exits
|
|
17
|
+
Application.on(Application.exitEvent, () => {
|
|
18
|
+
console.log('📱 NativeScript app exiting');
|
|
19
|
+
if (mesh) {
|
|
20
|
+
mesh.disconnect();
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function initializePeerPigeon() {
|
|
25
|
+
try {
|
|
26
|
+
console.log('🚀 Initializing PeerPigeon...');
|
|
27
|
+
|
|
28
|
+
// Create mesh instance with basic configuration
|
|
29
|
+
mesh = new PeerPigeonMesh({
|
|
30
|
+
enableWebDHT: true,
|
|
31
|
+
enableCrypto: false // Disable crypto for simplicity in this example
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
// Set up event listeners
|
|
35
|
+
setupEventListeners();
|
|
36
|
+
|
|
37
|
+
// Initialize the mesh
|
|
38
|
+
await mesh.init();
|
|
39
|
+
peerId = mesh.peerId;
|
|
40
|
+
console.log(`✅ PeerPigeon initialized with ID: ${peerId.substring(0, 8)}...`);
|
|
41
|
+
|
|
42
|
+
// Connect to signaling server
|
|
43
|
+
// Replace with your signaling server URL
|
|
44
|
+
const signalingUrl = 'wss://your-signaling-server.com';
|
|
45
|
+
await mesh.connect(signalingUrl);
|
|
46
|
+
|
|
47
|
+
console.log('🌐 Connected to signaling server');
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error('❌ Failed to initialize PeerPigeon:', error);
|
|
50
|
+
|
|
51
|
+
// Log environment info for debugging
|
|
52
|
+
const report = mesh?.environmentReport;
|
|
53
|
+
if (report) {
|
|
54
|
+
console.log('🔍 Environment Report:', JSON.stringify(report, null, 2));
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function setupEventListeners() {
|
|
60
|
+
// Connection events
|
|
61
|
+
mesh.addEventListener('connected', () => {
|
|
62
|
+
console.log('🔗 Connected to mesh network');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
mesh.addEventListener('disconnected', (data) => {
|
|
66
|
+
console.log('🔌 Disconnected from mesh network:', data.reason);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
// Peer events
|
|
70
|
+
mesh.addEventListener('peerConnected', (data) => {
|
|
71
|
+
console.log(`👋 Peer connected: ${data.peerId.substring(0, 8)}...`);
|
|
72
|
+
console.log(`📊 Total peers: ${mesh.getConnectedPeers().length}`);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
mesh.addEventListener('peerDisconnected', (data) => {
|
|
76
|
+
console.log(`👋 Peer disconnected: ${data.peerId.substring(0, 8)}...`);
|
|
77
|
+
console.log(`📊 Total peers: ${mesh.getConnectedPeers().length}`);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
// Message events
|
|
81
|
+
mesh.addEventListener('messageReceived', (data) => {
|
|
82
|
+
console.log(`📨 Message from ${data.fromPeerId.substring(0, 8)}...:`, data.message);
|
|
83
|
+
|
|
84
|
+
// Echo the message back
|
|
85
|
+
if (data.message.type === 'ping') {
|
|
86
|
+
sendMessage(data.fromPeerId, { type: 'pong', timestamp: Date.now() });
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Error events
|
|
91
|
+
mesh.addEventListener('error', (data) => {
|
|
92
|
+
console.error('❌ Mesh error:', data.error);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// Status events
|
|
96
|
+
mesh.addEventListener('statusChanged', (data) => {
|
|
97
|
+
console.log('📈 Status changed:', data);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Send a message to a specific peer or broadcast
|
|
102
|
+
async function sendMessage(targetPeerId, message) {
|
|
103
|
+
try {
|
|
104
|
+
if (targetPeerId) {
|
|
105
|
+
await mesh.sendMessage(targetPeerId, message);
|
|
106
|
+
console.log(`📤 Message sent to ${targetPeerId.substring(0, 8)}...`);
|
|
107
|
+
} else {
|
|
108
|
+
// Broadcast to all peers
|
|
109
|
+
const peers = mesh.getConnectedPeers();
|
|
110
|
+
for (const peer of peers) {
|
|
111
|
+
await mesh.sendMessage(peer.peerId, message);
|
|
112
|
+
}
|
|
113
|
+
console.log(`📡 Message broadcast to ${peers.length} peers`);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('❌ Failed to send message:', error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Example: Send a ping message every 30 seconds
|
|
121
|
+
setInterval(() => {
|
|
122
|
+
if (mesh && mesh.connected) {
|
|
123
|
+
const peers = mesh.getConnectedPeers();
|
|
124
|
+
if (peers.length > 0) {
|
|
125
|
+
const randomPeer = peers[Math.floor(Math.random() * peers.length)];
|
|
126
|
+
sendMessage(randomPeer.peerId, {
|
|
127
|
+
type: 'ping',
|
|
128
|
+
timestamp: Date.now(),
|
|
129
|
+
from: 'NativeScript'
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}, 30000);
|
|
134
|
+
|
|
135
|
+
// Example: Log mesh status every 60 seconds
|
|
136
|
+
setInterval(() => {
|
|
137
|
+
if (mesh) {
|
|
138
|
+
const peers = mesh.getConnectedPeers();
|
|
139
|
+
console.log(`📊 Mesh Status - Connected: ${mesh.connected}, Peers: ${peers.length}`);
|
|
140
|
+
|
|
141
|
+
if (peers.length > 0) {
|
|
142
|
+
console.log(`👥 Connected to: ${peers.map(p => p.peerId.substring(0, 8)).join(', ')}...`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}, 60000);
|
|
146
|
+
|
|
147
|
+
// Export functions for potential UI integration
|
|
148
|
+
export {
|
|
149
|
+
mesh,
|
|
150
|
+
sendMessage,
|
|
151
|
+
initializePeerPigeon
|
|
152
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "peerpigeon-nativescript-example",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "NativeScript example for PeerPigeon mesh networking",
|
|
5
|
+
"main": "app.js",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@nativescript/core": "^8.0.0",
|
|
8
|
+
"peerpigeon": "^0.0.7"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"@nativescript/cli": "^8.0.0"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"ios": "ns run ios",
|
|
15
|
+
"android": "ns run android",
|
|
16
|
+
"clean": "ns clean"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"nativescript",
|
|
20
|
+
"peerpigeon",
|
|
21
|
+
"webrtc",
|
|
22
|
+
"p2p",
|
|
23
|
+
"mesh-networking"
|
|
24
|
+
],
|
|
25
|
+
"author": "PeerPigeon",
|
|
26
|
+
"license": "MIT"
|
|
27
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { PeerPigeonMesh } from '../../index.js';
|
|
4
|
+
import WebSocket from 'ws';
|
|
5
|
+
import wrtc from '@koush/wrtc';
|
|
6
|
+
|
|
7
|
+
// Make WebRTC available globally for Node.js
|
|
8
|
+
global.RTCPeerConnection = wrtc.RTCPeerConnection;
|
|
9
|
+
global.RTCSessionDescription = wrtc.RTCSessionDescription;
|
|
10
|
+
global.RTCIceCandidate = wrtc.RTCIceCandidate;
|
|
11
|
+
global.WebSocket = WebSocket;
|
|
12
|
+
|
|
13
|
+
// Test crypto functionality with media streams
|
|
14
|
+
console.log('🔐🎥 PeerPigeon Crypto + Media Test\n');
|
|
15
|
+
|
|
16
|
+
async function createCryptoPeerWithMedia(peerName, enableMedia = false) {
|
|
17
|
+
const mesh = new PeerPigeonMesh({
|
|
18
|
+
enableWebDHT: true,
|
|
19
|
+
enableCrypto: true, // Enable crypto!
|
|
20
|
+
ignoreEnvironmentErrors: true
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Set up event handlers
|
|
24
|
+
mesh.addEventListener('statusChanged', (event) => {
|
|
25
|
+
console.log(`[${peerName}] Status: ${event.type} - ${event.message || ''}`);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
mesh.addEventListener('peerConnected', (event) => {
|
|
29
|
+
console.log(`[${peerName}] 🔗 Connected to peer: ${event.peerId.substring(0, 8)}...`);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
mesh.addEventListener('peerDisconnected', (event) => {
|
|
33
|
+
console.log(`[${peerName}] 💔 Disconnected from peer: ${event.peerId.substring(0, 8)}... (${event.reason})`);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
mesh.addEventListener('messageReceived', (event) => {
|
|
37
|
+
const fromPeerId = event.from || 'unknown';
|
|
38
|
+
console.log(`[${peerName}] 📨 Received: "${event.content}" from ${fromPeerId.substring(0, 8)}...`);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Crypto-specific events
|
|
42
|
+
mesh.addEventListener('peerKeyAdded', (event) => {
|
|
43
|
+
console.log(`[${peerName}] 🔑 Key added for peer: ${event.peerId.substring(0, 8)}...`);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
mesh.addEventListener('cryptoError', (event) => {
|
|
47
|
+
console.log(`[${peerName}] 🚫 Crypto error: ${event.error}`);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
// Media events
|
|
51
|
+
mesh.addEventListener('remoteStream', (event) => {
|
|
52
|
+
console.log(`[${peerName}] 🎥 Remote stream received from ${event.peerId.substring(0, 8)}...`);
|
|
53
|
+
const { stream } = event;
|
|
54
|
+
const audioTracks = stream.getAudioTracks();
|
|
55
|
+
const videoTracks = stream.getVideoTracks();
|
|
56
|
+
console.log(`[${peerName}] 🎥 Stream has ${audioTracks.length} audio tracks, ${videoTracks.length} video tracks`);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Initialize the mesh
|
|
60
|
+
await mesh.init();
|
|
61
|
+
console.log(`[${peerName}] Initialized with ID: ${mesh.peerId.substring(0, 8)}...`);
|
|
62
|
+
|
|
63
|
+
if (enableMedia) {
|
|
64
|
+
try {
|
|
65
|
+
console.log(`[${peerName}] Starting media stream...`);
|
|
66
|
+
// Start media stream (this will work in Node.js environment with mock streams)
|
|
67
|
+
await mesh.startMedia({ video: true, audio: true });
|
|
68
|
+
console.log(`[${peerName}] Media stream started successfully`);
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.log(`[${peerName}] Media stream failed (expected in Node.js): ${error.message}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return mesh;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async function testCryptoWithMedia() {
|
|
78
|
+
console.log('Testing crypto key exchange with media-enabled peers...\n');
|
|
79
|
+
|
|
80
|
+
// Create two peers - one with media, one without
|
|
81
|
+
const peer1 = await createCryptoPeerWithMedia('CRYPTO-MEDIA-PEER-1', true);
|
|
82
|
+
const peer2 = await createCryptoPeerWithMedia('CRYPTO-PEER-2', false);
|
|
83
|
+
|
|
84
|
+
console.log('\nConnecting to signaling server...\n');
|
|
85
|
+
|
|
86
|
+
// Connect to signaling server
|
|
87
|
+
await peer1.connect('ws://localhost:3000');
|
|
88
|
+
await peer2.connect('ws://localhost:3000');
|
|
89
|
+
|
|
90
|
+
// Wait for connection establishment
|
|
91
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
92
|
+
|
|
93
|
+
console.log('\nTesting encrypted message exchange with media connection...\n');
|
|
94
|
+
|
|
95
|
+
// Test encrypted messaging between peers (one with media, one without)
|
|
96
|
+
try {
|
|
97
|
+
console.log('Testing sendEncryptedMessage with media-enabled peer...');
|
|
98
|
+
|
|
99
|
+
const peer1Id = peer1.peerId;
|
|
100
|
+
const peer2Id = peer2.peerId;
|
|
101
|
+
|
|
102
|
+
// Send encrypted message from media peer to regular peer
|
|
103
|
+
await peer1.sendEncryptedMessage(peer2Id, 'This is an encrypted message from the media-enabled peer');
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
105
|
+
|
|
106
|
+
// Send encrypted message from regular peer to media peer
|
|
107
|
+
await peer2.sendEncryptedMessage(peer1Id, 'This is an encrypted message to the media-enabled peer');
|
|
108
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
109
|
+
|
|
110
|
+
console.log('✅ Encrypted messaging test with media peer completed successfully!');
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.log(`❌ Encrypted messaging test failed: ${error.message}`);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Check crypto status
|
|
116
|
+
console.log('\n--- Crypto Status with Media ---');
|
|
117
|
+
const crypto1Status = peer1.getCryptoStatus();
|
|
118
|
+
const crypto2Status = peer2.getCryptoStatus();
|
|
119
|
+
|
|
120
|
+
console.log(`CRYPTO-MEDIA-PEER-1: enabled=${crypto1Status.enabled}, keys=${Object.keys(crypto1Status.peerKeys || {}).length}`);
|
|
121
|
+
console.log(`CRYPTO-PEER-2: enabled=${crypto2Status.enabled}, keys=${Object.keys(crypto2Status.peerKeys || {}).length}`);
|
|
122
|
+
|
|
123
|
+
console.log('\n🏁 Crypto + Media test completed, cleaning up...\n');
|
|
124
|
+
|
|
125
|
+
// Clean up
|
|
126
|
+
peer1.disconnect();
|
|
127
|
+
peer2.disconnect();
|
|
128
|
+
|
|
129
|
+
setTimeout(() => {
|
|
130
|
+
process.exit(0);
|
|
131
|
+
}, 1000);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
testCryptoWithMedia().catch(error => {
|
|
135
|
+
console.error('Crypto + Media test failed:', error);
|
|
136
|
+
process.exit(1);
|
|
137
|
+
});
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { PeerPigeonMesh } from '../../index.js';
|
|
4
|
+
import WebSocket from 'ws';
|
|
5
|
+
import wrtc from '@koush/wrtc';
|
|
6
|
+
|
|
7
|
+
// Make WebRTC available globally for Node.js
|
|
8
|
+
global.RTCPeerConnection = wrtc.RTCPeerConnection;
|
|
9
|
+
global.RTCSessionDescription = wrtc.RTCSessionDescription;
|
|
10
|
+
global.RTCIceCandidate = wrtc.RTCIceCandidate;
|
|
11
|
+
global.WebSocket = WebSocket;
|
|
12
|
+
|
|
13
|
+
// Test crypto functionality
|
|
14
|
+
console.log('🔐 PeerPigeon Crypto Test\n');
|
|
15
|
+
|
|
16
|
+
// Check if signaling server is running
|
|
17
|
+
async function checkSignalingServer() {
|
|
18
|
+
// Simple check - just try to create a WebSocket connection briefly
|
|
19
|
+
return true; // Assume server is running for now
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function createCryptoPeer(peerName, peerId = null) {
|
|
23
|
+
const mesh = new PeerPigeonMesh({
|
|
24
|
+
peerId,
|
|
25
|
+
enableWebDHT: true,
|
|
26
|
+
enableCrypto: true, // Enable crypto!
|
|
27
|
+
ignoreEnvironmentErrors: true
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Set up event handlers
|
|
31
|
+
mesh.addEventListener('statusChanged', (event) => {
|
|
32
|
+
console.log(`[${peerName}] Status: ${event.type} - ${event.message || ''}`);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
mesh.addEventListener('peerConnected', (event) => {
|
|
36
|
+
console.log(`[${peerName}] 🔗 Connected to peer: ${event.peerId.substring(0, 8)}...`);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
mesh.addEventListener('peerDisconnected', (event) => {
|
|
40
|
+
console.log(`[${peerName}] 💔 Disconnected from peer: ${event.peerId.substring(0, 8)}... (${event.reason})`);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
mesh.addEventListener('messageReceived', (event) => {
|
|
44
|
+
const fromPeerId = event.from || 'unknown';
|
|
45
|
+
console.log(`[${peerName}] 📨 Received: "${event.content}" from ${fromPeerId.substring(0, 8)}...`);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// Crypto-specific events
|
|
49
|
+
mesh.addEventListener('peerKeyAdded', (event) => {
|
|
50
|
+
console.log(`[${peerName}] 🔑 Key added for peer: ${event.peerId.substring(0, 8)}...`);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
mesh.addEventListener('cryptoError', (event) => {
|
|
54
|
+
console.log(`[${peerName}] 🚫 Crypto error: ${event.error}`);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Initialize the mesh
|
|
58
|
+
await mesh.init();
|
|
59
|
+
console.log(`[${peerName}] Initialized with ID: ${mesh.peerId.substring(0, 8)}...`);
|
|
60
|
+
|
|
61
|
+
return mesh;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async function testCrypto() {
|
|
65
|
+
console.log('Checking signaling server at ws://localhost:3000...');
|
|
66
|
+
const serverRunning = await checkSignalingServer();
|
|
67
|
+
if (!serverRunning) {
|
|
68
|
+
console.log('❌ Signaling server not running. Start it with: npm run server');
|
|
69
|
+
process.exit(1);
|
|
70
|
+
}
|
|
71
|
+
console.log('✅ Signaling server is running\n');
|
|
72
|
+
|
|
73
|
+
console.log('Creating two crypto-enabled mesh peers...\n');
|
|
74
|
+
|
|
75
|
+
// Create two peers with crypto enabled
|
|
76
|
+
const peer1 = await createCryptoPeer('CRYPTO-PEER-1');
|
|
77
|
+
const peer2 = await createCryptoPeer('CRYPTO-PEER-2');
|
|
78
|
+
|
|
79
|
+
console.log('\nConnecting to signaling server...\n');
|
|
80
|
+
|
|
81
|
+
// Connect to signaling server
|
|
82
|
+
await peer1.connect('ws://localhost:3000');
|
|
83
|
+
await peer2.connect('ws://localhost:3000');
|
|
84
|
+
|
|
85
|
+
// Wait for connection establishment
|
|
86
|
+
await new Promise(resolve => setTimeout(resolve, 3000));
|
|
87
|
+
|
|
88
|
+
console.log('\nTesting encrypted message exchange...\n');
|
|
89
|
+
|
|
90
|
+
// Test direct encrypted messaging between peers
|
|
91
|
+
try {
|
|
92
|
+
console.log('Testing sendEncryptedMessage...');
|
|
93
|
+
|
|
94
|
+
// Get the peer IDs for direct encrypted messaging
|
|
95
|
+
const peer1Id = peer1.peerId;
|
|
96
|
+
const peer2Id = peer2.peerId;
|
|
97
|
+
|
|
98
|
+
// Send encrypted message from peer1 to peer2
|
|
99
|
+
await peer1.sendEncryptedMessage(peer2Id, 'This is an encrypted message from peer 1');
|
|
100
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
101
|
+
|
|
102
|
+
// Send encrypted message from peer2 to peer1
|
|
103
|
+
await peer2.sendEncryptedMessage(peer1Id, 'This is an encrypted message from peer 2');
|
|
104
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
105
|
+
|
|
106
|
+
console.log('✅ Encrypted messaging test completed successfully!');
|
|
107
|
+
} catch (error) {
|
|
108
|
+
console.log(`❌ Encrypted messaging test failed: ${error.message}`);
|
|
109
|
+
console.log('Falling back to regular messaging...');
|
|
110
|
+
|
|
111
|
+
// Fallback to regular messaging
|
|
112
|
+
peer1.sendMessage('Hello unencrypted world from peer 1!');
|
|
113
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
114
|
+
|
|
115
|
+
peer2.sendMessage('Hello unencrypted world from peer 2!');
|
|
116
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Test a few more regular messages
|
|
120
|
+
for (let i = 1; i <= 2; i++) {
|
|
121
|
+
peer1.sendMessage(`Test message #${i}`);
|
|
122
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
123
|
+
peer2.sendMessage(`Response #${i}`);
|
|
124
|
+
await new Promise(resolve => setTimeout(resolve, 500));
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
console.log('\n--- Crypto Stats ---');
|
|
128
|
+
console.log(`CRYPTO-PEER-1: ${peer1.getConnectedPeerCount()} connected peers`);
|
|
129
|
+
console.log(`CRYPTO-PEER-2: ${peer2.getConnectedPeerCount()} connected peers`);
|
|
130
|
+
|
|
131
|
+
console.log('\n🏁 Crypto test completed, cleaning up...\n');
|
|
132
|
+
|
|
133
|
+
// Clean up
|
|
134
|
+
peer1.disconnect();
|
|
135
|
+
peer2.disconnect();
|
|
136
|
+
|
|
137
|
+
// Give time for cleanup
|
|
138
|
+
setTimeout(() => {
|
|
139
|
+
process.exit(0);
|
|
140
|
+
}, 1000);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
testCrypto().catch(error => {
|
|
144
|
+
console.error('Crypto test failed:', error);
|
|
145
|
+
process.exit(1);
|
|
146
|
+
});
|