-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
129 lines (111 loc) · 3.49 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
const {WebSocketServer} = require('ws');
const http = require('http');
const fs = require('fs')
const CFG_PATH = __dirname + '/config.json'
let config = fs.existsSync(CFG_PATH)
let wsServer = null
let connections = {}
if (config === true) {
config = fs.readFileSync(CFG_PATH, 'utf8')
config = JSON.parse(config)
// check property
if (!config.hasOwnProperty('ws')) {
log('Launch Failed: No [ws] Config Found.')
}
if (!config.hasOwnProperty('http')) {
log('Launch Failed: No [http] Config Found.')
}
if (!config.hasOwnProperty('socket')) {
log('Launch Failed: No [socket] Config Found.')
}
log('[ws] Starting...')
start_ws(config.ws, function (status, wsServer) {
if (status === 1) {
wsServer.on('connection', function connection(ws, req) {
let path_check = req.url.substr(0, config.ws.path.length)
if (path_check === config.ws.path) {
let date = new Date()
ws.send('Welcome to Slog @' + date.toLocaleTimeString());
let uuid = req.url.substr(config.ws.path.length + 1)
if (connections.hasOwnProperty('uuid') === false) {
connections[uuid] = []
}
if (connections[uuid].indexOf(uuid) === -1) {
connections[uuid].push(ws)
}
ws.uuid = uuid
} else {
ws.send('Server Refused!')
ws.terminate()
}
});
}
})
log('[http] Starting...')
start_http(config.http, function (status) {
})
} else {
log('Launch Failed: No Config File Found.')
}
function log(msg, type = 'info') {
console.log('[' + type.toUpperCase() + ']@' + msg + "\n")
}
function start_ws(config, callback) {
let status = 0
if (config.enable === true) {
wsServer = new WebSocketServer({
port: config.port,
path: config.path
});
log('[ws] Started')
status = 1
} else {
log('[ws] Start Abort')
status = -1
}
callback(status, wsServer)
}
function send_to(path, obj) {
if (wsServer !== null) {
let uuid = decodeURI(path).replace(' ', '_').substr(1)
wsServer.clients.forEach(function each(client) {
if (client.uuid === uuid) {
client.send(obj);
} else {
// other clients
}
});
} else {
log('[ws] Inactive')
}
}
function start_http(config, callback) {
let status = 0
let httpServer = null
if (config.enable === true) {
httpServer = http.createServer(function (request, response) {
if (request.url === '/favicon.ico') {
return;
}
if ('POST' === request.method) {
let requestBody = '';
request.on('data', function (data) {
requestBody += data;
});
request.on('end', function () {
response.end('success');
send_to(request.url, requestBody)
});
} else {
response.end('Only Post Accept - Slog Http Server');
}
});
httpServer.listen(config.port);
log('[http] Started')
status = 1
} else {
log('[http] Start Abort')
status = -1
}
callback(status, httpServer)
}