|
| 1 | +# Alist Manager Script for Windows |
| 2 | +# Version: 3.57.0 (Random password only on first install) |
| 3 | + |
| 4 | +# ----------------------------- |
| 5 | +# Auto-detect PowerShell version and set output encoding |
| 6 | +$psVersion = $PSVersionTable.PSVersion.Major |
| 7 | +if ($psVersion -ge 7) { |
| 8 | + chcp 65001 > $null |
| 9 | + [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 |
| 10 | + $EncName = "UTF-8" |
| 11 | +} else { |
| 12 | + chcp 936 > $null |
| 13 | + [Console]::OutputEncoding = [System.Text.Encoding]::GetEncoding(936) |
| 14 | + $EncName = "GBK" |
| 15 | +} |
| 16 | +Write-Host "Current terminal encoding: $EncName" -ForegroundColor Yellow |
| 17 | +# ----------------------------- |
| 18 | + |
| 19 | +param($Action, $InstallPath) |
| 20 | +if (-not $Action) { $Action = "menu" } |
| 21 | +if (-not $InstallPath) { $InstallPath = "C:\alist" } |
| 22 | + |
| 23 | +# ----------------------------- |
| 24 | +# Colors & constants |
| 25 | +$Green = "Green" |
| 26 | +$Red = "Red" |
| 27 | +$Yellow = "Yellow" |
| 28 | +$White = "White" |
| 29 | +$ServiceName = "AlistService" |
| 30 | +$nssmPath = "$InstallPath\nssm.exe" |
| 31 | + |
| 32 | +# ----------------------------- |
| 33 | +function Write-Info($msg, $color="White") { |
| 34 | + $validColors = @("Black","DarkBlue","DarkGreen","DarkCyan","DarkRed", |
| 35 | + "DarkMagenta","DarkYellow","Gray","DarkGray","Blue", |
| 36 | + "Green","Cyan","Red","Magenta","Yellow","White") |
| 37 | + if ($validColors -contains $color) { |
| 38 | + [Console]::ForegroundColor = $color |
| 39 | + [Console]::WriteLine($msg) |
| 40 | + [Console]::ResetColor() |
| 41 | + } else { |
| 42 | + [Console]::WriteLine($msg) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function Pause-English { |
| 47 | + Write-Host "Press Enter to continue..." -ForegroundColor Yellow |
| 48 | + [void][System.Console]::ReadLine() |
| 49 | +} |
| 50 | + |
| 51 | +function Download-File($url, $output) { |
| 52 | + Try { |
| 53 | + Invoke-WebRequest -Uri $url -OutFile $output -UseBasicParsing -TimeoutSec 30 |
| 54 | + return $true |
| 55 | + } Catch { |
| 56 | + Write-Info "Download failed: $url" $Red |
| 57 | + return $false |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function Get-LatestVersion { |
| 62 | + $apiUrl = "https://api.github.com/repos/alist-org/alist/releases/latest" |
| 63 | + Try { |
| 64 | + $json = Invoke-RestMethod -Uri $apiUrl -Headers @{ "User-Agent" = "PowerShell" } -TimeoutSec 10 |
| 65 | + return $json.tag_name.TrimStart("v") |
| 66 | + } Catch { |
| 67 | + Write-Info "Failed to fetch version info, using default version 3.52.0" $Yellow |
| 68 | + return "3.52.0" |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +# ----------------------------- |
| 73 | +# Get real physical network adapter IPv4 |
| 74 | +function Get-LocalIP { |
| 75 | + $realNICs = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { |
| 76 | + $_.IPAddress -ne "127.0.0.1" -and |
| 77 | + $_.IPAddress -notlike "169.*" -and |
| 78 | + $_.InterfaceAlias -notmatch "vEthernet|Virtual|VMware|Loopback|WSL|Hyper-V" -and |
| 79 | + $_.ValidLifetime -ne "Infinite" |
| 80 | + } |
| 81 | + |
| 82 | + $ip = $realNICs | Sort-Object InterfaceMetric | Select-Object -First 1 -ExpandProperty IPAddress |
| 83 | + |
| 84 | + if (-not $ip) { $ip = "127.0.0.1" } |
| 85 | + return $ip |
| 86 | +} |
| 87 | + |
| 88 | +# ----------------------------- |
| 89 | +function Install-NSSM { |
| 90 | + if (-Not (Test-Path $nssmPath)) { |
| 91 | + $tmpZip = "$env:TEMP\nssm.zip" |
| 92 | + $tmpDir = "$env:TEMP\nssm" |
| 93 | + Write-Info "Downloading nssm ..." $Green |
| 94 | + if (-Not (Download-File "https://nssm.cc/release/nssm-2.24.zip" $tmpZip)) { |
| 95 | + Write-Info "nssm download failed, please manually download nssm-2.24.zip and place it in $InstallPath" $Red |
| 96 | + exit 1 |
| 97 | + } |
| 98 | + if (Test-Path $tmpDir) { Remove-Item $tmpDir -Recurse -Force } |
| 99 | + Expand-Archive $tmpZip -DestinationPath $tmpDir -Force |
| 100 | + |
| 101 | + $nssmExeSrc = Get-ChildItem -Path $tmpDir -Recurse -Filter "nssm.exe" | Where-Object { $_.FullName -match "win64" } | Select-Object -First 1 |
| 102 | + if (-Not $nssmExeSrc) { |
| 103 | + Write-Info "nssm.exe not found" $Red |
| 104 | + exit 1 |
| 105 | + } |
| 106 | + |
| 107 | + Copy-Item $nssmExeSrc.FullName $InstallPath -Force |
| 108 | + Remove-Item $tmpZip -Force |
| 109 | + Remove-Item $tmpDir -Recurse -Force |
| 110 | + Write-Info "nssm installation completed" $Green |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function Get-Arch { |
| 115 | + switch ($env:PROCESSOR_ARCHITECTURE) { |
| 116 | + "AMD64" { return "amd64" } |
| 117 | + "ARM64" { return "arm64" } |
| 118 | + default { return "386" } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function Install-Alist { |
| 123 | + if (-Not (Test-Path $InstallPath)) { New-Item -ItemType Directory -Path $InstallPath | Out-Null } |
| 124 | + |
| 125 | + $arch = Get-Arch |
| 126 | + Write-Info "Detected CPU architecture: $arch" $Green |
| 127 | + |
| 128 | + $version = Get-LatestVersion |
| 129 | + Write-Info "Latest version: $version" $Green |
| 130 | + |
| 131 | + # Official mirror URL (based on CPU arch) |
| 132 | + $filename = "alist-$version-windows-$arch.zip" |
| 133 | + $officialUrl = "https://alistgo.com/download/Alist/v$version/$filename" |
| 134 | + $tmpZip = "$env:TEMP\alist.zip" |
| 135 | + Write-Info "Trying to download from official mirror: $officialUrl" $Green |
| 136 | + $success = Download-File $officialUrl $tmpZip |
| 137 | + |
| 138 | + if (-not $success) { |
| 139 | + Write-Info "Official mirror download failed!" $Yellow |
| 140 | + Write-Info "Do you want to download from GitHub instead?" $Green |
| 141 | + Write-Info "1 = GitHub default URL" $Green |
| 142 | + Write-Info "2 = GitHub proxy" $Green |
| 143 | + $choice = Read-Host "Choose download source [1-2] (default 1)" |
| 144 | + if ($choice -eq "2") { |
| 145 | + $proxyInput = Read-Host "Enter proxy URL (https://..., end with /)" |
| 146 | + if ($proxyInput) { |
| 147 | + $ghProxy = $proxyInput |
| 148 | + $downloadBase = "${ghProxy}https://github.com/alist-org/alist/releases/latest/download" |
| 149 | + } else { |
| 150 | + $downloadBase = "https://github.com/alist-org/alist/releases/latest/download" |
| 151 | + } |
| 152 | + } else { |
| 153 | + $downloadBase = "https://github.com/alist-org/alist/releases/latest/download" |
| 154 | + } |
| 155 | + |
| 156 | + $url = "$downloadBase/$filename" |
| 157 | + Write-Info "Downloading from GitHub: $url" $Green |
| 158 | + if (-Not (Download-File $url $tmpZip)) { |
| 159 | + Write-Info "GitHub download failed! Please check your network or proxy" $Red |
| 160 | + exit 1 |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + # Extract |
| 165 | + Expand-Archive -Path $tmpZip -DestinationPath $InstallPath -Force |
| 166 | + Remove-Item $tmpZip -Force |
| 167 | + Write-Info "Alist installed at $InstallPath" $Green |
| 168 | +} |
| 169 | + |
| 170 | +# ----------------------------- |
| 171 | +function Invoke-AlistAdminRandom { |
| 172 | + if (-Not (Test-Path "$InstallPath\alist.exe")) { |
| 173 | + throw "$InstallPath\alist.exe not found. Please install Alist first." |
| 174 | + } |
| 175 | + Push-Location $InstallPath |
| 176 | + try { |
| 177 | + $output = & "$InstallPath\alist.exe" admin random 2>&1 |
| 178 | + } finally { |
| 179 | + Pop-Location |
| 180 | + } |
| 181 | + |
| 182 | + $uMatch = ($output | Select-String -Pattern 'username:\s*(\S+)' -AllMatches).Matches | Select-Object -First 1 |
| 183 | + $pMatch = ($output | Select-String -Pattern 'password:\s*(\S+)' -AllMatches).Matches | Select-Object -First 1 |
| 184 | + if (-not $uMatch -or -not $pMatch) { |
| 185 | + throw "Failed to parse random username/password. Command output:`n$output" |
| 186 | + } |
| 187 | + $username = $uMatch.Groups[1].Value |
| 188 | + $password = $pMatch.Groups[1].Value |
| 189 | + return @{ Username = $username; Password = $password; Raw = $output } |
| 190 | +} |
| 191 | + |
| 192 | +# ----------------------------- |
| 193 | +function Service-InstallAndStart { |
| 194 | + if (-Not (Test-Path "$InstallPath\alist.exe")) { |
| 195 | + Write-Info "Please install Alist first before registering the service" $Red |
| 196 | + exit 1 |
| 197 | + } |
| 198 | + |
| 199 | + Install-NSSM |
| 200 | + Write-Info "Registering Windows service $ServiceName ..." $Green |
| 201 | + & $nssmPath install $ServiceName "$InstallPath\alist.exe" "server" |
| 202 | + & $nssmPath set $ServiceName Start SERVICE_AUTO_START |
| 203 | + |
| 204 | + Write-Info "Starting service $ServiceName ..." $Green |
| 205 | + & $nssmPath start $ServiceName |
| 206 | + |
| 207 | + Write-Info "First install, generating random admin username/password..." $Green |
| 208 | + try { |
| 209 | + $creds = Invoke-AlistAdminRandom |
| 210 | + Write-Info "Username: $($creds.Username)" $Green |
| 211 | + Write-Info "Password: $($creds.Password)" $Green |
| 212 | + } catch { |
| 213 | + Write-Info $_.Exception.Message $Red |
| 214 | + Write-Info "Random generation failed (service started). You can retry manually: alist.exe admin random" $Yellow |
| 215 | + } |
| 216 | + |
| 217 | + Write-Info "Login URL: http://$(Get-LocalIP):5244" $Yellow |
| 218 | +} |
| 219 | + |
| 220 | +function Service-Start { |
| 221 | + Install-NSSM |
| 222 | + Write-Info "Starting service $ServiceName ..." $Green |
| 223 | + & $nssmPath start $ServiceName |
| 224 | + Write-Info "Service started. Password will not change automatically. To reset: alist.exe admin random" $Yellow |
| 225 | + Write-Info "Login URL: http://$(Get-LocalIP):5244" $Yellow |
| 226 | +} |
| 227 | + |
| 228 | +function Service-Stop { Install-NSSM; & $nssmPath stop $ServiceName } |
| 229 | +function Service-Restart { Service-Stop; Start-Sleep -Seconds 2; Service-Start } |
| 230 | +function Service-Remove { |
| 231 | + Install-NSSM |
| 232 | + Write-Info "Removing Windows service $ServiceName ..." $Yellow |
| 233 | + & $nssmPath stop $ServiceName | Out-Null |
| 234 | + & $nssmPath remove $ServiceName confirm | Out-Null |
| 235 | + Write-Info "Service removed" $Green |
| 236 | +} |
| 237 | +function Service-Status { Install-NSSM; & $nssmPath status $ServiceName } |
| 238 | + |
| 239 | +# ----------------------------- |
| 240 | +function Show-Menu { |
| 241 | + while ($true) { |
| 242 | + Clear-Host |
| 243 | + Write-Info "`n=== Alist Windows Manager ===`n" $Green |
| 244 | + Write-Info "1. Install Alist" $White |
| 245 | + Write-Info "2. Update Alist" $White |
| 246 | + Write-Info "3. Uninstall Alist" $White |
| 247 | + Write-Info "-------------------------" $White |
| 248 | + Write-Info "4. Register and start service (random admin on first install)" $White |
| 249 | + Write-Info "5. Remove Windows service" $White |
| 250 | + Write-Info "6. Start service (no password change)" $White |
| 251 | + Write-Info "7. Stop service" $White |
| 252 | + Write-Info "8. Restart service (no password change)" $White |
| 253 | + Write-Info "9. Show service status" $White |
| 254 | + Write-Info "-------------------------" $White |
| 255 | + Write-Info "0. Exit" $White |
| 256 | + $choice = Read-Host "Enter your choice" |
| 257 | + |
| 258 | + switch ($choice) { |
| 259 | + "1" { Install-Alist; Pause-English } |
| 260 | + "2" { Install-Alist; Pause-English } |
| 261 | + "3" { Remove-Item -Recurse -Force $InstallPath; Write-Info "Uninstalled" ; Pause-English } |
| 262 | + "4" { Service-InstallAndStart; Pause-English } |
| 263 | + "5" { Service-Remove; Pause-English } |
| 264 | + "6" { Service-Start; Pause-English } |
| 265 | + "7" { Service-Stop; Pause-English } |
| 266 | + "8" { Service-Restart; Pause-English } |
| 267 | + "9" { Service-Status; Pause-English } |
| 268 | + "0" { exit 0 } |
| 269 | + default { Write-Info "Invalid choice" $Red; Pause-English } |
| 270 | + } |
| 271 | + } |
| 272 | +} |
| 273 | + |
| 274 | +# ----------------------------- |
| 275 | +switch ($Action) { |
| 276 | + "install" { Install-Alist } |
| 277 | + "update" { Install-Alist } |
| 278 | + "uninstall" { Remove-Item -Recurse -Force $InstallPath; Write-Info "Uninstalled" } |
| 279 | + "service-install" { Service-InstallAndStart } |
| 280 | + "service-remove" { Service-Remove } |
| 281 | + "start" { Service-Start } |
| 282 | + "stop" { Service-Stop } |
| 283 | + "restart" { Service-Restart } |
| 284 | + "status" { Service-Status } |
| 285 | + "menu" { Show-Menu } |
| 286 | + default { Show-Menu } |
| 287 | +} |
0 commit comments