extension installation on Windows

This commit is contained in:
antelle 2021-04-23 23:17:35 +02:00
parent 822602a38f
commit 233721bf51
No known key found for this signature in database
GPG Key ID: 63C9777AAB7C563C
3 changed files with 97 additions and 32 deletions

View File

@ -40,7 +40,6 @@
<li><a href="https://github.com/antelle/node-yubikey-chalresp" target="_blank">node-yubikey-chalresp</a><span class="muted-color">, YubiKey challenge-response API for node.js, &copy; 2020 Antelle</span></li>
<li><a href="https://github.com/antelle/node-secure-enclave" target="_blank">node-secure-enclave</a><span class="muted-color">, Secure Enclave module for node.js and Electron, &copy; 2020 Antelle</span></li>
<li><a href="https://github.com/antelle/node-keyboard-auto-type" target="_blank">node-keyboard-auto-type</a><span class="muted-color">, node.js bindings for keyboard-auto-type, &copy; 2021 Antelle</span></li>
<li><a href="https://github.com/Eugeny/windows-native-registry" target="_blank">windows-native-registry</a><span class="muted-color">, native windows registry access for Node, &copy; 2020 Eugeny</span></li>
</ul>
{{/if}}

View File

@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const windowsRegistry = require('./windows-registry');
const { isDev } = require('./app-info');
const { app } = require('electron');
@ -17,8 +18,6 @@ function getManifestDir(browser) {
default:
return undefined;
}
case 'win32':
throw new Error('not implemented');
case 'linux':
switch (browser) {
case 'Chrome':
@ -33,15 +32,39 @@ function getManifestDir(browser) {
}
}
function getManifestFileName(extension) {
function getWindowsRegistryPath(browser) {
switch (browser) {
case 'Chrome':
return 'HKCU\\Software\\Google\\Chrome\\NativeMessagingHosts\\';
case 'Firefox':
return 'HKCU\\Software\\Mozilla\\NativeMessagingHosts\\';
case 'Edge':
return 'HKCU\\Software\\Microsoft\\Edge\\NativeMessagingHosts\\';
default:
return undefined;
}
}
function getWindowsManifestFileName(browser) {
const suffix = browser === 'Firefox' ? 'firefox' : 'chrome';
const manifestName = `native-messaging.${suffix}.json`;
return path.join(app.getPath('userData'), manifestName);
}
function getNativeHostName(extension) {
switch (extension) {
case 'KWC':
return 'net.antelle.keeweb.keeweb_connect.json';
return 'net.antelle.keeweb.keeweb_connect';
case 'KPXC':
return 'org.keepassxc.keepassxc_browser.json';
return 'org.keepassxc.keepassxc_browser';
}
}
function getManifestFileName(extension) {
const nativeHostName = getNativeHostName(extension);
return nativeHostName ? nativeHostName + '.json' : undefined;
}
function createManifest(browser, extension) {
switch (extension) {
case 'KWC':
@ -92,41 +115,71 @@ function getNativeMessagingHostPath() {
}
module.exports.install = async function (browser, extension) {
const manifestDir = getManifestDir(browser);
if (!manifestDir) {
return;
}
await fs.promises.mkdir(manifestDir, { recursive: true });
const manifestFileName = getManifestFileName(extension);
if (!manifestFileName) {
return;
}
const fullPath = path.join(manifestDir, manifestFileName);
const manifest = createManifest(browser, extension);
if (!manifest) {
return;
}
manifest.path = getNativeMessagingHostPath();
await fs.promises.writeFile(fullPath, JSON.stringify(manifest, null, 4));
if (process.platform === 'win32') {
const registryPath = getWindowsRegistryPath(browser);
if (!registryPath) {
return;
}
const registryKeyName = getNativeHostName(extension);
if (!registryKeyName) {
return;
}
const manifestFileName = getWindowsManifestFileName(browser);
await fs.promises.writeFile(manifestFileName, JSON.stringify(manifest, null, 4));
windowsRegistry.createKey(registryPath + registryKeyName, manifestFileName);
} else {
const manifestDir = getManifestDir(browser);
if (!manifestDir) {
return;
}
await fs.promises.mkdir(manifestDir, { recursive: true });
const manifestFileName = getManifestFileName(extension);
if (!manifestFileName) {
return;
}
const fullPath = path.join(manifestDir, manifestFileName);
await fs.promises.writeFile(fullPath, JSON.stringify(manifest, null, 4));
}
};
module.exports.uninstall = async function (browser, extension) {
const manifestDir = getManifestDir(browser);
if (!manifestDir) {
return;
}
if (process.platform === 'win32') {
const registryPath = getWindowsRegistryPath(browser);
if (!registryPath) {
return;
}
const manifestFileName = getManifestFileName(extension);
if (!manifestFileName) {
return;
}
const fullPath = path.join(manifestDir, manifestFileName);
const registryKeyName = getNativeHostName(extension);
if (!registryKeyName) {
return;
}
await fs.promises.unlink(fullPath);
windowsRegistry.deleteKey(registryPath + registryKeyName);
} else {
const manifestDir = getManifestDir(browser);
if (!manifestDir) {
return;
}
const manifestFileName = getManifestFileName(extension);
if (!manifestFileName) {
return;
}
const fullPath = path.join(manifestDir, manifestFileName);
await fs.promises.unlink(fullPath);
}
};

View File

@ -0,0 +1,13 @@
const { spawn } = require('child_process');
function reg(...args) {
spawn('REG', args);
}
module.exports.createKey = function (key, value) {
return reg('ADD', key, '/ve', '/d', value, '/f');
};
module.exports.deleteKey = function (key) {
return reg('DELETE', key, '/f');
};