Proxy/Install.ps1

$BaseDir = Split-Path -Path $PSScriptRoot -Parent

. (Join-Path $PSScriptRoot 'ConfigLoader.ps1')
. (Join-Path $PSScriptRoot 'Dependencies.ps1')
. (Join-Path $PSScriptRoot 'FRPPanel.ps1')
. (Join-Path $PSScriptRoot 'InstallHelpers.ps1')
. (Join-Path $PSScriptRoot 'OpenSSL.ps1')
. (Join-Path $BaseDir 'Generic' 'NSSM.ps1')

$Config = Get-InitConfig (Join-Path $BaseDir 'config' 'paths.yaml')

function Install-DoremyProxy {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Alias('s')]
        [Parameter(Mandatory)]
        [string]$Secret,

        [Alias('i')]
        [Parameter(Mandatory)]
        [string]$Identity,

        [Alias('r')]
        [Parameter(Mandatory)]
        [string]$Remote,

        [Alias('p')]
        [Parameter(Mandatory=$false)]
        [string]$Path = $Config.frpp.config_path,

        [Parameter(Mandatory=$false)]
        [switch] $PrunePreviousInstallation
    )
    if ($PrunePreviousInstallation) {
        Remove-Proxy
    }
    $frppInstallDir = Split-Path -Path $Config.frpp.path -Parent

    $opensslPath = 
        Install-Dependencies `
        $frppInstallDir `
        -WhatIf:$WhatIfPreference
    
    Save-FRPConfig `
        -Secret $Secret `
        -Identity $Identity `
        -Remote $Remote `
        -Path $Path

    Write-Host $Config.frpp.path
    New-NssmService `
        -ServiceName $Config.frpp.service_name `
        -ExecutablePath $Config.frpp.path `
        -Arguments "-s $Secret -i $Identity --rpc-url $Remote" `
        -StdOutLog $Config.frpp.stdout_log `
        -StdErrLog $Config.frpp.stderr_log `
        -Rotate $Config.frpp.rotate `
        -RotateBytes $Config.frpp.rotate_bytes `
        -Start `
        -WhatIf:$WhatIfPreference

    Add-ExeScheduledTask `
        -TaskName 'Proxy script update' `
        -ExecutablePath (Join-Path $BaseDir 'bin' 'NoGui.exe') `
        -Arguments @(
            '--',
            "'C:\Program Files\PowerShell\7\pwsh.exe'",
            '-Command',
            "Update-Module Doremy"
        )
}