Skip to content
This repository was archived by the owner on Apr 25, 2022. It is now read-only.

Commit 07ac599

Browse files
committed
Merge branch 'master' of github.com:CodFrm/cxmooc-tools
2 parents 0984437 + 96c7171 commit 07ac599

4 files changed

Lines changed: 64 additions & 7 deletions

File tree

package-lock.json

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"body-parser": "^1.18.3",
3232
"express": "^4.16.3",
3333
"md5": "^2.2.1",
34-
"mongodb": "^3.1.10"
34+
"mongodb": "^3.1.10",
35+
"redis": "^2.8.0"
3536
}
3637
}

src/server/listen.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var path = require('path');
88
var fs = require('fs');
99
const http = require('http');
1010
const https = require('https');
11+
const redisCli = require('./redis');
1112

1213
var privateKey = fs.readFileSync(path.join(__dirname, './certificate/private.key'), 'utf8');
1314
var certificate = fs.readFileSync(path.join(__dirname, './certificate/file.crt'), 'utf8');
@@ -32,6 +33,7 @@ httpsServer.listen(SSLPORT, function () {
3233

3334

3435
var mooc = new moocModel();
36+
var redis = new redisCli();
3537

3638
app.use(bodyParser.json());
3739
app.use(bodyParser.urlencoded({
@@ -49,6 +51,13 @@ app.use(express.static(path.join(__dirname, 'static'), {
4951
maxage: '7d'
5052
}));
5153

54+
//在线人数中间件
55+
app.use(function (req, res, next) {
56+
var ip = getClientIp(req);
57+
redis.onlineAdd(ip);
58+
next();
59+
});
60+
5261
app.get('/', function (req, res) {
5362
ret = '<h2>欢迎使用超星慕课刷课插件</h2><p>这个服务器将会记录你正确的答题答案,并不会记录你的任何账号信息</p>';
5463
ret += '<p>并且接口没有任何权限,全由插件提交上传,还请大家不要故意上传错误的答案 (๑• . •๑) </p><br/>';
@@ -143,11 +152,14 @@ function mergeAnswer(source, answer) {
143152
}
144153

145154
app.get('/update', function (req, res) {
146-
res.send({
147-
version: config.version,
148-
url: config.update,
149-
enforce: config.enforce,
150-
injection: config.injection
155+
redis.onlineNum(function (err, data) {
156+
res.send({
157+
version: config.version,
158+
url: config.update,
159+
enforce: config.enforce,
160+
injection: config.injection,
161+
onlinenum: data
162+
});
151163
});
152164
})
153165

src/server/redis.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var redis = require('redis');
2+
3+
module.exports = function () {
4+
var client = redis.createClient(6379,"127.0.0.1");
5+
client.on("error", function (err) {
6+
console.log("Redis error:" + err);
7+
});
8+
this.onlineAdd = function (ip) {
9+
client.zadd("cxmooc:online", [Date.parse(new Date()), ip]);
10+
}
11+
this.onlineNum = function (call) {
12+
var time = Date.parse(new Date());
13+
client.zcount("cxmooc:online", [time - (5 * 60 * 1000), time], function (err, res) {
14+
call(err, res)
15+
});
16+
}
17+
return this
18+
}

0 commit comments

Comments
 (0)