Files
migu_video/app.js

147 lines
3.9 KiB
JavaScript

import http from "node:http"
import { host, pass, port, programInfoUpdateInterval, token, userId } from "./config.js";
import { getDateTimeStr } from "./utils/time.js";
import update from "./utils/updateData.js";
import { printBlue, printGreen, printMagenta, printRed } from "./utils/colorOut.js";
import { delay } from "./utils/fetchList.js";
import { channel, interfaceStr } from "./utils/appUtils.js";
// 运行时长
var hours = 0
let loading = false
const server = http.createServer(async (req, res) => {
while (loading) {
await delay(50)
}
loading = true
// 获取请求方法、URL 和请求头
let { method, url, headers } = req;
// 身份认证
if (pass != "") {
const urlSplit = url.split("/")
if (urlSplit[1] != pass) {
printRed(`身份认证失败`)
res.writeHead(200, { 'Content-Type': 'application/json;charset=UTF-8' });
res.end(`身份认证失败`); // 发送文件内容
loading = false
return
} else {
printGreen("身份认证成功")
// 有密码且传入用户信息
if (urlSplit.length > 3) {
url = url.substring(pass.length + 1)
} else {
url = urlSplit.length == 2 ? "/" : "/" + urlSplit[urlSplit.length - 1]
}
}
}
let urlToken = ""
let urlUserId = ""
// 匹配是否存在用户信息
if (/\/{1}[^\/\s]{1,}\/{1}[^\/\s]{1,}/.test(url)) {
const urlSplit = url.split("/")
if (urlSplit.length >= 3) {
urlUserId = urlSplit[1]
urlToken = urlSplit[2]
url = urlSplit.length == 3 ? "/" : "/" + urlSplit[urlSplit.length - 1]
}
} else {
urlUserId = userId
urlToken = token
}
// printGreen("")
printMagenta("请求地址:" + url)
if (method != "GET") {
res.writeHead(200, { 'Content-Type': 'application/json;charset=UTF-8' });
res.end(JSON.stringify({
data: '请使用GET请求',
}));
printRed(`使用非GET请求:${method}`)
loading = false
return
}
const interfaceList = "/,/interface.txt,/m3u,/txt,/playback.xml"
// 接口
if (interfaceList.indexOf(url) !== -1) {
const interfaceObj = interfaceStr(url, headers, urlUserId, urlToken)
if (interfaceObj.content == null) {
interfaceObj.content = "获取失败"
}
// 设置响应头
res.setHeader('Content-Type', interfaceObj.contentType);
if (url == "/m3u") {
res.setHeader('content-disposition', "inline; filename=\"interface.m3u\"");
}
res.statusCode = 200;
res.end(interfaceObj.content); // 发送文件内容
loading = false
return
}
// 频道
const result = await channel(url, urlUserId, urlToken)
// 结果异常
if (result.code != 302) {
printRed(result.desc)
res.writeHead(result.code, {
'Content-Type': 'application/json;charset=UTF-8',
});
res.end(result.desc)
loading = false
return
}
res.writeHead(result.code, {
'Content-Type': 'application/json;charset=UTF-8',
location: result.playURL
});
res.end()
loading = false
})
server.listen(port, async () => {
const updateInterval = parseInt(programInfoUpdateInterval)
// 更新
setInterval(async () => {
printBlue(`准备更新文件 ${getDateTimeStr(new Date())}`)
hours += updateInterval
try {
await update(hours)
} catch (error) {
console.log(error)
printRed("更新失败")
}
printBlue(`当前已运行${hours}小时`)
}, updateInterval * 60 * 60 * 1000);
try {
// 初始化数据
await update(hours)
} catch (error) {
console.log(error)
printRed("更新失败")
}
printGreen(`本地地址: http://localhost:${port}${pass == "" ? "" : "/" + pass}`)
printGreen(`本程序完全免费,如果您是通过付费渠道获取,那么恭喜你成功被骗了`)
printGreen("开源地址: https://github.com/develop202/migu_video 欢迎issue 感谢star")
if (host != "") {
printGreen(`自定义地址: ${host}${pass == "" ? "" : "/" + pass}`)
}
})