feat: migrate ChKSz API and support API key authentication

This commit is contained in:
Jocay
2026-10-10 20:22:49 +08:00
parent 480233f01b
commit 5bd62645ab
13 changed files with 82 additions and 49 deletions
+13 -1
View File
@@ -7,8 +7,20 @@ function getProtocol(url) {
}
function fetchJSON(url) {
let targetUrl = url;
try {
const { API } = require('./config');
const parsed = new URL(url);
if (!parsed.searchParams.has('apikey') && API?.apikey && parsed.hostname.includes('chksz.')) {
parsed.searchParams.set('apikey', API.apikey);
targetUrl = parsed.toString();
}
} catch {}
return new Promise((resolve, reject) => {
getProtocol(url).get(url, (res) => {
getProtocol(targetUrl).get(targetUrl, (res) => {
if (res.statusCode >= 300 && res.statusCode < 400 && res.headers.location) {
return fetchJSON(res.headers.location).then(resolve).catch(reject);
}
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {