This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
server.RestApiServer.js
executable file
·212 lines (189 loc) · 5.22 KB
/
server.RestApiServer.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/* jshint asi: true */
/* jshint esnext: true */
var ChatClientBase = require('./server.ChatClientBase.js');
var util = require("util");
var http = require("http");
var express = require("express");
var session = require('express-session');
var tokenChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
function generateToken(clients) {
var token;
do {
token = '';
for (var i = 0; i < 32; ++i)
token += tokenChars[Math.floor(Math.random()*tokenChars.length)];
} while(clients[token] !== void 0);
return token;
};
function RestClient(chatServerBase, clients, req) {
this.clients = clients;
this.chatServerBase = chatServerBase;
this.updateTimeout(req);
this.updateIp(req, chatServerBase.config.x_forwarded_for);
this.token = req.session.id;
this.clients[this.token] = this;
this.storage = [];
}
util.inherits(RestClient, ChatClientBase);
RestClient.prototype.login = function(nick, channel) {
this.nick = nick;
this.channel = channel;
/*var msg = {cmd:'join', channel: channel, nick: nick, pass: data.pass};
chatServerBase.onMessage(this, msg);*/
};
RestClient.prototype.getId = function() {
return this.ircClient;
};
RestClient.prototype.addToStorage = function(data) {
this.storage.push(data);
};
RestClient.prototype.applyStorage = function(list) {
while (this.storage.length > 0)
list.push(this.storage.shift());
};
RestClient.prototype.updateTimeout = function(req) {
var self = this;
if (this.timeout !== void 0)
clearTimeout(this.timeout);
// 6 mins timeout, clients should ping at least every 4 mins
var nextTimeout = 6*60*1000;
this.timeout = setTimeout(function() { self.close(); }, nextTimeout);
req.session.cookie.expires = new Date(Date.now() + nextTimeout);
req.session.cookie.maxAge = nextTimeout;
};
RestClient.prototype.updateIp = function(req, bForwardingEnabled) {
if (bForwardingEnabled) {
this.ip = req.get('x-forwarded-for') || req.ip;
} else {
this.ip = req.ip;
}
};
RestClient.prototype.getIpAddress = function() {
return this.ip;
};
RestClient.prototype.close = function() {
clearTimeout(this.timeout);
this.chatServerBase.onClose(this);
delete this.clients[this.token];
};
RestClient.prototype.send = function(causingClient, data) {
this.addToStorage(data);
};
function RestApiServer() {
}
module.exports = function() {
return new RestApiServer();
};
RestApiServer.prototype.initialize = function(config) {
this.config = config;
};
RestApiServer.prototype.run = function(chatServerBase) {
this.chatServerBase = chatServerBase;
var self = this;
var app = express();
var clients = {}; // token to client
var secret = '';
for(var i = 0; i < 32; ++i)
secret += String.fromCharCode(Math.floor(Math.random()*26));
app.use(session({
genid: function(req) {
return generateToken(clients) // use UUIDs for session IDs
},
secret: secret,
resave: true,
rolling: true,
saveUninitialized: true
}));
app.get('/', function(req, res) {
res.send('');
});
app.get('/login', function(req, res) {
var client = clients[req.session.id] = new RestClient(chatServerBase, clients, req);
client.updateTimeout(req);
client.updateIp(req, self.config.x_forwarded_for);
res.send(JSON.stringify({results:[{login:'ok'}]}));
});
app.get('/logout', function(req, res) {
var client = clients[req.session.id];
if (client === void 0) {
res.send('');
return;
}
client.updateTimeout(req);
client.updateIp(req, self.config.x_forwarded_for);
var outData = {
results: [
]
};
client.applyStorage(outData.results);
res.send(JSON.stringify(outData));
client.close();
});
app.get('/ping', function(req, res) { // required at least 5 mins after last poll
var client = clients[req.session.id];
if (client === void 0) {
res.send('');
return;
}
client.updateTimeout(req);
client.updateIp(req, self.config.x_forwarded_for);
var outData = {
results: [
{cmd:'pong'}
]
};
// dont move store on pings
res.send(JSON.stringify(outData));
});
app.get('/poll', function(req, res) {
var client = clients[req.session.id];
if (client === void 0) {
res.send('');
return;
}
client.updateTimeout(req);
client.updateIp(req, self.config.x_forwarded_for);
var outData = {
results: [
]
};
client.applyStorage(outData.results);
res.send(JSON.stringify(outData));
});
app.post('/send', function(req, res) {
var client = clients[req.session.id];
if (client === void 0) {
res.send('');
return;
}
var content = [];
req.on('data', function (data) {
content.push(data);
});
req.on('end', function() {
var data = JSON.parse(content.join(''));
chatServerBase.onMessage(client, data);
client.updateTimeout(req);
client.updateIp(req, self.config.x_forwarded_for);
var outData = {
results: [
]
};
client.applyStorage(outData.results);
res.send(JSON.stringify(outData));
});
});
// error callback must be defined last
app.use(function(err, req, res, next) {
console.error(err);
res.send(500);
});
var httpServer = http.createServer();
httpServer.on('request', app);
httpServer.on('error', function(error) {
console.error("Error in RestApi Server:");
console.error(error);
});
httpServer.listen(this.config.port, this.config.ip);
console.log("Started rest api server on " + this.config.ip + ":" + this.config.port);
};