2
头图

Since writing the Electron series, many students have asked some questions, mainly for the incremental update of electron. Before I wrote incremental update of , but the Windows method requires an administrator privilege package. It’s not very elegant, and if you want the drag-and-drop function, due to permission issues, the software after the privilege will be forbidden to drag (the external file will be dragged into the software window and there will be a prohibition icon), then this issue I will increase Update a more detailed description and provide a temporary upgrade update plan for your reference.

Mac update

Some students asked how to incrementally update the system under the Mac platform. In fact, the asar file of Electron under the Mac can be directly replaced, so after downloading and replacing, start a timer and restart.

Windows

In fact, most of our desktop software does not require administrator rights (that is, the software has a shield). We found that many softwares will have a pop-up window when they need to be updated. After you click it, they will be temporarily escalated to update. , The goal of this issue is to achieve this temporary privilege escalation function for incremental updates.

Update question

In fact, our incremental update has two difficulties. To achieve this, we must overcome these two main problems:

  1. When does uac need to escalate rights?
  2. app.asar is occupied and locked after the Electron application is started?

Release Notes

  1. The Windows system has different user authority levels. For example, when I am a normal user modifying files in certain folders of the c drive, there will be a pop-up window that allows you to create or modify files with administrator rights. When the software is installed For the c drive, we need administrator rights to replace the app.asar file. exe
  2. Regarding the locking problem, it has been explained in Increment 2. The way to deal with it is to close our Electron application, and then replace the app.asar

Update steps

  1. Write the bat script, execute the closure of the Electron application in the script, app.asar file, restart the Electron application, and then package the bat script into an exe file.
  2. Download update.asar (the updated version of app.asar), and determine whether the user software is installed on the c drive. Yes: use the sudo-prompt package to temporarily increase the right to execute the above exe, no: you can directly use node's exec to execute the above without mentioning the right Exe.

Update preparation

The first two updates of the simulation request have already been said, click here incremental update (2) , take a look at the steps, no more instructions here.

Build exe

@echo off
taskkill /f /im %3
timeout /T 1 /NOBREAK
del /f /q /a %1\app.asar
move %2\update.asar %1
ren %1\update.asar app.asar
explorer.exe %4

To explain briefly, %1 and %2 are the parameters passed in to run the script, such as update.bat aaa bbb , then %1 is aaa and %2 is bbb, which are passed in when we execute the exe above,
That is, %1 is resourcesPath (that is, the directory where our app.asar is located), %2 is the directory where update.asar is downloaded and updated, %3 is the process name of the software (can be viewed in the task manager), and %4 is Start the exe of the software.
The logic here is the Electron application, pause for 1 second, then delete app.asar, move update.asar to the app.asar directory, rename it to app.asar, and start the Electron application.
Download the Bat To Exe Converter software and convert update.bat to update.exe. For detailed operations, please see the detailed instructions in the previous two issues.

Compared with the previous issue, we found that the start command was not used when starting, but explorer.exe, which is the program manager that comes with the Windows system. The processing here is that the packaged Electron application does not have administrator rights, but If we execute this exe to start the Electron application after raising the rights, we will find that the Electron application also has administrator rights at this time, so we need to use explorer.exe to lower the rights to start.

sudo-prompt

Install sudo-prompt, npm i sudo-prompt . The sudo-prompt package is similar to the node's exec command. When executed, a privilege escalation popup will appear. After the user confirms, the command will be executed with administrator privileges. Create a new sudoPrompt.js:

var sudo = require('sudo-prompt')
var options = {
  name: 'Electron',
}

export default (shell) => {
  return new Promise((resolve, reject) => {
    sudo.exec(shell, options, function(error, stdout, stderr) {
      if (error) {
        reject(error)
        console.log('error:' + error)
        return
      }
      resolve(stdout)
      console.log('stdout: ' + stdout)
    })
  })
}
// npm i https://github.com/xuxingeren/sudo-prompt
"sudo-prompt": "git+https://github.com/xuxingeren/sudo-prompt.git",

Note: The sudo-prompt package has not been maintained for a long time, and there are still some problems with the latest one. The main problem is that there are errors with Chinese paths. The original author has closed it and cannot mention pr. I have dealt with it here. If you use it, you can fork My branch to build, address

Main process update processing

The preparation before the update and the processing of the rendering process are skipped here, please see the content of the previous two issues

import downloadFile from './downloadFile'
import sudoPrompt from './sudoPrompt'
import { app } from 'electron'
const fse = require('fs-extra')
const path = require('path')
const AdmZip = require('adm-zip')
import log from '../config/log.js'

export default async (data) => {
  const resourcesPath = process.resourcesPath
  // 下载我们上面打包好的更新exe,我这里是放在app.getPath('userData')下的,其他位置也可
  if (!fse.pathExistsSync(path.join(app.getPath('userData'), './update.exe'))) {
    await downloadFile({
      url: data.upDateExe,
      targetPath: app.getPath('userData')
    }).catch((err) => {
      console.log(err)
      log.info(err)
    })
  }
  // 提权的方案,这里简写了
  const downloads = app.getPath('downloads')
  // 下载update.asar,解压(其实不用压缩也可)在系统的下载目录
  downloadFile({ url: data.upDateUrl, targetPath: downloads })
    .then(async (filePath) => {
      const zip = new AdmZip(filePath)
      zip.extractAllToAsync(downloads, true, (err) => {
        if (err) {
          console.error(err)
          return
        }
        fse.removeSync(filePath)
        // 这里可以添加判断,如果软件是安装在c盘使用sudoPrompt进行提权执行update.exe,不是的话可以直接执行update.exe
        // 临时提权运行exe,exe中关闭主进程,替换安装c盘中的asar(提权是为了处理c盘,如果安装其他盘,可以直接用node.exec运行exe替换)
        // 由于提权后的exe打开electron,导致其启动后也会是管理员权限,故需降权进行启动,explorer.exe
        sudoPrompt(
          `"${path.join(
            app.getPath('userData'),
            './update.exe'
          )}" "${resourcesPath}" ${downloads} "${
            process.env.VUE_APP_PRODUCTNAME
          }.exe" "${app.getPath('exe')}"`
        )
      })
    })
    .catch((err) => {
      log.info(err)
      console.log(err)
    })
}

For manual debugging, you can execute update.exe xxx xxx xxx xxx in cmd and pass in the corresponding updated address to see if the execution is successful.

This series of updates can only be organized during weekends and after get off work hours. If there are more content, the update will be slower. I hope it can be helpful to you. Please support it by star or like favorites.

This article address: link
This github address: link


陌路凡歌
7.9k 声望259 粉丝

人笨,多听,多写,多看书