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

Commit

Permalink
Merge branch 'master' of github.com:CodFrm/cxmooc-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Mar 28, 2019
2 parents 0984437 + 96c7171 commit 07ac599
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
28 changes: 27 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"body-parser": "^1.18.3",
"express": "^4.16.3",
"md5": "^2.2.1",
"mongodb": "^3.1.10"
"mongodb": "^3.1.10",
"redis": "^2.8.0"
}
}
22 changes: 17 additions & 5 deletions src/server/listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var path = require('path');
var fs = require('fs');
const http = require('http');
const https = require('https');
const redisCli = require('./redis');

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


var mooc = new moocModel();
var redis = new redisCli();

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

//在线人数中间件
app.use(function (req, res, next) {
var ip = getClientIp(req);
redis.onlineAdd(ip);
next();
});

app.get('/', function (req, res) {
ret = '<h2>欢迎使用超星慕课刷课插件</h2><p>这个服务器将会记录你正确的答题答案,并不会记录你的任何账号信息</p>';
ret += '<p>并且接口没有任何权限,全由插件提交上传,还请大家不要故意上传错误的答案 (๑• . •๑) </p><br/>';
Expand Down Expand Up @@ -143,11 +152,14 @@ function mergeAnswer(source, answer) {
}

app.get('/update', function (req, res) {
res.send({
version: config.version,
url: config.update,
enforce: config.enforce,
injection: config.injection
redis.onlineNum(function (err, data) {
res.send({
version: config.version,
url: config.update,
enforce: config.enforce,
injection: config.injection,
onlinenum: data
});
});
})

Expand Down
18 changes: 18 additions & 0 deletions src/server/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var redis = require('redis');

module.exports = function () {
var client = redis.createClient(6379,"127.0.0.1");
client.on("error", function (err) {
console.log("Redis error:" + err);
});
this.onlineAdd = function (ip) {
client.zadd("cxmooc:online", [Date.parse(new Date()), ip]);
}
this.onlineNum = function (call) {
var time = Date.parse(new Date());
client.zcount("cxmooc:online", [time - (5 * 60 * 1000), time], function (err, res) {
call(err, res)
});
}
return this
}

0 comments on commit 07ac599

Please sign in to comment.