Windows 上的安卓应用运行器:双击 APK 即装,应用以独立窗口与桌面图标运行,自持 QEMU 运行时
'use strict';
/**
* 双源(GitHub / Gitee)+ 镜像前缀的统一下载来源管理。
*
* 国内直连 GitHub 有两个坑:
* 1. api.github.com 经常几十秒才响应,甚至直接超时 —— 表现是「检查更新转圈」;
* 2. 资产下载(objects.githubusercontent.com)单连接限速,常在 100KB/s 上下 ——
* 1.5 GB 的运行时包要下 4 个小时。
*
* 所以这里不预设「谁是主源」,而是每次用真实字节测速:
* - 版本信息:GitHub / Gitee 两边**并发**问,谁先给出更高版本用谁的;
* - 下载地址:把「GitHub 直连 / Gitee 同名资产 / 镜像前缀拼接」全部列出来,
* 每个地址真下 128KB,按实测速度排序,取最快的那个。
*
* 测速结果在内存里缓存 30 分钟(同一进程内反复检查更新/下多个组件不必重复测)。
*/
const https = require('https');
const http = require('http');
const { URL } = require('url');
const { cmpVer } = require('./version');
const OWNER_GH = 'RoutineMiku';
const OWNER_GITEE = 'daily-miku';
const REPO = 'apkstart';
const SOURCES = {
github: {
id: 'github',
name: 'GitHub',
api: `https://api.github.com/repos/${OWNER_GH}/${REPO}`,
web: `https://github.com/${OWNER_GH}/${REPO}`,
},
gitee: {
id: 'gitee',
name: 'Gitee',
api: `https://gitee.com/api/v5/repos/${OWNER_GITEE}/${REPO}`,
web: `https://gitee.com/${OWNER_GITEE}/${REPO}`,
},
};
/** 已知可用的 GitHub 加速前缀。只在用户开启 autoMirror 且未自定义时参与测速。 */
const KNOWN_MIRRORS = [
'https://ghfast.top/',
'https://gh-proxy.com/',
];
/**
* 一次测速的采样字节数。
* 128KB 太小:握手 + TLS 就有几百毫秒,测出来的「速度」其实主要在测延迟,
* 会把「延迟高但带宽大」的源误判成慢。256KB 能把两者分开,流量也可以接受。
*/
const SAMPLE_BYTES = 256 * 1024;
function openUrl(url, { timeout = 10000, headers = {}, redirects = 0, followRedirect = true } = {}) {
return new Promise((resolve, reject) => {
if (redirects > 6) return reject(new Error('重定向次数过多'));
let u;
try { u = new URL(url); } catch (_) { return reject(new Error('地址无效')); }
const mod = u.protocol === 'http:' ? http : https;
const req = mod.request(url, {
method: 'GET',
timeout,
headers: {
'User-Agent': 'ApkStart/0.3 (Windows)',
'Accept-Encoding': 'identity',
...headers,
},
}, (res) => {
if ([301, 302, 303, 307, 308].includes(res.statusCode) && res.headers.location && followRedirect) {
res.resume();
const next = new URL(res.headers.location, url).href;
openUrl(next, { timeout, headers, redirects: redirects + 1, followRedirect }).then(resolve, reject);
return;
}
resolve(res);
});
req.on('timeout', () => req.destroy(new Error('连接超时')));
req.on('error', reject);
req.end();
});
}
/** 取一小段 JSON(带重定向、超时、大小上限) */
async function getJson(url, timeout = 10000) {
const res = await openUrl(url, { timeout });
if (res.statusCode < 200 || res.statusCode >= 300) {
res.resume();
throw new Error(`${new URL(url).host} ${res.statusCode}`);
}
const chunks = [];
let len = 0;
await new Promise((resolve, reject) => {
res.on('data', (c) => {
len += c.length;
if (len > 4 * 1024 * 1024) { res.destroy(); resolve(); return; }
chunks.push(c);
});
res.on('end', resolve);
res.on('error', reject);
});
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
}
/**
* 真下载一小段字节来测速度。
* @returns {Promise<{url:string, ok:boolean, speed:number, got:number, ms:number, error?:string}>}
*/
function measure(url, { bytes = SAMPLE_BYTES, timeoutMs = 9000 } = {}) {
return new Promise((resolve) => {
const t0 = Date.now();
let firstAt = 0;
let got = 0;
let settled = false;
let req = null;
const done = (ok, error) => {
if (settled) return;
settled = true;
try { req?.destroy(); } catch (_) { /* ignore */ }
const now = Date.now();
// 吞吐只算「开始收到字节之后」的时间:TLS 握手 + DNS 那几百毫秒是固定开销,
// 对 1.4GB 的包几乎不影响总耗时,混进速度里会把判断带偏。
const span = Math.max(1, now - (firstAt || t0));
resolve({
url,
ok: ok && got > 0,
got,
ms: now - t0,
ttfb: firstAt ? firstAt - t0 : 0,
speed: ok && got > 0 ? (got / span) * 1000 : 0,
error,
});
};
const timer = setTimeout(() => done(got > 0), timeoutMs);
if (timer.unref) timer.unref();
openUrl(url, {
timeout: timeoutMs,
headers: { Range: `bytes=0-${bytes - 1}` },
}).then((res) => {
req = res;
if (res.statusCode >= 400) {
res.resume();
clearTimeout(timer);
done(false, `HTTP ${res.statusCode}`);
return;
}
res.on('data', (c) => {
if (!firstAt) firstAt = Date.now();
got += c.length;
if (got >= bytes) { clearTimeout(timer); done(true); }
});
// 资产本身比样本还小:读到 EOF 也算成功
res.on('end', () => { clearTimeout(timer); done(got > 0); });
res.on('error', (e) => { clearTimeout(timer); done(false, e.message); });
}).catch((e) => { clearTimeout(timer); done(false, e.message); });
});
}
/** 把 GitHub 资产地址套上镜像前缀 */
function withMirror(url, prefix) {
if (!prefix || !/^https?:\/\//i.test(prefix)) return '';
const p = prefix.endsWith('/') ? prefix : `${prefix}/`;
return `${p}${url}`;
}
/**
* Gitee 发行版附件单文件上限 100MB,安装包 105~110MB 装不下,所以发布时切成
* xxx.zip.part1 / xxx.zip.part2。这里把分片还原成一个逻辑资产:
* { name: 'xxx.zip', size: 总和, parts: [{index, size, urls:[...]}] }
* 分片必须从 part1 开始连续,否则视为残缺、丢弃(宁可用整包,也不下一个坏包)。
*/
function groupParts(assets) {
const groups = new Map();
const out = [];
for (const a of assets) {
const m = /^(.*)\.part(\d+)$/i.exec(String(a.name || ''));
if (!m) { out.push(a); continue; }
const base = m[1];
const idx = Number(m[2]);
let g = groups.get(base);
if (!g) {
g = { name: base, size: 0, parts: [], partial: true };
groups.set(base, g);
out.push(g);
}
let p = g.parts.find((x) => x.index === idx);
if (!p) {
p = { index: idx, size: Number(a.size) || 0, urls: [] };
g.parts.push(p);
}
if (a.url && !p.urls.includes(a.url)) p.urls.push(a.url);
if (!p.size) p.size = Number(a.size) || 0;
}
for (const g of groups.values()) {
g.parts.sort((x, y) => x.index - y.index);
const contiguous = g.parts.every((p, i) => p.index === i + 1);
g.size = g.parts.reduce((n, p) => n + (Number(p.size) || 0), 0);
// 注意:Gitee 的发行版资产条目只有 name/url,**没有 size**。
// 以「size>0」作为完整性条件会把整组分片判成残缺而丢弃(实测踩过),
// 所以这里只要求「分片连续 + 每个分片有地址」;大小未知时进度按已收字节显示。
if (!contiguous || !g.parts.every((p) => p.urls.length)) g.broken = true;
}
return { list: out.filter((a) => !a.broken), broken: [...groups.values()].filter((g) => g.broken).map((g) => g.name) };
}
/** mirrors 列表:用户自定义优先,其次内置(autoMirror 关掉就只用自定义的) */
function mirrorList(settings) {
const r = (settings && settings.runtime) || {};
const custom = String(r.mirrorPrefix || process.env.APKSTART_MIRROR || '').trim();
if (custom) return [custom];
if (r.autoMirror === false) return [];
return KNOWN_MIRRORS.slice();
}
class Sources {
constructor(settingsFn) {
this.settings = typeof settingsFn === 'function' ? settingsFn : () => ({});
this._rankCache = new Map(); // url -> {at, speed, ok}
this._rankTtl = 30 * 60 * 1000;
// 版本检查结果缓存:Gitee 的匿名调用限流很紧(连续几次就 403),
// 而「有没有新版本」本来也不需要秒级新鲜度。
this._relCache = { at: 0, key: '', value: null };
this._relTtl = 10 * 60 * 1000;
// 熔断:某个源被限流/报错后,一段时间内直接跳过它,不再每次启动都撞墙
this._blocked = new Map(); // sourceId -> until(ms)
this._blockMs = 15 * 60 * 1000;
}
/** 该源是否处于熔断期;返回 null 表示可用,否则返回给用户看的原因 */
blockedReason(id) {
const until = this._blocked.get(id) || 0;
if (until <= Date.now()) return null;
return `暂时跳过(${Math.ceil((until - Date.now()) / 60000)} 分钟内不再尝试)`;
}
/** 各源的最新 release(并发,任何一个失败都不影响另一个) */
/**
* 不依赖 API 的兜底:直接跟随 `/releases/latest` 的 302 拿到 tag。
*
* 为什么必须有这一条:GitHub 的匿名 API 每个 IP 每小时只有 60 次,
* Gitee 的匿名 API 更紧(实测连点几次就 403 Rate Limit Exceeded)。
* 公司/校园网、运营商 NAT 出口下,用户很容易「检查更新失败」——
* 而 `releases/latest` 这个网页跳转是走 CDN 的,不消耗 API 额度。
*
* 拿到 tag 之后资产名可以按发布约定拼出来:
* ApkStart-<tag>-win64-portable.zip / ApkStart-<version>-Setup.exe / ApkStart-runtime-<tag>.zip
*/
async _releaseTagViaRedirect(id) {
const s = SOURCES[id];
const page = `${s.web}/releases/latest`;
const res = await openUrl(page, { timeout: 9000, followRedirect: false });
const loc = String(res.headers.location || '');
try { res.resume(); } catch (_) { /* ignore */ }
const m = /\/releases\/tag\/([^/?#]+)/i.exec(loc);
return m ? decodeURIComponent(m[1]) : '';
}
/** 用 tag 拼一份「最小可用」的 release(没有 API 时用) */
_syntheticRelease(id, tag) {
const s = SOURCES[id];
const ver = String(tag).replace(/^v/i, '');
const names = [
`ApkStart-${tag}-win64-portable.zip`,
`ApkStart-${ver}-Setup.exe`,
`ApkStart-runtime-${tag}.zip`,
];
// GitHub 和 Gitee 都支持 /releases/download/<tag>/<文件名> 这种规范地址
// (实测 Gitee 会 302 到 foruda.gitee.com 的附件地址,能正常下到完整字节),
// 所以没有 API 时也能拼出可用的下载地址。Gitee 上没有的资产会 404,
// 后面的测速排序会自动跳过它。
const assets = names.map((n) => ({
name: n,
size: 0,
browser_download_url: `${s.web}/releases/download/${tag}/${n}`,
}));
return {
tag_name: tag,
name: tag,
body: '',
html_url: `${s.web}/releases/tag/${tag}`,
published_at: '',
assets,
synthetic: true,
};
}
async fetchReleases() {
const prefs = String((this.settings().runtime || {}).sourcePreference || 'auto');
const ids = prefs === 'github' || prefs === 'gitee' ? [prefs] : ['github', 'gitee'];
// 公开仓库的 releases 接口匿名就能读;仓库若是私有的,Gitee 会回 404
// "Not Found Project"(而不是 401),那种情况才需要在这里填一个只读 token。
// 另外匿名调用有频率限制,感兴趣的用户可以填 token 提高额度。
const token = String((this.settings().runtime || {}).giteeToken || '').trim();
const withToken = (id, url) => (id === 'gitee' && token
? `${url}${url.includes('?') ? '&' : '?'}access_token=${encodeURIComponent(token)}`
: url);
const jobs = ids.map(async (id) => {
const s = SOURCES[id];
const skip = this.blockedReason(id);
if (skip) return { source: id, name: s.name, web: s.web, error: skip };
try {
const rel = await getJson(withToken(id, `${s.api}/releases/latest`), 9000);
return { source: id, name: s.name, web: s.web, rel };
} catch (e) {
// Gitee 没有正式 release、或 GitHub 被墙,都退回列表取第一条
try {
const list = await getJson(withToken(id, `${s.api}/releases?per_page=10`), 9000);
const first = (Array.isArray(list) ? list : []).find((r) => r && !r.draft);
if (first) return { source: id, name: s.name, web: s.web, rel: first };
} catch (_) { /* 继续尝试不依赖 API 的兜底 */ }
// API 被限流/被墙时的兜底:网页跳转拿 tag,资产名按约定拼
try {
const tag = await this._releaseTagViaRedirect(id);
if (tag) {
return { source: id, name: s.name, web: s.web, rel: this._syntheticRelease(id, tag), degraded: true };
}
} catch (_) { /* 兜底也不行 */ }
const limited = /\b(403|429)\b/.test(e.message);
if (limited) this._blocked.set(id, Date.now() + this._blockMs);
const hint = limited
? '(被限流,已暂停使用该源一段时间)'
: (id === 'gitee' && !token ? '(可稍后重试,或在设置里填只读 token 提高额度)' : '');
return { source: id, name: s.name, web: s.web, error: `${e.message}${hint}` };
}
});
return Promise.all(jobs);
}
/**
* 合并两边的最新版本:版本号更高的胜出,但**资产列表取并集**,
* 这样即使 Gitee 只放了元数据、资产在 GitHub,也能两条路都试。
*/
async latest({ force = false } = {}) {
const pref = String((this.settings().runtime || {}).sourcePreference || 'auto');
const cached = this._relCache;
if (!force && cached.value && cached.key === pref && Date.now() - cached.at < this._relTtl) {
return cached.value;
}
const all = await this.fetchReleases();
// 被限流的源记入熔断(放在这里而不是 fetchReleases 内部,是为了让「谁来提供
// 结果」这件事可被替换/测试,同时保证任何入口拿到的 403 都会触发熔断)
for (const x of all) {
if (x && x.error && /\b(403|429)\b/.test(x.error)) {
this._blocked.set(x.source, Date.now() + this._blockMs);
}
}
const ok = all.filter((x) => x && x.rel);
if (!ok.length) {
const why = all.map((x) => `${x.name || x.source}: ${x.error || '无数据'}`).join(';');
// 全挂了但手里有上一次的结果:宁可给一份「可能是旧的」版本信息,
// 也不要让界面直接报错 —— 用户至少还能看到当前版本和发布页入口。
if (cached.value) return { ...cached.value, stale: true, staleReason: why };
throw new Error(`连不上更新服务器(${why})`);
}
ok.sort((a, b) => cmpVer(b.rel.tag_name || b.rel.name, a.rel.tag_name || a.rel.name));
const top = ok[0];
const tag = String(top.rel.tag_name || top.rel.name || '');
const version = tag.replace(/^v/i, '');
// 只有同版本的资产才算数(不同源的旧版本资产不能拿来装)。
// 同名整包 + 分片组同时存在时优先用整包:分片要拼装,多一个失败点。
let raw = [];
for (const item of ok) {
const t = String(item.rel.tag_name || item.rel.name || '').replace(/^v/i, '');
if (t !== version) continue;
for (const a of (item.rel.assets || [])) {
const key = String(a.name || '');
if (!key) continue;
// Gitee 会把「源码包」自动挂成资产(v0.3.2.zip / v0.3.2.tar.gz),
// 这些不是安装包,`\.zip$` 的匹配规则会把它当成更新包下载下来 —— 必须排除。
// 我们自己的发布资产统一以 ApkStart 开头,按这个前缀白名单更安全。
if (!/^ApkStart/i.test(key)) continue;
raw.push({
name: key,
size: Number(a.size) || 0,
url: a.browser_download_url || a.url || '',
source: item.source,
});
}
}
const grouped = groupParts(raw);
const wholeNames = new Set(grouped.list.filter((a) => !a.partial).map((a) => a.name));
raw = grouped.list.filter((a) => !(a.partial && wholeNames.has(a.name)));
// 同名资产两个源都有(整包不再切分片以后就是这种常态):把地址都留着当候选。
// 以前是「按名字去重、谁先来用谁」—— GitHub 排在前面,Gitee 的地址就被丢掉了,
// 「自动选网络更好的源」实际退化成只走 GitHub。现在按实测速度挑(见 rank())。
// 每个地址的大小单独记:两个源的包不是同一份字节(压缩器/构建机不同,能差几 MB),
// downloadMulti 只会混用「声明总大小一致」的地址,所以绝不能把两者的大小平均掉。
const assets = [];
const byName = new Map();
for (const a of raw) {
const hit = byName.get(a.name);
if (!hit) {
const item = { ...a, urls: a.url ? [a.url] : [], sizes: {} };
if (a.url && a.size) item.sizes[a.url] = a.size;
byName.set(a.name, item);
assets.push(item);
continue;
}
if (a.url && !hit.urls.includes(a.url)) hit.urls.push(a.url);
if (a.url && a.size) hit.sizes[a.url] = a.size;
if (!hit.size && a.size) hit.size = a.size;
}
const result = {
version,
tag,
name: top.rel.name || tag,
notes: top.rel.body || '',
htmlUrl: top.rel.html_url || top.web,
publishedAt: top.rel.published_at || top.rel.created_at || '',
assets,
sources: ok.map((x) => ({ source: x.source, name: x.name, tag: x.rel.tag_name || x.rel.name || '' })),
unreachable: all.filter((x) => x && x.error).map((x) => ({ source: x.source, name: x.name, error: x.error })),
bySource: top.source,
};
this._relCache = { at: Date.now(), key: pref, value: result };
return result;
}
/**
* 某个资产在所有源 + 镜像下的候选地址(去重、保序:源站优先,其次镜像)。
* 镜像只套在 GitHub 的地址上:ghfast / gh-proxy 只代理 GitHub,
* 把 Gitee 的链接拼上去只会得到一个 403,白白多测一次速。
*/
candidates(assetOrUrl) {
// 支持三种入参:单个地址、资产对象(url)、合并后的资产对象(urls 数组,含两个源)
const urls = (typeof assetOrUrl === 'string'
? [assetOrUrl]
: (assetOrUrl && (assetOrUrl.urls && assetOrUrl.urls.length ? assetOrUrl.urls : [assetOrUrl.url])))
.filter(Boolean);
const out = [];
if (!urls.length) return out;
const mirrors = mirrorList(this.settings());
for (const url of urls) {
if (out.includes(url)) continue;
out.push(url);
let host = '';
try { host = new URL(url).host.toLowerCase(); } catch (_) { continue; }
const isGithub = /(^|\.)github\.com$/.test(host) || /(^|\.)githubusercontent\.com$/.test(host);
if (!isGithub) continue;
for (const m of mirrors) {
const u = withMirror(url, m);
if (u && !out.includes(u)) out.push(u);
}
}
return out;
}
/**
* 按实测速度排序候选地址。测速结果缓存 30 分钟;
* 全部测速失败就退回原始顺序(保证「最坏情况还是能下」)。
*/
async rank(urls, { onProgress } = {}) {
const list = (urls || []).filter(Boolean);
if (list.length <= 1) return list;
const now = Date.now();
const need = [];
for (const u of list) {
const c = this._rankCache.get(u);
if (!c || now - c.at > this._rankTtl) need.push(u);
}
if (need.length) {
onProgress?.({ stage: 'probe', message: `正在测速 ${need.length} 个下载源…` });
// 顺序测而不是并发测:并发会互相抢带宽,测出来的名次变成「谁抢得凶」,
// 而不是「谁真的快」。每个源 256KB,正常也就一秒左右。
// 单个源最多等 6 秒就放弃(旧版 9 秒 × 4 个源 = 最长 36 秒没有任何字节进度,
// 用户看到的就是「卡在准备运行时」);测速失败只会排到最后,不影响能下载。
let i = 0;
for (const u of need) {
i += 1;
let host = u;
try { host = new URL(u).host; } catch (_) { /* 地址不合法就原样显示 */ }
onProgress?.({ stage: 'probe', message: `正在测速第 ${i}/${need.length} 个源:${host}…` });
// eslint-disable-next-line no-await-in-loop
const r = await measure(u, { timeoutMs: 6000 });
this._rankCache.set(u, { at: Date.now(), ok: r.ok, speed: r.speed });
}
}
const score = (u) => {
const c = this._rankCache.get(u);
return c && c.ok ? c.speed : -1;
};
const sorted = list.slice().sort((a, b) => score(b) - score(a));
const best = sorted[0];
const s = score(best);
if (s > 0) {
onProgress?.({
stage: 'probe',
message: `已选最快的源:${new URL(best).host}(约 ${(s / 1048576).toFixed(2)} MB/s)`,
});
}
return sorted;
}
/** 测速缓存清空(用户改了镜像前缀 / 换了网络后调用) */
reset() {
this._rankCache.clear();
this._relCache = { at: 0, key: '', value: null };
this._blocked.clear();
}
}
module.exports = { Sources, SOURCES, measure, KNOWN_MIRRORS, withMirror, mirrorList };