refactor: Download API

Now the download APIs are moved into a new file
This commit is contained in:
Peng-YM
2022-06-30 12:19:43 +08:00
parent 9653b09844
commit bb87a6c41e
14 changed files with 231 additions and 220 deletions

View File

@@ -16,12 +16,16 @@ export default async function download(url, ua) {
});
const result = new Promise((resolve, reject) => {
http.get(url).then((resp) => {
const body = resp.body;
if (body.replace(/\s/g, '').length === 0)
reject(new Error('订阅内容为空!'));
else resolve(body);
});
http.get(url)
.then((resp) => {
const body = resp.body;
if (body.replace(/\s/g, '').length === 0)
reject(new Error('订阅内容为空!'));
else resolve(body);
})
.catch(() => {
reject(new Error(`无法下载 URL${url}`));
});
});
cache.set(id, result);

15
backend/src/utils/flow.js Normal file
View File

@@ -0,0 +1,15 @@
import { HTTP } from '@/vendor/open-api';
export async function getFlowHeaders(url) {
const http = HTTP();
const { headers } = await http.get({
url,
headers: {
'User-Agent': 'Quantumult%20X/1.0.30 (iPhone14,2; iOS 15.6)',
},
});
const subkey = Object.keys(headers).filter((k) =>
/SUBSCRIPTION-USERINFO/i.test(k),
)[0];
return headers[subkey];
}

View File

@@ -0,0 +1,23 @@
export function getPlatformFromHeaders(headers) {
const keys = Object.keys(headers);
let UA = '';
for (let k of keys) {
if (/USER-AGENT/i.test(k)) {
UA = headers[k];
break;
}
}
if (UA.indexOf('Quantumult%20X') !== -1) {
return 'QX';
} else if (UA.indexOf('Surge') !== -1) {
return 'Surge';
} else if (UA.indexOf('Decar') !== -1 || UA.indexOf('Loon') !== -1) {
return 'Loon';
} else if (UA.indexOf('Shadowrocket') !== -1) {
return 'Clash';
} else if (UA.indexOf('Stash') !== -1) {
return 'Stash';
} else {
return 'JSON';
}
}