From f48e7aadb8978d3de98874f89e8deff6968a7540 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Mon, 16 Jun 2025 19:25:14 +0800 Subject: [PATCH] fix: enhance AppUpdater with IP country detection (#7235) * fix: downgrade version in package.json and enhance AppUpdater with IP country detection - Downgraded the application version from 1.4.2 to 1.4.1 in package.json. - Added a new private method `_getIpCountry` in AppUpdater to fetch the user's IP country with a timeout mechanism. - Updated the `setAutoUpdate` method to adjust the feed URL based on the detected country, improving update handling for users outside of China. * fix: adjust timeout duration and enhance IP country logging in AppUpdater * fix: extend timeout duration in AppUpdater for improved fetch reliability --------- Co-authored-by: beyondkmp --- src/main/services/AppUpdater.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/services/AppUpdater.ts b/src/main/services/AppUpdater.ts index 203a0d5f1c..4c71a05556 100644 --- a/src/main/services/AppUpdater.ts +++ b/src/main/services/AppUpdater.ts @@ -64,6 +64,30 @@ export default class AppUpdater { this.autoUpdater = autoUpdater } + private async _getIpCountry() { + try { + // add timeout using AbortController + const controller = new AbortController() + const timeoutId = setTimeout(() => controller.abort(), 5000) + + const ipinfo = await fetch('https://ipinfo.io/json', { + signal: controller.signal, + headers: { + 'User-Agent': + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36', + 'Accept-Language': 'en-US,en;q=0.9' + } + }) + + clearTimeout(timeoutId) + const data = await ipinfo.json() + return data.country || 'CN' + } catch (error) { + logger.error('Failed to get ipinfo:', error) + return 'CN' + } + } + public setAutoUpdate(isActive: boolean) { autoUpdater.autoDownload = isActive autoUpdater.autoInstallOnAppQuit = isActive @@ -82,6 +106,12 @@ export default class AppUpdater { } } + const ipCountry = await this._getIpCountry() + logger.info('ipCountry', ipCountry) + if (ipCountry !== 'CN') { + this.autoUpdater.setFeedURL(FeedUrl.EARLY_ACCESS) + } + try { const update = await this.autoUpdater.checkForUpdates() if (update?.isUpdateAvailable && !this.autoUpdater.autoDownload) {