-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathbin.ts
executable file
·181 lines (155 loc) · 5.16 KB
/
bin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env node
import * as chalk from 'chalk';
import * as minimist from 'minimist';
import * as path from 'path';
import { flipFuses, getCurrentFuseWire } from '.';
import { FuseConfig, FuseV1Options, FuseVersion } from './config';
import { FuseState } from './constants';
interface FuseReadCLIArgs {
app?: string;
help?: boolean;
}
interface FuseWriteCLIArgs {
app?: string;
help?: boolean;
}
const mode = process.argv[2];
const readHelpText = `electron-fuses read --app [path-to-app]`;
const writeHelpText = `electron-fuses write --app [path-to-app] <...key=on/off>`;
if (mode !== 'read' && mode !== 'write') {
console.error('Invalid mode, check the usage below:');
console.info(readHelpText);
console.info(writeHelpText);
process.exit(0);
}
function stringForState(state: FuseState) {
switch (state) {
case FuseState.ENABLE:
return chalk.green('Enabled');
case FuseState.DISABLE:
return chalk.red('Disabled');
case FuseState.INHERIT:
return chalk.yellow('Inherited');
case FuseState.REMOVED:
return chalk.strikethrough(chalk.red('Removed'));
}
}
if (mode === 'read') {
const argv = minimist<FuseReadCLIArgs>(process.argv.slice(3), {
string: ['app'],
boolean: ['help'],
});
if (argv.help) {
console.log(readHelpText);
process.exit(0);
}
if (!argv.app) {
console.error('--app argument is required');
process.exit(1);
}
console.log('Analyzing app:', chalk.cyan(path.basename(argv.app)));
getCurrentFuseWire(argv.app)
.then((config) => {
const { version, resetAdHocDarwinSignature, strictlyRequireAllFuses, ...rest } = config;
console.log(`Fuse Version: ${chalk.cyan(`v${version}`)}`);
switch (config.version) {
case FuseVersion.V1:
for (const key of Object.keys(rest)) {
console.log(
` ${chalk.yellow(FuseV1Options[key as any])} is ${stringForState(
rest[key as any as keyof typeof rest]!,
)}`,
);
}
break;
}
})
.catch((err) => {
console.error(err);
process.exit(1);
});
} else {
const argv = minimist<FuseWriteCLIArgs>(process.argv.slice(3), {
string: ['app'],
boolean: ['help'],
});
if (argv.help) {
console.log(writeHelpText);
process.exit(0);
}
if (!argv.app) {
console.error('--app argument is required');
process.exit(1);
}
console.log('Analyzing app:', chalk.cyan(path.basename(argv.app)));
getCurrentFuseWire(argv.app)
.then((config) => {
const { version, resetAdHocDarwinSignature, ...rest } = config;
console.log(`Fuse Version: ${chalk.cyan(`v${version}`)}`);
const keyPairs = argv._ || [];
for (const keyPair of keyPairs) {
const [key, state] = keyPair.split('=');
if (!key || !state) {
console.error('Invalid fuse:', keyPair);
console.error('Must be in the format FuseName=on/off');
process.exit(1);
}
if (state !== 'on' && state !== 'off') {
console.error('Invalid fuse state:', chalk.yellow(keyPair));
console.error(
`Fuses can only be set to the "${chalk.green('on')}" or "${chalk.red('off')}" state`,
);
process.exit(1);
}
switch (config.version) {
case FuseVersion.V1:
const validFuseNames = Object.keys(FuseV1Options).filter((k) => !/^[0-9]+$/.test(k));
if (!validFuseNames.includes(key)) {
console.error('Invalid fuse name', chalk.yellow(key));
console.error(
'Expected name to be one of',
chalk.yellow(JSON.stringify(validFuseNames)),
);
process.exit(1);
}
const currentState = (config as any)[FuseV1Options[key as any]]!;
const newState = state === 'on' ? FuseState.ENABLE : FuseState.DISABLE;
if (currentState === newState) {
console.log(
` ${chalk.yellow(key)} is already ${stringForState(
currentState,
)} and will not be changed`,
);
} else {
console.log(
` ${chalk.yellow(key)} is ${stringForState(
currentState,
)} and will become ${stringForState(newState)}`,
);
}
(config as any)[FuseV1Options[key as any]] = newState;
break;
}
}
console.log('Writing to app:', chalk.cyan(path.basename(argv.app!)));
function adaptConfig(config: FuseConfig<FuseState>): FuseConfig<boolean> {
const { version, resetAdHocDarwinSignature, ...rest } = config;
const fuseConfig: FuseConfig<boolean> = {
version,
resetAdHocDarwinSignature,
};
for (const key of Object.keys(rest)) {
(fuseConfig as any)[key] = (rest as any)[key] === FuseState.ENABLE;
}
return fuseConfig;
}
return flipFuses(argv.app!, adaptConfig(config));
})
.then(() => {
console.log(chalk.green('Fuses written to disk'));
})
.catch((err) => {
console.error(err);
process.exit(1);
});
}