-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglt_launcher.ps1
437 lines (383 loc) · 12.6 KB
/
glt_launcher.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
<#PSScriptInfo
.VERSION 0.2.7
.GUID fe8bfeb4-23ab-4a96-824c-6a36d85d61b3
.AUTHOR MatthewWertman
.COMPANYNAME bitcap-shell
#>
<#
.DESCRIPTION
glt launcher
#>
[CmdletBinding()]
$VerbosePreference = 'SilentlyContinue'
$PSScriptInfo = (Test-ScriptFileInfo .\glt_launcher.ps1)
$config = (Get-Content .\config.json) | ConvertFrom-Json
$FORCE_LOCAL = $config.forceLocalPrograms
$AM_API = $config.params.awesomeMinerAPIConfig.enabled
$AM_API_URL = $config.params.awesomeMinerAPIConfig.awesomeHostURL
$AM_API_KEY = $config.params.awesomeMinerAPIConfig.awesomeAPIKey
Function Get-Version
{
return $PSScriptInfo.Version
}
Function Show-Help
{
$cmd_args = '.\help_man.ps1;', 'PAUSE'
Start-Process -FilePath powershell.exe -ArgumentList $cmd_args
}
function Update-Script
{
Start-Process 'https://github.com/bitcap-co/bitcap-glt/releases/latest'
}
Function Get-Miner-Obj
{
$miner_type = $null
if ($miner.gpuList)
{
$miner_type = 'GPU'
}
elseif ($miner.asicList)
{
$miner_type = 'ASIC'
}
return [PSCustomObject]@{
id = $miner.id
name = $miner.name
hostname = $miner.hostname
type = $miner_type
}
}
Function Update-AM-Miner-List
{
# Fetch and store all miners from the AM dashboard
$ProgressPreference = 'SilentlyContinue'
$miners = (Invoke-WebRequest -UseBasicParsing -Uri "$AM_API_URL/miners?key=$AM_API_KEY").Content | ConvertFrom-Json
$ProgressPreference = 'Continue'
$miners_json = @()
$groups = $miners.groupList
foreach ($group in $groups)
{
if ($group.groupList)
{
foreach ($group in $group.groupList)
{
if ($group.minerList)
{
foreach ($miner in $group.minerList)
{
$miners_json += (Get-Miner-Obj)
}
}
}
}
if ($group.minerList)
{
foreach ($miner in $group.minerList)
{
$miners_json += (Get-Miner-Obj)
}
}
}
$miners_json | ConvertTo-Json | Out-File 'miners.json' -Force
if ((Test-Path .\miners.json) -and (Get-Item .\miners.json).length -gt 0)
{
Write-Host 'Successfully refreshed the miner list.'
}
else
{ Write-Error 'Failed to fetch miner list from the API' }
}
Function Get-Program
{
param(
[string[]]$path_arr
)
$program_path = ''
foreach ($path in $path_arr)
{
if (Test-Path $path)
{
$program_path = Resolve-Path $path
Break
}
}
Return $program_path
}
## External Programs
# cmd
$CMD = 'C:\Windows\System32\cmd.exe'
# 7Zip
Write-Verbose 'Finding 7-zip installation path...'
$P7ZipLocations = 'C:\Program Files\7-Zip\', 'C:\Program Files (x86)\7-Zip\'
$P7Zip = (Get-ChildItem -Path (& Get-Program $P7ZipLocations) -File 7z.exe).FullName
if (! $P7Zip)
{
Write-Warning 'Unable able to find 7Zip.'
if (-not $FORCE_LOCAL)
{
Write-Verbose 'Fetching 7Zip4PowerShell Module...'
if (-not (Get-Command Expand-7Zip -ErrorAction Ignore))
{
Install-Package -Scope CurrentUser -Force 7Zip4Powershell > $null
}
Write-Verbose 'Successfully installed 7Zip4PowerShell module!'
}
else
{ Throw 'ERROR (Unable to fetch 7zip! Is -ForceLocalPrograms on?)' }
}
else
{ Write-Verbose "Found 7-zip at $P7Zip." }
# plink
Write-Verbose 'Finding PuTTY installation path...'
$PuTTYLocations = 'C:\Program Files\PuTTY\', 'C:\Program File (x86)\PuTTY\', $env:TEMP
$putty = (Get-ChildItem -Path (& Get-Program $PuTTYLocations) -File putty.exe).FullName
if (! $putty)
{
Write-Warning 'Unable to launch PuTTY Session (putty.exe not found). Continuing...'
}
$plink = (Get-ChildItem -Path (& Get-Program $PuTTYLocations) -File plink.exe).FullName
if (! $plink)
{
Write-Warning 'Unable able to find plink.'
if (-not $FORCE_LOCAL)
{
Write-Verbose 'Fetching Plink.exe from upstream...'
# if plink is not found, lets go and get it!
$Uri = 'https://the.earth.li/~sgtatham/putty/latest/w64/plink.exe'
$Path = $env:TEMP
if (-not(Test-Path $Path -ErrorAction SilentlyContinue))
{
New-Item -ItemType Directory -Path $Path
}
$filename = $Uri.Substring($Uri.LastIndexOf('/') + 1)
Write-Verbose "Downloading Plink from $Uri to $Path..."
Invoke-WebRequest -UseBasicParsing -Uri $Uri -OutFile (Join-Path $Path $filename)
$plink = (Join-Path $Path $filename)
}
else
{ Throw 'ERROR (Unable to fetch plink! Is -ForceLocalPrograms on?)' }
}
Write-Verbose "Found plink at $plink."
## End External Programs
# Load in WPF depends
Write-Verbose 'Trying to load WPF dependecies...'
try
{
Add-Type -AssemblyName PresentationCore, PresentationFramework, WindowsBase, System.Windows.Forms
}
catch
{ Throw 'ERROR (Failed Operation: Unable to load WPF Dependencies)' }
$GUI_PATH = '.\GUI.xaml'
[xml]$XML_WPF = Get-Content -Path $GUI_PATH
if (! $XML_WPF) { Throw "ERROR (Failed Operation: Unabled to find 'GUI.xaml' in script directory.)" }
Write-Verbose 'Found GUI xaml...'
$XAML_GUI = [System.Windows.Markup.XamlReader]::Load((New-Object System.Xml.XmlNodeReader $XML_WPF))
# Import named components as variables
Write-Verbose 'Importing objects from GUI xaml...'
$XML_WPF.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object {
Set-Variable -Name ($_.Name) -Value $XAML_GUI.FindName($_.Name)
}
# Update version
$AboutTagVersion.Header += "$( Get-Version )"
#Load Window Icon
$icon_URI = New-Object System.uri((Get-ChildItem '.\resources\icons\BitCapLngLogo_BLK-04.png').FullName)
$MainWindow.Icon = New-Object System.Windows.Media.Imaging.BitmapImage $icon_URI
$FilterElements = @($LabelFilterTitle, $LabelFilter, $TextFilterList)
$FilterElements | ForEach-Object { $_.Visibility = 'Hidden' }
$OptToolCheckFilter.add_click(
{
if ($OptToolCheckFilter.IsChecked)
{
# Lets hide the logo when filtering is enabled
$SVGLogoViewBox.Opacity = 0
$FilterElements | ForEach-Object { $_.Visibility = 'Visible' }
}
else
{ $SVGLogoViewBox.Opacity = 0.75; $FilterElements | ForEach-Object { $_.Visibility = 'Hidden' } }
}
)
# Hook 'Set Default Password' button to store current password
$OptToolButtonDefault.add_click(
{
try
{
$DefaultENV = ConvertFrom-SecureString -SecureString $PasswdBox.SecurePassword -ErrorAction Stop
Write-Host 'Created new default password. Writing to system...'
try
{
[System.Environment]::SetEnvironmentVariable('Default', $DefaultENV, 'User')
Remove-Variable DefaultENV
}
catch
{ Write-Error 'ERROR (Unable to store environment variable. Skipping...' }
}
catch [System.Management.Automation.PSArgumentException]
{ Write-Error 'ERROR (Invalid input: supplied password cannot be set as default)' }
}
)
# Hook 'Refresh Miner List' button to fetch latest data from am api
$OptToolButtonRefresh.add_click(
{
if ($AM_API)
{
Update-AM-Miner-List
}
else
{
Write-Warning 'AwesomeMiner API is disabled in config. Ignoring...'
}
}
)
$OptToolButtonKillOutput.add_click(
{
Stop-Process -Name 'cmd' -ErrorAction SilentlyContinue
}
)
# Hook "Help" button to display man page
$AboutButtonHelp.add_click(
{
Show-Help
}
)
# Hook 'Check for updates' to fetch latest release
$AboutButtonUpdate.add_click(
{
Update-Script
}
)
# Hook AcceptButton
$AcceptButton.add_click(
{
$instance_json += [PSCustomObject]@{
# params
params = $config.params
# programs
programs = [PSCustomObject]@{
putty = $putty
plink = $plink
p7zip = $P7Zip
}
# debug
debug = [PSCustomObject]@{
debugMode = $DebugToolCheckDebug.IsChecked
keepFiles = $DebugToolCheckKeep.IsChecked
genExpected = $DebugToolCheckGenExpected.IsChecked
debugBIOS = $DebugToolCheckBIOS.IsChecked
}
# options
options = [PSCustomObject]@{
filterMode = $OptToolCheckFilter.IsChecked
listView = $OptToolCheckLView.IsChecked
# input
input = [PSCustomObject]@{
remoteIP = $TextRemoteIP.Text
username = $TextUser.Text
passwd = if ($PasswdBox.Password.Length) { ($PasswdBox.SecurePassword | ConvertFrom-SecureString) } else { '' }
filterList = $TextFilterList.Text
}
# quick options
checkAll = $CheckAll.IsChecked
checkBypassAM = $CheckBypassAM.IsChecked
checkGI = !$CheckBypassAM.IsChecked
checkPutty = $CheckPuTTY.IsChecked
}
}
$instance_json | ConvertTo-Json | Out-File 'instance.json'
# reset options
# $TextRemoteIP.Text = '192.168.'
$PasswdBox.Password = ''
$TextFilterList.Text = ''
$CheckBoxElements = @($DebugToolCheckDebug, $DebugToolCheckKeep, $DebugToolCheckGenExpected, $DebugToolCheckBIOS, $OptToolCheckFilter, $OptToolCheckLView)
$CheckBoxElements | ForEach-Object {
if ($null -ne $_)
{
$_.IsChecked = $FALSE
}
}
$FilterElements = @($LabelFilterTitle, $LabelFilter, $TextFilterList)
$FilterElements | ForEach-Object { $_.Visibility = 'Hidden' }
$SVGLogoViewBox.Opacity = 0.75;
$DebugToolCheckKeep.IsChecked = $TRUE
$CheckAll.IsChecked = $TRUE
$CMD_TITLE = $instance_json.options.input.remoteIP
if (-not $instance_json.debug.debugMode)
{
if ((Test-Path .\miners.json))
{
$miners = (Get-Content .\miners.json) | ConvertFrom-Json
foreach ($miner in $miners)
{
if ($miner.hostname -eq $instance_json.options.input.remoteIP)
{
$CMD_TITLE = $($miner.name)
Break
}
}
}
}
else
{
$CMD_TITLE = '- DEBUG -'
}
$cmd_args = '/k', 'TITLE', "$CMD_TITLE", '&', 'powershell.exe', '-ep', 'Bypass', '.\gpu_lookup_tableGUI.ps1'
Start-Process -FilePath $CMD -ArgumentList $cmd_args
}
)
if ($MyInvocation.MyCommand.CommandType -eq 'ExternalScript')
{ $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition }
else
{
$ScriptPath = Split-Path -Parent -Path ([Environment]::GetCommandLineArgs()[0])
if (!$ScriptPath) { $ScriptPath = '.' }
}
# set default for $CheckBypassAM whether or not $AM_API is enabled
if ($AM_API)
{
$CheckBypassAM.IsChecked = $FALSE
if (-not $AM_API_URL -or -not $AM_API_KEY)
{
# bypass one time with warning
Write-Warning 'No AwesomeMiner API host or key provided. Please check config.json. Ignoring...'
$config.params.awesomeMinerAPIConfig.enabled = $FALSE
$CheckBypassAM.IsChecked = $TRUE
}
}
else
{
$CheckBypassAM.IsChecked = $TRUE
}
# first-time launch
if (-not (Test-Path .\miners.json))
{
Write-Verbose 'Detected first-time launch. fetching miner list...'
if ($config.params.awesomeMinerAPIConfig.enabled)
{
Update-AM-Miner-List
}
# Make a shortcut to the app on the desktop
$WScriptShell = New-Object -ComObject WScript.Shell
$app_shortcut = $WScriptShell.CreateShortcut("$env:USERPROFILE\Desktop\glt_launcher.exe.lnk")
$app_shortcut.WorkingDirectory = "$ScriptPath"
$app_shortcut.TargetPath = "$ScriptPath\glt_launcher.exe"
$app_shortcut.IconLocation = "$ScriptPath\resources\icons\BitCapLngLogo_BLK-04.ico"
$app_shortcut.Save()
}
else
{
if ($config.params.awesomeMinerAPIConfig.enabled)
{
# update miner list if older than a week
$last_accessed = (Get-Item .\miners.json).LastWriteTime | Get-Date -UFormat %s
if ((Get-Date -UFormat %s) - $last_accessed -ge 604800)
{
Update-AM-Miner-List
}
}
}
Write-Verbose 'Activating and Showing GUI dialog...'
$XAML_GUI.Activate() | Out-Null
$gui_result = $XAML_GUI.ShowDialog()
if ($gui_result -eq $FALSE)
{
Write-Verbose 'Dialog closed, Exitting...'
Exit 0
}