-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwww.js
159 lines (153 loc) · 4.96 KB
/
www.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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const NodeMediaServer = require("node-media-server");
const request = require("request");
const port = 1308;
// const mainserver = "http://cyxsh.top";
const mainserver = "http://fqo3.site";
const subserver = ["http://cyxsh.top", "http://hejie.fqo3.site", "http://hld.cyxsh.top", "http://hnd.cyxsh.top", "http://fqo3.site"];
// 内蒙 贺捷家 范鸿喆服 王颢然家 我家
const uploadsubserver = [80, 80, 80, 40, 40];
const view = [];
const stream = 3256;
const backup = ["http://pdx.cyxsh.top", "http://lax.cyxsh.top"];
//开启推流服务器
const config = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 60,
ping_timeout: 30,
},
http: {
port: 8001,
allow_origin: "*",
},
};
const nms = new NodeMediaServer(config);
nms.run();
//初始化
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
//设置跨域响应头
function setHeaders(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
}
app.use(setHeaders);
//网页发送系统
app.use(function (req, res, next) {
if (req.path == "/index.html" || req.path == "/" || req.path == "/index") {
res.redirect("/index/index.html");
}
next();
});
app.use(express.static("PC"));
//留言系统
let comments = []; // Array to store comments
app.get("/admin", (req, res) => {
comments = [];
res.sendStatus(200);
});
app.get("/comments", (req, res) => {
res.json(comments);
// console.log(comments);
});
app.post("/comments", (req, res) => {
var { name, comment } = req.body;
comments.unshift({ name, comment });
res.status(201).json({ message: "Comment added successfully" });
const ip = req.connection.remoteAddress;
console.log(`message send ip: ${ip}`);
console.log(req.body);
});
//管理员密码系统
app.post("/admin/adm.html", (req, res) => {
const currentTime = new Date();
today = String(currentTime.getMonth() + 1) + String(currentTime.getDate());
pass = req.query.pass;
if (pass == today) {
res.status(200).send("viewers.html");
}
else {
res.status(200).send("密码错误");
}
});
const viewer = {
url: '',
viewer: 999
};
//自动分配
app.post("/api/url/:id", async (req, res) => {
viewer.viewer = 999;
var code = req.params;
viewer.url = `http://fqo3.site:8001/live/${code.id}.flv`;
const promises = subserver.map(async (num, index) => {
if (num == mainserver) {
await asksub(`http://127.0.0.1:8001/api/streams/live/${code.id}`, num, code.id, index);
}
else {
await asksub(`${num}:8001/api/streams/live/${code.id}`, num, code.id, index);
}
});
await Promise.all(promises);
const ipAddress = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
console.log(`为${ipAddress}分配使用${viewer.url}`);
// if(viewer.url.includes("cyxsh")) viewer.url=`http://192.168.1.8:8001/live/${code.id}.flv`
res.send(viewer.url);
});
async function asksub(url, domain, code, index) {
return new Promise((resolve, reject) => {
request.get({ url: url, timeout: 1000 }, function (error, response, body) {
if (!error && response.statusCode == 200) {
var helth = JSON.parse(body);
console.log(`${domain}观看人数: ${helth.viewers}人`);
if (helth.isLive && helth.viewers >= view[index]) {
console.log(`${domain}的观看人数已满,跳过分配`);
}
else if (helth.isLive && helth.viewers < viewer.viewer) {
viewer.url = `${domain}:8001/live/${code}.flv`;
viewer.viewer = helth.viewers;
// console.log("in"+domain);
}
else if (!helth.isLive) {
request.post({ url: `${domain}:1308/api/create/${code}`, timeout: 3000 }, (error, response) => {
if (!error && response.statusCode === 200) {
console.log(`【成功】为子服${domain}创建${code}流`);
} else {
console.log(`为子服${domain}创建${code}流【失败】`);
console.log(error || response.statusMessage);
}
});
}
} else {
console.log(`${domain}连接失败`);
}
resolve();
}).on('error', (error) => {
console.log(`无法连接至${domain},或连接超时`);
});
});
}
//开启服务器
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
uploadsubserver.map((num, index) => {
view[index] = Math.floor(num * 1024 / stream);
});
console.log(view);
});
//系统检查
function checkPC(req) {
var agentstr = req.headers['user-agent'].toLowerCase(); // nodejs
var agentreg = /(iphone|ipod|ipad|android|symbianos|windows phone|playbook|mobile)/;
var agentph = agentstr.match(agentreg);
if (agentph) {
return false;
} else {
return true;
}
}