-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInclude.ps1
789 lines (638 loc) · 33.2 KB
/
Include.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
<#
This file is part of NPlusMiner
Copyright (c) 2018 Nemo
Copyright (c) 2018 MrPlus
NPlusMiner is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
NPlusMiner is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
#>
<#
Product: NPlusMiner
File: include.ps1
version: 2.3
version date: 20180408
#>
Function Update-Status ($Text) {
Write-host $Text
$Variables.StatusText = $Text
$LabelStatus.Lines += $Text
$LabelStatus.SelectionStart = $LabelStatus.TextLength;
$LabelStatus.ScrollToCaret();
# $LabelStatus.Text = $Text
# $LabelStatus.Invoke
$LabelStatus.Refresh | out-null
# $MainForm.refresh
}
Function DetectGPUCount {
Update-Status("Fetching GPU Count")
try {
$DetectedGPU = @(Get-WmiObject Win32_PnPSignedDriver | Select DeviceName,DriverVersion,Manufacturer,DeviceClass | Where { $_.Manufacturer -like "*NVIDIA*" -and $_.DeviceClass -like "*display*"}) } catch { $DetectedGPU = @()}
$DetectedGPUCount = $DetectedGPU.Count
# $DetectedGPUCount = @(Get-WmiObject Win32_PnPSignedDriver | Select DeviceName,DriverVersion,Manufacturer,DeviceClass | Where { $_.Manufacturer -like "*NVIDIA*" -and $_.DeviceClass -like "*display*"}).count } catch { $DetectedGPUCount = 0}
$i=0
$DetectedGPU | foreach {Update-Status("$($i): $($_.DeviceName)") | Out-Null;$i++}
Update-Status("Found $($DetectedGPUCount) GPU(s)")
$DetectedGPUCount
}
Function Load-Config {
param(
[Parameter(Mandatory=$true)]
[String]$ConfigFile
)
If (Test-Path $ConfigFile){
$Config = Get-Content $ConfigFile | ConvertFrom-json
$Config
}
}
Function Write-Config {
param(
[Parameter(Mandatory=$true)]
[PSCustomObject]$Config,
[Parameter(Mandatory=$true)]
[String]$ConfigFile
)
If ($Config -ne $null){
if (Test-Path $ConfigFile){Copy-Item $ConfigFile "$($ConfigFile).backup"}
$OrderedConfig = [PSCustomObject]@{};($config | select -Property * -ExcludeProperty PoolsConfig) | %{$_.psobject.properties | sort Name | %{$OrderedConfig | Add-Member -Force @{$_.Name = $_.Value}}}
$OrderedConfig | ConvertTo-json | out-file $ConfigFile
$PoolsConfig = Get-Content ".\Config\PoolsConfig.json" | ConvertFrom-Json
$OrderedPoolsConfig = [PSCustomObject]@{};$PoolsConfig | %{$_.psobject.properties | sort Name | %{$OrderedPoolsConfig | Add-Member -Force @{$_.Name = $_.Value}}}
$OrderedPoolsConfig.default | Add-member -Force @{Wallet = $Config.Wallet}
$OrderedPoolsConfig.default | Add-member -Force @{UserName = $Config.UserName}
$OrderedPoolsConfig.default | Add-member -Force @{WorkerName = $Config.WorkerName}
$OrderedPoolsConfig.default | Add-member -Force @{APIKey = $Config.APIKey}
$OrderedPoolsConfig | ConvertTo-json | out-file ".\Config\PoolsConfig.json"
}
}
Function Get-FreeTcpPort {
$StartPort = 4068
$PortFound = $false
$Port = $StartPort
While ($Port -le ($StartPort + 10) -and !$PortFound) {try{$Null = New-Object System.Net.Sockets.TCPClient -ArgumentList 127.0.0.1,$Port;$Port++} catch {$Port;$PortFound=$True}}
}
function Set-Stat {
param(
[Parameter(Mandatory = $true)]
[String]$Name,
[Parameter(Mandatory = $true)]
[Double]$Value,
[Parameter(Mandatory = $false)]
[DateTime]$Date = (Get-Date)
)
$Path = "Stats\$Name.txt"
$Date = $Date.ToUniversalTime()
$SmallestValue = 1E-20
$Stat = [PSCustomObject]@{
Live = $Value
Minute = $Value
Minute_Fluctuation = 1 / 2
Minute_5 = $Value
Minute_5_Fluctuation = 1 / 2
Minute_10 = $Value
Minute_10_Fluctuation = 1 / 2
Hour = $Value
Hour_Fluctuation = 1 / 2
Day = $Value
Day_Fluctuation = 1 / 2
Week = $Value
Week_Fluctuation = 1 / 2
Updated = $Date
}
if (Test-Path $Path) {$Stat = Get-Content $Path | ConvertFrom-Json}
$Stat = [PSCustomObject]@{
Live = [Double]$Stat.Live
Minute = [Double]$Stat.Minute
Minute_Fluctuation = [Double]$Stat.Minute_Fluctuation
Minute_5 = [Double]$Stat.Minute_5
Minute_5_Fluctuation = [Double]$Stat.Minute_5_Fluctuation
Minute_10 = [Double]$Stat.Minute_10
Minute_10_Fluctuation = [Double]$Stat.Minute_10_Fluctuation
Hour = [Double]$Stat.Hour
Hour_Fluctuation = [Double]$Stat.Hour_Fluctuation
Day = [Double]$Stat.Day
Day_Fluctuation = [Double]$Stat.Day_Fluctuation
Week = [Double]$Stat.Week
Week_Fluctuation = [Double]$Stat.Week_Fluctuation
Updated = [DateTime]$Stat.Updated
}
$Span_Minute = [Math]::Min(($Date - $Stat.Updated).TotalMinutes, 1)
$Span_Minute_5 = [Math]::Min((($Date - $Stat.Updated).TotalMinutes / 5), 1)
$Span_Minute_10 = [Math]::Min((($Date - $Stat.Updated).TotalMinutes / 10), 1)
$Span_Hour = [Math]::Min(($Date - $Stat.Updated).TotalHours, 1)
$Span_Day = [Math]::Min(($Date - $Stat.Updated).TotalDays, 1)
$Span_Week = [Math]::Min((($Date - $Stat.Updated).TotalDays / 7), 1)
$Stat = [PSCustomObject]@{
Live = $Value
Minute = ((1 - $Span_Minute) * $Stat.Minute) + ($Span_Minute * $Value)
Minute_Fluctuation = ((1 - $Span_Minute) * $Stat.Minute_Fluctuation) +
($Span_Minute * ([Math]::Abs($Value - $Stat.Minute) / [Math]::Max([Math]::Abs($Stat.Minute), $SmallestValue)))
Minute_5 = ((1 - $Span_Minute_5) * $Stat.Minute_5) + ($Span_Minute_5 * $Value)
Minute_5_Fluctuation = ((1 - $Span_Minute_5) * $Stat.Minute_5_Fluctuation) +
($Span_Minute_5 * ([Math]::Abs($Value - $Stat.Minute_5) / [Math]::Max([Math]::Abs($Stat.Minute_5), $SmallestValue)))
Minute_10 = ((1 - $Span_Minute_10) * $Stat.Minute_10) + ($Span_Minute_10 * $Value)
Minute_10_Fluctuation = ((1 - $Span_Minute_10) * $Stat.Minute_10_Fluctuation) +
($Span_Minute_10 * ([Math]::Abs($Value - $Stat.Minute_10) / [Math]::Max([Math]::Abs($Stat.Minute_10), $SmallestValue)))
Hour = ((1 - $Span_Hour) * $Stat.Hour) + ($Span_Hour * $Value)
Hour_Fluctuation = ((1 - $Span_Hour) * $Stat.Hour_Fluctuation) +
($Span_Hour * ([Math]::Abs($Value - $Stat.Hour) / [Math]::Max([Math]::Abs($Stat.Hour), $SmallestValue)))
Day = ((1 - $Span_Day) * $Stat.Day) + ($Span_Day * $Value)
Day_Fluctuation = ((1 - $Span_Day) * $Stat.Day_Fluctuation) +
($Span_Day * ([Math]::Abs($Value - $Stat.Day) / [Math]::Max([Math]::Abs($Stat.Day), $SmallestValue)))
Week = ((1 - $Span_Week) * $Stat.Week) + ($Span_Week * $Value)
Week_Fluctuation = ((1 - $Span_Week) * $Stat.Week_Fluctuation) +
($Span_Week * ([Math]::Abs($Value - $Stat.Week) / [Math]::Max([Math]::Abs($Stat.Week), $SmallestValue)))
Updated = $Date
}
if (-not (Test-Path "Stats")) {New-Item "Stats" -ItemType "directory"}
[PSCustomObject]@{
Live = [Decimal]$Stat.Live
Minute = [Decimal]$Stat.Minute
Minute_Fluctuation = [Double]$Stat.Minute_Fluctuation
Minute_5 = [Decimal]$Stat.Minute_5
Minute_5_Fluctuation = [Double]$Stat.Minute_5_Fluctuation
Minute_10 = [Decimal]$Stat.Minute_10
Minute_10_Fluctuation = [Double]$Stat.Minute_10_Fluctuation
Hour = [Decimal]$Stat.Hour
Hour_Fluctuation = [Double]$Stat.Hour_Fluctuation
Day = [Decimal]$Stat.Day
Day_Fluctuation = [Double]$Stat.Day_Fluctuation
Week = [Decimal]$Stat.Week
Week_Fluctuation = [Double]$Stat.Week_Fluctuation
Updated = [DateTime]$Stat.Updated
} | ConvertTo-Json | Set-Content $Path
$Stat
}
function Get-Stat {
param(
[Parameter(Mandatory = $true)]
[String]$Name
)
if (-not (Test-Path "Stats")) {New-Item "Stats" -ItemType "directory"}
Get-ChildItem "Stats" | Where-Object Extension -NE ".ps1" | Where-Object BaseName -EQ $Name | Get-Content | ConvertFrom-Json
}
function Get-ChildItemContent {
param(
[Parameter(Mandatory = $true)]
[String]$Path,
[Parameter(Mandatory = $false)]
[Array]$Include = @()
)
$ChildItems = Get-ChildItem -Recurse -Path $Path -Include $Include | ForEach-Object {
$Name = $_.BaseName
$Content = @()
if ($_.Extension -eq ".ps1") {
$Content = &$_.FullName
}
else {
$Content = $_ | Get-Content | ConvertFrom-Json
}
$Content | ForEach-Object {
[PSCustomObject]@{Name = $Name; Content = $_}
}
}
$ChildItems | ForEach-Object {
$Item = $_
$ItemKeys = $Item.Content.PSObject.Properties.Name.Clone()
$ItemKeys | ForEach-Object {
if ($Item.Content.$_ -is [String]) {
$Item.Content.$_ = Invoke-Expression "`"$($Item.Content.$_)`""
}
elseif ($Item.Content.$_ -is [PSCustomObject]) {
$Property = $Item.Content.$_
$PropertyKeys = $Property.PSObject.Properties.Name
$PropertyKeys | ForEach-Object {
if ($Property.$_ -is [String]) {
$Property.$_ = Invoke-Expression "`"$($Property.$_)`""
}
}
}
}
}
$ChildItems
}
<#
function Set-Algorithm {
param(
[Parameter(Mandatory=$true)]
[String]$API,
[Parameter(Mandatory=$true)]
[Int]$Port,
[Parameter(Mandatory=$false)]
[Array]$Parameters = @()
)
$Server = "localhost"
switch($API)
{
"nicehash"
{
}
}
}
#>
function Get-HashRate {
param(
[Parameter(Mandatory = $true)]
[String]$API,
[Parameter(Mandatory = $true)]
[Int]$Port,
[Parameter(Mandatory = $false)]
[Object]$Parameters = @{},
[Parameter(Mandatory = $false)]
[Bool]$Safe = $false
)
$Server = "localhost"
$Multiplier = 1000
$Delta = 0.05
$Interval = 5
$HashRates = @()
$HashRates_Dual = @()
try {
switch ($API) {
"xgminer" {
$Message = @{command = "summary"; parameter = ""} | ConvertTo-Json -Compress
do {
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request.Substring($Request.IndexOf("{"), $Request.LastIndexOf("}") - $Request.IndexOf("{") + 1) -replace " ", "_" | ConvertFrom-Json
$HashRate = if ($Data.SUMMARY.HS_5s -ne $null) {[Double]$Data.SUMMARY.HS_5s * [Math]::Pow($Multiplier, 0)}
elseif ($Data.SUMMARY.KHS_5s -ne $null) {[Double]$Data.SUMMARY.KHS_5s * [Math]::Pow($Multiplier, 1)}
elseif ($Data.SUMMARY.MHS_5s -ne $null) {[Double]$Data.SUMMARY.MHS_5s * [Math]::Pow($Multiplier, 2)}
elseif ($Data.SUMMARY.GHS_5s -ne $null) {[Double]$Data.SUMMARY.GHS_5s * [Math]::Pow($Multiplier, 3)}
elseif ($Data.SUMMARY.THS_5s -ne $null) {[Double]$Data.SUMMARY.THS_5s * [Math]::Pow($Multiplier, 4)}
elseif ($Data.SUMMARY.PHS_5s -ne $null) {[Double]$Data.SUMMARY.PHS_5s * [Math]::Pow($Multiplier, 5)}
if ($HashRate -ne $null) {
$HashRates += $HashRate
if (-not $Safe) {break}
}
$HashRate = if ($Data.SUMMARY.HS_av -ne $null) {[Double]$Data.SUMMARY.HS_av * [Math]::Pow($Multiplier, 0)}
elseif ($Data.SUMMARY.KHS_av -ne $null) {[Double]$Data.SUMMARY.KHS_av * [Math]::Pow($Multiplier, 1)}
elseif ($Data.SUMMARY.MHS_av -ne $null) {[Double]$Data.SUMMARY.MHS_av * [Math]::Pow($Multiplier, 2)}
elseif ($Data.SUMMARY.GHS_av -ne $null) {[Double]$Data.SUMMARY.GHS_av * [Math]::Pow($Multiplier, 3)}
elseif ($Data.SUMMARY.THS_av -ne $null) {[Double]$Data.SUMMARY.THS_av * [Math]::Pow($Multiplier, 4)}
elseif ($Data.SUMMARY.PHS_av -ne $null) {[Double]$Data.SUMMARY.PHS_av * [Math]::Pow($Multiplier, 5)}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += $HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"ccminer" {
$Message = "summary"
do {
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request -split ";" | ConvertFrom-StringData
$HashRate = if ([Double]$Data.KHS -ne 0 -or [Double]$Data.ACC -ne 0) {$Data.KHS}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate * $Multiplier
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"XMRig" {
$Message = "summary"
do {
$Request = Invoke-WebRequest "http://$($Server):$Port/h" -UseBasicParsing
$Data = $Request | ConvertFrom-Json
$HashRate = [Double]$Data.hashrate.total[0]
if ($HashRate -eq "") {$HashRate = [Double]$Data.hashrate.total[1]}
if ($HashRate -eq "") {$HashRate = [Double]$Data.hashrate.total[2]}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
}while ($HashRates.count -lt 6)
}
"dstm" {
$Message = "summary"
do {
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request | ConvertFrom-Json
$HashRate = [Double]($Data.result.sol_ps | Measure-Object -Sum).Sum
if (-not $HashRate) {$HashRate = [Double]($Data.result.speed_sps | Measure-Object -Sum).Sum} #ewbf fix
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"nicehashequihash" {
$Message = "status"
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
do {
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request | ConvertFrom-Json
$HashRate = $Data.result.speed_hps
if ($HashRate -eq $null) {$HashRate = $Data.result.speed_sps}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"nicehash" {
$Message = @{id = 1; method = "algorithm.list"; params = @()} | ConvertTo-Json -Compress
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
do {
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request | ConvertFrom-Json
$HashRate = $Data.algorithms.workers.speed
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]($HashRate | Measure-Object -Sum).Sum
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"ewbf" {
$Message = @{id = 1; method = "getstat"} | ConvertTo-Json -Compress
$Client = New-Object System.Net.Sockets.TcpClient $server, $port
$Writer = New-Object System.IO.StreamWriter $Client.GetStream()
$Reader = New-Object System.IO.StreamReader $Client.GetStream()
$Writer.AutoFlush = $true
do {
$Writer.WriteLine($Message)
$Request = $Reader.ReadLine()
$Data = $Request | ConvertFrom-Json
$HashRate = $Data.result.speed_sps
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]($HashRate | Measure-Object -Sum).Sum
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"claymore" {
do {
$Request = Invoke-WebRequest "http://$($Server):$Port" -UseBasicParsing
$Data = $Request.Content.Substring($Request.Content.IndexOf("{"), $Request.Content.LastIndexOf("}") - $Request.Content.IndexOf("{") + 1) | ConvertFrom-Json
$HashRate = $Data.result[2].Split(";")[0]
$HashRate_Dual = $Data.result[4].Split(";")[0]
if ($HashRate -eq $null -or $HashRate_Dual -eq $null) {$HashRates = @(); $HashRate_Dual = @(); break}
if ($Request.Content.Contains("ETH:")) {$HashRates += [Double]$HashRate * $Multiplier; $HashRates_Dual += [Double]$HashRate_Dual * $Multiplier}
else {$HashRates += [Double]$HashRate; $HashRates_Dual += [Double]$HashRate_Dual}
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"fireice" {
do {
$Request = Invoke-WebRequest "http://$($Server):$Port/h" -UseBasicParsing
$Data = $Request.Content -split "</tr>" -match "total*" -split "<td>" -replace "<[^>]*>", ""
$HashRate = $Data[1]
if ($HashRate -eq "") {$HashRate = $Data[2]}
if ($HashRate -eq "") {$HashRate = $Data[3]}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
"wrapper" {
do {
Start-Sleep 480 #plus 10mins to HSRNeo Benchmark
$HashRate = Get-Content ".\PalginNeoHashrate.txt"
if ($HashRate -eq $null) {Start-Sleep $Interval; $HashRate = [PSCustomObject]@{(Get-Algorithm($_)) = $Stats."$($Name)_$(Get-Algorithm($_))_HashRate".Week}}
if ($HashRate -eq $null) {$HashRates = @(); break}
$HashRates += [Double]$HashRate
if (-not $Safe) {break}
Start-Sleep $Interval
} while ($HashRates.Count -lt 6)
}
}
$HashRates_Info = $HashRates | Measure-Object -Maximum -Minimum -Average
if ($HashRates_Info.Maximum - $HashRates_Info.Minimum -le $HashRates_Info.Average * $Delta) {$HashRates_Info.Maximum}
$HashRates_Info_Dual = $HashRates_Dual | Measure-Object -Maximum -Minimum -Average
if ($HashRates_Info_Dual.Maximum - $HashRates_Info_Dual.Minimum -le $HashRates_Info_Dual.Average * $Delta) {$HashRates_Info_Dual.Maximum}
}
catch {
}
}
filter ConvertTo-Hash {
$Hash = $_
switch ([math]::truncate([math]::log($Hash, [Math]::Pow(1000, 1)))) {
0 {"{0:n2} H" -f ($Hash / [Math]::Pow(1000, 0))}
1 {"{0:n2} KH" -f ($Hash / [Math]::Pow(1000, 1))}
2 {"{0:n2} MH" -f ($Hash / [Math]::Pow(1000, 2))}
3 {"{0:n2} GH" -f ($Hash / [Math]::Pow(1000, 3))}
4 {"{0:n2} TH" -f ($Hash / [Math]::Pow(1000, 4))}
Default {"{0:n2} PH" -f ($Hash / [Math]::Pow(1000, 5))}
}
}
function Get-Combination {
param(
[Parameter(Mandatory = $true)]
[Array]$Value,
[Parameter(Mandatory = $false)]
[Int]$SizeMax = $Value.Count,
[Parameter(Mandatory = $false)]
[Int]$SizeMin = 1
)
$Combination = [PSCustomObject]@{}
for ($i = 0; $i -lt $Value.Count; $i++) {
$Combination | Add-Member @{[Math]::Pow(2, $i) = $Value[$i]}
}
$Combination_Keys = $Combination | Get-Member -MemberType NoteProperty | Select-Object -ExpandProperty Name
for ($i = $SizeMin; $i -le $SizeMax; $i++) {
$x = [Math]::Pow(2, $i) - 1
while ($x -le [Math]::Pow(2, $Value.Count) - 1) {
[PSCustomObject]@{Combination = $Combination_Keys | Where-Object {$_ -band $x} | ForEach-Object {$Combination.$_}}
$smallest = ($x -band - $x)
$ripple = $x + $smallest
$new_smallest = ($ripple -band - $ripple)
$ones = (($new_smallest / $smallest) -shr 1) - 1
$x = $ripple -bor $ones
}
}
}
function Start-SubProcess {
param(
[Parameter(Mandatory = $true)]
[String]$FilePath,
[Parameter(Mandatory = $false)]
[String]$ArgumentList = "",
[Parameter(Mandatory = $false)]
[String]$WorkingDirectory = ""
)
$Job = Start-Job -ArgumentList $PID, $FilePath, $ArgumentList, $WorkingDirectory {
param($ControllerProcessID, $FilePath, $ArgumentList, $WorkingDirectory)
$ControllerProcess = Get-Process -Id $ControllerProcessID
if ($ControllerProcess -eq $null) {return}
$ProcessParam = @{}
$ProcessParam.Add("FilePath", $FilePath)
$ProcessParam.Add("WindowStyle", 'Minimized')
if ($ArgumentList -ne "") {$ProcessParam.Add("ArgumentList", $ArgumentList)}
if ($WorkingDirectory -ne "") {$ProcessParam.Add("WorkingDirectory", $WorkingDirectory)}
$Process = Start-Process @ProcessParam -PassThru
if ($Process -eq $null) {
[PSCustomObject]@{ProcessId = $null}
return
}
[PSCustomObject]@{ProcessId = $Process.Id; ProcessHandle = $Process.Handle}
$ControllerProcess.Handle | Out-Null
$Process.Handle | Out-Null
do {if ($ControllerProcess.WaitForExit(1000)) {$Process.CloseMainWindow() | Out-Null}}
while ($Process.HasExited -eq $false)
}
do {Start-Sleep 1; $JobOutput = Receive-Job $Job}
while ($JobOutput -eq $null)
$Process = Get-Process | Where-Object Id -EQ $JobOutput.ProcessId
$Process.Handle | Out-Null
$Process
}
function Expand-WebRequest {
param(
[Parameter(Mandatory = $true)]
[String]$Uri,
[Parameter(Mandatory = $true)]
[String]$Path
)
$FolderName_Old = ([IO.FileInfo](Split-Path $Uri -Leaf)).BaseName
$FolderName_New = Split-Path $Path -Leaf
$FileName = "$FolderName_New$(([IO.FileInfo](Split-Path $Uri -Leaf)).Extension)"
if (Test-Path $FileName) {Remove-Item $FileName}
if (Test-Path "$(Split-Path $Path)\$FolderName_New") {Remove-Item "$(Split-Path $Path)\$FolderName_New" -Recurse}
if (Test-Path "$(Split-Path $Path)\$FolderName_Old") {Remove-Item "$(Split-Path $Path)\$FolderName_Old" -Recurse}
Invoke-WebRequest $Uri -OutFile $FileName -UseBasicParsing
Start-Process "7z" "x $FileName -o$(Split-Path $Path)\$FolderName_Old -y -spe" -Wait
if (Get-ChildItem "$(Split-Path $Path)\$FolderName_Old" | Where-Object PSIsContainer -EQ $false) {
Rename-Item "$(Split-Path $Path)\$FolderName_Old" "$FolderName_New"
}
else {
Get-ChildItem "$(Split-Path $Path)\$FolderName_Old" | Where-Object PSIsContainer -EQ $true | ForEach-Object {Move-Item "$(Split-Path $Path)\$FolderName_Old\$_" "$(Split-Path $Path)\$FolderName_New"}
Remove-Item "$(Split-Path $Path)\$FolderName_Old"
}
}
function Get-Algorithm {
param(
[Parameter(Mandatory = $true)]
[String]$Algorithm
)
$Algorithms = Get-Content "Algorithms.txt" | ConvertFrom-Json
$Algorithm = (Get-Culture).TextInfo.ToTitleCase(($Algorithm -replace "-", " " -replace "_", " ")) -replace " "
if ($Algorithms.$Algorithm) {$Algorithms.$Algorithm}
else {$Algorithm}
}
function Get-Location {
param(
[Parameter(Mandatory = $true)]
[String]$Location
)
$Locations = Get-Content "Locations.txt" | ConvertFrom-Json
$Location = (Get-Culture).TextInfo.ToTitleCase(($Location -replace "-", " " -replace "_", " ")) -replace " "
if ($Locations.$Location) {$Locations.$Location}
else {$Location}
}
Function Autoupdate{
# GitHub Supporting only TLSv1.2 on feb 22 2018
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
Set-Location (Split-Path $script:MyInvocation.MyCommand.Path)
Write-host (Split-Path $script:MyInvocation.MyCommand.Path)
Update-Status("Checking AutoUpdate")
# write-host "Checking autoupdate"
try {
$AutoUpdateVersion = Invoke-WebRequest "http://tiny.cc/m155qyTESTREMOVE" -UseBasicParsing -Headers @{"Cache-Control"="no-cache"} | ConvertFrom-Json} catch {$AutoUpdateVersion = Get-content ".\Config\AutoUpdateVersion.json" | Convertfrom-json}
If ($AutoUpdateVersion -ne $null){$AutoUpdateVersion | ConvertTo-json | Out-File ".\Config\AutoUpdateVersion.json"}
If ($AutoUpdateVersion.Product -eq $Variables.CurrentProduct -and [Version]$AutoUpdateVersion.Version -gt $Variables.CurrentVersion -and $AutoUpdateVersion.AutoUpdate) {
Update-Status("Version $($AutoUpdateVersion.Version) available. (You are running $($Variables.CurrentVersion))")
# Write-host "Version $($AutoUpdateVersion.Version) available. (You are running $($Variables.CurrentVersion))"
$LabelNewVersion.ForeColor = "Green"
$LabelNewVersion.Text = "Version $([Version]$AutoUpdateVersion.Version) available"
If ($AutoUpdateVersion.Autoupdate) {
# Setting autostart to true
$Config.autostart = $true
Write-Config -ConfigFile $ConfigFile -Config $Config
# Backup current version folder here in zip file
Update-Status("Backing up current version...")
$BackupFileName = ("AutoupdateBackup-$(Get-Date -Format u).zip").replace(" ", "_").replace(":", "")
Start-Process "7z" "a $($BackupFileName) .\* -x!AutoupdateBackup-*" -Wait -WindowStyle hidden
If (!(test-path .\$BackupFileName)) {Update-Status("Backup failed");return}
# Download CRC File from a different location
# Abort if failed
Update-Status("Retrieving update CRC")
try {
$UpdateCRC = Invoke-WebRequest "http://tiny.cc/NPlusMinerUpdateCRC" -UseBasicParsing -Headers @{"Cache-Control"="no-cache"} | ConvertFrom-Json
$UpdateCRC = $UpdateCRC | ? {$_.Product -eq $AutoUpdateVersion.Product -and $_.Version -eq $AutoUpdateVersion.Version}
} catch {Update-Status("Cannot get update CRC from server");return}
If (! $UpdateCRC) {Update-Status("Cannot find CRC for version $($AutoUpdateVersion.Version)");return}
# Download update file
# Change uri to $RepoUri + path with harcoded github repo uri
# Has a security measure, this ensures the file is downloaded from the right GitHub repo
$UpdateFileName = ".\$($AutoUpdateVersion.Product)-$($AutoUpdateVersion.Version)"
Update-Status("Downloading version $($AutoUpdateVersion.Version)")
try {
Invoke-WebRequest $AutoUpdateVersion.Uri -OutFile "$($UpdateFileName).zip" -UseBasicParsing
} catch {Update-Status("Update download failed");return}
If (!(test-path ".\$($UpdateFileName).zip")) {Update-Status("Cannot find update file");return}
# Calculate and validate update file CRC
# Abort if any issue
Update-Status("Validating update file")
If ((Get-FileHash ".\$($UpdateFileName).zip").Hash -ne $UpdateCRC.CRC) {
Update-Status("Update file CRC not valid!");return
} else {
Update-Status("Update file validated. Updating NPlusMiner")
}
# unzip in child folder excluding config
Update-Status("Unzipping update...")
Start-Process "7z" "x $($UpdateFileName).zip -o.\ -y -spe -xr!config" -Wait -WindowStyle hidden
# copy files
# Deal with config files format updates?
# Any per update script? get from json file?
Update-Status("Copying files...")
Copy-Item .\$UpdateFileName\* .\ -force -Recurse
#Remove temp files
Update-Status("Removing temporary files...")
Remove-Item .\$UpdateFileName -Force -Recurse
Remove-Item ".\$($UpdateFileName).zip" -Force
# Update-Status("Processing cleanup...")
# $AutoUpdateVersion.CleanupFiles | foreach {Remove-Item ".\$_" -Force}
# Start new instance (Wait and confirm start)
# Kill old instance
If ($AutoUpdateVersion.RequireRestart) {
Update-Status("Starting my brother")
$StartCommand = ((gwmi win32_process -filter "ProcessID=$PID" | select commandline).CommandLine)
$NewKid = Invoke-WmiMethod -Class Win32_Process -Name Create -ArgumentList @($StartCommand, (Split-Path $script:MyInvocation.MyCommand.Path))
# Giving 10 seconds for process to start
$Waited = 0
sleep 10
While (!(Get-Process -id $NewKid.ProcessId -EA silentlycontinue) -and ($waited -le 10)) {sleep 1;$waited++}
If (!(Get-Process -id $NewKid.ProcessId -EA silentlycontinue)) {Update-Status("Failed to start new instance of NPlusMiner");return}
Update-Status("NPlusMiner successfully updated to version $($AutoUpdateVersion.Version)")
Update-Status("Killing myself")
If (Get-Process -id $NewKid.ProcessId) {Stop-process -id $PID}
} else {
Update-Status("NPlusMiner successfully updated to version $($AutoUpdateVersion.Version)")
}
} elseif (!($Config.Autostart)){
UpdateStatus("Cannot autoupdate as autostart not selected")
} else {
UpdateStatus("New version available $($AutoUpdateVersion.Product)-$($AutoUpdateVersion.Version). No candidate for Autoupdate")
}
} else {Update-Status("No candidate for Autoupdate")}
}