Skip to content

Commit

Permalink
use post to print
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 10, 2017
1 parent de550b3 commit 38c7146
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 45 deletions.
20 changes: 20 additions & 0 deletions README.md
Expand Up @@ -2,8 +2,28 @@

> 咕咕机 API
## 特性

- 强制微博登录

- 打印频率控制

- 黑名单

- 打印文本长度控制

## 使用

`/login`: 登录微博

`/logout`: 登出微博

`/account`: 微博个人信息

`/print`: 打印

`/status`: 打印状态

## 搭建

需要环境:Node.js
Expand Down
10 changes: 8 additions & 2 deletions config.example.js
@@ -1,6 +1,12 @@
module.exports = {
ak: '',
ak: '', // 咕咕机开放平台
memobirdID: '',
useridentifying: '',
frequency: 5
appKey: '', // 微博开放平台
appSecret: '',
frequency: 5, // 每人每半个小时允许打印次数
maxLength: 500, // 最大打印长度
URL: '', // API 地址
redirectURL: '', // 登录成功转跳链接
allowOrigin: ['anotherhome.net', 'diygod.me'] // 允许的域
};
106 changes: 64 additions & 42 deletions routes/print.js
Expand Up @@ -17,56 +17,78 @@ module.exports = function (req, res) {
var type = query.type;
var content = query.content;

// check black ip
var blanklist = fs.readFileSync('blacklist').toString().split('\n');
if (blanklist.indexOf(ip.split(',')[0]) !== -1) {
logger.info(`Reject gugu print for black ip, IP: ${ip}`);
res.send(`{"code": 1, "msg": "抱歉,您已被加入黑名单"}`);
return;
}
req.on('data', dataListener);
req.on('end', endListener);

if (content.length > config.maxLength) {
logger.info(`Reject gugu print for max length, IP: ${ip}`);
res.send(`{"code": 2, "msg": "超出最大文本长度"}`);
return;
function dataListener (chunk) {
body += chunk;
}
function endListener () {
cleanListener();
try {
jsonStr = JSON.parse(body);
type = jsonStr.type;
content = jsonStr.content;
} catch (err) {
jsonStr = {};
}

// frequency limitation
if (reqIP[ip] && reqIP[ip] >= config.frequency) {
logger.info(`Reject gugu print for frequent operation, IP: ${ip}`);
res.send(`{"code": 3, "msg": "操作频繁,频繁次数过多会被拉入黑名单哦"}`);
return;
}
else {
if (reqIP[ip]) {
reqIP[ip]++;
// check black ip
var blanklist = fs.readFileSync('blacklist').toString().split('\n');
if (blanklist.indexOf(ip.split(',')[0]) !== -1) {
logger.info(`Reject gugu print for black ip, IP: ${ip}`);
res.send(`{"code": 1, "msg": "抱歉,您已被加入黑名单"}`);
return;
}

if (content.length > config.maxLength) {
logger.info(`Reject gugu print for max length, IP: ${ip}`);
res.send(`{"code": 2, "msg": "超出最大文本长度"}`);
return;
}

// frequency limitation
if (reqIP[ip] && reqIP[ip] >= config.frequency) {
logger.info(`Reject gugu print for frequent operation, IP: ${ip}`);
res.send(`{"code": 3, "msg": "操作频繁,频繁次数过多会被拉入黑名单哦"}`);
return;
}
else {
reqIP[ip] = 1;
if (reqIP[ip]) {
reqIP[ip]++;
}
else {
reqIP[ip] = 1;
}
setTimeout(function () {
reqIP[ip]--;
}, 1800000); // 30 min
}
setTimeout(function () {
reqIP[ip]--;
}, 1800000); // 30 min
}

if (!req.user) {
logger.info(`Reject gugu print for not login, IP: ${ip}`);
res.send(`{"code": 4, "msg": " 请先登录"}`);
return;
}
if (!req.user) {
logger.info(`Reject gugu print for not login, IP: ${ip}`);
res.send(`{"code": 4, "msg": " 请先登录"}`);
return;
}

logger.info(`gugu print ${type} ${content}, IP: ${ip}`);
logger.info(`gugu print ${type} ${content}, IP: ${ip}`);

switch (type) {
case '1':
gugu.printText(req.user.displayName + ': ' + content)
.then(printcontentid => res.send(`{"code": 0, "msg": "成功发送打印请求", "user": "${req.user.displayName}", "printcontentid": "${printcontentid}"}`));
break;
case '2':
gugu.printImage(content)
.then(printcontentid => res.send(`{"code": 0, "msg": "成功发送打印请求", "user": "${req.user.displayName}", "printcontentid": "${printcontentid}"}`));
break;
default:
res.send(`{"code": 1, "msg": "参数错误"}`);
}
}

switch (type) {
case '1':
gugu.printText(req.user.displayName + ': ' + content)
.then(printcontentid => res.send(`{"code": 0, "msg": "成功发送打印请求", "user": "${req.user.displayName}", "printcontentid": "${printcontentid}"}`));
break;
case '2':
gugu.printImage(content)
.then(printcontentid => res.send(`{"code": 0, "msg": "成功发送打印请求", "user": "${req.user.displayName}", "printcontentid": "${printcontentid}"}`));
break;
default:
res.send(`{"code": 1, "msg": "参数错误"}`);
function cleanListener () {
req.removeListener('data', dataListener);
req.removeListener('end', endListener);
}
};
2 changes: 1 addition & 1 deletion tools/passport.js
Expand Up @@ -13,7 +13,7 @@ passport.deserializeUser(function (obj, done) {
passport.use(new WeiboStrategy({
clientID: config.appKey,
clientSecret: config.appSecret,
callbackURL: config.callbackURL
callbackURL: config.URL + '/callback'
},
function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
Expand Down

0 comments on commit 38c7146

Please sign in to comment.