-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathRepair.ps1
205 lines (177 loc) · 8.92 KB
/
Repair.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
if ($script:MyInvocation.MyCommand.Path) {Set-Location (Split-Path $script:MyInvocation.MyCommand.Path)}
#[Environment]::CurrentDirectory = $ExecutionContext.SessionState.Path.CurrentFileSystemLocation
if ($Global:IsWindows -eq $null) {
$Global:IsWindows = [System.Environment]::OSVersion.Platform -eq "Win32NT" -or [System.Boolean](Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Ignore)
$Global:IsLinux = -not $IsWindows
$Global:IsMacOS = $false
}
$Global:7zip = if ($Global:IsWindows) {".\7z.exe"} else {"7z"}
if ($Global:IsLinux) {
$Global:OSArch = try {
Switch -Regex ("$(uname -m)".Trim()) {
"(i386|i686)" {"i386"; Break}
"x86_64" {"amd64"; Break}
"(arm|aarch64)" {if ("$(dpkg --print-architecture)" -match "arm64") {"arm64"} else {"arm"}; Break}
default {$PSItem}
}
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
"amd64"
}
if (-not (Get-Command $Global:7zip -ErrorAction Ignore)) {
$Path_7zz = ".\IncludesLinux\bin\7zz-$(if ($Global:OSArch -eq "arm") {"arm64"} else {$Global:OSArch})"
if (Test-Path $Path_7zz) {
$Global:7zip = $Path_7zz
try {
Get-ChildItem $Global:7zip -File -ErrorAction Stop | Foreach-Object {
& chmod x "$($_.FullName)" > $null
}
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
}
}
}
} elseif ($Global:IsWindows) {
$Global:OSArch = if ([System.Environment]::Is64BitOperatingSystem) {"amd64"} else {"i386"}
}
if ("$((Get-Culture).NumberFormat.NumberGroupSeparator)$((Get-Culture).NumberFormat.NumberDecimalSeparator)" -notmatch "^[,.]{2}$") {
[CultureInfo]::CurrentCulture = 'en-US'
}
if (-not (Get-Command "Start-ThreadJob" -ErrorAction SilentlyContinue)) {Set-Alias -Scope Global Start-ThreadJob Start-Job}
if ([Net.ServicePointManager]::SecurityProtocol -notmatch [Net.SecurityProtocolType]::Tls12) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
}
if (Test-Path ".\Downloads\config.json") {
try {
$DownloaderConfig = Get-Content -Raw ".\Downloads\config.json" | ConvertFrom-Json -ErrorAction Ignore
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
}
}
if (-not $DownloaderConfig) {
$DownloaderConfig = [PSCustomObject]@{
EnableMinerBackups = $true
EnableKeepDownloads = $true
}
}
$Name = "RainbowMiner"
try {
$ReposURI = "https://api.github.com/repos/rainbowminer/$Name/releases/latest"
$Request = Invoke-RestMethod $ReposURI -UseBasicParsing -TimeoutSec 30
$RemoteVersion = ($Request.tag_name -replace '^v')
if ($RemoteVersion) {
if ($IsWindows) {
$DownloadURI = $Request.assets | Where-Object Name -EQ "$($Name)V$($RemoteVersion)_win.zip" | Select-Object -ExpandProperty browser_download_url
} elseif ($IsLinux) {
$DownloadURI = $Request.assets | Where-Object Name -EQ "$($Name)V$($RemoteVersion)_linux.zip" | Select-Object -ExpandProperty browser_download_url
}
if (-not $DownloadURI) {
$DownloadURI = $Request.assets | Where-Object Name -EQ "$($Name)V$($RemoteVersion).zip" | Select-Object -ExpandProperty browser_download_url
}
}
Write-Host "Repair & Update v$($RemoteVersion)" -ForegroundColor Yellow
Write-Host " (1/3) Downloading $($DownloadURI) .. "
if (-not (Test-Path ".\Downloads")) {New-Item "Downloads" -ItemType "directory" | Out-Null}
$FileName = Join-Path ".\Downloads" (Split-Path $DownloadURI -Leaf)
if (Test-Path $FileName) {Remove-Item $FileName}
if ($DownloadURI -eq "") {throw}
Invoke-WebRequest $DownloadURI -OutFile $FileName -UseBasicParsing
if (-not (Test-Path $FileName) -or (Get-Item $FileName).Length -lt 2MB) {throw}
Write-Host " (2/3) Deleting and backup old files .."
@("Start.bat","start.sh") | Foreach-Object {if (Test-Path $_) {Copy-Item $_ "$($_).saved" -Force -ErrorAction Ignore}}
if ((Test-Path "Miners") -and (Test-Path "MinersOldVersions")) {$PreserveMiners = Compare-Object @(Get-ChildItem "Miners" | Select-Object -ExpandProperty Name) @(Get-ChildItem "MinersOldVersions" | Select-Object -ExpandProperty Name) -IncludeEqual -ExcludeDifferent | Select-Object -ExpandProperty InputObject}
@("Miners","APIs","Balances","Pools") | Foreach-Object {if (Test-Path ".\$($_)") {Remove-Item ".\$($_)" -Recurse -Force -ErrorAction Ignore}}
Get-ChildItem ".\Data" -Filter "*.json" -File | Where-Object {$_.Name -notin @("lastdrun.json","localapiport.json","minerdata.json","mrrinfo.json","poolsdata.json","unprofitable.json","version.json")} | Foreach-Object {Remove-Item $_.FullName -Force -ErrorAction Ignore}
Write-Host " (3/3) Extracting new files .."
$FromFullPath = [IO.Path]::GetFullPath($FileName)
$ToFullPath = [IO.Path]::GetFullPath(".")
if ($IsWindows) {
$Params = @{
FilePath = $Global:7zip
ArgumentList = "x `"$FromFullPath`" -o`"$ToFullPath`" -y -spe"
}
} else {
$Params = @{
FilePath = $Global:7zip
ArgumentList = "x `"$FromFullPath`" -o`"$ToFullPath`" -y"
RedirectStandardOutput = Join-Path ".\Logs" "7z-console.log"
RedirectStandardError = Join-Path ".\Logs" "7z-error.log"
}
}
$Params.PassThru = $true
(Start-Process @Params).WaitForExit() > $null
if ($PreserveMiners) {$PreserveMiners | Foreach-Object {if (Test-Path "MinersOldVersions\$_") {Copy-Item "MinersOldVersions\$_" "Miners\$_" -Force}}}
if ($IsWindows) {
#Handle write locks
try {
if (-not (Test-Path "_update")) {New-Item "_update" -ItemType "directory" > $null}
$Params = @{
FilePath = $Global:7zip
ArgumentList = "x `"$FromFullPath`" -o`"$(Join-Path $ToFullPath "_update")`" 7z.exe 7z.dll `"Includes\curl\x32\curl.exe`" `"Includes\curl\x64\curl.exe`" `"Includes\curl\x32\libcurl.dll`" `"Includes\curl\x64\libcurl-x64.dll`" `"Includes\getcpu\GetCPU.exe`" `"Includes\getcpu\OpenHardwareMonitorLib.dll`" -y -spe"
PassThru = $true
}
(Start-Process @Params).WaitForExit() > $null
Get-ChildItem "_update" -Recurse -File | Foreach-Object {
$FileNameTo = $_.FullName -replace "^. \\_update\\"
if (-not (Test-Path $FileNameTo) -or ((Get-FileHash $FileNameTo -Algorithm MD5).Hash -ne (Get-FileHash $_.FullName -Algorithm MD5).Hash)) {
Write-Host "Update $FileNameTo"
try {
$RetryLock = 20
$IsLocked = $true
do {
Try {
Copy-Item -Path $_.FullName -Destination $FileNameTo -Force
$IsLocked = $False
} Catch {
$RetryLock--
if ($RetryLock -gt 0) {Sleep -Milliseconds 250}
}
} while ($IsLocked -and ($RetryLock -gt 0))
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
}
if ($IsLocked) {
Write-Host "Failed to update $FileNameTo. Please download manually from Github." -ForegroundColor Yellow
}
}
}
if (Test-Path "_update") {
Remove-Item "_update" -Force -Recurse
}
} catch {
if ($Error.Count){$Error.RemoveAt(0)}
Write-Host "Failed to update exe files. Please download manually from Github." -ForegroundColor Yellow
}
} else {
Get-ChildItem ".\*.sh" -File | Foreach-Object {try {& chmod x "$($_.FullName)" > $null} catch {}}
Get-ChildItem ".\IncludesLinux\bash\*" -File | Foreach-Object {try {& chmod x "$($_.FullName)" > $null} catch {}}
Get-ChildItem ".\IncludesLinux\bin\*" -File | Foreach-Object {try {& chmod x "$($_.FullName)" > $null} catch {}}
}
if (-not $DownloaderConfig.EnableKeepDownloads -and (Test-Path $FileName)) {
Get-ChildItem $FileName -File | Foreach-Object {Remove-Item $_}
}
Write-Host "Repair & Update finished." -ForegroundColor Green
}
catch {
Write-Host "$Name failed to update. Please download manually at $($DownloadURI)" -ForegroundColor Yellow
if ($calledfrom -ne "core") {
$message = "Press any key to return to $name"
if ($psISE)
{
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.MessageBox]::Show("$message")
}
else
{
Write-Host "$message" -ForegroundColor Yellow
[void]($Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown'))
}
} else {
Sleep 2
}
exit 0
}
if (-not $psISE) {
exit 1
}