-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
153 lines (137 loc) · 5.07 KB
/
index.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
'use strict'
var fs = require('fs');
var path = require('path');
var logger = require('morgan');
var crypto = require('crypto');
var image = require('./image');
var express = require('express');
var app = express(), router = express.Router();
var uaFilter = /^Mozilla\/\S*\ /
var softwareRefererFilter = /^https?:\/\/(www\.mcbbs\.net|mcbbs\.net|mcbbs\.tvt\.im)\/forum\-software/;
var developmentRefererFilter = /^https?:\/\/(www\.mcbbs\.net|mcbbs\.net|mcbbs\.tvt\.im)\/forum\-development/;
var qandaRefererFilter = /^https?:\/\/(www\.mcbbs\.net|mcbbs\.net|mcbbs\.tvt\.im)\/forum\-(qanda|multiqanda|modqanda|1566)/;
var developmentFileMap = {
'post.png': image.drawDirect.bind(image, 'development-post.png'),
'applications.png': image.drawDirect.bind(image, 'development-applications.png'),
'rule.png': image.drawDirect.bind(image, 'development-rule.png'),
'recommendation.png': image.drawDirect.bind(image, 'recommendation-list.png'),
'tips.png': image.drawDirect.bind(image, 'development-tips.png'),
'header.png': function (isRealVisit) {
if (isRealVisit()) {
return image.increaseDevelopmentViews().then(image.drawDevelopmentHeader);
} else {
return image.drawDevelopmentHeader();
}
}
};
var softwareFileMap = {
'post.png': image.drawDirect.bind(image, 'software-post.png'),
'download.png': image.drawDirect.bind(image, 'software-download.png'),
'rule.png': image.drawDirect.bind(image, 'software-rule.png'),
'recommendation.png': image.drawDirect.bind(image, 'recommendation-list.png'),
'mojang.png': image.drawSoftwareMojangState.bind(image),
'header.png': function (isRealVisit) {
if (isRealVisit()) {
return image.increaseSoftwareViews().then(image.drawSoftwareHeader);
} else {
return image.drawSoftwareHeader();
}
}
};
var qandaFileMap = {
'header.png': function (isRealVisit) {
if (isRealVisit()) {
return image.increaseQandaViews().then(image.drawQandaHeader);
} else {
return image.drawQandaHeader();
}
}
};
var ipRecords = {}, maxVisitsInPeriod = 6, period = 3600000; // milliseconds
function isTheseIPVisitsTooFrequently (list) {
return isThisIPVisitsTooFrequently(list.length == 0 ? "" : list[0])
}
function isThisIPVisitsTooFrequently (ip) {
var visitRecords = ipRecords[ip];
var now = Date.now();
if (!visitRecords) {
ipRecords[ip] = [now];
return false;
} else if (visitRecords.length < maxVisitsInPeriod) {
visitRecords.push(now);
return false;
} else if (visitRecords[0] + period <= now) {
visitRecords.shift();
visitRecords.push(now);
return false;
} else {
return true;
}
}
function getUserIP(req) {
var forwardedFor = req.get('X-Forwarded-For');
return (((forwardedFor === undefined) ? req.ip : forwardedFor) + '').replace(' ','').split(',');
}
router.get('/development/:name', function (req, res, next) {
var name = req.params['name'];
if (name && developmentFileMap[name]) {
res.setHeader('Content-Type', 'image/png');
developmentFileMap[name](function () {
var isValidUA = uaFilter.test(req.get('User-Agent'));
var isValidReferer = developmentRefererFilter.test(req.get('Referer'));
var isThisIPNotRestricted = !isTheseIPVisitsTooFrequently(getUserIP(req));
return isValidUA && isValidReferer && isThisIPNotRestricted;
}).then(function (stream) {
stream.pipe(res);
}).catch(next);
} else {
next();
}
});
router.get('/software/:name', function (req, res, next) {
var name = req.params['name'];
if (name && softwareFileMap[name]) {
res.setHeader('Content-Type', 'image/png');
softwareFileMap[name](function () {
var isValidUA = uaFilter.test(req.get('User-Agent'));
var isValidReferer = softwareRefererFilter.test(req.get('Referer'));
var isThisIPNotRestricted = !isTheseIPVisitsTooFrequently(getUserIP(req));
return isValidUA && isValidReferer && isThisIPNotRestricted;
}).then(function (stream) {
stream.pipe(res);
}).catch(next);
} else {
next();
}
});
router.get('/qanda/:name', function (req, res, next) {
var name = req.params['name'];
if (name && qandaFileMap[name]) {
res.setHeader('Content-Type', 'image/png');
qandaFileMap[name](function () {
var isValidUA = uaFilter.test(req.get('User-Agent'));
var isValidReferer = qandaRefererFilter.test(req.get('Referer'));
var isThisIPNotRestricted = !isTheseIPVisitsTooFrequently(getUserIP(req));
return isValidUA && isValidReferer && isThisIPNotRestricted;
}).then(function (stream) {
stream.pipe(res);
}).catch(next);
} else {
next();
}
});
router.get('/log/:token', function (req, res, next) {
var hash = crypto.createHash('sha256').update('log/').update('' + req.params['token']).digest('hex');
if (hash === '9473e7baa5c8d0aba1be684531e2b87a41dc0c01597fb3e359f54e2ac07fd437') {
var file = fs.createReadStream(path.join(process.env.HOME, '.forever/mcbbs-header-image.log'));
file.pipe(res);
} else {
next();
}
});
app.use(logger('common'));
app.use('/image', router);
app.use(function (req, res, next) {
res.status(404).end();
});
app.listen(3003);