Skip to content

Commit

Permalink
Merge pull request #88 from nutdotnet/80-accessdenied-crash
Browse files Browse the repository at this point in the history
Improvements to Login subroutine

Closing to merge changes and continue broader efforts towards #79 and #80 per PR #86 .
  • Loading branch information
gbakeman authored Jul 2, 2023
2 parents 616af20 + 8446ac6 commit f0991d9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 91 deletions.
67 changes: 39 additions & 28 deletions WinNUT_V2/WinNUT-Client_Common/Nut_Socket.vb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Imports System.IO
Public Class Nut_Socket

#Region "Properties"

Public ReadOnly Property ConnectionStatus As Boolean
Get
If NutSocket IsNot Nothing Then
Expand All @@ -33,6 +32,13 @@ Public Class Nut_Socket
End Get
End Property

Private _isLoggedIn As Boolean = False
Public ReadOnly Property IsLoggedIn() As Boolean
Get
Return _isLoggedIn
End Get
End Property

Private Nut_Ver As String
Public ReadOnly Property Nut_Version() As String
Get
Expand All @@ -46,7 +52,6 @@ Public Class Nut_Socket
Return Net_Ver
End Get
End Property

#End Region

Private LogFile As Logger
Expand All @@ -64,10 +69,6 @@ Public Class Nut_Socket
''' </summary>
Private streamInUse As Boolean


Public Auth_Success As Boolean = False
' Private ReadOnly WatchDog As New Timer

Public Event Socket_Broken(ex As NutException)

''' <summary>
Expand Down Expand Up @@ -115,7 +116,12 @@ Public Class Nut_Socket
End Try

If ConnectionStatus Then
AuthLogin(Login, Password)
Try
AuthLogin(Login, Password)
Catch ex As NutException
' TODO: Make friendly message string for user.
LogFile.LogTracing("Error while attempting to log in: " & ex.Message, LogLvl.LOG_ERROR, Me)
End Try

Dim Nut_Query = Query_Data("VER")

Expand All @@ -132,6 +138,32 @@ Public Class Nut_Socket
End If
End Sub

''' <summary>
''' Register with the UPSd server as being dependant on it for power.
''' </summary>
''' <param name="Login"></param>
''' <param name="Password"></param>
''' <exception cref="NutException">A protocol error was encountered while trying to authenticate.</exception>
Private Sub AuthLogin(Login As String, Password As String)
If _isLoggedIn Then
Throw New InvalidOperationException("Attempted to login when already logged in.")
End If

LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me)

If Not String.IsNullOrEmpty(Login) Then
Query_Data("USERNAME " & Login)

If Not String.IsNullOrEmpty(Password) Then
Query_Data("PASSWORD " & Password)
End If
End If

Query_Data("LOGIN")
_isLoggedIn = True
LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me)
End Sub

