Skip to content

Instantly share code, notes, and snippets.

@hagerliu
Last active November 18, 2023 05:02
Show Gist options
  • Select an option

  • Save hagerliu/c4ba5d310e0095a0fc67d894da8f7dd6 to your computer and use it in GitHub Desktop.

Select an option

Save hagerliu/c4ba5d310e0095a0fc67d894da8f7dd6 to your computer and use it in GitHub Desktop.

Revisions

  1. hagerliu revised this gist Nov 18, 2023. 1 changed file with 12 additions and 9 deletions.
    21 changes: 12 additions & 9 deletions GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,10 @@ $url = $baseDownloadUrl + "sing-box-geodata.zip"
    $outputFile = Join-Path $tempDirectory "sing-box-geodata.zip"
    $sha256sumUrl = $url + ".sha256sum"

    # 设置clashApi参数
    $ip_port = ""
    $secret = ""

    # 设置重试次数
    $maxRetries = 3
    $currentRetry = 0
    @@ -82,13 +86,12 @@ if ($currentRetry -eq $maxRetries) {
    Remove-Item $outputFile
    Remove-Item "$outputFile.sha256sum"

    # 结束进程
    Stop-Process -Name $processName

    # 等待一段时间
    Start-Sleep -Seconds 10

    # 启动程序
    Start-Process -FilePath $programPath
    Write-Host "$processName 已重新启动。"
    #重载配置
    $endpoint = "http://$ip_port/configs?force=true"
    try {
    $response = Invoke-RestMethod -Uri $endpoint -Method Put -Headers @{"Authorization" = "Bearer $secret"} -Body '{"path": "", "payload": ""}' -ErrorAction Stop
    Write-Host "$bin_name 配置重载成功。"
    } catch {
    Write-Host "$bin_name 配置重载失败!"
    }
    }
  2. hagerliu revised this gist Nov 17, 2023. 1 changed file with 18 additions and 23 deletions.
    41 changes: 18 additions & 23 deletions GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    $OutputEncoding = [System.Text.Encoding]::UTF8

    # 设置代理服务器地址和端口
    $proxy = "http://127.0.0.1:2080"

    @@ -15,36 +17,29 @@ $url = $baseDownloadUrl + "sing-box-geodata.zip"
    $outputFile = Join-Path $tempDirectory "sing-box-geodata.zip"
    $sha256sumUrl = $url + ".sha256sum"

    # 设置geosite路径
    $geositeDbPath = [System.IO.Path]::Combine($targetDirectory, "geosite.db")

    # 设置重试次数
    $maxRetries = 3
    $currentRetry = 0

    # 设置程序路径
    $programPath = Join-Path $targetDirectory "nekoray.exe"

    # 获取进程名
    $processName = [System.IO.Path]::GetFileNameWithoutExtension($programPath)

    # 检查进程是否正在运行
    $runningProcess = Get-Process -Name $processName -ErrorAction SilentlyContinue

    # 检查geosite文件的修改时间和是否存在
    if (-not $geositeDbPath) {
    Write-Host "目标路径为空。"
    # 继续执行脚本,因为文件不存在
    } elseif (Test-Path $geositeDbPath) {
    # 判断是否需要执行脚本
    $geositeDbLastWriteTime = (Get-Item $geositeDbPath).LastWriteTime
    $programPath = "C:\portapps\nekoray\nekoray.exe"
    $processName = (Get-Item $programPath).BaseName

    # 判断程序运行和geosite文件的修改时间
    try {
    $runningProcesses = Get-Process -Name $processName -ErrorAction Stop

    $geositeDbCreationTime = (Get-Item (Join-Path $targetDirectory "geosite.db")).CreationTime
    $midnight4AM = (Get-Date).Date.AddHours(4)

    if ($geositeDbLastWriteTime -ge $midnight4AM -and $runningProcess) {
    Write-Host "geosite.db 文件修改时间在今天的凌晨4点以后$processName 进程正在运行,脚本结束。"
    if ($geositeDbCreationTime -ge $midnight4AM -and $runningProcesses) {
    Write-Host "geosite.db 文件创建时间在今天的凌晨4点以后$processName 进程正在运行,脚本结束。"
    exit
    }
    }
    catch {
    Write-Host "进程 $processName 未在运行。"
    }

    # 循环尝试下载和验证文件
    do {
    @@ -87,11 +82,11 @@ if ($currentRetry -eq $maxRetries) {
    Remove-Item $outputFile
    Remove-Item "$outputFile.sha256sum"

    # 结束主进程及其子进程
    Stop-Process -Name $processName -Force -ErrorAction SilentlyContinue
    # 结束进程
    Stop-Process -Name $processName

    # 等待一段时间
    Start-Sleep -Seconds 5
    Start-Sleep -Seconds 10

    # 启动程序
    Start-Process -FilePath $programPath
  3. hagerliu revised this gist Nov 17, 2023. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -90,6 +90,9 @@ if ($currentRetry -eq $maxRetries) {
    # 结束主进程及其子进程
    Stop-Process -Name $processName -Force -ErrorAction SilentlyContinue

    # 等待一段时间
    Start-Sleep -Seconds 5

    # 启动程序
    Start-Process -FilePath $programPath
    Write-Host "$processName 已重新启动。"
  4. hagerliu revised this gist Nov 16, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    $proxy = "http://127.0.0.1:2080"

    # 共同部分的链接
    $baseDownloadUrl = "https://github.com/hagerliu/v2ray-rules-dat/releases/latest/download/"
    $baseDownloadUrl = "https://github.com/hagerliu/v2ray-rules-dat/raw/release/"

    # 设置解压目标目录
    $targetDirectory = "C:\portapps\nekoray"
  5. hagerliu revised this gist Nov 16, 2023. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -38,10 +38,10 @@ if (-not $geositeDbPath) {
    } elseif (Test-Path $geositeDbPath) {
    # 判断是否需要执行脚本
    $geositeDbLastWriteTime = (Get-Item $geositeDbPath).LastWriteTime
    $midnight3AM = (Get-Date).Date.AddHours(3)
    $midnight4AM = (Get-Date).Date.AddHours(4)

    if ($geositeDbLastWriteTime -ge $midnight3AM -and $runningProcess) {
    Write-Host "geosite.db 文件修改时间在今天的凌晨3点以后$processName 进程正在运行,脚本结束。"
    if ($geositeDbLastWriteTime -ge $midnight4AM -and $runningProcess) {
    Write-Host "geosite.db 文件修改时间在今天的凌晨4点以后$processName 进程正在运行,脚本结束。"
    exit
    }
    }
  6. hagerliu created this gist Nov 16, 2023.
    96 changes: 96 additions & 0 deletions GeoDataUpdate.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    # 设置代理服务器地址和端口
    $proxy = "http://127.0.0.1:2080"

    # 共同部分的链接
    $baseDownloadUrl = "https://github.com/hagerliu/v2ray-rules-dat/releases/latest/download/"

    # 设置解压目标目录
    $targetDirectory = "C:\portapps\nekoray"

    # 获取临时目录
    $tempDirectory = $targetDirectory

    # 设置文件下载地址和文件名
    $url = $baseDownloadUrl + "sing-box-geodata.zip"
    $outputFile = Join-Path $tempDirectory "sing-box-geodata.zip"
    $sha256sumUrl = $url + ".sha256sum"

    # 设置geosite路径
    $geositeDbPath = [System.IO.Path]::Combine($targetDirectory, "geosite.db")

    # 设置重试次数
    $maxRetries = 3
    $currentRetry = 0

    # 设置程序路径
    $programPath = Join-Path $targetDirectory "nekoray.exe"

    # 获取进程名
    $processName = [System.IO.Path]::GetFileNameWithoutExtension($programPath)

    # 检查进程是否正在运行
    $runningProcess = Get-Process -Name $processName -ErrorAction SilentlyContinue

    # 检查geosite文件的修改时间和是否存在
    if (-not $geositeDbPath) {
    Write-Host "目标路径为空。"
    # 继续执行脚本,因为文件不存在
    } elseif (Test-Path $geositeDbPath) {
    # 判断是否需要执行脚本
    $geositeDbLastWriteTime = (Get-Item $geositeDbPath).LastWriteTime
    $midnight3AM = (Get-Date).Date.AddHours(3)

    if ($geositeDbLastWriteTime -ge $midnight3AM -and $runningProcess) {
    Write-Host "geosite.db 文件修改时间在今天的凌晨3点以后,$processName 进程正在运行,脚本结束。"
    exit
    }
    }

    # 循环尝试下载和验证文件
    do {
    # 删除已存在的文件
    Remove-Item $outputFile -ErrorAction SilentlyContinue
    Remove-Item "$outputFile.sha256sum" -ErrorAction SilentlyContinue

    # 下载文件
    Invoke-WebRequest -Uri $url -Proxy $proxy -OutFile $outputFile -UseBasicParsing

    # 下载sha256sum文件
    Invoke-WebRequest -Uri $sha256sumUrl -Proxy $proxy -OutFile "$outputFile.sha256sum" -UseBasicParsing

    # 计算下载文件的sha256值
    $downloadedSha256 = Get-FileHash -Path $outputFile -Algorithm SHA256 | Select-Object -ExpandProperty Hash

    # 读取期望的sha256值
    $sha256sum = Get-Content "$outputFile.sha256sum"
    $expectedSha256 = ($sha256sum -split ' ')[0]

    # 检查两个值是否不为空且匹配
    if ($downloadedSha256 -and $expectedSha256 -and ($downloadedSha256 -eq $expectedSha256)) {
    Write-Host "文件完整性验证通过"
    break
    } else {
    Write-Host "文件完整性验证未通过,重新尝试下载..."
    $currentRetry++
    }
    } while ($currentRetry -lt $maxRetries)

    # 检查是否达到最大重试次数
    if ($currentRetry -eq $maxRetries) {
    Write-Host "达到最大重试次数,无法验证文件完整性。"
    } else {
    # 解压文件到目标目录
    Expand-Archive -Path $outputFile -DestinationPath $targetDirectory -Force
    Write-Host "文件解压完成并覆盖到目标目录: $targetDirectory"

    # 删除临时文件
    Remove-Item $outputFile
    Remove-Item "$outputFile.sha256sum"

    # 结束主进程及其子进程
    Stop-Process -Name $processName -Force -ErrorAction SilentlyContinue

    # 启动程序
    Start-Process -FilePath $programPath
    Write-Host "$processName 已重新启动。"
    }