-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
141 lines (134 loc) · 4.43 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
133
134
135
136
137
138
139
140
141
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const http = require('http').Server(app);
const io = require('socket.io')(http);
app.use(express.static('public'))
app.use(bodyParser.urlencoded({
extended: true
}))
app.set('view engine', 'ejs')
let public_channel = {
name: "main",
key: genID(10),
public: true
};
let chats = [public_channel];
let usernames = [];
let chatsnames = [];
let instanceID = "test";
app.get('/chat', function(req, res) {
res.render('chat', {
id: instanceID,
error: null
});
})
const server = http.listen(7777, function() {
console.log("started");
});
io.sockets.on('connection', function(socket) {
setInterval(function() {
chatsnames = [];
for (const element of chats) {
if (element != undefined && element != null)
chatsnames.push(element.name);
}
io.emit('channels', chatsnames);
}, 500);
socket.on('create_chat', function(name, key, public) {
var alreadyUsed = false;
for (const element of chatsnames)
if (element == name)
alreadyUsed = true;
if (!alreadyUsed) {
if(name != null && name != undefined && key != null && key != undefined) {
if (name !== "" && name.indexOf("<script>") <= 0 && name.indexOf("</script>") <= 0 && name.indexOf("<style>") <= 0 && name.indexOf("</style>") <= 0) {
if (key !== "" && key.indexOf("<script>") <= 0 && key.indexOf("</script>") <= 0 && key.indexOf("<style>") <= 0 && key.indexOf("</style>") <= 0) {
chats.push({
name: name,
key: key,
public: public
});
console.log("created");
}
}
}
}
});
socket.on('get_key', function(channel, requestid) {
if (getChannelAuth(channel)) {
io.emit('set_key', getChannelKey(channel), channel, requestid);
} else {
io.emit('need_password', channel, requestid);
}
});
socket.on('username', function(username, chat_name) {
if (rc4(getChannelKey(chat_name), username) !== "" && rc4(getChannelKey(chat_name), username).indexOf("<script>") <= 0 && rc4(getChannelKey(chat_name), username).indexOf("</script>") <= 0 && rc4(getChannelKey(chat_name), username).indexOf("<style>") <= 0 && rc4(getChannelKey(chat_name), username).indexOf("</style>") <= 0) {
socket.username = username;
} else {
socket.username = rc4(getChannelKey(chat_name), genID(5));
}
});
socket.on('encryptedUsername', function(user, chat_name, key) {
if (getChannelKey(chat_name) != key) {
io.emit('invalid_key', user, chat_name);
}
});
socket.on('chat_message', function(message, chat_name) {
if (rc4(getChannelKey(chat_name), message) !== "" && rc4(getChannelKey(chat_name), message).indexOf("<script>") <= 0 && rc4(getChannelKey(chat_name), message).indexOf("</script>") <= 0 && rc4(getChannelKey(chat_name), message).indexOf("<style>") <= 0 && rc4(getChannelKey(chat_name), message).indexOf("</style>") <= 0) {
io.emit('chat_message', rc4(getChannelKey(chat_name), '<strong>' + socket.username + '</strong>: ' + rc4(getChannelKey(chat_name), message)), chat_name);
}
});
});
function rc4(key, str) {
var s = [],
j = 0,
x, res = '';
if (str !== undefined) {
for (var i = 0; i < 256; i++) {
s[i] = i;
}
for (i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % key.length)) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
}
i = 0;
j = 0;
for (var y = 0; y < str.length; y++) {
i = (i + 1) % 256;
j = (j + s[i]) % 256;
x = s[i];
s[i] = s[j];
s[j] = x;
res += String.fromCharCode(str.charCodeAt(y) ^ s[(s[i] + s[j]) % 256]);
}
}
return res;
}
function getChannelKey(channel) {
var key = "";
for (const element of chats) {
if (element.name == channel)
key = element.key;
}
return key;
}
function getChannelAuth(channel) {
var public = false;
for (const element of chats) {
if (element.name == channel)
public = element.public;
}
return public;
}
function genID(length) {
var result = '';
var characters = 'ABDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var charactersLength = characters.length;
for (var i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}