337 lines
12 KiB
VB.net
337 lines
12 KiB
VB.net
Imports System.IO
|
|
Imports System.IO.Compression
|
|
Imports System.Runtime.InteropServices
|
|
Imports System.Security.Cryptography
|
|
Imports Microsoft.Win32
|
|
Imports Toolbelt.Drawing
|
|
|
|
Module Helper
|
|
|
|
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (<System.Runtime.InteropServices.OutAttribute()> ByRef Description As Integer, ReservedValue As Integer) As Boolean
|
|
Public Function InternetState() As Boolean
|
|
Dim num As Integer
|
|
Return InternetGetConnectedState(num, 0)
|
|
End Function
|
|
Public Function UnixTimeToDateTime(ByVal unixtime As Long) As DateTime
|
|
Dim dtDateTime As System.DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Local)
|
|
Try
|
|
dtDateTime = dtDateTime.AddSeconds(unixtime).ToLocalTime()
|
|
Catch
|
|
dtDateTime = DateTime.MaxValue
|
|
End Try
|
|
Return dtDateTime
|
|
End Function
|
|
Public Function FilesType(filePath As String) As String
|
|
Dim description As String = Nothing
|
|
Try
|
|
Dim classesRoot As RegistryKey = Microsoft.Win32.Registry.ClassesRoot
|
|
Dim extension As String = IO.Path.GetExtension(filePath)
|
|
Dim registryKey As RegistryKey = classesRoot.OpenSubKey(extension, False)
|
|
If registryKey Is Nothing Then
|
|
classesRoot.Close()
|
|
Return GetDefaultDescription(extension)
|
|
End If
|
|
|
|
Dim name As String = Convert.ToString(registryKey.GetValue("", ""))
|
|
registryKey.Close()
|
|
|
|
If String.IsNullOrEmpty(name) Then
|
|
classesRoot.Close()
|
|
Return GetDefaultDescription(extension)
|
|
End If
|
|
|
|
Dim registryKey2 As RegistryKey = classesRoot.OpenSubKey(name)
|
|
If registryKey2 Is Nothing Then
|
|
classesRoot.Close()
|
|
Return GetDefaultDescription(extension)
|
|
End If
|
|
|
|
description = Convert.ToString(registryKey2.GetValue("", ""))
|
|
registryKey2.Close()
|
|
classesRoot.Close()
|
|
|
|
Catch
|
|
description = Nothing
|
|
End Try
|
|
|
|
If String.IsNullOrEmpty(description) Then
|
|
Dim ext As String = IO.Path.GetExtension(filePath).Replace(".", "").ToUpper()
|
|
If String.IsNullOrEmpty(ext) Then
|
|
Return "File"
|
|
Else
|
|
Return ext & " File"
|
|
End If
|
|
End If
|
|
|
|
Return description
|
|
End Function
|
|
|
|
Private Function GetDefaultDescription(ext As String) As String
|
|
Dim extClean = ext.Replace(".", "").ToUpper()
|
|
If String.IsNullOrEmpty(extClean) Then
|
|
Return "File"
|
|
Else
|
|
Return extClean & " File"
|
|
End If
|
|
End Function
|
|
|
|
Function LVLog(ByVal str As String, ByVal Color As Color)
|
|
Try
|
|
Dim LV As ListViewItem = New ListViewItem()
|
|
|
|
LV.Text = DateTime.Now.ToLongTimeString()
|
|
|
|
LV.ForeColor = Color
|
|
|
|
LV.SubItems.Add(str)
|
|
|
|
Main.Lv2.Items.Insert(0, LV)
|
|
Catch ex As Exception
|
|
Debug.WriteLine(ex.Message)
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetNotes(ByVal ID As String) As String
|
|
Try
|
|
Dim SNM As String = Microsoft.Win32.Registry.GetValue(SettingsHelper.RegSave & "\Notes", ID, Nothing)
|
|
If String.IsNullOrEmpty(SNM) Then
|
|
Return "Null"
|
|
Else
|
|
Return SNM
|
|
End If
|
|
Catch ex As Exception
|
|
Return "Error"
|
|
End Try
|
|
End Function
|
|
|
|
Public Function NewNotes(ByVal ID As String, ByVal Str As String) As String
|
|
Try
|
|
Microsoft.Win32.Registry.SetValue(SettingsHelper.RegSave & "\Notes", ID, Str)
|
|
Catch ex As Exception
|
|
MessageBox.Show(ex.Message)
|
|
End Try
|
|
End Function
|
|
|
|
Function SB(ByVal s As String) As Byte()
|
|
Return System.Text.Encoding.UTF8.GetBytes(s)
|
|
End Function
|
|
|
|
Function BS(ByVal b As Byte()) As String
|
|
Return System.Text.Encoding.UTF8.GetString(b)
|
|
End Function
|
|
|
|
|
|
Function Decompress(ByVal input As Byte()) As Byte()
|
|
Using source = New MemoryStream(input)
|
|
Dim lengthBytes As Byte() = New Byte(3) {}
|
|
source.Read(lengthBytes, 0, 4)
|
|
Dim length = BitConverter.ToInt32(lengthBytes, 0)
|
|
|
|
Using decompressionStream = New GZipStream(source, CompressionMode.Decompress)
|
|
Dim result = New Byte(length - 1) {}
|
|
decompressionStream.Read(result, 0, length)
|
|
Return result
|
|
End Using
|
|
End Using
|
|
End Function
|
|
Function Compress(ByVal input As Byte()) As Byte()
|
|
Using result = New MemoryStream()
|
|
Dim lengthBytes = BitConverter.GetBytes(input.Length)
|
|
result.Write(lengthBytes, 0, 4)
|
|
|
|
Using compressionStream = New GZipStream(result, CompressionMode.Compress)
|
|
compressionStream.Write(input, 0, input.Length)
|
|
compressionStream.Flush()
|
|
End Using
|
|
|
|
Return result.ToArray()
|
|
End Using
|
|
End Function
|
|
|
|
Public Function GetChecksum(ByVal f As String) As String
|
|
Using stream As FileStream = File.OpenRead(f)
|
|
Dim sha As SHA256Managed = New SHA256Managed()
|
|
Dim checksum As Byte() = sha.ComputeHash(stream)
|
|
Return BitConverter.ToString(checksum).Replace("-", String.Empty)
|
|
End Using
|
|
End Function
|
|
|
|
|
|
Public Function BytesToString(ByVal byteCount As Long) As String
|
|
Dim suf As String() = {"B", "KB", "MB", "GB", "TB", "PB", "EB"}
|
|
If byteCount = 0 Then Return "0" & suf(0)
|
|
Dim bytes As Long = Math.Abs(byteCount)
|
|
Dim place As Integer = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)))
|
|
Dim num As Double = Math.Round(bytes / Math.Pow(1024, place), 1)
|
|
Return (Math.Sign(byteCount) * num).ToString() & suf(place)
|
|
End Function
|
|
|
|
Function AES_Encryptor(ByVal input As Byte()) As Byte()
|
|
Dim AES As New Security.Cryptography.RijndaelManaged
|
|
Dim Hash As New Security.Cryptography.MD5CryptoServiceProvider
|
|
Dim ciphertext As String = ""
|
|
Try
|
|
AES.Key = Hash.ComputeHash(SB(SettingsHelper.KEY))
|
|
AES.Mode = Security.Cryptography.CipherMode.ECB
|
|
Dim DESEncrypter As Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
|
|
Dim Buffer As Byte() = input
|
|
Return DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
|
|
Catch ex As Exception
|
|
Debug.WriteLine("AES_Encryptor" & ex.Message)
|
|
End Try
|
|
End Function
|
|
|
|
Function AES_Decryptor(ByVal input As Byte(), Optional C As Client = Nothing) As Byte()
|
|
Dim AES As New Security.Cryptography.RijndaelManaged
|
|
Dim Hash As New Security.Cryptography.MD5CryptoServiceProvider
|
|
Try
|
|
AES.Key = Hash.ComputeHash(SB(SettingsHelper.KEY))
|
|
AES.Mode = Security.Cryptography.CipherMode.ECB
|
|
Dim DESDecrypter As Security.Cryptography.ICryptoTransform = AES.CreateDecryptor
|
|
Dim Buffer As Byte() = input
|
|
Return DESDecrypter.TransformFinalBlock(Buffer, 0, Buffer.Length)
|
|
Catch ex As Exception
|
|
Debug.WriteLine("AES_Decryptor" & ex.Message)
|
|
If C.IsConnected Then
|
|
If Not SettingsHelper.Blocked.Contains(C.IP.ToString.Split(":")(0)) Then
|
|
SettingsHelper.Blocked.Add(C.IP.ToString.Split(":")(0))
|
|
C.isDisconnected()
|
|
Exit Function
|
|
End If
|
|
End If
|
|
End Try
|
|
Return Nothing
|
|
End Function
|
|
Public Function CTMGORHR(Input As Integer) As Object
|
|
Dim flag As Boolean = Input >= 1024
|
|
Dim result As Object
|
|
If flag Then
|
|
result = Strings.FormatNumber(CDbl(Input) / 1024.0, 1, TriState.UseDefault, TriState.UseDefault, TriState.UseDefault) & " GHz"
|
|
Else
|
|
flag = (Input >= 0 AndAlso Input <= 1023)
|
|
If flag Then
|
|
result = Strings.FormatNumber(Input, 1, TriState.UseDefault, TriState.UseDefault, TriState.UseDefault) & " MHz"
|
|
Else
|
|
result = "????"
|
|
End If
|
|
End If
|
|
Return result
|
|
End Function
|
|
Public Function uptimeToDHMS(ByVal inSeconds As Integer) As String
|
|
Try
|
|
Dim seconds As Integer
|
|
seconds = inSeconds Mod 60
|
|
inSeconds = (inSeconds - seconds) / 60
|
|
Dim minutes As Integer
|
|
minutes = inSeconds Mod 60
|
|
inSeconds = (inSeconds - minutes) / 60
|
|
Dim hours As Integer
|
|
hours = inSeconds Mod 24
|
|
inSeconds = (inSeconds - hours) / 24
|
|
Dim days As Integer
|
|
days = inSeconds
|
|
Return days & "d " & hours & "h " & minutes & "m " & seconds & "s"
|
|
Catch ex As Exception
|
|
Return "Error"
|
|
End Try
|
|
End Function
|
|
|
|
Public Const MAX_PATH As Int32 = 260
|
|
Public Const SHGFI_ICON As Int32 = &H100
|
|
Public Const SHGFI_USEFILEATTRIBUTES As Int32 = &H10
|
|
Public Const FILE_ATTRIBUTE_NORMAL As Int32 = &H80
|
|
|
|
Public Structure SHFILEINFO
|
|
Public hIcon As IntPtr
|
|
Public iIcon As Int32
|
|
Public dwAttributes As Int32
|
|
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)>
|
|
Public szDisplayName As String
|
|
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)>
|
|
Public szTypeName As String
|
|
End Structure
|
|
|
|
Public Enum IconSize
|
|
SHGFI_LARGEICON = 0
|
|
SHGFI_SMALLICON = 1
|
|
End Enum
|
|
|
|
<DllImport("shell32.dll", CharSet:=CharSet.Auto)>
|
|
Public Function SHGetFileInfo(
|
|
ByVal pszPath As String,
|
|
ByVal dwFileAttributes As Int32,
|
|
ByRef psfi As SHFILEINFO,
|
|
ByVal cbFileInfo As Int32,
|
|
ByVal uFlags As Int32) As IntPtr
|
|
End Function
|
|
|
|
<DllImport("user32.dll", SetLastError:=True)>
|
|
Public Function DestroyIcon(ByVal hIcon As IntPtr) As Boolean
|
|
End Function
|
|
Public Function GetFileIcon(ByVal fileExt As String, Optional ByVal ICOsize As IconSize = IconSize.SHGFI_SMALLICON) As Bitmap
|
|
Try
|
|
Dim shinfo As New SHFILEINFO
|
|
shinfo.szDisplayName = New String(Chr(0), MAX_PATH)
|
|
shinfo.szTypeName = New String(Chr(0), 80)
|
|
SHGetFileInfo(fileExt, FILE_ATTRIBUTE_NORMAL, shinfo, Marshal.SizeOf(shinfo), SHGFI_ICON Or ICOsize Or SHGFI_USEFILEATTRIBUTES)
|
|
Dim bmp As Bitmap = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap
|
|
DestroyIcon(shinfo.hIcon)
|
|
Return bmp
|
|
Catch ex As Exception
|
|
Debug.WriteLine(ex.Message)
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetIcon(ByVal path As String) As String
|
|
Try
|
|
Dim tempFile As String = IO.Path.GetTempFileName() & ".ico"
|
|
|
|
Using fs As FileStream = New FileStream(tempFile, FileMode.Create)
|
|
IconExtractor.Extract1stIconTo(path, fs)
|
|
End Using
|
|
|
|
Return tempFile
|
|
Catch
|
|
End Try
|
|
|
|
Return ""
|
|
End Function
|
|
|
|
|
|
Public Async Sub FadeIn(ByVal o As Form, ByVal Optional interval As Integer = 80)
|
|
Try
|
|
While o.Opacity < 1.0
|
|
Await Task.Delay(interval)
|
|
o.Opacity += 0.05
|
|
End While
|
|
o.Opacity = 1
|
|
Catch ex As Exception
|
|
Debug.WriteLine(ex.Message)
|
|
End Try
|
|
End Sub
|
|
|
|
|
|
Public Async Sub FadeInMain(ByVal o As Form, ByVal Optional interval As Integer = 80)
|
|
|
|
Try
|
|
While o.Opacity < 1.0
|
|
Await Task.Delay(interval)
|
|
o.Opacity += 0.05
|
|
End While
|
|
|
|
Try
|
|
My.Computer.Audio.Play(Path.Combine(Application.StartupPath, "Sounds", "Intro.wav"))
|
|
Catch ex As Exception
|
|
Debug.WriteLine(ex.Message)
|
|
End Try
|
|
|
|
o.Opacity = 1
|
|
|
|
Main.trans = True
|
|
|
|
Catch ex As Exception
|
|
Debug.WriteLine(ex.Message)
|
|
End Try
|
|
End Sub
|
|
|
|
End Module |