-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
180 lines (178 loc) · 4.88 KB
/
utils.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
const querystring = require('querystring');
const url = require('url');
const fs = require('fs');
const crypto = require('crypto');
const mime = require('mime');
const etag = require('etag');
const ejs = require('ejs');
const consts = require('./consts');
const {promisify} = require('util');
class Utils{
constructor(){}
postData(req, parse) {
return new Promise(function(resolve, reject) {
let data = "";
req.on("data", chunk => {
if(!data) data = chunk;
else data = Buffer.concat([data, chunk]);
});
req.on("end", () => {
if(!req.complete) {
reject(new Error("Incomplete Request"));
}
if(parse) {
try{
resolve(querystring.parse(data.toString()));
} catch(e) {
reject(e);
}
} else {
resolve(data);
}
});
});
}
createCallback(buffer) {
//check if `buffer` isn't a buffer
//in case of issues with length of Chinese
if(!buffer instanceof Buffer) {
buffer = Buffer.from(buffer);
}
const tag = etag(buffer);
return (req, resp) => {
const {pathname} = url.parse(req.url);
if(req.headers["if-none-match"] === tag) {
resp.writeHead(304, {ETag:tag});
resp.end();
return;
}
let type = mime.getType(pathname.substr(1));
if(pathname === "/" || type === "text/html") {
type = "text/html; charset=utf8";
}
resp.writeHead(200, {
"Content-Type":type,
"Content-Length":buffer.length,
ETag:tag
});
resp.end(buffer);
};
};
redirect(resp, path) {
resp.writeHead(302, {"Location":path});
resp.end();
}
htmlHead(resp, status) {
resp.writeHead(status || 200, {"Content-Type":"text/html; charset=utf8"});
}
async serveEjs(req, resp, path, obj) {
const session = new Session(req, resp);
let user;
if(session.get("userid")) {
user = new User(session.get("userid"))
}
obj = Object.assign(obj, {
require,
session,
req,
resp,
user,
path:url.parse(req.url).pathname
});
resp.end(await ejs.renderFile(consts.http.ejsRoot + path, obj, {
root:consts.http.ejsRoot,
rmWhitespace:true
}));
}
authenticate(req, resp) {
const session = new Session(req, resp);
const uid = session.get("userid");
if(!uid) {
session.set("from", req.url);
this.redirect(resp, "/");
return;
}
return new User(uid);
}
handleLoggedin(req, resp) {
const session = new Session(req, resp);
const from = session.get("from");
if(from) {
session.remove("from");
this.redirect(resp, from);
return;
}
let location = "/home";
if(new User(session.get("userid")).messagesReceived.some(msg => !msg.read)) {
location = "/messages";
}
this.redirect(resp, location);
}
logRequest(req, resp, e) {
const path = url.parse(req.url).pathname;
const ip = req.headers[consts.http.realIpHeader] || req.socket.address().address;
let uid = "unknown";
try {
uid = (new Session(req, resp)).get("userid");
} catch(e) {}
const log = (`${ip} ${uid} ${req.method} ${path}: ${resp.statusCode}\n${new Date()} ${req.headers["user-agent"]}\n`);
fs.appendFile(consts.http.logFile, log, () => {});
if(e) {
console.error(e.stack || e);
}
}
logEvent(user, name, details) {
const uid = user.id;
const log = (`[${new Date().toLocaleString()}] ${uid} ${name}: ${details}\n`);
fs.appendFile(consts.event.logFile, log, () => {});
}
logEventSync(user, name, details) {
const uid = user.id;
const log = (`[${new Date().toLocaleString()}] ${uid} ${name}: ${details}\n`);
fs.appendFileSync(consts.event.logFile, log);
}
get eventLog() {
return promisify(fs.readFile)(consts.event.logFile);
}
verifyCsrfToken(req, data) {
if(!data || !data._csrf) return false;
try {
const path = url.parse(req.url).pathname;
const session = new Session(req, {});
return session.verifyCsrfToken(path, data._csrf);
} catch(e) {
return false;
}
}
onExit(cb) {
['SIGTERM', 'SIGINT', 'SIGHUP', 'SIGBREAK'].forEach(event => {
process.on(event, cb);
});
}
notifyOfficer(user, msg, isRoom) {
const type = isRoom ? "room" : user.type;
User.all
.filter(_user => _user.role === "officer" && _user.type === type)
.forEach(officer => {
wechat.send({
user:officer.name,
msg
});
});
}
notifyAdmin(msg) {
User.all
.filter(user => user.role === "admin")
.forEach(admin => {
wechat.send({
user:admin.name,
msg
});
});
}
}
module.exports = new Utils();
//require('./session') at last to prevent cross-require()ing
const Session = require('./session');
const User = require('./user');
const wechat = require('./wechat');