private string GetHideId(EwsRequest request, PayloadDefinition definition)
 {
     try
     {
         if (definition.IsHideRequired)
         {
             //Getting the HideID by making request to the URL provided
             string         hideId       = string.Empty;
             var            hideUrl      = new Uri(string.Format(CultureInfo.CurrentCulture, definition.HideUrl, Device.Address));
             HttpWebRequest httpRequest  = BuildRequest(hideUrl, HttpVerb.Post, definition, SessionId, definition.Payload);
             var            hideResponse = HttpMessenger.ExecuteRequest(httpRequest);
             if (!string.IsNullOrEmpty(hideResponse.Body))
             {
                 int start = hideResponse.Body.IndexOf("name=\"Hide\" VALUE=\"", StringComparison.Ordinal) + 19;
                 int end   = hideResponse.Body.IndexOf("\"", start, StringComparison.Ordinal);
                 hideId = (hideResponse.Body.Substring(start, (end - start)));
             }
             //Getting the session ID from the response recieved
             if (!string.IsNullOrEmpty(hideId))
             {
                 request.AddWithoutValidate("HideID", hideId);
             }
             return(hideId);
         }
     }
     catch (ArgumentOutOfRangeException ex)
     {
         throw new DeviceCommunicationException("Exception occurred while fetching the Hide Id ", ex.InnerException);
     }
     throw new DeviceCommunicationException("Could not get the Hide Id, please check the Hide Id URL.");
 }
Example #2
0
        private static PayloadDefinition GeneratePayLoadDefinition(XElement request)
        {
            PayloadDefinition definition = new PayloadDefinition();

            if (request != null)
            {
                definition.TargetUrl = request.Element("URL")?.Value;
                var httpMethod = request.Element("Method")?.Value;
                if (httpMethod.Equals("UPLOAD"))
                {
                    definition.IsUpload         = true;
                    definition.HttpMethod       = HttpVerb.Post;
                    definition.IsRemoveSolution = false;
                }
                else
                {
                    definition.IsUpload   = false;
                    definition.HttpMethod = (HttpVerb)Enum.Parse(typeof(HttpVerb), httpMethod, ignoreCase: true);
                }

                definition.IsSessionIdRequired = !string.IsNullOrEmpty(request.Attribute("sessionID")?.Value);

                if (definition.HttpMethod != HttpVerb.Get)
                {
                    definition.Payload             = SanitizePayLoad(request);
                    definition.IsViewStateRequired = !string.IsNullOrEmpty(request.Attribute("Viewstate")?.Value);
                    definition.IsWizardIdRequired  = !string.IsNullOrEmpty(request.Attribute("WizardID")?.Value);
                    definition.IsHideRequired      = !string.IsNullOrEmpty(request.Attribute("Hide")?.Value);

                    definition.ViewStateUrl = request.Element("ViewstateURL")?.Value;
                    definition.WizardIdUrl  = request.Element("WizardIDURL")?.Value;
                    definition.HideUrl      = request.Element("HideURL")?.Value;

                    definition.IsRemoveSolution = request.Element("IsRemoveSolution") != null && Convert.ToBoolean(request.Element("IsRemoveSolution")?.Value, CultureInfo.InvariantCulture);
                }

                SetPayloadHeaders(request, definition);

                var namevaluepairs =
                    request.Element("NameValuePairs")?
                    .Elements("NameValuePair")
                    .OrderBy(x => x.Attribute("Order")?.Value)
                    .ToList();
                if (namevaluepairs == null)
                {
                    return(definition);
                }
                foreach (var namevaluepair in namevaluepairs)
                {
                    definition.NameValuePairs.Add(namevaluepair.Element("Name")?.Value, namevaluepair.Element("Value")?.Value);
                }
            }
            return(definition);
        }
Example #3
0
        private static void SetPayloadHeaders(XElement request, PayloadDefinition definition)
        {
            var headers = request.Element("Headers")?.Elements("Header").OrderBy(x => x.Attribute("Order")?.Value).ToList();

            if (headers != null)
            {
                foreach (var header in headers)
                {
                    definition.Headers.Add(header.Element("HeaderName")?.Value, header.Element("HeaderValue")?.Value);
                }
            }
        }
