-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
132 lines (122 loc) · 4.09 KB
/
server.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
require('dotenv').config()
global.crypto = require("hypercore-crypto");
global.Packr = require("msgpackr").Packr;
global.Hyperbee = require('hyperbee');
global.SDK = require("hyper-sdk");
const express = global.express = require('express'); //runtime lib downloading/async
global.socketio = require('socket.io');
const http = require('http');
const verify = require('./utils/verify');
const action = require('./utils/action');
const discord = require('./utils/discord');
const fs = require('fs');
(async () => {
const app = express();
app.use(express.static('public'));
const server = http.createServer(app);
const packr = new Packr();
const { Webhook } = require('discord-webhook-node');
const { WebSocketServer, OPEN } = require('ws');
const wss = new WebSocketServer({ server });
const broadcast = async (room, action, args) => {
if (action == 'message') {
if (!args.fromDiscord && args.channel.discordWebhook) {
const hook = new Webhook(args.channel.discordWebhook);
hook.setUsername(args.name);
const avatar = args.avatar||`https://avatars.dicebear.com/api/pixel-art-neutral/${args.name}.png`;
hook.setAvatar(avatar);
hook.send(args.body);
}
const data = packr.pack({ action, p: args })
wss.clients.forEach(function each(client) {
if (client.readyState === OPEN) {
if (client.rooms && client.rooms.includes(room)) {
client.send(data);
}
}
})
};
}
discord(action, ()=>{}, broadcast);
const sockets = {};
wss.on('connection', function connection(ws) {
console.log('connection')
const socketId = crypto.randomBytes(32).toString('hex');
ws.rooms = [];
let key = null;
const emit = async (to, action, id, args, ws) => {
if(action == 'join') {
if(!ws.rooms.includes(to)) ws.rooms.push(to)
} else {
ws.send(packr.pack({ action, i:id, p: args }))
}
};
var debounce = require('debounce');
/*fs.watch('public', debounce((eventType, filename) => {
const data = packr.pack({ action: 'refresh', i: {} })
wss.clients.forEach(function each(client) {
if (client.readyState === OPEN) {
client.send(data);
}
client.close();
})
}, 1000));*/
ws.on('message', async function incoming(message) {
try {
const out = verify.verify(message);
if(!out.t || !out.k) return false;
if(!sockets[out.k]) sockets[out.k] = [];
key = out.k;
if(!sockets[key]) sockets[key] = [];
sockets[key].push(socketId);
const socketEmit = (to, type, id, args) => {
emit(to, type, id, args, ws)
};
emit(out.k, 'join', '', {}, ws);
const socketBroadcast = (to, type, args) => { broadcast(to, type, args, ws) };
const ret = await action(out.t.i, out, socketEmit, socketBroadcast, socketId);
await ws.send(packr.pack({ i:out.t.i, action: out.t.a, p: ret }));
} catch(e) {
console.error(e);
console.trace(e);
}
});
ws.on('close', () => {
if(sockets[key] && sockets[key].includes(socketId)) sockets[key] = sockets[key].filter(a=>a!=socketId)
})
ws.on('error', () => {
if(sockets[key] && sockets[key].includes(socketId)) sockets[key] = sockets[key].filter(a=>a!=socketId)
})
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
var b32 = require("hi-base32");
console.log(
"listening",
b32
.encode(
require("hyper-relay")().serve(
"chatbitch",
"3000",
false,
"127.0.0.1"
)
)
.replace("====", "")
.toLowerCase()
);
console.log(
"listening",
b32
.encode(
require("hyper-relay")().serve(
"chatbitch-liverload",
"35729",
false,
"127.0.0.1"
)
)
.replace("====", "")
.toLowerCase()
);
})()