Enhance GitHub API integration: Added support for dynamic mirror selection and improved version handling. Introduced local JSON fallback for release data and refactored version retrieval logic. Updated UI to reflect mirror changes.

This commit is contained in:
刘嘉伟 2025-01-07 14:43:58 +08:00
parent 91b97df99a
commit 337a3b6831
3 changed files with 47051 additions and 41 deletions

View File

@ -11,6 +11,7 @@ const zlib = require("zlib");
const { download } = require("electron-dl"); const { download } = require("electron-dl");
const AdmZip = require("adm-zip"); const AdmZip = require("adm-zip");
const log = require("electron-log"); const log = require("electron-log");
import frpReleasesJson from "../json/frp-releases.json";
const versionRelation = { const versionRelation = {
win32_x64: ["window", "amd64"], win32_x64: ["window", "amd64"],
@ -97,12 +98,18 @@ export const initGitHubApi = () => {
} }
return false; return false;
}); });
if (asset) { // if (asset) {
log.info(`找到对应版本 ${asset.name}`); // log.info(`找到对应版本 ${asset.name}`);
} // }
return asset; return asset;
}; };
/**
*
* @param bytes
* @param decimals
* @returns
*/
const formatBytes = (bytes: number, decimals: number = 2): string => { const formatBytes = (bytes: number, decimals: number = 2): string => {
if (bytes === 0) return "0 Bytes"; if (bytes === 0) return "0 Bytes";
const k = 1024; // 1 KB = 1024 Bytes const k = 1024; // 1 KB = 1024 Bytes
@ -114,29 +121,13 @@ export const initGitHubApi = () => {
}; };
/** /**
* github上的frp所有版本 * handle github api release json
* @param githubReleaseJsonStr jsonStr
* @returns versions
*/ */
ipcMain.on("github.getFrpVersions", async event => { const handleApiResponse = (githubReleaseJsonStr: string) => {
const request = net.request({
method: "get",
// url: "https://api.github.com/repos/fatedier/frp/releases?page=1&per_page=1000"
url: "https://api.jwinks.com/github/releases"
});
request.on("response", response => {
let responseData: Buffer = Buffer.alloc(0);
response.on("data", (data: Buffer) => {
responseData = Buffer.concat([responseData, data]);
});
response.on("end", () => {
log.info(
`开始获取frp版本 当前架构:${currArch} 对应frp架构${frpArch} 状态码:${response.statusCode}`
);
const downloadPath = path.join(app.getPath("userData"), "download"); const downloadPath = path.join(app.getPath("userData"), "download");
if (response.statusCode === 200) { versions = JSON.parse(githubReleaseJsonStr);
versions = JSON.parse(responseData.toString());
}
// const borderContent: Electron.WebContents =
// BrowserWindow.getFocusedWindow().webContents;
if (versions) { if (versions) {
const returnVersionsData = versions const returnVersionsData = versions
.filter(f => getAdaptiveAsset(f.id)) .filter(f => getAdaptiveAsset(f.id))
@ -156,12 +147,63 @@ export const initGitHubApi = () => {
return m; return m;
}); });
// log.debug(`获取到frp版本${JSON.stringify(returnVersionsData)}`) // log.debug(`获取到frp版本${JSON.stringify(returnVersionsData)}`)
event.reply("Download.frpVersionHook", returnVersionsData); return returnVersionsData;
} else { } else {
event.reply("Download.frpVersionHook", []); return [];
} }
};
/**
* conventMirrorUrl
* @param mirror mirror
* @returns mirrorUrl
*/
const conventMirrorUrl = (mirror: string) => {
switch (mirror) {
case "github":
return "https://api.github.com";
default:
return "https://api.github.com";
}
};
/**
* github上的frp所有版本
*/
ipcMain.on("github.getFrpVersions", async (event, mirror: string) => {
const mirrorUrl = conventMirrorUrl(mirror);
log.info("request mirror Url", mirrorUrl);
const request = net.request({
method: "get",
url: `${mirrorUrl}/repos/fatedier/frp/releases?page=1&per_page=1000`
});
let githubReleaseJsonStr = null;
request.on("response", response => {
log.info("request mirror Status Code", response.statusCode);
let responseData: Buffer = Buffer.alloc(0);
response.on("data", (data: Buffer) => {
responseData = Buffer.concat([responseData, data]);
});
response.on("end", () => {
if (response.statusCode === 200) {
githubReleaseJsonStr = responseData.toString();
} else {
log.info("use local json", response.statusCode);
githubReleaseJsonStr = JSON.stringify(frpReleasesJson);
}
const versions = handleApiResponse(githubReleaseJsonStr);
event.reply("Download.frpVersionHook", versions);
}); });
}); });
request.on("error", error => {
log.info("error use local json", error);
githubReleaseJsonStr = JSON.stringify(frpReleasesJson);
const versions = handleApiResponse(githubReleaseJsonStr);
event.reply("Download.frpVersionHook", versions);
});
request.end(); request.end();
}); });

File diff suppressed because it is too large Load Diff

View File

@ -20,10 +20,6 @@ const mirrors = ref<Array<GitHubMirror>>([
{ {
id: "github", id: "github",
name: "github" name: "github"
},
{
id: "ghproxy",
name: "ghproxy"
} }
]); ]);
@ -31,7 +27,7 @@ const mirrors = ref<Array<GitHubMirror>>([
* 获取版本 * 获取版本
*/ */
const handleLoadVersions = () => { const handleLoadVersions = () => {
ipcRenderer.send("github.getFrpVersions"); ipcRenderer.send("github.getFrpVersions", currMirror.value);
}; };
/** /**
@ -108,6 +104,10 @@ const handleInitDownloadHook = () => {
}); });
}; };
const handleMirrorChange = () => {
handleLoadVersions();
}
onMounted(() => { onMounted(() => {
handleLoadVersions(); handleLoadVersions();
handleInitDownloadHook(); handleInitDownloadHook();
@ -128,7 +128,7 @@ onUnmounted(() => {
<breadcrumb> <breadcrumb>
<div class="h-full flex items-center justify-center"> <div class="h-full flex items-center justify-center">
<span class="text-sm font-bold">下载源 </span> <span class="text-sm font-bold">下载源 </span>
<el-select class="w-40" v-model="currMirror"> <el-select class="w-40" v-model="currMirror" @change="handleMirrorChange">
<el-option <el-option
v-for="m in mirrors" v-for="m in mirrors"
:label="m.name" :label="m.name"