-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
197 lines (170 loc) · 4.36 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const dns = require('node:dns');
const express = require('express');
const socket = require('socket.io');
dns.setDefaultResultOrder("ipv4first");
const app = express();
const flutterV = "flutter-v0.4";
let num = 'error';
let buf = '404';
let userCounter = 0;
let whiteList = {};
let blackList = {};
let addr = '404';
app.get("/", (req, res) => {
for (var key in blackList){
if (blackList[key] == (req.headers['x-forwarded-for'] || req.connection.remoteAddress).split('.').at(-1)){
if (`blacklist ${key}의 접근 감지` == buf){
return;
}
buf = `blacklist ${key}의 접근 감지`;
sendMessage(`blacklist ${key}의 접근 감지`);
return res.send('blocked');
}
}
if (req.headers['user-header'] == flutterV){
return res.json({
title : 'DSM_Chat',
address : addr,
main : "Chat log"
});
}
res.sendFile(__dirname + '/index.html');
});
app.get('/privacy', (req, res) => {
if (req.headers['user-header'] == flutterV){
return res.json({
title : '개인정보처리방침',
main : [
'수집 정보 : ip주소',
'수집 목적 : 사용자 식별, 서버 장애 발생 원인 제거',
'수집 기한 : 페이지에 들어왔을 때 부터, 서버가 종료될 때 까지'
]
});
}
res.sendFile(__dirname + '/privacy.html');
});
const server = app.listen(8080, () => {
console.log('listening...');
});
const addWhiteList = (name, ip) => {
if (name in whiteList){
return false;
}
whiteList[name] = ip;
return true;
}
const unaddWhiteList = (identifer) => {
delete whiteList[identifer];
return true;
}
const addBlackList = (name, ip) => {
if (name in blackList){
return false;
}
blackList[name] = ip;
return true;
}
const unaddBlackList = (identifer) => {
delete blackList[identifer];
return true;
}
const command = (data) => {
let temp = data.split(' ');
if (temp[0] == '!add'){
if (addWhiteList(temp[1], temp[2])){
sendMessage(`페이지가 ${temp[1]}을 기억합니다`);
}
return true;
}
if (temp[0] == '!block'){
if (addBlackList(temp[1], temp[2])){
sendMessage(`페이지가 ${temp[1]}을 차단합니다`);
}
return true;
}
if (temp[0] == '!unadd'){
if (unaddWhiteList(temp[1])){
sendMessage(`페이지가 ${temp[1]}을 까먹습니다`);
}
return true;
}
if (temp[0] == '!unblock'){
if (unaddBlackList(temp[1])){
sendMessage(`페이지가 ${temp[1]}을 용서합니다`);
}
return true;
}
if (temp[0] == '!cls'){
io.emit('cls', '페이지가 강제 새로고침 되었습니다.');
return true;
}
return false;
}
/** 메세지, 이름, 번호, 타입, 색 */
const sendMessage = (Gmessage, Gname = null, Gnum = null, Gtype = false, Gcolor = 'black') => {
let data = {
Sname : Gname,
Snum : Gnum,
Smessage : Gmessage,
Stype : Gtype,
Scolor : Gcolor
}
io.emit('sendAll', JSON.stringify(data));
return;
}
const io = socket(server, { path: '/socket.io' });
IsJsonString = (str) => {
try {
var json = JSON.parse(str);
return (typeof json === 'object');
} catch (e) {
return false;
}
}
io.on('connection', (ws) => {
let clientIp = `${ws.request.connection.remoteAddress}`;
clientIp = (clientIp == '::1') ? 'host' : clientIp.split('.').at(-1);
for (key in whiteList){
if (clientIp = whiteList[key]){
sendMessage(`${key} 들어옴`);
}
}
ws.emit('address', addr);
ws.on('getNum', (name) => {
num = clientIp.toString();
ws.emit('yourNum', num);
sendMessage(`${name}[${num}] 접속 | 총 방문 : ${++userCounter}명`);
});
ws.on('error', (err) => {
console.error(err);
});
ws.on('message', (message) => {
if (!IsJsonString(message)){
console.log("not a json");
return;
}
for (var ip in blackList){
if (blackList[ip] == clientIp){
return;
}
}
message = JSON.parse(message);
if (message['message'].length > 32 || message['name'].length > 16) {
return;
}
let data = {
Sname : message['name'],
Snum : message['num'],
Smessage : message['message'],
Stype : true,
Scolor : 'black'
}
if (clientIp == 'host'){
if (command(message['message'])){
return;
}
}
io.emit('sendAll', JSON.stringify(data));
});
});
addr = 'https://dsm-chat.ijw.app/';