Proxy/InstallHelpers.ps1

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

function Add-ExeScheduledTask {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [string] $TaskName,           # The name of the scheduled task

        [Parameter(Mandatory)]
        [string] $ExecutablePath,     # Full path to the .exe to run

        [Parameter()]
        [string[]] $Arguments,        # Optional arguments for the executable

        [Parameter()]
        [datetime] $StartTime = (Get-Date).Date.AddHours(3),  # Default start today at 3 AM

        [Parameter()]
        [int] $DaysInterval = 3       # How often to run the task, in days (default = 3)
    )

    Begin {
        # Build action for the scheduled task
        $action = New-ScheduledTaskAction -Execute $ExecutablePath -Argument ($Arguments -join ' ')

        # Build a daily trigger that runs every $DaysInterval days at the specified time
        $trigger = New-ScheduledTaskTrigger -Daily -DaysInterval $DaysInterval -At $StartTime

        # Define principal to run with highest privileges (SYSTEM account)
        $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
    }

    Process {
        try {
            # Register the new scheduled task
            Register-ScheduledTask -TaskName $TaskName `
                                   -Action $action `
                                   -Trigger $trigger `
                                   -Principal $principal `
                                   -Description "Runs $ExecutablePath every $DaysInterval days."

            Write-Verbose "Task '$TaskName' successfully registered to run every $DaysInterval days."
        }
        catch {
            Write-Error "Failed to register task '$TaskName': $_"
        }
    }
}

function Build-NoGui {
    param (
        [Parameter(Mandatory)]
        [string] $DestinationPath
    )
    
    Write-Host 'Building ' -NoNewline
    Write-Host 'NoGui.exe ' -ForegroundColor Cyan -NoNewline
    Write-Host 'in Powershell 5.1'
    $externalDataDir = (Join-Path $BaseDir 'External')
    powershell.exe (Join-Path $externalDataDir 'NoGui.ps1')
    Move-Item (Join-Path (Get-Location) 'NoGui.exe') $DestinationPath -Force
}

function Remove-Proxy {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Mandatory=$false)]
        [string] $AppRegPath = 'HKLM:\SOFTWARE\Doremy\Proxy',
        [Parameter(Mandatory=$false)]
        [string] $ProgramDataPath = (Join-Path $env:ProgramData 'doremy'),
        [Parameter(Mandatory=$false)]
        [string] $FRPServiceName = "Fast Reverse Proxy",
        [Parameter(Mandatory=$false)]
        [string] $VLESSServiceName = "VLESS"
    )

    Write-Host "Purging program data" -ForegroundColor Red
    Write-Host "Removing all services"
    if ($PSCmdlet.ShouldProcess("NSSM", `
        "stop $FRPServiceName; remove $FRPServiceName confirm")) {
        nssm stop $FRPServiceName *> out-null
        nssm remove $FRPServiceName confirm *> out-null
    }
    if ($PSCmdlet.ShouldProcess("NSSM", `
        "stop $VLESSServiceName; remove $VLESSServiceName confirm")) {
        nssm stop $VLESSServiceName *> out-null
        nssm remove $VLESSServiceName confirm *> out-null
    }
    Write-Host "Removing registry keys and program data"
    if ($PSCmdlet.ShouldProcess("$AppRegPath; $AppDataPath", `
        "Removing registry keys and program data")) {
        Remove-Item `
            -Path $AppRegPath, $ProgramDataPath `
            -Recurse `
            -Force `
            -ErrorAction SilentlyContinue
    }
    Write-Host "Done"
}