Example #4
0
        internal static List <PayloadDefinition> CreatePayloadDefinitions(EwsRequest request, string firmware, string deviceFamily)
        {
            List <PayloadDefinition> payloadDefinitions = new List <PayloadDefinition>();
            XDocument xDoc = GetPayloadXDoc(request.RequestType);
            //Geting the Number of Requests
            int numberOfRequests = GetPayLoadRequestCount(firmware, deviceFamily, request.RequestType, request.RequestSubtype);

            for (int i = 1; i <= numberOfRequests; i++)
            {
                PayloadDefinition payloadDefinition = GetPayLoadFromFile(xDoc, firmware, deviceFamily, request.RequestSubtype, i);
                payloadDefinitions.Add(payloadDefinition);
            }
            return(payloadDefinitions);
        }
        private void FillStateValues(EwsRequest request, string sessionId, PayloadDefinition definition)
        {
            if (definition.IsViewStateRequired)
            {
                Uri viewStateUrl = null;
                //Getting the viewstate by making request to the URL provided
                //Implemented the below functionality to get the tray ID for which the setting needs to be implemented
                if (request.RequestType.Equals("ManageTrays") && request.RequestSubtype.Equals("Tray"))
                {
                    viewStateUrl = new Uri(string.Format(CultureInfo.CurrentCulture, definition.ViewStateUrl, Device.Address, request.PayloadValues["Tray"]));
                }
                else
                {
                    viewStateUrl = new Uri(string.Format(CultureInfo.CurrentCulture, definition.ViewStateUrl, Device.Address));
                }
                HttpWebRequest httpRequest       = BuildRequest(viewStateUrl, HttpVerb.Get, definition, sessionId);
                var            viewStateResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string viewstate = GetViewState(viewStateResponse.Body);
                if (!string.IsNullOrEmpty(viewstate))
                {
                    //Manage trays expects HPViewState and not just ViewState in the payload
                    if (request.RequestType.Equals("ManageTrays") && request.RequestSubtype.Equals("Tray"))
                    {
                        viewstate = System.Web.HttpUtility.HtmlDecode(viewstate);
                        request.AddWithoutValidate("HPViewState", viewstate);
                    }
                    else
                    {
                        request.AddWithoutValidate("ViewState", viewstate);
                    }
                }
            }

            if (definition.IsWizardIdRequired)
            {
                //Getting the viewstate by making request to the URL provided
                var            wizardIdUrl    = new Uri(string.Format(CultureInfo.CurrentCulture, definition.WizardIdUrl, Device.Address));
                HttpWebRequest httpRequest    = BuildRequest(wizardIdUrl, HttpVerb.Get, definition, sessionId);
                var            wizardResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string wizardId = GetWizardId(wizardResponse.Body);
                if (!string.IsNullOrEmpty(wizardId))
                {
                    request.AddWithoutValidate("WizardID", wizardId);
                }
            }
        }
Example #6
0
        protected override HttpWebRequest BuildRequest(Uri url, HttpVerb method, PayloadDefinition payloadDefinition, string sessionId, string payload = null, string hideId = null)
        {
            HttpMessenger messenger = new HttpMessenger()
            {
                ContentType       = "application/x-www-form-urlencoded",
                Accept            = "text/html, application/xhtml+xml, image/jxr, */*",
                AllowAutoRedirect = true,
                Encoding          = Encoding.GetEncoding("iso-8859-1"),
                Expect100Continue = false
            };

            WebHeaderCollection headers = new WebHeaderCollection();

            foreach (var header in payloadDefinition.Headers.Keys)
            {
                headers.Add(header, payloadDefinition.Headers[header]);
            }

            if (!string.IsNullOrEmpty(Password))
            {
                var    plainTextBytes = Encoding.UTF8.GetBytes("admin:" + Password);
                string authorization  = Convert.ToBase64String(plainTextBytes);
                headers.Add(HttpRequestHeader.Authorization, "Basic " + authorization + "");
            }

            var cookieContainer = new CookieContainer();

            if (!string.IsNullOrEmpty(sessionId))
            {
                headers.Add(HttpRequestHeader.Cookie, $"sessionId={sessionId}");
                cookieContainer.Add(new Cookie("sessionId", sessionId)
                {
                    Domain = url.Host
                });
            }
            if (!string.IsNullOrEmpty(hideId))
            {
                headers.Add(HttpRequestHeader.Cookie, $"ipsecwizardid={hideId}");
                cookieContainer.Add(new Cookie("ipsecwizardid", hideId)
                {
                    Domain = url.Host
                });
            }

            HttpWebRequest request = messenger.BuildRequest(url, method, headers, payload);

            request.CookieContainer = new CookieContainer();
            request.CookieContainer = cookieContainer;
            return(request);
        }
Example #7
0
 protected override string GetSessionId(PayloadDefinition definition)
 {
     try
     {
         //[Kelly]:the signin Uri might need to be modified to have the port 4242 appended if you are targetting the simulator
         var    signInUri = new Uri(string.Format(CultureInfo.CurrentCulture, OmniDeviceSignInUrl, Device.Address));
         string sessionId = string.Empty;
         CsrfToken = GetCsrfToken(signInUri.ToString(), ref sessionId);
         return(sessionId);
     }
     catch (WebException)
     {
         return(string.Empty);
     }
 }
Example #8
0
        protected override string SignIn(string sessionId)
        {
            PayloadDefinition definition = EwsPayloadFactory.GetPayLoad(Device.GetDeviceInfo().FirmwareRevision, "Jedi", "SignIn", string.Empty, 1);

            EwsRequest signInRequest = new EwsRequest("SignIn", "SignIn", null);

            signInRequest.Add("Password", Password.Replace("admin:", string.Empty));
            string signInPayload  = definition.PreparePayload(signInRequest);
            string targetUrl      = string.Format(CultureInfo.CurrentCulture, JediDeviceSignInUrl, Device.Address);
            var    signInResponse = ExecuteRequest(new Uri(targetUrl), signInPayload, definition, null);

            sessionId = signInResponse.Headers.GetValues("Cookie")?.FirstOrDefault()?.Split(';').FirstOrDefault();
            sessionId = sessionId?.Replace("sessionId=", string.Empty);

            return(sessionId);
        }
Example #9
0
        protected override string GetSessionId(PayloadDefinition definition)
        {
            try
            {
                var signInUri = new Uri(string.Format(CultureInfo.CurrentCulture, SiriusDeviceSignInUrl, Device.Address));
                //Getting the session ID by making request to /hp/device/DeviceStatus/Index
                HttpWebRequest httpRequest     = BuildRequest(signInUri, HttpVerb.Get, definition, null);
                var            sessionResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string sessionId = (sessionResponse.Headers.GetValues("Set-Cookie")?.FirstOrDefault()?.Split(';').FirstOrDefault());
                sessionId = sessionId?.Replace("sid=", string.Empty);
                return(sessionId);
            }
            catch (WebException)
            {
                return(string.Empty);
            }
        }
Example #10
0
        protected string GetSolutionId(string sessionId, string solutionName)
        {
            try
            {
                PayloadDefinition payload = new PayloadDefinition {
                    Headers = new Dictionary <string, string>()
                };
                //Getting the session ID by making request to /hp/device/SolutionInstaller
                HttpWebRequest httpRequest     = BuildRequest(new Uri(string.Format(CultureInfo.CurrentCulture, OmniDeviceHpacSolutionUrl, Device.Address)), HttpVerb.Get, payload, sessionId);
                var            sessionResponse = HttpMessenger.ExecuteRequest(httpRequest);
                //Getting the session ID from the response recieved

                HtmlDocument doc = new HtmlDocument();
                doc.LoadHtml(sessionResponse.Body);
                CsrfToken = doc.GetElementbyId("CSRFToken").Attributes["value"].Value;

                //var nodes = doc.g
                HtmlNode table = doc.DocumentNode.SelectSingleNode("//table[@id='SolutionsTable']");
                if (table == null)
                {
                    throw new DeviceInvalidOperationException("Solution not installed");
                }
                foreach (var cell in table.SelectNodes(".//tr/td")) // **notice the .**
                {
                    if (cell.InnerText.StartsWith(solutionName, StringComparison.OrdinalIgnoreCase))
                    {
                        if (cell.Id.EndsWith("_Name", StringComparison.OrdinalIgnoreCase))
                        {
                            return(cell.Id.Substring(0, cell.Id.Length - 5));
                        }
                    }
                }
                return(string.Empty);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                throw new DeviceCommunicationException("Exception occurred while fetching the Solution Id ", ex.InnerException);
            }
        }
        protected HttpResponse ExecuteRequest(Uri url, string payload, PayloadDefinition payloadDefinition, string hideId)
        {
            string solutionName = string.Empty;

            if (payloadDefinition.IsRemoveSolution)
            {
                solutionName = payload;
                payload      = string.Empty;
            }
            var httpRequest = BuildRequest(url, payloadDefinition.HttpMethod, payloadDefinition, SessionId, payload, hideId);

            if (payloadDefinition.IsUpload)
            {
                FillUploadData(httpRequest, payload, "certificate", payloadDefinition.NameValuePairs);
            }
            else if (payloadDefinition.IsRemoveSolution)
            {
                FillRemoveSolutionData(httpRequest, solutionName, SessionId);
            }

            return(HttpMessenger.ExecuteRequest(httpRequest));
        }
Example #12
0
        protected override string GetSessionId(PayloadDefinition definition)
        {
            try
            {
                //Getting the session ID by making request to PhoenixDeviceSignInURL
                var            signInUri       = new Uri(string.Format(CultureInfo.CurrentCulture, PhoenixDeviceSignInUrl, Device.Address));
                HttpWebRequest httpRequest     = BuildRequest(signInUri, HttpVerb.Get, definition, null);
                var            sessionResponse = HttpMessenger.ExecuteRequest(httpRequest);

                //Getting the session ID from the response recieved
                string sessionId = string.Empty;
                if (sessionResponse.Headers.GetValues("Set-Cookie") != null)
                {
                    sessionId = (sessionResponse.Headers.GetValues("Set-Cookie")?.FirstOrDefault()?.Split(';').FirstOrDefault());
                }
                return(sessionId);
            }
            catch (WebException)
            {
                return(string.Empty);
            }
        }
 protected abstract HttpWebRequest BuildRequest(Uri url, HttpVerb method, PayloadDefinition payloadDefinition, string sessionId, string payload = null, string hideId = null);
 protected abstract string GetSessionId(PayloadDefinition definition);