-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibp2p.js
165 lines (133 loc) · 4.16 KB
/
Libp2p.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import process from 'node:process'
import {createLibp2p} from 'libp2p'
import {tcp} from '@libp2p/tcp'
import {noise} from '@chainsafe/libp2p-noise'
import {mplex} from '@libp2p/mplex'
import {multiaddr} from 'multiaddr'
import {createFromJSON} from "@libp2p/peer-id-factory";
import {pipe} from "it-pipe";
import {toString as uint8ArrayToString} from "uint8arrays/to-string";
import {fromString as uint8ArrayFromString} from "uint8arrays/from-string";
import axios from "axios"
import * as dotenv from 'dotenv'
dotenv.config();
let nodeResponseLoaded = false;
let nodesResponse = [];
async function loadNodes() {
axios.get("https://monitor1.muon.net/nodes")
.then(({data}) => {
if (data.success) {
nodeResponseLoaded = true;
nodesResponse = data.result;
}
})
.catch((e) => {
console.log("error checkActiveNodes: " + e.message);
return false;
});
}
loadNodes();
setInterval(loadNodes, 10 * 60000);
function getNodeByIp(ip) {
for (let i = 0; i < nodesResponse.length; i++)
if (nodesResponse[i].ip == ip)
return nodesResponse[i];
return null
}
function getNodeById(id) {
for (let i = 0; i < nodesResponse.length; i++)
if (nodesResponse[i].id == id)
return nodesResponse[i];
return null
}
const chatProtocol = '/muon/network/remote-call/1.0.0';
let dialerPeerId = {
id: process.env.NODE_ID,
privKey: process.env.NODE_PRV_KEY,
pubKey: process.env.NODE_PUB_KEY
};
const peerId = await
createFromJSON(dialerPeerId);
const libp2p = await
createLibp2p({
peerId,
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
transports: [tcp()],
connectionEncryption: [noise()],
streamMuxers: [mplex()],
});
await
libp2p.start();
console.log('libp2p has started');
console.log('listening on addresses:');
libp2p.getMultiaddrs().forEach((addr) => {
console.log(addr.toString())
});
const stop = async () => {
await libp2p.stop();
console.log('libp2p has stopped');
process.exit(0)
};
export async function call(req) {
let request = req.body;
let module = req.params.module;
let method = req.params.method;
if (module == "BaseAppPlugin")
module = "DynamicExtended(tss@BaseAppPlugin)";
if (request.method) {
method = request.method;
} else {
method = module + "." + method;
}
let deployerNodeIps = ["104.131.177.195", "167.71.60.172", "3.130.24.220", "18.221.53.56", "194.195.211.27", "194.195.244.101", "209.250.252.247", "95.179.139.243"];
let ma;
if (request.ma)
ma = multiaddr(request.ma);
else if (deployerNodeIps.includes(request.ip)) {
ma = multiaddr(`/ip4/${request.ip}/tcp/5000`);
} else if (request.ip || request.id) {
let nodeInfo;
if (request.ip)
nodeInfo = getNodeByIp(request.ip);
if (request.id)
nodeInfo = getNodeById(request.id);
if (!nodeInfo)
return "node info not found";
ma = multiaddr(`/ip4/${nodeInfo.ip}/tcp/${nodeInfo.networkingPort}`);
} else {
//default
ma = multiaddr(`/ip4/104.131.177.195/tcp/5000`);
}
let stream;
try {
stream = await
libp2p.dialProtocol(ma, chatProtocol);
} catch (e) {
return "Error dialProtocol " + e.message;
}
let data = {
callId: "1gnuln3hnjdqtm3" + Math.floor(Math.random() * 1e12).toString(),
method: "NetworkIpcHandler.exec-ipc-remote-call",
params: {
method: method,
params: request.params
}
};
if (method.includes("NetworkIpcHandler"))
data = {
callId: "1gnuln3hnjdqtm3" + Math.floor(Math.random() * 1e12).toString(),
method: method,
params: request.params
};
let dataStr = JSON.stringify(data);
return pipe([uint8ArrayFromString(dataStr)], stream, async function (source) {
for await (const data of source) {
let jsonResp = JSON.parse(uint8ArrayToString(data.subarray()));
return jsonResp;
}
});
}
process.on('SIGTERM', stop);
process.on('SIGINT', stop);