-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.vb
468 lines (407 loc) · 18.5 KB
/
Form1.vb
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
Imports System.IO
Imports System.Net
Imports LibreHardwareMonitor.Hardware
Public Class Form1
Public ConfigFile As String = Application.StartupPath & "\config.ini" ' Declare configuration file path
Dim computer As Computer = New Computer With {.IsGpuEnabled = True, .IsCpuEnabled = True} 'Declare Computer for Libre Hardware Monitor readings (CPU/GPU enabled)
'Declare all variables to store settings from config file
Dim MinerEXEPath As String = ""
Dim MinerEXEArguments As String = ""
Dim MinerGamingArguments As String = ""
Dim GPUsMAXTemp As String = ""
Dim ForceClose As Boolean = False
Dim MaxTempReachedAction As String = ""
Dim WatchDogIsEnabled As Boolean = False
Dim NotifyByEmail As Boolean = False
Dim MailSendTo As String = ""
Dim MyTemp As String = ""
Private Sub SetLabelText(ByVal txt As String, ByVal lbl As Label)
Dim fn As String = IO.Path.GetFileName(txt)
Dim pth As String = IO.Path.GetFullPath(txt)
Using g As Graphics = Graphics.FromHwnd(lbl.Handle)
Dim sw As Single = g.MeasureString(txt, lbl.Font).Width
If sw > lbl.Width Then
pth &= "...\" & fn
While sw > lbl.Width
pth = pth.Remove(pth.IndexOf("...\") - 1, 1)
sw = g.MeasureString(pth, lbl.Font).Width
End While
End If
End Using
lbl.Text = pth
End Sub
Public Sub GetMinerIcon()
On Error Resume Next
Dim ico As Icon = Icon.ExtractAssociatedIcon(MinerEXEPath)
PictureBox1.Image = ico.ToBitmap
SetLabelText(MinerEXEPath, Label3)
'Label3.Text = MinerEXEPath
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
WriteLog("Aplication started", 0)
UpdateMinerSettings() 'Read all configuration variables from config file
If MinerEXEPath = "" Or MinerEXEArguments = "" Or GPUsMAXTemp = "" Or MaxTempReachedAction = "" Then
WriteLog("MinerBoot needs to be configured!", 2)
MsgBox("Please update settings!", vbExclamation, "Empty values detected")
settings.ShowDialog()
End If
Try
computer.Open() 'Start Libre Hardware Monitor
For Each hardware As IHardware In computer.Hardware
'GET CPU NAME-----------------------------------------------------------------------------------
If hardware.HardwareType = HardwareType.Cpu Then
CPUName.Text = hardware.Name.ToString
End If
Next
WriteLog("LHM started succesfully", 0)
Catch ex As Exception
MsgBox(ex.Message, vbCritical, "Critical LHM error") 'On error, exit application
End
End Try
WhatsMyIP()
WriteLog("Public IP address retrieved succesfully", 2)
GetMinerIcon()
End Sub 'REVISED OK
Public Sub UpdateMinerSettings()
If Not IO.File.Exists(ConfigFile) Then
MsgBox("Configuration file not found at specified path:" & vbCrLf & vbCrLf & ConfigFile & vbCrLf & vbCrLf & "MinerBoot will close, please restore settings file.", vbCritical, "Critical error")
End
End If
MinerEXEPath = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "MinerEXEPath")
MinerEXEArguments = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "MinerEXEArguments")
MinerGamingArguments = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "MinerGamingArguments")
GPUsMAXTemp = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "GPUsMAXTemp")
MaxTempReachedAction = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "MaxTempReachedAction")
MailSendTo = INIReadWrite.ReadINI(ConfigFile, "EmailSettings", "SendTo")
GPUMaxTempLabel.Text = GPUsMAXTemp
Dim FC As String = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "ForceMinerClose")
If FC = "True" Then
ForceClose = True
ElseIf FC = "False" Then
ForceClose = False
Else
ForceClose = False
End If
Dim WIE As String = INIReadWrite.ReadINI(ConfigFile, "MinerSettings", "WatchDogEnbled")
If WIE = "True" Then
WatchDogIsEnabled = True
ElseIf WIE = "False" Then
WatchDogIsEnabled = False
Else
WatchDogIsEnabled = False
End If
Dim NBE = INIReadWrite.ReadINI(ConfigFile, "EmailSettings", "SendMail")
If NBE = "True" Then
NotifyByEmail = True
ElseIf NBE = "False" Then
NotifyByEmail = False
Else
NotifyByEmail = False
End If
WriteLog("Settings from config.ini file loaded succesfully", 0)
If WatchDogIsEnabled = True Then
WatchDog.Enabled = True
WatchDogMenu.Text = "ON"
WriteLog("WatchDog is now enabled", 0)
ElseIf WatchDogIsEnabled = False Then
WatchDog.Enabled = False
WatchDogMenu.Text = "OFF"
WriteLog("WatchDog is now disabled", 0)
End If
End Sub 'REVISED OK
Private Sub PutSystemToSleep()
WriteLog("Hibernating system...", 0)
Application.SetSuspendState(PowerState.Suspend, True, False)
End Sub 'REVISED OK
Private Sub KillMiner()
Try
For Each proc As Process In Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(MinerEXEPath))
If ForceClose = True Then
proc.Kill()
ElseIf ForceClose = False Then
proc.CloseMainWindow()
Else
proc.Kill()
End If
Next
Catch ex As Exception
WriteLog("Error halting miner", 1)
End Try
End Sub 'REVISED OK
Private Sub LaunchMiner()
WriteLog("Cleaning previous miner instances...", 0)
KillMiner() ' Kill previous miner processes
WriteLog("Reloading settings...", 0)
UpdateMinerSettings() ' Update settings
Try
Dim MyNewMiner As New ProcessStartInfo
MyNewMiner.FileName = MinerEXEPath
MyNewMiner.Verb = "runas"
If MiningToolStripMenuItem.Checked = True Then
MyNewMiner.Arguments = MinerEXEArguments
ElseIf GamingToolStripMenuItem.Checked = True Then
MyNewMiner.Arguments = MinerGamingArguments
End If
MyNewMiner.WindowStyle = ProcessWindowStyle.Maximized
Process.Start(MyNewMiner)
WriteLog("Miner started succesfully!", 0)
Catch ex As Exception
KillMiner()
WriteLog("Error starting Miner!", 1)
End Try
End Sub 'REVISED OK
Private Sub Wait(ByVal seconds As Integer)
On Error Resume Next
For i As Integer = 0 To seconds * 100
System.Threading.Thread.Sleep(10)
Application.DoEvents()
Next
End Sub 'REVISED OK
Public Sub AddGPUCard(ByVal ItemText As String, ByVal Temp As String, ByVal IconImage As Integer)
On Error Resume Next
Dim objItem As ListViewItem = ListView1.Items.Add(ItemText)
With objItem
.UseItemStyleForSubItems = False
If Temp <= 60 Then
.SubItems.Add(Temp).ForeColor = Color.Green
ElseIf Temp > 60 And Temp <= 70 Then
.SubItems.Add(Temp).ForeColor = Color.Orange
ElseIf Temp > 70 Then
.SubItems.Add(Temp).ForeColor = Color.Red
End If
.ImageIndex = IconImage
End With
End Sub 'REVISED OK
Private Sub StartMiningToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StartMiningToolStripMenuItem.Click
LaunchMiner()
End Sub
Private Sub StopMiningToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles StopMiningToolStripMenuItem.Click
WatchDog.Enabled = False
WatchDogMenu.Text = "OFF"
KillMiner()
WriteLog("Miner halted by user!", 0)
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
On Error Resume Next
Dim result As DialogResult = MsgBox("Do you want to finish miner process after closing MinerBoot?", vbYesNoCancel + vbQuestion, "Please confirm")
If result = DialogResult.Yes Then
computer.Close()
WriteLog("Closing MinerBoot...", 0)
WatchDog.Enabled = False
KillMiner()
Application.Exit()
ElseIf result = DialogResult.No Then
computer.Close()
WriteLog("Closing MinerBoot...", 0)
WatchDog.Enabled = False
Application.Exit()
ElseIf result = DialogResult.Cancel Then
Exit Sub
End If
End Sub
Private Sub SettingsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SettingsToolStripMenuItem.Click
settings.ShowDialog()
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
If Me.WindowState = FormWindowState.Minimized Then
Me.ShowInTaskbar = False
NotifyIcon1.Visible = True
Me.Hide()
End If
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
If Me.WindowState = FormWindowState.Minimized = True Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
Me.Show()
NotifyIcon1.Visible = False
End If
End Sub
Private Sub WhatsMyIP()
Try
Dim request As WebRequest = WebRequest.Create("https://api.ipify.org")
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
response.Close()
IPLabel.Text = responseFromServer
Catch error_t As Exception
WriteLog("Error retrieving public IP address!", 1)
IPLabel.Text = "0.0.0.0"
End Try
End Sub
Private Sub AlwaysOnTopToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AlwaysOnTopToolStripMenuItem.Click
If AlwaysOnTopToolStripMenuItem.Checked = False Then
AlwaysOnTopToolStripMenuItem.Checked = True
Me.TopMost = True
ElseIf AlwaysOnTopToolStripMenuItem.Checked = True Then
AlwaysOnTopToolStripMenuItem.Checked = False
Me.TopMost = False
End If
End Sub
Public Sub SendMail(ByVal temp As String)
If NotifyByEmail = True Then
Try
Dim request As WebRequest = WebRequest.Create("https://francescpaez.com/programas/minerboot/sendmail.php?email=" & MailSendTo & "&temp=" & temp & "&action=" & MaxTempReachedAction)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
Dim reader As StreamReader = New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
reader.Close()
response.Close()
WriteLog("Notification email was sent!", 0)
Catch error_t As Exception
WriteLog("Notification email was not sent!", 1)
End Try
End If
End Sub
Private Sub WatchDog_Tick(sender As Object, e As EventArgs) Handles WatchDog.Tick
Try
Dim listProc() As Process = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(MinerEXEPath))
If listProc.Count < 1 Then
WriteLog("WATCHDOG: Miner not found, restarting...", 2)
Label4.Text = "Miner not found..."
Label4.ForeColor = Color.Red
LaunchMiner()
Else
Label4.Text = "Miner found!"
Label4.ForeColor = Color.Green
End If
Catch ex As Exception
WatchDogIsEnabled = False
WatchDog.Enabled = False
WatchDogMenu.Text = "OFF"
WriteLog("WATCHDOG: Error launching miner!", 1)
WriteLog("WATCHDOG: Miner disabled", 2)
End Try
End Sub
Private Sub CheckOverheath(ByVal Temp As String)
If Temp >= GPUsMAXTemp Then
WriteLog("GPUs overheath detected!", 2)
If MaxTempReachedAction = "Pause miner" Then
WatchDog.Enabled = False
WriteLog("Closing previous miner instances...", 0)
KillMiner()
SendMail(Temp)
WriteLog("Waiting 200 seconds to cooldown GPUs...", 2)
Wait(200)
WriteLog("Cooldown finished, restarting miner...", 2)
'LaunchMiner()
If WatchDogIsEnabled = True Then
WatchDog.Enabled = True
End If
ElseIf MaxTempReachedAction = "Hibernate" Then
KillMiner()
PutSystemToSleep()
ElseIf MaxTempReachedAction = "Reboot" Then
System.Diagnostics.Process.Start("shutdown", "-r -t 00")
Else
KillMiner()
PutSystemToSleep()
End If
End If
End Sub
Private Sub CheckTemperaturesTimer_Tick(sender As Object, e As EventArgs) Handles CheckTemperaturesTimer.Tick
ListView1.Items.Clear()
Try
For Each hardware As IHardware In computer.Hardware
'IF GPU IS NVIDIA-----------------------------------------------------------------------------------
If hardware.HardwareType = HardwareType.GpuNvidia Then
hardware.Update()
Dim tempSensors = hardware.Sensors.Where(Function(s) s.SensorType = SensorType.Temperature)
tempSensors.ToList.ForEach(Sub(s) MyTemp = s.Value)
AddGPUCard(hardware.Name, MyTemp, 0)
CheckOverheath(MyTemp)
'IF GPU IS AMD-------------------------------------------------------------------------------------
ElseIf hardware.HardwareType = HardwareType.GpuAmd Then
hardware.Update()
Dim tempSensors = hardware.Sensors.Where(Function(s) s.SensorType = SensorType.Temperature)
tempSensors.ToList.ForEach(Sub(s) MyTemp = s.Value)
AddGPUCard(hardware.Name, MyTemp, 1)
CheckOverheath(MyTemp)
End If
Next
If ListView1.Items.Count = 0 Then
AddGPUCard("No GPUs were detected!", "0", 2)
End If
Catch ex As Exception
'ERROR READING TEMPERATURES, RISK OF OVERHEATING!: KILL MINER, PAUSE TEMPERATURE READINGS AND WATCHDOG TIMERS
CheckTemperaturesTimer.Enabled = False
WatchDog.Enabled = False
KillMiner()
WriteLog("LHM Error while reading temperatures", 1)
WriteLog("Miner halted by LHM_CRITICAL_ERROR", 1)
End Try
End Sub
Private Sub AboutMinerBootToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutMinerBootToolStripMenuItem.Click
About.ShowDialog()
End Sub
Private Sub DonateWithPayPalToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DonateWithPayPalToolStripMenuItem.Click
On Error Resume Next
Process.Start("https://paypal.me/fpaezcom/")
End Sub
Private Sub GitHubPageToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GitHubPageToolStripMenuItem.Click
On Error Resume Next
Process.Start("https://github.com/fpaezf/MinerBoot")
End Sub
Private Sub WatchDogMenu_Click(sender As Object, e As EventArgs) Handles WatchDogMenu.Click
If WatchDogMenu.Text = "ON" Then
WatchDogMenu.Text = "OFF"
WatchDog.Enabled = False
WriteLog("WATCHDOG: Disabled by user", 2)
ElseIf WatchDogMenu.Text = "OFF" Then
WatchDogMenu.Text = "ON"
WatchDog.Enabled = True
WriteLog("WATCHDOG: Enabled by user", 2)
End If
End Sub
Public Sub WriteLog(ByVal evento As String, ByVal Icon As Integer)
On Error Resume Next
Dim objItem As ListViewItem = ListView2.Items.Add(DateTime.Now.ToString("dd/MM/yyyy - HH:mm:ss") & ": " & evento)
With objItem
.ImageIndex = Icon
End With
ListView2.Items(ListView2.Items.Count - 1).EnsureVisible()
End Sub 'REVISED OK
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
ExitToolStripMenuItem.PerformClick()
End If
End Sub
Private Sub IPLabel_Click(sender As Object, e As EventArgs) Handles IPLabel.Click
Clipboard.SetText(IPLabel.Text, TextDataFormat.Text)
MsgBox("Public IP address was succesfully copied!", vbInformation, "Done")
End Sub
Private Sub ClearEventsListToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ClearEventsListToolStripMenuItem.Click
ListView2.Items.Clear()
End Sub
Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
ExitToolStripMenuItem.PerformClick()
End Sub
Private Sub RestoreToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RestoreToolStripMenuItem.Click
If Me.WindowState = FormWindowState.Minimized = True Then
Me.WindowState = FormWindowState.Normal
Me.ShowInTaskbar = True
Me.Show()
NotifyIcon1.Visible = False
End If
End Sub
Private Sub UpdateIPLabel_Tick(sender As Object, e As EventArgs) Handles UpdateIPLabel.Tick
WhatsMyIP()
End Sub
Private Sub MiningToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MiningToolStripMenuItem.Click
KillMiner()
MiningToolStripMenuItem.Checked = True
GamingToolStripMenuItem.Checked = False
'UpdateMinerSettings()
End Sub
Private Sub GamingToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GamingToolStripMenuItem.Click
KillMiner()
MiningToolStripMenuItem.Checked = False
GamingToolStripMenuItem.Checked = True
'UpdateMinerSettings()
End Sub
End Class