-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (189 loc) · 6.1 KB
/
Copy pathindex.js
File metadata and controls
214 lines (189 loc) · 6.1 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
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
const schedule = require("node-schedule");
const axios = require("axios");
const OSS = require("ali-oss");
const fs = require("fs");
const path = require("path");
const stream = require("stream");
const { promisify } = require("util");
const Core = require('@alicloud/pop-core');
require("dotenv").config();
const pipeline = promisify(stream.pipeline);
// 配置阿里云OSS客户端
const ossClient = new OSS({
region: process.env.OSS_REGION,
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
bucket: process.env.OSS_BUCKET,
});
// 配置阿里云CDN客户端
const cdnClient = new Core({
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
endpoint: 'https://cdn.aliyuncs.com',
apiVersion: '2018-05-10'
});
// 要下载的资源
const resources = [
{
url: "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.dat",
filename: "clash-rules/geoip.dat",
},
{
url: "https://testingcf.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geosite.dat",
filename: "clash-rules/geosite.dat",
},
{
url: "https://fastly.jsdelivr.net/gh/MetaCubeX/meta-rules-dat@release/geoip.metadb",
filename: "clash-rules/geoip.metadb",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/direct.txt",
filename: "clash-rules/direct.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/proxy.txt",
filename: "clash-rules/proxy.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/reject.txt",
filename: "clash-rules/reject.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/private.txt",
filename: "clash-rules/private.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/apple.txt",
filename: "clash-rules/apple.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/icloud.txt",
filename: "clash-rules/icloud.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/google.txt",
filename: "clash-rules/google.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/gfw.txt",
filename: "clash-rules/gfw.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/tld-not-cn.txt",
filename: "clash-rules/tld-not-cn.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/telegramcidr.txt",
filename: "clash-rules/telegramcidr.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/lancidr.txt",
filename: "clash-rules/lancidr.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/cncidr.txt",
filename: "clash-rules/cncidr.txt",
},
{
url: "https://raw.githubusercontent.com/Loyalsoldier/clash-rules/release/applications.txt",
filename: "clash-rules/applications.txt",
},
];
// 下载文件
async function downloadFile(url, filename) {
const response = await axios({
method: "GET",
url: url,
responseType: "stream",
});
const filePath = path.join(__dirname, "downloads", filename);
// 确保目录存在
await fs.promises.mkdir(path.dirname(filePath), { recursive: true });
const writer = fs.createWriteStream(filePath);
try {
await pipeline(response.data, writer);
console.log(`文件下载成功: ${filename}`);
} catch (error) {
console.error(`文件下载失败: ${filename}`, error);
throw error;
} finally {
writer.close();
}
// 验证文件是否成功下载
if (!fs.existsSync(filePath) || fs.statSync(filePath).size === 0) {
throw new Error(`文件下载失败或为空: ${filename}`);
}
}
// 上传文件到OSS
async function uploadToOSS(filename) {
try {
const result = await ossClient.put(
filename,
path.join(__dirname, "downloads", filename)
);
console.log(`上传成功: ${filename}`);
return result;
} catch (error) {
console.error(`上传失败: ${filename}`, error);
}
}
// 刷新阿里云CDN缓存
async function refreshCDNCache(refreshDirectories) {
try {
const cdnUrl = process.env.CDN_URL;
for (const directory of refreshDirectories) {
const refreshUrl = `${cdnUrl}/${directory}/`;
const params = {
"ObjectPath": refreshUrl,
"ObjectType": "Directory"
};
const requestOption = {
method: 'POST'
};
const result = await cdnClient.request('RefreshObjectCaches', params, requestOption);
console.log(`CDN缓存刷新成功: ${refreshUrl}`, result);
}
} catch (error) {
console.error('CDN缓存刷新失败:', error);
}
}
// 主任务
async function main() {
console.log("开始下载和上传任务...");
const directories = new Set();
for (const resource of resources) {
try {
await downloadFile(resource.url, resource.filename);
console.log(`下载成功: ${resource.filename}`);
await uploadToOSS(resource.filename);
// 删除本地文件
fs.unlinkSync(path.join(__dirname, "downloads", resource.filename));
// 提取目录
const directory = path.dirname(resource.filename);
directories.add(directory);
} catch (error) {
console.error(`处理 ${resource.filename} 时出错:`, error);
}
}
// 所有文件上传完成后刷新CDN缓存
await refreshCDNCache(Array.from(directories));
console.log("任务完成");
}
// 项目启动时执行一次
console.log("项目已启动,立即执行任务...");
main()
.then(() => {
console.log("初始任务完成");
// 设置每天凌晨2点执行的定时任务
const job = schedule.scheduleJob("0 2 * * *", main);
console.log("定时任务已设置,等待执行...");
// 优雅退出
process.on("SIGTERM", () => {
console.log("收到SIGTERM信号,正在优雅退出...");
job.cancel();
process.exit(0);
});
})
.catch((err) => {
console.error("初始任务执行失败:", err);
});