-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
87 lines (71 loc) · 1.79 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
const net = require('net');
const fs = require('fs');
const path = require('path');
const crypto = require('crypto');
const port = process.argv[2] || 5000;
const server = net.createServer();
class Server {
port = port;
#clients = [];
server = server;
#blockList = new net.BlockList();
constructor() {
this.connection();
this.listen(this.port);
}
connection() {
this.server.on('connection', (socket) => {
this.blockIpAddress();
this.clientInfo(socket);
this.checkClientIpAddress(socket);
this.#clients.push(socket);
this.chat(socket);
this.error(socket);
this.closeClient(socket);
});
};
clientInfo(socket) {
console.log(`Client connected ${socket.remoteAddress}:${socket.remotePort}`);
}
blockIpAddress() {
const host = '127.0.0.5';
const host1 = '127.0.0.6';
this.#blockList.addAddress(host);
this.#blockList.addAddress(host1);
}
checkClientIpAddress(socket) {
const checkHost = this.#blockList.check(socket.remoteAddress);
if (checkHost) {
this.socket.write(`Bu hostda ${socket.remoteAddress} sizga ruxsat yuq `);
process.exit(1);
}
};
chat(socket) {
socket.on('data', (data) => {
this.sendMessageForConnected(socket, data.toString());
});
};
sendMessageForConnected(socket, message) {
this.#clients.forEach((client) => {
client.write(message);
})
}
closeClient(socket) {
socket.once('close', () => {
console.log('client closed');
});
};
error(socket) {
socket.on('error', (err) => {
console.log(err.message);
socket.closed();
process.exit();
});
};
listen(port) {
this.server.listen(port, '127.0.0.1', () => {
console.log(`socket server listen on ${port}`);
});
}
};
new Server();