-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
112 lines (97 loc) · 3.67 KB
/
index.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
// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })
const osu = require('node-os-utils')
const { BloomFilter } = require('bloom-filters')
const io = require('socket.io')(fastify.server, {
cors: {
origin: "*",
methods: ["GET", "POST"]
},
transports: ['websocket'],
serveClient: false
});
const ipfsClient = require('ipfs-http-client');
const ipfs = ipfsClient('http://ipfs_host:5001');
const ApiHandler = require('./APIHandler');
let handler = null;
let clientData = null;
// Declare a route
fastify.get('/', async (request, reply) => {
return JSON.stringify(await ipfs.pubsub.ls())
})
async function welcomeConnection(socket) {
let usage = await osu.cpu.usage();
let status = "CONN_OK"
let message = "You are now connected."
console.log(`CPU USAGE: ${usage}`)
/*
We give the user different status messages.
CONN_OK
The connection is OK. The user can do whatever.
CONN_HIGH_LOAD
If the CPU usage is above 70% this status is given.
A connection is still accepted but not preferred.
CONN_KILLED_TOO_HIGH_CPU_LOAD
If the CPU usahe gets above 90%, all new connections are
given this status and killed immediately after.
The user must connect to a different server.
*/
if (usage > 70.0 && usage <= 90.0) {
console.log(`PLEASE USE ANOTHER SERVER`)
status = "CONN_HIGH_LOAD"
message = "Please consider using another server. This one has a high CPU load. (>70%)"
} else if (usage > 90.0) {
console.log(`KICKING USER, too high CPU load`)
status = "CONN_KILLED_TOO_HIGH_CPU_LOAD"
message = "The CPU load is above 90%. Not accepting new connections, closing. Use a different server."
}
// Install a bloom filter on the socket. This is later used to determine if a message has already been send in order to not send the same data twice.
socket.bloom = new BloomFilter(10000, 4);
return {
id: socket.id,
status: status,
message: message
}
}
io.on('connection', async (socket) => {
console.log(`Connected. Socket id: ${socket.id}`)
let welcomePackage = await welcomeConnection(socket)
let stringData = JSON.stringify(welcomePackage)
let buffer = Buffer.from(stringData).toString('base64')
socket.emit(`info`, buffer)
if (welcomePackage.status == "CONN_KILLED_TOO_HIGH_CPU_LOAD") {
socket.disconnect();
console.log(`Forcefully disconnected ${socket.id} due to high CPU load.`)
return;
}
socket.on('message', (data) => {
console.log(data)
});
socket.on('disconnect', () => {
// At this point we have no clue which socket was connected to which API method.
// So just ask the handler (which will ask all mappings) to remove any socket with this id.
// This could be inefficient if we have thousands of open sockets...
console.log(`Going to disconnect. Socket id: ${socket.id}`)
handler.remove(socket.id)
});
socket.on('subscribe', (channel) => {
console.log(`Attempting to register channel: ${channel} to socket id: ${socket.id}`);
handler.registerSubscribe(channel, socket)
});
socket.on('publish', (data) => {
handler.publish(socket, data)
});
});
// Run the server!
const start = async () => {
try {
clientData = await ipfs.id();
handler = new ApiHandler(ipfs, clientData);
await fastify.listen(80, '0.0.0.0')
fastify.log.info(`server listening on ${fastify.server.address().port}`)
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()