diff --git a/plugins/GFS/plugin-temp-alpha.js b/plugins/GFS/plugin-temp-alpha.js new file mode 100644 index 0000000..6a89df5 --- /dev/null +++ b/plugins/GFS/plugin-temp-alpha.js @@ -0,0 +1,151 @@ +/* 触发器 手动触发 */ +const onRun = async () => { + await Rolling() +} + +/* 触发器 启动APP时 */ +const onStartup = async () => { + if (Plugin.AutoRollingMode === 'onStartup') { + // 延迟检测,确保内核已经启动 + setTimeout(() => Rolling(false), (Plugin.AutoRollingDelay || 10) * 1000) + } +} + +/* 触发器 APP就绪后 */ +const onReady = async () => { + if (Plugin.AutoRollingMode === 'onReady') { + // 延迟检测,确保内核已经启动 + setTimeout(() => Rolling(false), (Plugin.AutoRollingDelay || 10) * 1000) + } +} + +/* + * 右键菜单 - 滚动版本 + * params: confirm 是否进行交互式确认 + */ +const Rolling = async (confirm = true) => { + await checkRollingReleaseEnabled() + await checkLatestVersion() + + const GFC_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.Clash/releases/tags/rolling-release' + const GFS_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.SingBox/releases/tags/rolling-release-alpha' + const url = Plugins.APP_TITLE.includes('Clash') ? GFC_URL : GFS_URL + + const { update, destroy, error } = Plugins.message.info(`[${Plugin.name}] 检测中...`, 999999) + + const { body } = await Plugins.HttpGet(url, { + Authorization: Plugins.getGitHubApiAuthorization() + }) + + if (body.message) { + destroy() + throw body.message + } + + const ZipFile = 'data/.cache/rolling-release.zip' + const BackupFile = 'data/.cache/rolling-release.backup' + const ZipUrl = body.assets.find((v) => v.name === 'rolling-release.zip')?.browser_download_url + const VersionUrl = body.assets.find((v) => v.name === 'version.txt')?.browser_download_url + const ChangelogUrl = body.assets.find((v) => v.name === 'changelog.md')?.browser_download_url + + if (!ZipUrl || !VersionUrl) { + destroy() + throw '出现一些错误,无法找到更新资源包' + } + + let localVersion = '' + let remoteVersion = '' + + try { + const { body } = await Plugins.HttpGet(VersionUrl) + remoteVersion = body + + const res = await fetch('/version.txt') + localVersion = await res.text() + } catch (err) {} + + if (!remoteVersion) { + destroy() + throw '无法获取远程版本信息' + } + + if (localVersion === remoteVersion) { + Plugins.message.success(`[${Plugin.name}] 当前版本已是最新(内测版)`) + destroy() + return + } + + let changelog = '维护性更新' + + if (ChangelogUrl && confirm) { + update('正在获取更新日志...') + const { body } = await Plugins.HttpGet(ChangelogUrl) + changelog = body + } + destroy() + + confirm && (await Plugins.confirm(Plugin.name, changelog, { type: 'markdown' })) + + const { update: update2, destroy: destroy2 } = Plugins.message.info('正在更新...') + try { + await Plugins.Download(ZipUrl, ZipFile, {}, (progress, total) => { + update2('正在更新...' + ((progress / total) * 100).toFixed(2) + '%') + }) + await Plugins.ignoredError(Plugins.Movefile, 'data/rolling-release', BackupFile) + await Plugins.UnzipZIPFile(ZipFile, 'data') + await Plugins.Removefile(ZipFile) + await Plugins.Removefile(BackupFile) + destroy2() + const ok = await Plugins.confirm(Plugin.name, '更新成功,是否立即重载界面?').catch(() => 0) + ok && Plugins.WindowReloadApp() + } catch (err) { + error(err.message || err) + } finally { + Plugins.sleep(1500).then(() => destroy2()) + } +} + +/** + * 右键菜单 - 恢复版本 + */ +const Recovery = async () => { + await checkRollingReleaseEnabled() + if (!(await Plugins.FileExists('data/rolling-release'))) { + Plugins.message.info('无需恢复,此版本已是默认版本。') + return + } + await Plugins.confirm(Plugin.name, '是否移除当前版本,恢复为默认版本?\n这将移除 data/rolling-release 目录。') + await Plugins.Removefile('data/rolling-release') + const ok = await Plugins.confirm(Plugin.name, '恢复成功,是否立即重载界面').catch(() => 0) + ok && (await Plugins.WindowReloadApp()) +} + +/** + * 右键菜单 - 更新日志 + */ +const Changelog = async () => { + const url = `https://github.com/GUI-for-Cores/${Plugins.APP_TITLE}/releases/download/rolling-release/changelog.md` + const { body } = await Plugins.HttpGet(url) + await Plugins.alert(Plugin.name, body, { type: 'markdown' }) +} + +const checkRollingReleaseEnabled = async () => { + const appSettings = Plugins.useAppSettingsStore() + if (!appSettings.app.rollingRelease) { + throw '请在【设置】中,开启【启用滚动发行】功能。' + } +} + +const checkLatestVersion = async () => { + const GFC_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.Clash/releases/latest' + const GFS_URL = 'https://api.github.com/repos/GUI-for-Cores/GUI.for.SingBox/releases/latest' + const url = Plugins.APP_TITLE.includes('Clash') ? GFC_URL : GFS_URL + const { body } = await Plugins.HttpGet(url, { + Authorization: Plugins.getGitHubApiAuthorization() + }) + const { tag_name, message } = body + if (message) throw message + if (tag_name !== Plugins.APP_VERSION) { + throw '无法跨大版本升级,请通过 设置 - 关于,更新APP!' + } +} diff --git a/plugins/gfs.json b/plugins/gfs.json index 84b6d90..918a115 100644 --- a/plugins/gfs.json +++ b/plugins/gfs.json @@ -1,4 +1,42 @@ [ + { + "id": "plugin-temp-alpha", + "name": "滚动发行(内测版)", + "description": "此版本属于开发内测版,欢迎加入测试。", + "type": "Http", + "url": "https://raw.githubusercontent.com/GUI-for-Cores/Plugin-Hub/main/plugins/GFS/plugin-temp-alpha.js", + "path": "data/plugins/plugin-temp-alpha.js", + "triggers": ["on::manual", "on::ready", "on::startup"], + "menus": { + "滚动版本": "Rolling", + "恢复版本": "Recovery", + "更新日志": "Changelog" + }, + "configuration": [ + { + "id": "ID_wqicvdoc", + "title": "自动更新策略", + "description": "自动滚动至最新版本", + "key": "AutoRollingMode", + "component": "Radio", + "value": "disabled", + "options": ["不自动更新,disabled", "启动APP时,onStartup", "APP就绪时,onReady"] + }, + { + "id": "ID_vognsfjh", + "title": "自动更新策略延迟", + "description": "延迟一段时间再执行更新检测,单位秒", + "key": "AutoRollingDelay", + "component": "Input", + "value": "10", + "options": [] + } + ], + "status": 0, + "disabled": false, + "install": false, + "installed": false + }, { "id": "plugin-add-rulesets", "name": "一键添加规则集",