diff --git a/LVNC-RS/IniFile.vb b/LVNC-RS/IniFile.vb new file mode 100644 index 0000000..8db9f1d --- /dev/null +++ b/LVNC-RS/IniFile.vb @@ -0,0 +1,98 @@ +Imports System.IO +Imports System.Runtime.InteropServices +Imports System.Text + +Namespace Ini + Public Class IniFile +#Region "DLL Import" + ''' + ''' Записывает ключ в заданный раздел INI-файла. + ''' + ''' Имя раздела. + ''' Имя ключа. + ''' Значение ключа. + ''' Путь к INI-файлу. + + Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal value As String, ByVal filePath As String) As Long + End Function + + ''' + ''' Считывает ключ заданного раздела INI-файла. + ''' + ''' Имя раздела. + ''' Имя ключа. + ''' + ''' + ''' + ''' Путь к INI-файлу. + ''' С помощью конструктора записываем путь до файла и его имя. + + Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal [default] As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer + End Function +#End Region '/DLL Import + +#Region "Constructor" + ''' + ''' Имя файла. + ''' + Private IniPath As String + + ''' + ''' Читаем ini-файл и возвращаем значение указного ключа из заданной секции. + ''' + ''' + Public Sub New(ByVal iniPath As String) + Me.IniPath = New FileInfo(iniPath).FullName.ToString + End Sub +#End Region + +#Region "INI-file Methods" + ''' + ''' Проверяет, что заданный ключ существует в INI-файле. + ''' + ''' Имя раздела. + ''' Имя ключа. + Public Function KeyExists(ByVal section As String, ByVal key As String) As Boolean + Return (Me.ReadKey(section, key).Length > 0) + End Function + + ''' + ''' Читает значение заданного ключа в заданном разделе INI-файла. + ''' + ''' Имя раздела. + ''' Имя ключа. + Public Function ReadKey(ByVal section As String, ByVal key As String) As String + Dim retVal As New StringBuilder(&HFF) + IniFile.GetPrivateProfileString(section, key, "", retVal, &HFF, Me.IniPath) + Return retVal.ToString() + End Function + + ''' + ''' Создаёт заданный ключ в заданном разделе. Если раздел не существует, он будет создан. + ''' + ''' Имя раздела. + ''' Имя ключа. + ''' Значение ключа. Если NULL, то ключ будет удалён. Если String.Empty, то присвоится пустое значение. + Public Sub WriteKey(ByVal section As String, ByVal key As String, ByVal value As String) + IniFile.WritePrivateProfileString(section, key, value, Me.IniPath) + End Sub + + ''' + ''' Удаляет заданный ключ из заданного раздела INI-файла. + ''' + ''' Имя раздела. + ''' Имя ключа. + Public Sub DeleteKey(ByVal section As String, ByVal key As String) + Me.WriteKey(section, key, Nothing) + End Sub + + ''' + ''' Удаляет заданный раздел INI-файла. + ''' + ''' Имя раздела. + Public Sub DeleteSection(ByVal section As String) + Me.WriteKey(section, Nothing, Nothing) + End Sub +#End Region + End Class +End Namespace diff --git a/LVNC-RS/LVNC-RS.vbproj b/LVNC-RS/LVNC-RS.vbproj index 0aef6df..059c7f4 100644 --- a/LVNC-RS/LVNC-RS.vbproj +++ b/LVNC-RS/LVNC-RS.vbproj @@ -91,6 +91,7 @@ + Form diff --git a/LVNC-RS/Services.vb b/LVNC-RS/Services.vb index 82b9fa3..29a4adf 100644 --- a/LVNC-RS/Services.vb +++ b/LVNC-RS/Services.vb @@ -1,7 +1,13 @@ Module Services - Public sRunTimeDir As String = getRunTimeDir() & "\LVNC-RS" - Public sConfigFile As String = sRunTimeDir & "\lvnc-rs.ini" - Public lSupportID As Long = 0 + Private sRunTimeDir As String = getRunTimeDir() & "\LVNC-RS" + Private oConfigFile As New Ini.IniFile(sRunTimeDir & "\lvnc-rs.ini") + + Public sSupportID As String = String.Empty + Public sSupportPasswd As String = String.Empty + Public byProxyStatus As Byte = 0 + + Public sProxyHost As String = oConfigFile.ReadKey("repeater", "host") + Public sProxyPort As String = oConfigFile.ReadKey("repeater", "serverport") Private aExecResources(,) As String = { {"lvnc_rs_ini", "\lvnc_rs.ini"}, @@ -72,7 +78,7 @@ Public Sub getSupportID() Dim aNics() As Net.NetworkInformation.NetworkInterface = Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() Dim sMAC As String = String.Empty - Dim lID As Long = 0 + Dim iID As Integer = 0 For Each nic As Net.NetworkInformation.NetworkInterface In aNics If (nic.NetworkInterfaceType = Net.NetworkInformation.NetworkInterfaceType.Ethernet Or @@ -91,17 +97,58 @@ Next i Dim aMAC() = strBuilder.ToString.Split(":") - Dim n As Long = 0 + Dim n As Integer = 0 For i As Integer = 0 To UBound(aMAC) n = Convert.ToInt32(aMAC(i), 16) Debug.Print(n) - lID = ((lID * 16) + n) Mod 99999999 + iID = ((iID * 16) + n) Mod 99999999 - If lID < 99999999 Then - lID = lID + 10000000 + If iID < 99999999 Then + iID = iID + 10000000 End If Next - lSupportID = lID + sSupportID = iID.ToString + End Sub + + Public Sub getSupportPasswd() + Dim rndGen As New System.Random + + sSupportPasswd = rndGen.Next(1000, 10000).ToString + End Sub + + Public Sub checkProxy(ByRef sHost As String, ByRef sPort As String) + Dim hostEntry As Net.IPHostEntry + Dim ipAddress As Net.IPAddress + Dim epHost As Net.IPEndPoint + Dim socket As Net.Sockets.Socket + + Try + hostEntry = Net.Dns.GetHostEntry(sHost) + Catch ex As Exception + byProxyStatus = 2 + + Exit Sub + End Try + + If hostEntry.AddressList.Count > 0 Then + ipAddress = hostEntry.AddressList.FirstOrDefault + epHost = New Net.IPEndPoint(ipAddress, Convert.ToInt32(sPort)) + socket = New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp) + + Try + socket.Connect(epHost) + Catch ex As Exception + + End Try + End If + + If socket.Connected Then + byProxyStatus = 1 + Else + byProxyStatus = 2 + End If + + socket.Disconnect(False) End Sub End Module diff --git a/LVNC-RS/frmLVNC-RS-Main.Designer.vb b/LVNC-RS/frmLVNC-RS-Main.Designer.vb index 9c8e4f0..448649e 100644 --- a/LVNC-RS/frmLVNC-RS-Main.Designer.vb +++ b/LVNC-RS/frmLVNC-RS-Main.Designer.vb @@ -132,7 +132,7 @@ Partial Class frmLVNCRSMain 'tsLabelSrvStatus ' Me.tsLabelSrvStatus.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft - Me.tsLabelSrvStatus.Margin = New System.Windows.Forms.Padding(0) + Me.tsLabelSrvStatus.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0) Me.tsLabelSrvStatus.Name = "tsLabelSrvStatus" Me.tsLabelSrvStatus.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never Me.tsLabelSrvStatus.Size = New System.Drawing.Size(0, 21) @@ -143,7 +143,7 @@ Partial Class frmLVNCRSMain Me.tsButtonRunAs.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image Me.tsButtonRunAs.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None Me.tsButtonRunAs.ImageTransparentColor = System.Drawing.Color.Magenta - Me.tsButtonRunAs.Margin = New System.Windows.Forms.Padding(0, 0, 1, 0) + Me.tsButtonRunAs.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0) Me.tsButtonRunAs.Name = "tsButtonRunAs" Me.tsButtonRunAs.Overflow = System.Windows.Forms.ToolStripItemOverflow.Never Me.tsButtonRunAs.Size = New System.Drawing.Size(23, 21) diff --git a/LVNC-RS/frmLVNC-RS-Main.vb b/LVNC-RS/frmLVNC-RS-Main.vb index 68f5f60..6eac379 100644 --- a/LVNC-RS/frmLVNC-RS-Main.vb +++ b/LVNC-RS/frmLVNC-RS-Main.vb @@ -2,6 +2,8 @@ Private Sub frmLVNCRSMain_Load(sender As Object, e As EventArgs) Handles Me.Load Services.checkRunTimeComponents() Services.getSupportID() + Services.getSupportPasswd() + Services.checkProxy(Services.sProxyHost, Services.sProxyPort) If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then tsButtonRunAs.Image = Global.LVNC_RS.My.Resources.is_admin_on @@ -9,7 +11,16 @@ tsButtonRunAs.Image = Global.LVNC_RS.My.Resources.is_admin_off End If - txtLVNCSupportID.Text = Format(Services.lSupportID, "### ### ##0") + If Services.byProxyStatus = 1 Then + tsLabelSrvStatus.Image = Global.LVNC_RS.My.Resources.online + Else + tsLabelSrvStatus.Image = Global.LVNC_RS.My.Resources.offline + End If + + txtLVNCSupportID.Text = Format(Convert.ToInt32(Services.sSupportID), "### ### ##0") + txtLVNCSupportPasswd.Text = Services.sSupportPasswd + + tCheckTimer.Enabled = True End Sub Private Sub tsButtonRunAs_Click(sender As Object, e As EventArgs) Handles tsButtonRunAs.Click @@ -40,4 +51,22 @@ ' 'Application.Exit() End Sub + + Private Sub frmLVNCRSMain_Shown(sender As Object, e As EventArgs) Handles Me.Shown + Me.Show() + + Dim sProxy As String = "digital-freak.ru" + Dim sport As String = "5900" + Services.checkProxy(sProxy, sport) + End Sub + + Private Sub tCheckTimer_Tick(sender As Object, e As EventArgs) Handles tCheckTimer.Tick + Services.checkProxy(Services.sProxyHost, Services.sProxyPort) + + If Services.byProxyStatus = 1 Then + tsLabelSrvStatus.Image = Global.LVNC_RS.My.Resources.online + Else + tsLabelSrvStatus.Image = Global.LVNC_RS.My.Resources.offline + End If + End Sub End Class