''' <summary>
''' Perform various functions necessary to disconnect the socket from the NUT server.
''' </summary>
Expand Down Expand Up @@ -356,27 +388,6 @@ Public Class Nut_Socket
End If
End Function

Private Sub AuthLogin(Login As String, Password As String)
LogFile.LogTracing("Attempting authentication...", LogLvl.LOG_NOTICE, Me)
Auth_Success = False
If Not String.IsNullOrEmpty(Login) AndAlso String.IsNullOrEmpty(Password) Then
Dim Nut_Query = Query_Data("USERNAME " & Login)

If Nut_Query.ResponseType <> NUTResponse.OK Then
Throw New NutException(Nut_Query)
End If

Nut_Query = Query_Data("PASSWORD " & Password)

If Nut_Query.ResponseType <> NUTResponse.OK Then
Throw New NutException(Nut_Query)
End If
End If

LogFile.LogTracing("Authenticated successfully.", LogLvl.LOG_NOTICE, Me)
Auth_Success = True
End Sub

Private Sub Event_WatchDog(sender As Object, e As EventArgs)
Dim Nut_Query = Query_Data("")
If Nut_Query.ResponseType = NUTResponse.NORESPONSE Then
Expand Down
84 changes: 21 additions & 63 deletions WinNUT_V2/WinNUT-Client_Common/UPS_Device.vb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Public Class UPS_Device

Public ReadOnly Property IsAuthenticated As Boolean
Get
Return Nut_Socket.Auth_Success
Return Nut_Socket.IsLoggedIn
End Get
End Property

Expand Down Expand Up @@ -61,6 +61,26 @@ Public Class UPS_Device
End Property

#End Region

#Region "Events"

' Public Event Unknown_UPS()
Public Event DataUpdated()
Public Event Connected(sender As UPS_Device)
Public Event ReConnected(sender As UPS_Device)
' Notify that the connection was closed gracefully.
Public Event Disconnected()
' Notify of an unexpectedly lost connection (??)
Public Event Lost_Connect()
' Error encountered when trying to connect.
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
Public Event EncounteredNUTException(ex As NutException, sender As Object)
Public Event New_Retry()
' Public Event Shutdown_Condition()
' Public Event Stop_Shutdown()

#End Region

Private Const CosPhi As Double = 0.6
' How many milliseconds to wait before the Reconnect routine tries again.
Private Const DEFAULT_RECONNECT_WAIT_MS As Double = 5000
Expand All @@ -81,73 +101,11 @@ Public Class UPS_Device
Public Retry As Integer = 0
Public MaxRetry As Integer = 30
Private WithEvents Reconnect_Nut As New System.Windows.Forms.Timer
' Private ReadOnly WatchDog As New System.Windows.Forms.Timer
' Private Socket_Status As Boolean = False




Private LogFile As Logger
'Private ConnectionStatus As Boolean = False
'Private Server As String
'Private Port As Integer
'Private UPSName As String
'Private Delay As Integer
'Private Login As String
'Private Password As String
'Private Mfr As String
'Private Model As String
'Private Serial As String
'Private Firmware As String
'Private BattCh As Double
'Private BattV As Double
'Private BattRuntime As Double
'Private BattCapacity As Double
'Private PowerFreq As Double
'Private InputV As Double
'Private OutputV As Double
'Private Load As Double
'Private Status As String
'Private OutPower As Double
'Private InputA As Double
'Private Low_Batt As Integer
'Private Low_Backup As Integer
'Private LConnect As Boolean = False
'Private AReconnect As Boolean = False
'Private MaxRetry As Integer = 30
'Private Retry As Integer = 0
'Private ErrorStatus As Boolean = False
'Private ErrorMsg As String = ""
'Private Update_Nut As New System.Windows.Forms.Timer
'Private Reconnect_Nut As New System.Windows.Forms.Timer
'Private NutSocket As System.Net.Sockets.Socket
'Private NutTCP As System.Net.Sockets.TcpClient
'Private NutStream As System.Net.Sockets.NetworkStream
'Private ReaderStream As System.IO.StreamReader
'Private WriterStream As System.IO.StreamWriter
'Private Follow_FSD As Boolean = False
'Private Unknown_UPS_Name As Boolean = False
'Private Invalid_Data As Boolean = False
'Private Invalid_Auth_Data As Boolean = False

#Region "Properties"

#End Region

' Public Event Unknown_UPS()
Public Event DataUpdated()
Public Event Connected(sender As UPS_Device)
Public Event ReConnected(sender As UPS_Device)
' Notify that the connection was closed gracefully.
Public Event Disconnected()
' Notify of an unexpectedly lost connection (??)
Public Event Lost_Connect()
' Error encountered when trying to connect.
Public Event ConnectionError(sender As UPS_Device, innerException As Exception)
Public Event EncounteredNUTException(ex As NutException, sender As Object)
Public Event New_Retry()
' Public Event Shutdown_Condition()
' Public Event Stop_Shutdown()

''' <summary>
''' Raise an event when a status code is added to the UPS that wasn't there before.
Expand Down

0 comments on commit f0991d9

Please sign in to comment.