Created
July 9, 2021 16:26
-
-
Save teoxoy/519fc2441958cd152d1ef731d2e41538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os from 'os' | |
import cp from 'child_process' | |
function getPlatform(): NodeJS.Platform { | |
// Detect WSL | |
if (os.platform() === 'linux' && os.release().includes('Microsoft')) { | |
return 'win32' | |
} | |
return os.platform() | |
} | |
function entrust(cert: string): Promise<void> { | |
switch (getPlatform()) { | |
case 'win32': | |
return entrustWindows(cert) | |
case 'linux': | |
case 'darwin': | |
default: | |
return Promise.reject('Platform not supported!') | |
} | |
} | |
function withhold(cert: string): Promise<void> { | |
switch (getPlatform()) { | |
case 'win32': | |
return withholdWindows(cert) | |
case 'linux': | |
case 'darwin': | |
default: | |
return Promise.reject('Platform not supported!') | |
} | |
} | |
function entrustWindows(cert: string): Promise<void> { | |
const content = ` | |
$tmp = New-TemporaryFile | |
"${cert}" >> $tmp.FullName | |
Import-Certificate -FilePath $tmp.FullName -CertStoreLocation cert:\\CurrentUser\\Root | |
Remove-Item $tmp.FullName -Force` | |
return new Promise(resolve => { | |
const proc = cp.spawn('powershell.exe', ['-Command', content]) | |
// proc.stdout.on('data', data => console.log(data.toString())) | |
proc.stderr.on('data', data => console.error(data.toString())) | |
proc.on('exit', resolve) | |
}) | |
} | |
function withholdWindows(cert: string): Promise<void> { | |
const content = ` | |
$tmp = New-TemporaryFile | |
"${cert}" >> $tmp.FullName | |
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($tmp.FullName) | |
$path = Join-Path Cert:\\CurrentUser\\Root $cert.Thumbprint | |
Get-ChildItem $path | Remove-Item | |
Remove-Item $tmp.FullName -Force` | |
return new Promise(resolve => { | |
const proc = cp.spawn('powershell.exe', ['-Command', content]) | |
// proc.stdout.on('data', data => console.log(data.toString())) | |
proc.stderr.on('data', data => console.error(data.toString())) | |
proc.on('exit', resolve) | |
}) | |
} | |
export { entrust, withhold } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment