From ab0d516d911fa8308a22c7d6348800cd6581f7b1 Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Sat, 19 Mar 2022 11:14:12 +0800
Subject: [PATCH] chore: updater proxy

---
 scripts/release.mjs       | 37 +++++++++++++++++++++++++++++++++++--
 src-tauri/tauri.conf.json |  3 ++-
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/scripts/release.mjs b/scripts/release.mjs
index 4599e02..f2ffa21 100644
--- a/scripts/release.mjs
+++ b/scripts/release.mjs
@@ -3,6 +3,7 @@ import { getOctokit, context } from "@actions/github";
 
 const UPDATE_TAG_NAME = "updater";
 const UPDATE_JSON_FILE = "update.json";
+const UPDATE_JSON_PROXY = "update-proxy.json";
 
 /// generate update.json
 /// upload to update tag's release asset
@@ -83,7 +84,7 @@ async function resolveRelease() {
   console.log(updateData);
 
   // maybe should test the signature as well
-  const { darwin, win64 } = updateData.platforms;
+  const { darwin, win64, linux } = updateData.platforms;
   if (!darwin.url) {
     console.log(`[Error]: failed to parse release for darwin`);
     delete updateData.platforms.darwin;
@@ -92,6 +93,24 @@ async function resolveRelease() {
     console.log(`[Error]: failed to parse release for win64`);
     delete updateData.platforms.win64;
   }
+  if (!linux.url) {
+    console.log(`[Error]: failed to parse release for linux`);
+    delete updateData.platforms.linux;
+  }
+
+  // 生成一个代理github的更新文件
+  // 使用 https://hub.fastgit.xyz/ 做github资源的加速
+  const updateDataNew = JSON.parse(JSON.stringify(updateData));
+
+  Object.keys(updateDataNew.platforms).forEach((key) => {
+    if (updateDataNew.platforms[key]) {
+      updateDataNew.platforms[key].url = updateDataNew.platforms[
+        key
+      ].url.replace("https://github.com/", "https://hub.fastgit.xyz/");
+    } else {
+      console.log(`[Error]: updateDataNew.platforms.${key} is null`);
+    }
+  });
 
   // update the update.json
   const { data: updateRelease } = await github.rest.repos.getReleaseByTag({
@@ -99,22 +118,36 @@ async function resolveRelease() {
     tag: UPDATE_TAG_NAME,
   });
 
+  // delete the old assets
   for (let asset of updateRelease.assets) {
     if (asset.name === UPDATE_JSON_FILE) {
       await github.rest.repos.deleteReleaseAsset({
         ...options,
         asset_id: asset.id,
       });
-      break;
+    }
+
+    if (asset.name === UPDATE_JSON_PROXY) {
+      await github.rest.repos
+        .deleteReleaseAsset({ ...options, asset_id: asset.id })
+        .catch(console.error); // do not break the pipeline
     }
   }
 
+  // upload assets
   await github.rest.repos.uploadReleaseAsset({
     ...options,
     release_id: updateRelease.id,
     name: UPDATE_JSON_FILE,
     data: JSON.stringify(updateData, null, 2),
   });
+
+  await github.rest.repos.uploadReleaseAsset({
+    ...options,
+    release_id: updateRelease.id,
+    name: UPDATE_JSON_PROXY,
+    data: JSON.stringify(updateDataNew, null, 2),
+  });
 }
 
 // get the signature file content
diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json
index ff971a8..63a2d7b 100644
--- a/src-tauri/tauri.conf.json
+++ b/src-tauri/tauri.conf.json
@@ -52,7 +52,8 @@
     "updater": {
       "active": true,
       "endpoints": [
-        "https://github.com/zzzgydi/clash-verge/releases/download/updater/update.json"
+        "https://github.com/zzzgydi/clash-verge/releases/download/updater/update.json",
+        "https://hub.fastgit.xyz/zzzgydi/clash-verge/releases/download/updater/update-proxy.json"
       ],
       "dialog": false,
       "pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IDExNUFBNTBBN0FDNEFBRTUKUldUbHFzUjZDcVZhRVRJM25NS3NkSFlFVElxUkNZMzZ6bHUwRVJjb2F3alJXVzRaeDdSaTA2YWYK"
-- 
GitLab