Use agent's builtin support for services instead (#357)

This commit is contained in:
UUBulb
2024-05-21 23:11:43 +08:00
committed by GitHub
parent f5ca66d924
commit eaba7abd35
8 changed files with 343 additions and 179 deletions

View File

@@ -7,64 +7,69 @@ if($PSVersionTable.PSVersion.Major -lt 5){
exit
}
$agentrepo = "nezhahq/agent"
$nssmrepo = "nezhahq/nssm-backup"
# x86 or x64
# x86 or x64 or arm64
if ([System.Environment]::Is64BitOperatingSystem) {
$file = "nezha-agent_windows_amd64.zip"
if ($env:PROCESSOR_ARCHITECTURE -eq "ARM64") {
$file = "nezha-agent_windows_arm64.zip"
} else {
$file = "nezha-agent_windows_amd64.zip"
}
}
else {
$file = "nezha-agent_windows_386.zip"
}
$agentreleases = "https://api.github.com/repos/$agentrepo/releases"
$nssmreleases = "https://api.github.com/repos/$nssmrepo/releases"
#重复运行自动更新
if (Test-Path "C:\nezha") {
if (Test-Path "C:\nezha\nezha-agent.exe") {
Write-Host "Nezha monitoring already exists, delete and reinstall" -BackgroundColor DarkGreen -ForegroundColor White
C:/nezha/nssm.exe stop nezha
C:/nezha/nssm.exe remove nezha
C:\nezha\nezha-agent.exe service uninstall
Remove-Item "C:\nezha" -Recurse
}
#TLS/SSL
Write-Host "Determining latest nezha release" -BackgroundColor DarkGreen -ForegroundColor White
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$agenttag = (Invoke-WebRequest -Uri $agentreleases -UseBasicParsing | ConvertFrom-Json)[0].tag_name
$nssmtag = (Invoke-WebRequest -Uri $nssmreleases -UseBasicParsing | ConvertFrom-Json)[0].tag_name
if ([string]::IsNullOrWhiteSpace($agenttag)) {
$optionUrl = "https://fastly.jsdelivr.net/gh/nezhahq/agent/"
Try {
$response = Invoke-WebRequest -Uri $optionUrl -UseBasicParsing -TimeoutSec 10
if ($response.StatusCode -eq 200) {
$versiontext = $response.Content | findstr /c:"option.value"
$version = [regex]::Match($versiontext, "@(\d+\.\d+\.\d+)").Groups[1].Value
$agenttag = "v" + $version
}
} Catch {
$optionUrl = "https://gcore.jsdelivr.net/gh/nezhahq/agent/"
$response = Invoke-WebRequest -Uri $optionUrl -UseBasicParsing -TimeoutSec 10
if ($response.StatusCode -eq 200) {
$versiontext = $response.Content | findstr /c:"option.value"
$version = [regex]::Match($versiontext, "@(\d+\.\d+\.\d+)").Groups[1].Value
$agenttag = "v" + $version
}
}
}
#Region判断
$ipapi= Invoke-RestMethod -Uri "https://api.myip.com/" -UserAgent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.163 Safari/535.1"
$region=$ipapi.cc
echo $ipapi
if($region -ne "CN"){
$download = "https://github.com/$agentrepo/releases/download/$agenttag/$file"
$nssmdownload="https://github.com/$nssmrepo/releases/download/$nssmtag/nssm.zip"
Write-Host "Location:$region,connect directly!" -BackgroundColor DarkRed -ForegroundColor Green
}else{
$download = "https://github.com/$agentrepo/releases/download/$agenttag/$file"
$nssmdownload="https://github.com/$nssmrepo/releases/download/$nssmtag/nssm.zip"
Write-Host "Location:CN,use mirror address" -BackgroundColor DarkRed -ForegroundColor Green
}
echo $download
echo $nssmdownload
Invoke-WebRequest $download -OutFile "C:\nezha.zip"
#使用nssm安装服务
Invoke-WebRequest $nssmdownload -OutFile "C:\nssm.zip"
#解压
Expand-Archive "C:\nezha.zip" -DestinationPath "C:\temp" -Force
Expand-Archive "C:\nssm.zip" -DestinationPath "C:\temp" -Force
if (!(Test-Path "C:\nezha")) { New-Item -Path "C:\nezha" -type directory }
#整理文件
Move-Item -Path "C:\temp\nezha-agent.exe" -Destination "C:\nezha\nezha-agent.exe"
if ($file = "nezha-agent_windows_amd64.zip") {
Move-Item -Path "C:\temp\nssm-2.24\win64\nssm.exe" -Destination "C:\nezha\nssm.exe"
}
else {
Move-Item -Path "C:\temp\nssm-2.24\win32\nssm.exe" -Destination "C:\nezha\nssm.exe"
}
#清理垃圾
Remove-Item "C:\nezha.zip"
Remove-Item "C:\nssm.zip"
Remove-Item "C:\temp" -Recurse
#安装部分
C:\nezha\nssm.exe install nezha C:\nezha\nezha-agent.exe -s $server -p $key $tls -d
C:\nezha\nssm.exe start nezha
C:\nezha\nezha-agent.exe service install -s $server -p $key $tls
#enjoy
Write-Host "Enjoy It!" -BackgroundColor DarkGreen -ForegroundColor Red
Write-Host "Enjoy It!" -BackgroundColor DarkGreen -ForegroundColor Red