Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Option Explicit
- Type DATA_PARAMS
- HostName As BStr
- UserName As BStr
- Password As BStr
- RebootIt As Boolean
- Cookie As BStr
- End Type
- '====================================================================
- Public Sub Main()
- asRegisterAppPath
- InitCommonControls
- If Not asIsInIDE Then asSetWindowResIconAll asGetThunderMainHandle(), IDI_APPICON
- Dim DP As DATA_PARAMS
- If Not DetectCommandLine(DP) Then Exit Sub
- If Not LogonGoFlex(DP) Then Exit Sub
- If SendShutdownCommand(DP) Then
- MsgBox "Команда на " & _
- IIf(DP.RebootIt, "перезагрузку", "выключение") & _
- " устройства успешно отправлена.", vbInformation
- Else
- LogoffGoFlex DP
- End If
- End Sub
- '====================================================================
- Private Function DetectCommandLine(ByRef DP As DATA_PARAMS) As Boolean
- Dim TMP As Long
- If asIsInIDE Then ajpMain.CommandLine.SetNewCommandLine Command, True
- On Error GoTo hShowUsage
- DP.RebootIt = False
- TMP = 0
- Do
- Select Case UCase(ajpMain.CommandLine(TMP))
- Case "/H"
- DP.HostName = ajpMain.CommandLine(TMP + 1)
- TMP = TMP + 2
- Case "/U"
- DP.UserName = ajpMain.CommandLine(TMP + 1)
- TMP = TMP + 2
- Case "/P"
- DP.Password = ajpMain.CommandLine(TMP + 1)
- TMP = TMP + 2
- Case "/REBOOT"
- DP.RebootIt = True
- TMP = TMP + 1
- Case "/SHUTDOWN"
- DP.RebootIt = False
- TMP = TMP + 1
- Case Else
- Err.Raise 51
- End Select
- Loop While TMP < ajpMain.CommandLine.Count
- DP.HostName = asTrimEx2(DP.HostName, True, True, True, True, True)
- DP.UserName = asTrimEx2(DP.UserName, True, True, True, True, True)
- DP.Password = asTrimEx2(DP.Password, True, True, True, True, True)
- If DP.HostName = "" Then Err.Raise 51
- If DP.UserName = "" Then Err.Raise 51
- If DP.Password = "" Then Err.Raise 51
- DetectCommandLine = True
- Exit Function
- hShowUsage:
- ShowUsage
- DetectCommandLine = False
- End Function
- Private Sub ShowUsage()
- MsgBox "Использование: " & App.EXEName & "[.EXE] /H HostName " & _
- "/U UserName /P Password [/Shutdown | /Reboot]", vbInformation
- End Sub
- '====================================================================
- Private Function LogonGoFlex(ByRef DP As DATA_PARAMS) As Boolean
- Dim TXT As BStr
- Dim HTR As New ajpHTTPRequest
- TXT = GetLogonRequestXML(DP)
- HTR.ProxySettings = New ProxySettings
- hRetry:
- HTR.Execute DP.HostName, , "POST", "/api/2.0/rest/sessions", , , _
- "Content-type: text/xml; charset=utf-8", _
- asUNICODEToUTF8(TXT), , _
- HTR.BuildUserAgent(App), , , True
- ' GoFlex иногда присылает 200 Bad Request, а не 400 Bad Request...
- ' Это случается если выше использовать POST и в URL дописать
- ' &method=PUT
- If HTR.IsError Or (HTR.ResponseCode = 200 And LCase(HTR.ResponseText) = "bad request") Then
- If ajpErr.ErrorDisplayHTTPRequest(HTR, vbCritical + vbRetryCancel, , _
- "Не удалось выполнить аутентификацию " & _
- "на устройстве." & _
- GetGoFlexErrorMessage(HTR.ReceivedData)) = vbRetry Then
- GoTo hRetry
- Else
- Exit Function
- End If
- End If
- ' Извлекаем куки...
- DP.Cookie = Left(HTR.CookiesOut, InStr(HTR.CookiesOut, ";") - 1)
- If DP.Cookie = "" Then
- If MsgBox("Не удалось извлечь куки.", vbCritical + vbRetryCancel) = vbRetry Then
- GoTo hRetry
- Else
- Exit Function
- End If
- End If
- ' Debug...
- 'MsgBox HTR.ReceivedData
- 'LogonGoFlex = False
- 'Exit Function
- ' Уходим отсюда!
- LogonGoFlex = True
- End Function
- Private Function GetLogonRequestXML(ByRef DP As DATA_PARAMS) As BStr
- Dim DOM As New xhXMLDocument
- DOM.CreateXML
- With DOM.DocumentNode.AppendChildNode(xhElement, "session")
- .AppendChildNode(xhAttribute, "user").Text = DP.UserName
- .AppendChildNode(xhAttribute, "password").Text = DP.Password
- End With
- GetLogonRequestXML = DOM.DocumentNode.XML
- End Function
- Private Function GetGoFlexErrorMessage(ByVal ReceivedData As BStr) As BStr
- Dim DOM As New xhXMLDocument
- Dim ErrorCode As Long
- Dim ErrorText As BStr
- Dim TXT As BStr
- On Error GoTo hError
- If Not DOM.LoadXMLText(ReceivedData) Then Exit Function
- ErrorCode = CLng(DOM.GetNodeText(DOM.DocumentNode.SelectSingleNode("/errors/error/@code")))
- ErrorText = DOM.GetNodeText(DOM.DocumentNode.SelectSingleNode("/errors/error/@msg"))
- TXT = CRLFCRLF & _
- "GoFlex error: " & asGetErrorNumber(ErrorCode) & CRLF & _
- "Description: " & ErrorText
- GetGoFlexErrorMessage = TXT
- Exit Function
- hError:
- GetGoFlexErrorMessage = ""
- End Function
- '====================================================================
- Private Function SendShutdownCommand(ByRef DP As DATA_PARAMS) As Boolean
- Dim TXT As BStr
- Dim HTR As New ajpHTTPRequest
- TXT = GetShutdownRequestXML(DP)
- HTR.ProxySettings = New ProxySettings
- HTR.CookiesIn = DP.Cookie
- ' Debug...
- 'SendShutdownCommand = True
- 'Exit Function
- hRetry:
- HTR.Execute DP.HostName, , "PUT", "/api/2.0/rest/server/config/admin", , , _
- "Content-type: text/xml; charset=utf-8", _
- asUNICODEToUTF8(TXT), , _
- HTR.BuildUserAgent(App), , , True
- ' GoFlex иногда присылает 200 Bad Request, а не 400 Bad Request.
- ' Это случается если выше использовать POST и в URL дописать
- ' &method=PUT
- If HTR.IsError Or (HTR.ResponseCode = 200 And LCase(HTR.ResponseText) = "bad request") Then
- If ajpErr.ErrorDisplayHTTPRequest(HTR, vbCritical + vbRetryCancel, , _
- "Не удалось отправить команду на " & _
- IIf(DP.RebootIt, "перезагрузку", "выключение") & _
- " устройства." & _
- GetGoFlexErrorMessage(HTR.ReceivedData)) = vbRetry Then
- GoTo hRetry
- Else
- Exit Function
- End If
- End If
- ' Уходим отсюда!
- SendShutdownCommand = True
- End Function
- Private Function GetShutdownRequestXML(ByRef DP As DATA_PARAMS) As BStr
- Dim DOM As New xhXMLDocument
- DOM.CreateXML
- With DOM.DocumentNode.AppendChildNode(xhElement, "command")
- If DP.RebootIt Then
- .AppendChildNode(xhAttribute, "name").Text = "restart"
- Else
- .AppendChildNode(xhAttribute, "name").Text = "shutdown"
- End If
- End With
- GetShutdownRequestXML = DOM.DocumentNode.XML
- End Function
- '====================================================================
- Private Function LogoffGoFlex(ByRef DP As DATA_PARAMS) As Boolean
- ' Заглушка на всякий случай...
- LogoffGoFlex = True
- End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement