/
githubmirror
/
Motrix
Обзор
Документация
Войти
/
githubmirror
/
Motrix
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/core/UpdateManager.js
163 строки
4 KB
Dr_rOot
feat: setup proxy with scopes
20 май 2023, 13:29
20 май 2023, 13:29
ecbe7f4
Код
Авторство
О чём код?
import { EventEmitter } from 'node:events' import { resolve } from 'node:path' import { dialog } from 'electron' import is from 'electron-is' import { autoUpdater } from 'electron-updater' import { PROXY_SCOPES } from '@shared/constants' import logger from './Logger' import { getI18n } from '../ui/Locale' if (is.dev()) { autoUpdater.updateConfigPath = resolve(__dirname, '../../../app-update.yml') } export default class UpdateManager extends EventEmitter { constructor (options = {}) { super() this.options = options this.i18n = getI18n() this.isChecking = false this.updater = autoUpdater this.updater.autoDownload = false this.updater.autoInstallOnAppQuit = false this.updater.logger = logger logger.info('[Motrix] setup proxy:', this.options.proxy) this.setupProxy(this.options.proxy) this.autoCheckData = { checkEnable: this.options.autoCheck, userCheck: false } this.init() } setupProxy (proxy) { const { enable, server, scope = [] } = proxy if (!enable || !server || !scope.includes(PROXY_SCOPES.UPDATE_APP)) { this.updater.netSession.setProxy({ proxyRules: undefined }) return } const url = new URL(server) const { username, password, protocol = 'http:', host, port } = url const proxyRules = `${protocol}//${host}` logger.info(`[Motrix] setup proxy: ${proxyRules}`, username, password, protocol, host, port) this.updater.netSession.setProxy({ proxyRules }) if (server.includes('@')) { this.updater.signals.login((_authInfo, callback) => { callback(username, password) }) } } init () { // Event: error // Event: checking-for-update // Event: update-available // Event: update-not-available // Event: download-progress // Event: update-downloaded this.updater.on('checking-for-update', this.checkingForUpdate.bind(this)) this.updater.on('update-available', this.updateAvailable.bind(this)) this.updater.on('update-not-available', this.updateNotAvailable.bind(this)) this.updater.on('download-progress', this.updateDownloadProgress.bind(this)) this.updater.on('update-downloaded', this.updateDownloaded.bind(this)) this.updater.on('update-cancelled', this.updateCancelled.bind(this)) this.updater.on('error', this.updateError.bind(this)) if (this.autoCheckData.checkEnable && !this.isChecking) { this.autoCheckData.userCheck = false this.updater.checkForUpdates() } } check () { this.autoCheckData.userCheck = true this.updater.checkForUpdates() } checkingForUpdate () { this.isChecking = true this.emit('checking') } updateAvailable (event, info) { this.emit('update-available', info) dialog.showMessageBox({ type: 'info', title: this.i18n.t('app.check-for-updates-title'), message: this.i18n.t('app.update-available-message'), buttons: [this.i18n.t('app.yes'), this.i18n.t('app.no')], cancelId: 1 }).then(({ response }) => { if (response === 0) { this.updater.downloadUpdate() } else { this.emit('update-cancelled', info) } }) } updateNotAvailable (event, info) { this.isChecking = false this.emit('update-not-available', info) if (this.autoCheckData.userCheck) { dialog.showMessageBox({ title: this.i18n.t('app.check-for-updates-title'), message: this.i18n.t('app.update-not-available-message') }) } } /** * autoUpdater:download-progress * @param {Object} event * progress, * bytesPerSecond, * percent, * total, * transferred */ updateDownloadProgress (event) { this.emit('download-progress', event) } updateDownloaded (event, info) { this.emit('update-downloaded', info) this.updater.logger.log(`Update Downloaded: ${info}`) dialog.showMessageBox({ title: this.i18n.t('app.check-for-updates-title'), message: this.i18n.t('app.update-downloaded-message') }).then(_ => { this.isChecking = false this.emit('will-updated') setTimeout(() => { this.updater.quitAndInstall() }, 200) }) } updateCancelled () { this.isChecking = false } updateError (event, error) { this.isChecking = false this.emit('update-error', error) const msg = (error == null) ? this.i18n.t('app.update-error-message') : (error.stack || error).toString() this.updater.logger.warn(`[Motrix] update-error: ${msg}`) dialog.showErrorBox('Error', msg) } }