-
Notifications
You must be signed in to change notification settings - Fork 449
Expand file tree
/
Copy pathapp.js
More file actions
146 lines (125 loc) · 3.9 KB
/
app.js
File metadata and controls
146 lines (125 loc) · 3.9 KB
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
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}`)
}
})