diff --git a/plugins/GFS/plugin-sync-singbox-configuration-gists.js b/plugins/GFS/plugin-sync-singbox-configuration-gists.js new file mode 100644 index 0000000..318485e --- /dev/null +++ b/plugins/GFS/plugin-sync-singbox-configuration-gists.js @@ -0,0 +1,44 @@ +const onRun = async () => { + await updateGist(); +}; + +const updateGist = async () => { + if (!Plugin.GistId) throw '未配置GIST ID'; + const { id: messageId } = Plugins.message.info('正在更新 Gist...', 60 * 60 * 1000); + + try { + const configJsonContent = await Plugins.Readfile('data/sing-box/config.json'); + if (!configJsonContent) throw 'config.json 文件不存在'; + + const updatedGist = await updateGistFile(Plugin.GistId, configJsonContent); + Plugins.message.update(messageId, `Gist 更新成功: ${updatedGist}`, 'success'); + } catch (error) { + Plugins.message.update(messageId, `Gist 更新失败: ${error}`, 'error'); + } finally { + await Plugins.sleep(1500).then(() => Plugins.message.destroy(messageId)); + } +}; + + +async function updateGistFile(gistId, configJsonContent) { + if (!Plugin.Authorization) throw '未配置TOKEN'; + + const { body } = await Plugins.HttpPatch(`https://api.github.com/gists/${gistId}`, { + 'User-Agent': 'GUI.for.Cores', + 'Content-Type': 'application/json', + 'X-GitHub-Api-Version': '2022-11-28', + Accept: 'application/vnd.github+json', + Connection: 'close', + Authorization: 'Bearer ' + Plugin.Authorization + }, { + files: { + 'config.json': { + content: configJsonContent + } + } + }); + if (body.message) { + throw body.message + } + return 'ok' +} \ No newline at end of file diff --git a/plugins/gfs.json b/plugins/gfs.json index 55c9b0c..5f0f84c 100644 --- a/plugins/gfs.json +++ b/plugins/gfs.json @@ -28,5 +28,39 @@ "disabled": false, "install": true, "installed": false + }, + { + "id": "plugin-sync-singbox-configuration-gists", + "name": "同步singbox的配置 - Gists", + "description": "将sing-box的config.json同步到指定的Gist-Id下的config.json。Supported by: ccch", + "type": "Http", + "url": "https://raw.githubusercontent.com/GUI-for-Cores/Plugin-Hub/main/plugins/GFS/plugin-sync-singbox-configuration-gists.js", + "path": "data/plugins/plugin-sync-singbox-configuration-gists.js", + "triggers": ["on::manual"], + "menus": {}, + "status": 0, + "configuration": [ + { + "id": "ID_z8ofmiru", + "title": "TOKEN", + "description": "拥有Gists访问权限的 token", + "key": "Authorization", + "component": "Input", + "value": "", + "options": [] + }, + { + "id": "ID_b22adg38", + "title": "GIST ID", + "description": "已创建好的GistId", + "key": "Secret", + "component": "Input", + "value": "", + "options": [] + } + ], + "disabled": false, + "install": false, + "installed": false } ]