162 lines
5.5 KiB
PowerShell
162 lines
5.5 KiB
PowerShell
#Requires -Version 5.1
|
|
|
|
function Get-AlipayRepositoryRoot {
|
|
return Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
|
|
}
|
|
|
|
function Get-AlipayEncryptedConfigPath {
|
|
return Join-Path (Get-AlipayRepositoryRoot) 'runtime\private\alipay-account.dpapi'
|
|
}
|
|
|
|
function ConvertFrom-SecureStringForAlipay {
|
|
param([Parameter(Mandatory = $true)][Security.SecureString] $Value)
|
|
|
|
$pointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($Value)
|
|
try {
|
|
return [Runtime.InteropServices.Marshal]::PtrToStringBSTR($pointer)
|
|
} finally {
|
|
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($pointer)
|
|
}
|
|
}
|
|
|
|
function Assert-AlipayAccountConfig {
|
|
param([Parameter(Mandatory = $true)] $Config)
|
|
|
|
if ($Config.account -notmatch '^[A-Za-z0-9@._+-]{6,100}$') {
|
|
throw '支付宝账号格式无效,请使用手机号、邮箱或支付宝登录账号。'
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($Config.password)) {
|
|
throw '支付宝密码不能为空。'
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($Config.accountShort)) {
|
|
throw '支付宝账户简称不能为空。'
|
|
}
|
|
}
|
|
|
|
function Save-AlipayEncryptedAccountConfig {
|
|
param(
|
|
[Parameter(Mandatory = $true)][string] $Account,
|
|
[Parameter(Mandatory = $true)][Security.SecureString] $Password,
|
|
[Parameter(Mandatory = $true)][string] $AccountShort,
|
|
[string] $Path = (Get-AlipayEncryptedConfigPath)
|
|
)
|
|
|
|
$passwordText = ConvertFrom-SecureStringForAlipay $Password
|
|
try {
|
|
$config = [ordered]@{
|
|
account = $Account.Trim()
|
|
password = $passwordText
|
|
accountShort = $AccountShort.Trim()
|
|
}
|
|
Assert-AlipayAccountConfig ([pscustomobject] $config)
|
|
$json = $config | ConvertTo-Json -Compress
|
|
$secureJson = ConvertTo-SecureString -String $json -AsPlainText -Force
|
|
$cipherText = ConvertFrom-SecureString $secureJson
|
|
$directory = Split-Path -Parent $Path
|
|
New-Item -ItemType Directory -Force -Path $directory | Out-Null
|
|
[IO.File]::WriteAllText($Path, $cipherText, [Text.Encoding]::ASCII)
|
|
} finally {
|
|
if ($null -ne $passwordText) {
|
|
$passwordText = $null
|
|
}
|
|
}
|
|
}
|
|
|
|
function Read-AlipayEncryptedAccountConfig {
|
|
param([string] $Path = (Get-AlipayEncryptedConfigPath))
|
|
|
|
$path = $Path
|
|
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
|
|
return $null
|
|
}
|
|
|
|
$cipherText = [IO.File]::ReadAllText($path, [Text.Encoding]::ASCII)
|
|
$secureJson = ConvertTo-SecureString -String $cipherText
|
|
$json = ConvertFrom-SecureStringForAlipay $secureJson
|
|
try {
|
|
$config = $json | ConvertFrom-Json
|
|
Assert-AlipayAccountConfig $config
|
|
return [pscustomobject]@{
|
|
account = [string] $config.account
|
|
password = [string] $config.password
|
|
accountShort = [string] $config.accountShort
|
|
source = 'dpapi'
|
|
}
|
|
} finally {
|
|
if ($null -ne $json) {
|
|
$json = $null
|
|
}
|
|
}
|
|
}
|
|
|
|
function Read-AlipayLegacyAccountConfig {
|
|
$path = Join-Path (Get-AlipayRepositoryRoot) 'runtime\private\敏感信息.md'
|
|
if (-not (Test-Path -LiteralPath $path -PathType Leaf)) {
|
|
return $null
|
|
}
|
|
|
|
$text = [IO.File]::ReadAllText($path, [Text.UTF8Encoding]::new($false))
|
|
$config = [pscustomobject]@{
|
|
account = [regex]::Match($text, '账号:([^\r\n]+)').Groups[1].Value.Trim()
|
|
password = [regex]::Match($text, '密码:([^\r\n]+)').Groups[1].Value.Trim()
|
|
accountShort = [regex]::Match($text, '支付宝账户简称:([^\r\n]+)').Groups[1].Value.Trim()
|
|
source = 'legacy'
|
|
}
|
|
if ([string]::IsNullOrWhiteSpace($config.account) -or
|
|
[string]::IsNullOrWhiteSpace($config.password) -or
|
|
[string]::IsNullOrWhiteSpace($config.accountShort)) {
|
|
return $null
|
|
}
|
|
Assert-AlipayAccountConfig $config
|
|
|
|
return $config
|
|
}
|
|
|
|
function Read-AlipayAccountConfig {
|
|
$config = Read-AlipayEncryptedAccountConfig
|
|
if ($null -ne $config) {
|
|
return $config
|
|
}
|
|
|
|
return Read-AlipayLegacyAccountConfig
|
|
}
|
|
|
|
function Find-AlipayNodeExecutable {
|
|
$nodeCommand = Get-Command node.exe -ErrorAction SilentlyContinue | Select-Object -First 1
|
|
if ($null -ne $nodeCommand) {
|
|
return $nodeCommand.Source
|
|
}
|
|
|
|
$candidates = @(
|
|
(Join-Path $env:ProgramFiles 'nodejs\node.exe'),
|
|
(Join-Path $env:USERPROFILE '.cache\codex-runtimes\codex-primary-runtime\dependencies\node\bin\node.exe')
|
|
)
|
|
return $candidates | Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } | Select-Object -First 1
|
|
}
|
|
|
|
function Stop-AlipayBrowserSessions {
|
|
$profile = Join-Path (Get-AlipayRepositoryRoot) 'runtime\private\alipay-profile'
|
|
$processes = Get-CimInstance Win32_Process -Filter "Name = 'msedge.exe'" |
|
|
Where-Object { $_.CommandLine -like ('*' + $profile + '*') }
|
|
foreach ($process in $processes) {
|
|
Stop-Process -Id $process.ProcessId -Force -ErrorAction SilentlyContinue
|
|
}
|
|
if (@($processes).Count -gt 0) {
|
|
Start-Sleep -Seconds 1
|
|
}
|
|
}
|
|
|
|
function Set-AlipayNodeEnvironment {
|
|
param([Parameter(Mandatory = $true)] $Config)
|
|
|
|
$env:ALIPAY_ACCOUNT = [string] $Config.account
|
|
$env:ALIPAY_PASSWORD = [string] $Config.password
|
|
$env:ALIPAY_ACCOUNT_SHORT = [string] $Config.accountShort
|
|
}
|
|
|
|
function Clear-AlipayNodeEnvironment {
|
|
Remove-Item Env:ALIPAY_ACCOUNT -ErrorAction SilentlyContinue
|
|
Remove-Item Env:ALIPAY_PASSWORD -ErrorAction SilentlyContinue
|
|
Remove-Item Env:ALIPAY_ACCOUNT_SHORT -ErrorAction SilentlyContinue
|
|
}
|