mirror of
https://github.com/DIYgod/DPlayer
synced 2024-11-22 10:02:59 +00:00
Better api: log; blacklist; bug fixing
This commit is contained in:
parent
2a873bc91a
commit
f1a9b8664f
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
node_modules
|
||||
demo2
|
||||
npm-debug.log
|
||||
DPlayer.log
|
@ -7,7 +7,7 @@
|
||||
[![Travis](https://img.shields.io/travis/DIYgod/DPlayer.svg?style=flat-square)](https://travis-ci.org/DIYgod/DPlayer)
|
||||
[![%e2%9d%a4](https://img.shields.io/badge/made%20with-%e2%9d%a4-ff69b4.svg?style=flat-square)](https://www.anotherhome.net/)
|
||||
|
||||
Wow, such a lovely HTML5 danmaku video player
|
||||
> Wow, such a lovely HTML5 danmaku video player
|
||||
|
||||
## Introduction
|
||||
|
||||
@ -43,8 +43,6 @@ $ npm run build
|
||||
|
||||
- [ ] 快捷键: 空格暂停,双击全屏, 方向键控制进度
|
||||
|
||||
- [ ] 弹幕相关
|
||||
|
||||
- [ ] icon 动画
|
||||
|
||||
- [ ] 右键
|
||||
|
@ -66,7 +66,8 @@
|
||||
},
|
||||
danmaku: {
|
||||
id: '9E2E3368B56CDBB4',
|
||||
api: 'http://dplayer.daoapp.io/'
|
||||
api: 'https://dplayer.daoapp.io/',
|
||||
token: 'tokendemo'
|
||||
}
|
||||
});
|
||||
dp.init();
|
||||
|
4
dist/DPlayer.min.js
vendored
4
dist/DPlayer.min.js
vendored
File diff suppressed because one or more lines are too long
2
dist/DPlayer.min.js.map
vendored
2
dist/DPlayer.min.js.map
vendored
File diff suppressed because one or more lines are too long
3
nodejs/blacklist
Normal file
3
nodejs/blacklist
Normal file
@ -0,0 +1,3 @@
|
||||
Can be username and IP
|
||||
username
|
||||
0.0.0.0
|
167
nodejs/index.js
167
nodejs/index.js
@ -1,9 +1,39 @@
|
||||
var url = require('url');
|
||||
var fs = require('fs');
|
||||
var mongoose = require('mongoose');
|
||||
var mongodbUrl = 'mongodb://' + process.env.MONGODB_USERNAME + ':' + process.env.MONGODB_PASSWORD + '@' + process.env.MONGODB_PORT_27017_TCP_ADDR + ':' + process.env.MONGODB_PORT_27017_TCP_PORT + '/' + process.env.MONGODB_INSTANCE_NAME;
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
|
||||
var log4js = require('log4js');
|
||||
log4js.configure({
|
||||
appenders: [
|
||||
{
|
||||
type: "file",
|
||||
filename: 'DPlayer.log',
|
||||
maxLogSize: 20480,
|
||||
backups: 3,
|
||||
category: [ 'DPlayer','console' ]
|
||||
},
|
||||
{
|
||||
type: "console"
|
||||
}
|
||||
],
|
||||
replaceConsole: true
|
||||
});
|
||||
var logger = log4js.getLogger('DPlayer');
|
||||
logger.setLevel('INFO');
|
||||
logger.info(`🍻 DPlayer start! Cheers!`);
|
||||
|
||||
var postIP = [];
|
||||
|
||||
var mongodbUrl;
|
||||
if (process.env.MONGODB_USERNAME && process.env.MONGODB_PASSWORD && process.env.MONGODB_PORT_27017_TCP_ADDR && process.env.MONGODB_PORT_27017_TCP_PORT && process.env.MONGODB_INSTANCE_NAME) {
|
||||
mongodbUrl = 'mongodb://' + process.env.MONGODB_USERNAME + ':' + process.env.MONGODB_PASSWORD + '@' + process.env.MONGODB_PORT_27017_TCP_ADDR + ':' + process.env.MONGODB_PORT_27017_TCP_PORT + '/' + process.env.MONGODB_INSTANCE_NAME;
|
||||
}
|
||||
else {
|
||||
mongodbUrl = 'mongodb://localhost:27017/danmaku';
|
||||
}
|
||||
|
||||
var danmakuSchema = new mongoose.Schema({
|
||||
player: String,
|
||||
author: String,
|
||||
@ -27,16 +57,23 @@ app.all('*', function(req, res, next) {
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/', function(req, res) {
|
||||
app.get('/', function (req, res) {
|
||||
var ip = req.headers['x-forwarded-for'] ||
|
||||
req.connection.remoteAddress ||
|
||||
req.socket.remoteAddress ||
|
||||
req.connection.socket.remoteAddress;
|
||||
logger.info(`GET form IP: ${ip}`);
|
||||
|
||||
mongoose.connect(mongodbUrl);
|
||||
var db = mongoose.connection;
|
||||
db.on('error',console.error);
|
||||
db.on('error', errorListener);
|
||||
|
||||
var id = url.parse(req.url,true).query.id;
|
||||
db.once('open', function() {
|
||||
cleanListener();
|
||||
danmaku.find({player: id}, function (err, data) {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
logger.error(err);
|
||||
}
|
||||
|
||||
var json = `{"code": 1,"danmaku":[`;
|
||||
@ -47,32 +84,138 @@ app.get('/', function(req, res) {
|
||||
}
|
||||
}
|
||||
json += `]}`;
|
||||
res.write(json);
|
||||
res.end();
|
||||
res.send(json);
|
||||
db.close();
|
||||
})
|
||||
});
|
||||
|
||||
function errorListener (err) {
|
||||
cleanListener();
|
||||
logger.error(err);
|
||||
res.send(`{"code": 0, "msg": "Error happens, please contact system administrator."}`);
|
||||
}
|
||||
|
||||
function cleanListener () {
|
||||
db.removeListener('error', errorListener);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/', function (req, res) {
|
||||
var body = '', jsonStr;
|
||||
req.on('data', function (chunk) {
|
||||
var body = '';
|
||||
var jsonStr;
|
||||
var db;
|
||||
var ip = req.headers['x-forwarded-for'] ||
|
||||
req.connection.remoteAddress ||
|
||||
req.socket.remoteAddress ||
|
||||
req.connection.socket.remoteAddress;
|
||||
|
||||
// check black ip
|
||||
var blanklist = fs.readFileSync('blacklist').toString().split('\n');
|
||||
if (blanklist.indexOf(ip) !== -1) {
|
||||
logger.info(`Reject POST form ${ip} for black ip.`);
|
||||
res.send(`{"code": -1, "msg": "Rejected for black ip."}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// frequency limitation
|
||||
if (postIP.indexOf(ip) !== -1) {
|
||||
logger.info(`Reject POST form ${ip} for frequent operation.`);
|
||||
res.send(`{"code": -2, "msg": "Rejected for frequent operation."}`);
|
||||
return;
|
||||
}
|
||||
else {
|
||||
postIP.push(ip);
|
||||
setTimeout(function () {
|
||||
postIP.splice(0, 1);
|
||||
}, 1000);
|
||||
}
|
||||
|
||||
req.on('data', dataListener);
|
||||
req.on('end', endListener);
|
||||
|
||||
function dataListener (chunk) {
|
||||
body += chunk;
|
||||
});
|
||||
req.on('end', function () {
|
||||
}
|
||||
function endListener () {
|
||||
cleanListener();
|
||||
try {
|
||||
jsonStr = JSON.parse(body);
|
||||
} catch (err) {
|
||||
jsonStr = null;
|
||||
}
|
||||
|
||||
var dan = new danmaku({player: jsonStr.player, author: jsonStr.author, time: jsonStr.time, text: jsonStr.text, color: jsonStr.color, type: jsonStr.type});
|
||||
// check data
|
||||
console.log(jsonStr.player, jsonStr.author, jsonStr.time, jsonStr.text, jsonStr.color, jsonStr.type);
|
||||
if (jsonStr.player === undefined
|
||||
|| jsonStr.author === undefined
|
||||
|| jsonStr.time === undefined
|
||||
|| jsonStr.text === undefined
|
||||
|| jsonStr.color === undefined
|
||||
|| jsonStr.type === undefined) {
|
||||
logger.info(`Reject POST form ${ip} for illegal data: ${JSON.stringify(jsonStr)}`);
|
||||
res.send(`{"code": -3, "msg": "Rejected for illegal data"}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// check token: set it yourself
|
||||
function checkToken (token) {
|
||||
return true;
|
||||
}
|
||||
if (!checkToken(jsonStr.token)) {
|
||||
logger.info(`Rejected POST form ${ip} for illegal token: ${jsonStr.token}`);
|
||||
res.send(`{"code": -4, "msg": "Rejected for illegal token: ${jsonStr.token}"}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// check black username
|
||||
if (blanklist.indexOf(jsonStr.author) !== -1) {
|
||||
logger.info(`Reject POST form ${jsonStr.author} for black user.`);
|
||||
res.send(`{"code": -5, "msg": "Rejected for black user."}`);
|
||||
return;
|
||||
}
|
||||
|
||||
logger.info(`POST form ${ip}, data: ${JSON.stringify(jsonStr)}`);
|
||||
|
||||
mongoose.connect(mongodbUrl);
|
||||
db = mongoose.connection;
|
||||
db.on('error', errorListener);
|
||||
db.once('open', function() {
|
||||
cleandbListener();
|
||||
|
||||
var dan = new danmaku({
|
||||
player: jsonStr.player,
|
||||
author: jsonStr.author,
|
||||
time: jsonStr.time,
|
||||
text: jsonStr.text,
|
||||
color: jsonStr.color,
|
||||
type: jsonStr.type
|
||||
});
|
||||
dan.save(function (err, d) {
|
||||
if (err){
|
||||
console.error(err);
|
||||
logger.error(err);
|
||||
res.send(`{"code": 0, "msg": "Error happens, please contact system administrator."}`);
|
||||
}
|
||||
else {
|
||||
res.send(`{"code": 1, "data": ${JSON.stringify(d)}}`);
|
||||
}
|
||||
db.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function errorListener (err) {
|
||||
cleandbListener();
|
||||
logger.error(err);
|
||||
res.send(`{"code": 0, "msg": "Error happens, please contact system administrator."}`);
|
||||
}
|
||||
|
||||
function cleandbListener () {
|
||||
db.removeListener('error', errorListener);
|
||||
}
|
||||
function cleanListener () {
|
||||
req.removeListener('data', dataListener);
|
||||
req.removeListener('end', endListener);
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(1207);
|
@ -8,6 +8,7 @@
|
||||
"devDependencies": {},
|
||||
"dependencies": {
|
||||
"express": "^4.13.4",
|
||||
"log4js": "^0.6.36",
|
||||
"mongoose": "^4.1.9"
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dplayer",
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"description": "Wow, such a lovely HTML5 danmaku video player",
|
||||
"main": "dist/DPlayer.min.js",
|
||||
"scripts": {
|
||||
|
@ -804,6 +804,41 @@
|
||||
const commentSettingBox = this.element.getElementsByClassName('dplayer-comment-setting-box')[0];
|
||||
const commentSendIcon = this.element.getElementsByClassName('dplayer-send-icon')[0];
|
||||
|
||||
const sendComment = () => {
|
||||
// text can't be empty
|
||||
if (!commentInput.value.replace(/^\s+|\s+$/g, '')) {
|
||||
alert('要输入弹幕内容啊喂!');
|
||||
return;
|
||||
}
|
||||
|
||||
const danmakuData = {
|
||||
token: this.option.danmaku.token,
|
||||
player: this.option.danmaku.id,
|
||||
author: 'DIYgod',
|
||||
time: this.audio.currentTime,
|
||||
text: commentInput.value,
|
||||
color: this.element.querySelector('input[name="dplayer-danmaku-color"]:checked').value,
|
||||
type: this.element.querySelector('input[name="dplayer-danmaku-type"]:checked').value
|
||||
};
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === 4) {
|
||||
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
|
||||
console.log(JSON.parse(xhr.responseText));
|
||||
}
|
||||
else {
|
||||
console.log('Request was unsuccessful: ' + xhr.status);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.open('post', this.option.danmaku.api, true);
|
||||
xhr.send(JSON.stringify(danmakuData));
|
||||
|
||||
commentInput.value = '';
|
||||
closeComment();
|
||||
this.danmakuIn(danmakuData.text, danmakuData.color, danmakuData.type);
|
||||
};
|
||||
|
||||
const closeCommentSetting = () => {
|
||||
if (commentSettingBox.classList.contains('dplayer-comment-setting-open')) {
|
||||
commentSettingBox.classList.remove('dplayer-comment-setting-open');
|
||||
@ -861,40 +896,15 @@
|
||||
commentInput.addEventListener('click', () => {
|
||||
closeCommentSetting();
|
||||
});
|
||||
|
||||
commentSendIcon.addEventListener('click', () => {
|
||||
// text can't be empty
|
||||
if (!commentInput.value.replace(/^\s+|\s+$/g, '')) {
|
||||
alert('要输入弹幕内容啊喂!');
|
||||
return;
|
||||
commentInput.addEventListener('keydown', (e) => {
|
||||
const event = e || window.event;
|
||||
if (event.keyCode === 13) {
|
||||
sendComment();
|
||||
}
|
||||
|
||||
const danmakuData = {
|
||||
player: this.option.danmaku.id,
|
||||
time: this.audio.currentTime,
|
||||
text: commentInput.value,
|
||||
color: this.element.querySelector('input[name="dplayer-danmaku-color"]:checked').value,
|
||||
type: this.element.querySelector('input[name="dplayer-danmaku-type"]:checked').value
|
||||
};
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.onreadystatechange = () => {
|
||||
if (xhr.readyState === 4) {
|
||||
if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
|
||||
console.log(JSON.parse(xhr.responseText));
|
||||
}
|
||||
else {
|
||||
console.log('Request was unsuccessful: ' + xhr.status);
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.open('post', this.option.danmaku.api, true);
|
||||
xhr.send(JSON.stringify(danmakuData));
|
||||
|
||||
commentInput.value = '';
|
||||
closeComment();
|
||||
this.danmakuIn(danmakuData.text, danmakuData.color, danmakuData.type);
|
||||
});
|
||||
|
||||
commentSendIcon.addEventListener('click', sendComment);
|
||||
|
||||
|
||||
/**
|
||||
* full screen
|
||||
@ -982,12 +992,12 @@
|
||||
this.event[name].push(func);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||
module.exports = DPlayer
|
||||
}
|
||||
else {
|
||||
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
|
||||
module.exports = DPlayer;
|
||||
}
|
||||
else {
|
||||
window.DPlayer = DPlayer;
|
||||
}
|
||||
}
|
||||
})();
|
Loading…
Reference in New Issue
Block a user