-
Notifications
You must be signed in to change notification settings - Fork 30
/
app.js
233 lines (228 loc) · 7.81 KB
/
app.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var config = require("./config/auth");
var session = require('express-session');
var flash = require('connect-flash');
var jwt = require('jsonwebtoken');
var fs = require("fs");
var mongoose = require("mongoose");
var MongoStore = require('connect-mongo')(session);
var routes = require('./routes/index');
var app = express();
mongoose.connect("mongodb://" + config.dbuser + ":"+config.dbpassword+"@127.0.0.1/"+config.db);
var Setting = require('./models/setting');
var Fenfa = require("./models/fenfa");
var User = require('./models/user');
var Portal = require('./models/portal');
var moment = require('moment');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json({limit: '5mb'}));
app.use(express.urlencoded({limit: '5mb', extended: false }));
app.use(cookieParser());
app.use(session({
secret: config.secret,
resave: true,
saveUninitialized: false,
key: "hls",
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 30
}, //30day
store: new MongoStore({
url: 'mongodb://' + config.dbuser + ':'+config.dbpassword+'@127.0.0.1/'+config.db
})
}));
app.use("/videos/*/ts.key", function(req,res,next) {
Setting.find()
.exec(function(err, setting) {
if(err) {
console.log(err);
}
var antiurlarr = setting[0].antiurl;
if(antiurlarr[0]!="") {
if(antiurlarr.indexOf(req.headers.origin)!=-1){
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Methods", "POST, GET");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Content-Type");
}
next();
} else {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Methods", "POST, GET");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Headers", "Content-Type");
next();
}
})
});
app.use("/videos/:id/index.m3u8", openUsersystem, function (req, res, next) {
var id = req.params.id;
Setting.find()
.exec(function (err, setting) {
if (err) {
console.log(err);
}
if(setting[0].antikey!=""){
var token = req.query.token;
jwt.verify(token, setting[0].antikey, function (err, decoded) {
if (err) {
console.log(err);
res.statusCode = 404;
return res.send("对不起,您没有权限");
}
Fenfa.find()
.exec(function (err, fenfa) {
if (err) {
console.log(err);
}
if (fenfa[0].kaiguan == "on") {
var path = "./public/videos/" + id + "/index.m3u8";
var m3u8exists = fs.existsSync(path);
if (m3u8exists) {
var data = fs.readFileSync(path);
var datastring = data.toString('utf-8');
var m3u8arr = datastring.split("index");
var domains = fenfa[0].domains;
var domainslength = fenfa[0].domains.length;
var index = 0;
for (let i = 0; i < m3u8arr.length; i++) {
if (i > 0) {
if (index < domainslength) {
m3u8arr[i] = domains[index] + "/videos/" + id + "/index" + m3u8arr[i];
index++;
} else {
index = 1;
m3u8arr[i] = domains[0] + "/videos/" + id + "/index" + m3u8arr[i];
}
}
}
var newm3u8 = m3u8arr.join("");
res.send(newm3u8);
}
} else {
if (decoded.access == "view") {
if (req.usersystem) {
var path = "./public/videos/" + id + "/index.m3u8";
var m3u8exists = fs.existsSync(path);
if (m3u8exists) {
var data = fs.readFileSync(path);
var datastring = data.toString('utf-8');
var m3u8arr = datastring.split("index");
}
var newm3u8arr = [];
var length = m3u8arr.length>=18?18:m3u8arr.length;
for (let index = 0; index < length; index++) {
if(index == length-1) {
var lastm3u8 = m3u8arr[length-1];
var lastarr = lastm3u8.split("ts");
lastarr.pop();
lastarr.push("\n#EXT-X-ENDLIST\n");
newm3u8arr.push(lastarr.join("ts"));
} else {
newm3u8arr.push(m3u8arr[index]);
}
}
var newm3u8 = newm3u8arr.join("index");
if (req.session.leveluser) {
User.findOne({username: req.session.leveluser})
.exec(function(err, user) {
if(err) {
console.log(err);
}
if (user.level == 2 && moment(user.duedate).isAfter(Date.now())) {
next();
} else {
res.send(newm3u8);
}
})
} else {
res.send(newm3u8);
}
} else {
next();
}
}
}
})
})
} else {
next();
}
});
});
app.use(express.static(path.join(__dirname, 'public')));
app.use(function (req, res, next) {
res.locals.createPagination = function (pages, page) {
var url = require('url'),
qs = require('querystring'),
params = qs.parse(url.parse(req.url).query),
str = '',
list_len = 2,
total_list = list_len * 2 + 1,
j = 1,
pageNo = parseInt(page);
if (pageNo >= total_list) {
j = pageNo - list_len;
total_list = pageNo + list_len;
if (total_list > pages) {
total_list = pages;
}
} else {
j = 1;
if (total_list > pages) {
total_list = pages;
}
}
params.page = 0
for (j; j <= total_list; j++) {
params.page = j
clas = pageNo == j ? "active" : "no"
str += '<li class="' + clas + '"><a href="?' + qs.stringify(params) + '">' + j + '</a></li>'
}
return str
}
next();
});
app.use(flash());
// app.use(function (req, res, next) {
// res.setTimeout(480000, function () { // 4 minute timeout adjust for larger uploads
// console.log('Request has timed out.');
// res.send(408);
// });
// next();
// });
routes(app);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
function openUsersystem(req, res, next) {
Portal.find()
.exec(function(err, portals){
if(err) {
console.log(err);
}
if (portals[0].usersystem =='on'){
req.usersystem = true;
} else {
req.usersystem = false;
}
next();
})
}
module.exports = app;