Skip to main content

Electron Fuses熔丝

Package time feature toggles包时间功能切换

What are fuses?什么是熔丝?

For a subset of Electron functionality it makes sense to disable certain features for an entire application. 对于Electron功能的子集,禁用整个应用程序的某些功能是有意义的。For example, 99% of apps don't make use of ELECTRON_RUN_AS_NODE, these applications want to be able to ship a binary that is incapable of using that feature. 例如,99%的应用程序不使用ELECTRON_RUN_AS_NODE,这些应用程序希望能够提供无法使用该功能的二进制文件。We also don't want Electron consumers building Electron from source as that is both a massive technical challenge and has a high cost of both time and money.我们也不希望Electron消费者从源头上制造Electron产品,因为这是一个巨大的技术挑战,而且时间和金钱成本都很高。

Fuses are the solution to this problem, at a high level they are "magic bits" in the Electron binary that can be flipped when packaging your Electron app to enable / disable certain features / restrictions. 熔丝是这个问题的解决方案,在高层次上,它们是Electron二进制文件中的“神奇比特”,在封装Electron应用程序以启用/禁用某些功能/限制时可以翻转。Because they are flipped at package time before you code sign your app the OS becomes responsible for ensuring those bits aren't flipped back via OS level code signing validation (Gatekeeper / App Locker).因为它们在应用程序代码签名之前的包时间被翻转,所以操作系统负责确保这些位不会通过操作系统级代码签名验证(Gatekeeper/app Locker)翻转回来。

How do I flip the fuses?我该如何打开熔丝?

The easy way简单的方法

We've made a handy module, @electron/fuses, to make flipping these fuses easy. 我们制作了一个方便的模块,@electron/fuses,可以轻松翻转这些熔丝。Check out the README of that module for more details on usage and potential error cases.查看该模块的自述文件,了解有关使用和潜在错误情况的更多详细信息。

require('@electron/fuses').flipFuses(
// Path to electron
require('electron'),
// Fuses to flip
{
runAsNode: false
}
)

The hard way困难的方法

Quick Glossary

  • Fuse Wire: A sequence of bytes in the Electron binary used to control the fuses:Electron二进制中用于控制熔丝的字节序列
  • Sentinel: A static known sequence of bytes you can use to locate the fuse wire:可用于定位熔丝的静态已知字节序列
  • Fuse Schema: The format / allowed values for the fuse wire:熔丝的格式/允许值

Manually flipping fuses requires editing the Electron binary and modifying the fuse wire to be the sequence of bytes that represent the state of the fuses you want.手动翻转熔丝需要编辑Electron二进制,并将熔丝修改为表示所需熔丝状态的字节序列。

Somewhere in the Electron binary there will be a sequence of bytes that look like this:在Electron二进制中的某个位置,将有一个字节序列,如下所示:

| ...binary | sentinel_bytes | fuse_version | fuse_wire_length | fuse_wire | ...binary |
  • sentinel_bytes is always this exact string 始终是这个字符串dL7pKGdnNz796PbbjQWNKmHXBZaB9tsX
  • fuse_version is a single byte whose unsigned integer value represents the version of the fuse schema是一个单字节,其无符号整数值表示fuse架构的版本
  • fuse_wire_length is a single byte whose unsigned integer value represents the number of fuses in the following fuse wire是一个单字节,其无符号整数值表示以下熔丝中的熔丝数量
  • fuse_wire is a sequence of N bytes, each byte represents a single fuse and its state.是一个N字节的序列,每个字节表示一个熔丝及其状态。
    • "0" (0x30) indicates the fuse is disabled指示熔丝已禁用
    • "1" (0x31) indicates the fuse is enabled指示熔丝已启用
    • "r" (0x72) indicates the fuse has been removed and changing the byte to either 1 or 0 will have no effect.表示熔丝已拆除,将字节更改为1或0将无效。

To flip a fuse you find its position in the fuse wire and change it to "0" or "1" depending on the state you'd like.要翻转熔丝,请在熔丝中找到其位置,并根据您想要的状态将其更改为“0”或“1”。

You can view the current schema here.您可以在此处查看当前模式。