Advertisement
Linda-chan

AJPapps - Shutdown GoFlex 1.00

Aug 9th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Type DATA_PARAMS
  4.   HostName As BStr
  5.   UserName As BStr
  6.   Password As BStr
  7.   RebootIt As Boolean
  8.   Cookie As BStr
  9. End Type
  10.  
  11. '====================================================================
  12. Public Sub Main()
  13.   asRegisterAppPath
  14.   InitCommonControls
  15.   If Not asIsInIDE Then asSetWindowResIconAll asGetThunderMainHandle(), IDI_APPICON
  16.  
  17.   Dim DP As DATA_PARAMS
  18.  
  19.   If Not DetectCommandLine(DP) Then Exit Sub
  20.   If Not LogonGoFlex(DP) Then Exit Sub
  21.  
  22.   If SendShutdownCommand(DP) Then
  23.     MsgBox "Команда на " & _
  24.            IIf(DP.RebootIt, "перезагрузку", "выключение") & _
  25.            " устройства успешно отправлена.", vbInformation
  26.   Else
  27.     LogoffGoFlex DP
  28.   End If
  29. End Sub
  30.  
  31. '====================================================================
  32. Private Function DetectCommandLine(ByRef DP As DATA_PARAMS) As Boolean
  33.   Dim TMP As Long
  34.  
  35.   If asIsInIDE Then ajpMain.CommandLine.SetNewCommandLine Command, True
  36.  
  37.   On Error GoTo hShowUsage
  38.  
  39.   DP.RebootIt = False
  40.   TMP = 0
  41.  
  42.   Do
  43.     Select Case UCase(ajpMain.CommandLine(TMP))
  44.       Case "/H"
  45.         DP.HostName = ajpMain.CommandLine(TMP + 1)
  46.         TMP = TMP + 2
  47.       Case "/U"
  48.         DP.UserName = ajpMain.CommandLine(TMP + 1)
  49.         TMP = TMP + 2
  50.       Case "/P"
  51.         DP.Password = ajpMain.CommandLine(TMP + 1)
  52.         TMP = TMP + 2
  53.       Case "/REBOOT"
  54.         DP.RebootIt = True
  55.         TMP = TMP + 1
  56.       Case "/SHUTDOWN"
  57.         DP.RebootIt = False
  58.         TMP = TMP + 1
  59.       Case Else
  60.         Err.Raise 51
  61.     End Select
  62.   Loop While TMP < ajpMain.CommandLine.Count
  63.  
  64.   DP.HostName = asTrimEx2(DP.HostName, True, True, True, True, True)
  65.   DP.UserName = asTrimEx2(DP.UserName, True, True, True, True, True)
  66.   DP.Password = asTrimEx2(DP.Password, True, True, True, True, True)
  67.  
  68.   If DP.HostName = "" Then Err.Raise 51
  69.   If DP.UserName = "" Then Err.Raise 51
  70.   If DP.Password = "" Then Err.Raise 51
  71.  
  72.   DetectCommandLine = True
  73.   Exit Function
  74.  
  75. hShowUsage:
  76.   ShowUsage
  77.   DetectCommandLine = False
  78. End Function
  79.  
  80. Private Sub ShowUsage()
  81.   MsgBox "Использование: " & App.EXEName & "[.EXE] /H HostName " & _
  82.          "/U UserName /P Password [/Shutdown | /Reboot]", vbInformation
  83. End Sub
  84.  
  85. '====================================================================
  86. Private Function LogonGoFlex(ByRef DP As DATA_PARAMS) As Boolean
  87.   Dim TXT As BStr
  88.   Dim HTR As New ajpHTTPRequest
  89.  
  90.   TXT = GetLogonRequestXML(DP)
  91.   HTR.ProxySettings = New ProxySettings
  92.  
  93. hRetry:
  94.   HTR.Execute DP.HostName, , "POST", "/api/2.0/rest/sessions", , , _
  95.               "Content-type: text/xml; charset=utf-8", _
  96.               asUNICODEToUTF8(TXT), , _
  97.               HTR.BuildUserAgent(App), , , True
  98.  
  99.   ' GoFlex иногда присылает 200 Bad Request, а не 400 Bad Request...
  100.  ' Это случается если выше использовать POST и в URL дописать
  101.  ' &method=PUT
  102.  If HTR.IsError Or (HTR.ResponseCode = 200 And LCase(HTR.ResponseText) = "bad request") Then
  103.     If ajpErr.ErrorDisplayHTTPRequest(HTR, vbCritical + vbRetryCancel, , _
  104.                                       "Не удалось выполнить аутентификацию " & _
  105.                                       "на устройстве." & _
  106.                                       GetGoFlexErrorMessage(HTR.ReceivedData)) = vbRetry Then
  107.       GoTo hRetry
  108.     Else
  109.       Exit Function
  110.     End If
  111.   End If
  112.  
  113.   ' Извлекаем куки...
  114.  DP.Cookie = Left(HTR.CookiesOut, InStr(HTR.CookiesOut, ";") - 1)
  115.   If DP.Cookie = "" Then
  116.     If MsgBox("Не удалось извлечь куки.", vbCritical + vbRetryCancel) = vbRetry Then
  117.       GoTo hRetry
  118.     Else
  119.       Exit Function
  120.     End If
  121.   End If
  122.  
  123.   ' Debug...
  124.  'MsgBox HTR.ReceivedData
  125.  'LogonGoFlex = False
  126.  'Exit Function
  127.  
  128.   ' Уходим отсюда!
  129.  LogonGoFlex = True
  130. End Function
  131.  
  132. Private Function GetLogonRequestXML(ByRef DP As DATA_PARAMS) As BStr
  133.   Dim DOM As New xhXMLDocument
  134.  
  135.   DOM.CreateXML
  136.  
  137.   With DOM.DocumentNode.AppendChildNode(xhElement, "session")
  138.     .AppendChildNode(xhAttribute, "user").Text = DP.UserName
  139.     .AppendChildNode(xhAttribute, "password").Text = DP.Password
  140.   End With
  141.  
  142.   GetLogonRequestXML = DOM.DocumentNode.XML
  143. End Function
  144.  
  145. Private Function GetGoFlexErrorMessage(ByVal ReceivedData As BStr) As BStr
  146.   Dim DOM As New xhXMLDocument
  147.   Dim ErrorCode As Long
  148.   Dim ErrorText As BStr
  149.   Dim TXT As BStr
  150.  
  151.   On Error GoTo hError
  152.  
  153.   If Not DOM.LoadXMLText(ReceivedData) Then Exit Function
  154.  
  155.   ErrorCode = CLng(DOM.GetNodeText(DOM.DocumentNode.SelectSingleNode("/errors/error/@code")))
  156.   ErrorText = DOM.GetNodeText(DOM.DocumentNode.SelectSingleNode("/errors/error/@msg"))
  157.  
  158.   TXT = CRLFCRLF & _
  159.         "GoFlex error: " & asGetErrorNumber(ErrorCode) & CRLF & _
  160.         "Description:  " & ErrorText
  161.  
  162.   GetGoFlexErrorMessage = TXT
  163.   Exit Function
  164.  
  165. hError:
  166.   GetGoFlexErrorMessage = ""
  167. End Function
  168.  
  169. '====================================================================
  170. Private Function SendShutdownCommand(ByRef DP As DATA_PARAMS) As Boolean
  171.   Dim TXT As BStr
  172.   Dim HTR As New ajpHTTPRequest
  173.  
  174.   TXT = GetShutdownRequestXML(DP)
  175.   HTR.ProxySettings = New ProxySettings
  176.   HTR.CookiesIn = DP.Cookie
  177.  
  178.   ' Debug...
  179.  'SendShutdownCommand = True
  180.  'Exit Function
  181.  
  182. hRetry:
  183.   HTR.Execute DP.HostName, , "PUT", "/api/2.0/rest/server/config/admin", , , _
  184.               "Content-type: text/xml; charset=utf-8", _
  185.               asUNICODEToUTF8(TXT), , _
  186.               HTR.BuildUserAgent(App), , , True
  187.  
  188.   ' GoFlex иногда присылает 200 Bad Request, а не 400 Bad Request.
  189.  ' Это случается если выше использовать POST и в URL дописать
  190.  ' &method=PUT
  191.  If HTR.IsError Or (HTR.ResponseCode = 200 And LCase(HTR.ResponseText) = "bad request") Then
  192.     If ajpErr.ErrorDisplayHTTPRequest(HTR, vbCritical + vbRetryCancel, , _
  193.                                       "Не удалось отправить команду на " & _
  194.                                       IIf(DP.RebootIt, "перезагрузку", "выключение") & _
  195.                                       " устройства." & _
  196.                                       GetGoFlexErrorMessage(HTR.ReceivedData)) = vbRetry Then
  197.       GoTo hRetry
  198.     Else
  199.       Exit Function
  200.     End If
  201.   End If
  202.  
  203.   ' Уходим отсюда!
  204.  SendShutdownCommand = True
  205. End Function
  206.  
  207. Private Function GetShutdownRequestXML(ByRef DP As DATA_PARAMS) As BStr
  208.   Dim DOM As New xhXMLDocument
  209.  
  210.   DOM.CreateXML
  211.  
  212.   With DOM.DocumentNode.AppendChildNode(xhElement, "command")
  213.     If DP.RebootIt Then
  214.       .AppendChildNode(xhAttribute, "name").Text = "restart"
  215.     Else
  216.       .AppendChildNode(xhAttribute, "name").Text = "shutdown"
  217.     End If
  218.   End With
  219.  
  220.   GetShutdownRequestXML = DOM.DocumentNode.XML
  221. End Function
  222.  
  223. '====================================================================
  224. Private Function LogoffGoFlex(ByRef DP As DATA_PARAMS) As Boolean
  225.   ' Заглушка на всякий случай...
  226.  LogoffGoFlex = True
  227. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement