-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp2pnode.js
144 lines (125 loc) · 4.49 KB
/
p2pnode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { createRSAPeerId } from '@libp2p/peer-id-factory'
import { gossipsub } from '@chainsafe/libp2p-gossipsub';
// libp2p
let libp2pnode;
let myPeerId;
async function start() {
const { createLibp2p } = await import('libp2p');
const { floodsub } = await import('@libp2p/floodsub');
// const { webRTCStar } = await import('@libp2p/webrtc-star');
// const { sigServer } = await import('@libp2p/webrtc-star-signalling-server');
const { bootstrap } = await import('@libp2p/bootstrap');
const { tcp } = await import('@libp2p/tcp');
const { mdns } = await import('@libp2p/mdns');
const { mplex } = await import('@libp2p/mplex');
const { noise } = await import('@chainsafe/libp2p-noise');
const { unmarshalPrivateKey } = await import('@libp2p/crypto/keys');
const { createFromPrivKey } = await import('@libp2p/peer-id-factory');
const { preSharedKey } = await import('libp2p/pnet');
const { logger } = await import('@libp2p/logger');
const { pipe } = await import('it-pipe');
const uint8ArrayFromString = (await import('uint8arrays/from-string')).fromString;
const uint8ArrayToString = (await import('uint8arrays/to-string')).toString;
const log = logger('i2kn:api:libp2p');
log('libp2p starting');
const peerId = await createRSAPeerId();
const PK = await unmarshalPrivateKey(peerId.privateKey);
myPeerId = await createFromPrivKey(PK);
console.log(myPeerId.toString());
// const connectionProtector = preSharedKey({
// psk: new Uint8Array(Buffer.from(swarmKey, 'base64')),
// });
const p2pOptions = {
peerId: myPeerId,
addresses: {
listen: [
'/ip4/0.0.0.0/tcp/0',
],
},
transports: [
tcp(),
],
peerDiscovery: [
mdns(),
],
streamMuxers: [mplex()],
connectionEncryption: [noise()],
// connectionManager: {
// maxParallelDials: 150, // 150 total parallel multiaddr dials
// maxDialsPerPeer: 4, // Allow 4 multiaddrs to be dialed per peer in parallel
// dialTimeout: 30e3, // 10 second dial timeout per peer dial
// inboundUpgradeTimeout: 30e3,
// autoDial: true,
// },
pubsub: gossipsub({
allowPublishToZeroPeers:true, // or error thrown, not catchable...
// directPeers:
}),
// connectionProtector,
};
libp2pnode = await createLibp2p(p2pOptions);
libp2pnode.handle('/i2knV3', ({ stream }) => {
pipe(
stream,
async (source) => {
let message = '/i2knV3 msg : ';
// eslint-disable-next-line no-restricted-syntax
for await (const msg of source) {
message += uint8ArrayToString(msg.subarray());
}
log('handle msg :', message);
},
);
});
libp2pnode.addEventListener('peer:discovery', (evt) => {
const { detail: peer } = evt;
log('libp2p.onPeerDiscovery', peer.id.toString());
});
libp2pnode.connectionManager.addEventListener('peer:connect', async (evt) => {
const { detail: connection } = evt;
const { remotePeer } = connection;
log('libp2p.onPeerConnected', remotePeer.toString());
// send hello on CONNECT
try {
const stream = await libp2pnode.dialProtocol(remotePeer, ['/i2knV3']);
await pipe(
[uint8ArrayFromString(`HELLO FROM ${myPeerId.toString()}`)],
stream,
);
} catch (error) {
log('ERROR !!!', error);
}
});
libp2pnode.connectionManager.addEventListener('peer:disconnect', (evt) => {
const { detail: connection } = evt;
const { remotePeer } = connection;
log('libp2p.onPeerDisconnected', remotePeer.toString());
});
await libp2pnode.start();
log('libp2p started');
libp2pnode.pubsub.addEventListener('message', (evt) => {
log(`libp2p message: ${uint8ArrayToString(evt.detail.data)} on topic ${evt.detail.topic}`);
});
libp2pnode.pubsub.subscribe('i2knGS');
setInterval(()=>{
const peers = libp2pnode.pubsub.getSubscribers('i2knGS');
log('pubsub peers', peers);
try {
libp2pnode.pubsub.publish('i2knGS', new TextEncoder().encode(`PUBSUB FROM ${myPeerId.toString()}`));
} catch (error) {
log(error);
}
},5000)
const multiAddrs = libp2pnode.getMultiaddrs();
log(multiAddrs.map((m) => m.toString()));
}
async function stop() {
await libp2pnode.pubsub.publish('i2knGS', new TextEncoder().encode(`PEER OFFLINE : ${myPeerId.toString()}`));
libp2pnode.pubsub.unsubscribe('i2knGS');
await libp2pnode.stop();
return 'stopped';
}
async function send(msg) {
await libp2pnode.pubsub.publish('i2knGS', new TextEncoder().encode(`MSG : ${msg}`));
}
start();