init v1 script (#8)
* init v1 script * . * complete dashboard script * init agent script (posix) * complete agent script (posix)
This commit is contained in:
87
agent/install.ps1
Normal file
87
agent/install.ps1
Normal file
@ -0,0 +1,87 @@
|
||||
#Get server and key
|
||||
param($server, $key, $tls)
|
||||
# Download latest release from github
|
||||
if($PSVersionTable.PSVersion.Major -lt 5){
|
||||
Write-Host "Require PS >= 5,your PSVersion:"$PSVersionTable.PSVersion.Major -BackgroundColor DarkGreen -ForegroundColor White
|
||||
Write-Host "Refer to the community article and install manually! https://nyko.me/2020/12/13/nezha-windows-client.html" -BackgroundColor DarkRed -ForegroundColor Green
|
||||
exit
|
||||
}
|
||||
$agentrepo = "nezhahq/agent"
|
||||
# x86 or x64 or arm64
|
||||
if ([System.Environment]::Is64BitOperatingSystem) {
|
||||
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"
|
||||
#重复运行自动更新
|
||||
if (Test-Path "C:\nezha\nezha-agent.exe") {
|
||||
Write-Host "Nezha monitoring already exists, delete and reinstall" -BackgroundColor DarkGreen -ForegroundColor White
|
||||
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
|
||||
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 = ""
|
||||
$region = "Unknown"
|
||||
foreach ($url in ("https://dash.cloudflare.com/cdn-cgi/trace","https://developers.cloudflare.com/cdn-cgi/trace","https://1.0.0.1/cdn-cgi/trace")) {
|
||||
try {
|
||||
$ipapi = Invoke-RestMethod -Uri $url -TimeoutSec 5 -UseBasicParsing
|
||||
if ($ipapi -match "loc=(\w+)" ) {
|
||||
$region = $Matches[1]
|
||||
break
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Write-Host "Error occurred while querying $url : $_"
|
||||
}
|
||||
}
|
||||
echo $ipapi
|
||||
if($region -ne "CN"){
|
||||
$download = "https://github.com/$agentrepo/releases/download/$agenttag/$file"
|
||||
Write-Host "Location:$region,connect directly!" -BackgroundColor DarkRed -ForegroundColor Green
|
||||
}else{
|
||||
$download = "https://gitee.com/naibahq/agent/releases/download/$agenttag/$file"
|
||||
Write-Host "Location:CN,use mirror address" -BackgroundColor DarkRed -ForegroundColor Green
|
||||
}
|
||||
echo $download
|
||||
Invoke-WebRequest $download -OutFile "C:\nezha.zip"
|
||||
#解压
|
||||
Expand-Archive "C:\nezha.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"
|
||||
#清理垃圾
|
||||
Remove-Item "C:\nezha.zip"
|
||||
Remove-Item "C:\temp" -Recurse
|
||||
#安装部分
|
||||
C:\nezha\nezha-agent.exe service install -s $server -p $key $tls
|
||||
#enjoy
|
||||
Write-Host "Enjoy It!" -BackgroundColor DarkGreen -ForegroundColor Red
|
||||
165
agent/install.sh
Normal file
165
agent/install.sh
Normal file
@ -0,0 +1,165 @@
|
||||
#!/bin/sh
|
||||
|
||||
NZ_BASE_PATH="/opt/nezha"
|
||||
NZ_AGENT_PATH="${NZ_BASE_PATH}/agent"
|
||||
|
||||
red='\033[0;31m'
|
||||
green='\033[0;32m'
|
||||
plain='\033[0m'
|
||||
|
||||
err() {
|
||||
printf "${red}%s${plain}\n" "$*" >&2
|
||||
}
|
||||
|
||||
success() {
|
||||
printf "${green}%s${plain}\n" "$*"
|
||||
}
|
||||
|
||||
sudo() {
|
||||
myEUID=$(id -ru)
|
||||
if [ "$myEUID" -ne 0 ]; then
|
||||
if command -v sudo > /dev/null 2>&1; then
|
||||
command sudo "$@"
|
||||
else
|
||||
err "ERROR: sudo is not installed on the system, the action cannot be proceeded."
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
"$@"
|
||||
fi
|
||||
}
|
||||
|
||||
deps_check() {
|
||||
deps="wget unzip grep"
|
||||
set -- "$api_list"
|
||||
for dep in $deps; do
|
||||
if ! command -v "$dep" >/dev/null 2>&1; then
|
||||
err "$dep not found, please install it first."
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
geo_check() {
|
||||
api_list="https://blog.cloudflare.com/cdn-cgi/trace https://developers.cloudflare.com/cdn-cgi/trace"
|
||||
ua="Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0"
|
||||
set -- "$api_list"
|
||||
for url in $api_list; do
|
||||
text="$(curl -A "$ua" -m 10 -s "$url")"
|
||||
endpoint="$(echo "$text" | sed -n 's/.*h=\([^ ]*\).*/\1/p')"
|
||||
if echo "$text" | grep -qw 'CN'; then
|
||||
isCN=true
|
||||
break
|
||||
elif echo "$url" | grep -q "$endpoint"; then
|
||||
break
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
env_check() {
|
||||
mach=$(uname -m)
|
||||
case "$mach" in
|
||||
amd64)
|
||||
os_arch="amd64"
|
||||
;;
|
||||
i386|i686)
|
||||
os_arch="386"
|
||||
;;
|
||||
aarch64|arm64)
|
||||
os_arch="arm64"
|
||||
;;
|
||||
*arm*)
|
||||
os_arch="arm"
|
||||
;;
|
||||
s390x)
|
||||
os_arch="s390x"
|
||||
;;
|
||||
riscv64)
|
||||
os_arch="riscv64"
|
||||
;;
|
||||
mips)
|
||||
os_arch="mips"
|
||||
;;
|
||||
mipsel|mipsle)
|
||||
os_arch="mipsle"
|
||||
;;
|
||||
*)
|
||||
err "Unknown architecture: $uname"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
system=$(uname)
|
||||
case "$system" in
|
||||
*Linux*)
|
||||
os="linux"
|
||||
;;
|
||||
*Darwin*)
|
||||
os="darwin"
|
||||
;;
|
||||
*FreeBSD*)
|
||||
os="freebsd"
|
||||
;;
|
||||
*)
|
||||
err "Unknown architecture: $system"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
init() {
|
||||
deps_check
|
||||
env_check
|
||||
|
||||
## China_IP
|
||||
if [ -z "$CN" ]; then
|
||||
geo_check
|
||||
if [ -n "$isCN" ]; then
|
||||
CN=true
|
||||
fi
|
||||
fi
|
||||
|
||||
if [ -z "$CN" ]; then
|
||||
GITHUB_URL="github.com"
|
||||
else
|
||||
GITHUB_URL="gitee.com"
|
||||
fi
|
||||
}
|
||||
|
||||
install() {
|
||||
echo "Installing..."
|
||||
|
||||
if [ -z "$CN" ]; then
|
||||
NZ_AGENT_URL="https://${GITHUB_URL}/nezhahq/agent/releases/latest/download/nezha-agent_linux_${os_arch}.zip"
|
||||
else
|
||||
NZ_AGENT_URL="https://${GITHUB_URL}/naibahq/agent/releases/latest/download/nezha-agent_linux_${os_arch}.zip"
|
||||
fi
|
||||
|
||||
_cmd="wget -t 2 -T 60 -O /tmp/nezha-agent_${os}_${os_arch}.zip $NZ_AGENT_URL >/dev/null 2>&1"
|
||||
if ! eval "$_cmd"; then
|
||||
err "Download nezha-agent release failed, check your network connectivity"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sudo unzip -qo /tmp/nezha-agent_${os}_${os_arch}.zip -d $NZ_AGENT_PATH &&
|
||||
sudo rm -rf /tmp/nezha-agent_${os}_${os_arch}.zip
|
||||
|
||||
path="$NZ_AGENT_PATH/config.yml"
|
||||
if [ -f "$path" ]; then
|
||||
random=$(LC_ALL=C tr -dc a-z0-9 </dev/urandom | head -c 5)
|
||||
path=$(printf "%s" "$NZ_AGENT_PATH/config-$random.yml")
|
||||
fi
|
||||
|
||||
env="NZ_SERVER=$NZ_SERVER NZ_CLIENT_SECRET=$NZ_CLIENT_SECRET NZ_TLS=$NZ_TLS NZ_DISABLE_AUTO_UPDATE=$NZ_DISABLE_AUTO_UPDATE NZ_DISABLE_FORCE_UPDATE=$DISABLE_FORCE_UPDATE NZ_DISABLE_COMMAND_EXECUTE=$NZ_DISABLE_COMMAND_EXECUTE NZ_SKIP_CONNECTION_COUNT=$NZ_SKIP_CONNECTION_COUNT"
|
||||
|
||||
_cmd="sudo $env $NZ_AGENT_PATH/nezha-agent service -c "$path" install"
|
||||
if ! eval "$_cmd"; then
|
||||
err "Install nezha-agent service failed"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
success "nezha-agent successfully installed"
|
||||
}
|
||||
|
||||
init
|
||||
install
|
||||
Reference in New Issue
Block a user