Exemplo n.º 1
0
        protected void SendTestRequestToMockProxy(HttpRequestInfo testRequest,
                                                  HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse, int proxyPort = 0)
        {
            TrafficViewerFile mockSite  = new TrafficViewerFile();
            TrafficViewerFile dataStore = new TrafficViewerFile();

            MockProxy mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0);

            mockProxy.Start();

            //change the requests host and port to be the ones of the mock proxy
            testRequest.Host = mockProxy.Host;
            testRequest.Port = mockProxy.Port;

            mockSite.AddRequestResponse(testRequest.ToArray(false), testResponse.ToArray());

            IHttpClient client = GetHttpClient();

            receivedResponse = client.SendRequest(testRequest);
            // check what was received in the proxy
            byte[] receivedRequestBytes = dataStore.LoadRequestData(0);
            if (receivedRequestBytes == null)
            {
                receivedRequest = null;
            }
            else
            {
                receivedRequest = new HttpRequestInfo(receivedRequestBytes);
            }
            mockProxy.Stop();
        }
Exemplo n.º 2
0
        protected void SendTestRequestThroughMockProxy(HttpRequestInfo testRequest,
                                                       HttpResponseInfo testResponse, out HttpRequestInfo receivedRequest, out HttpResponseInfo receivedResponse,
                                                       ClientType clientType = ClientType.WebRequestClient, int proxyPort = 0)
        {
            TrafficViewerFile mockSite = new TrafficViewerFile();

            mockSite.AddRequestResponse(testRequest.ToArray(true), testResponse.ToArray());
            TrafficViewerFile dataStore = new TrafficViewerFile();
            MockProxy         mockProxy = new MockProxy(dataStore, mockSite, "127.0.0.1", proxyPort, 0);

            mockProxy.Start();

            IHttpClient client = GetHttpClient(mockProxy.Port);

            receivedResponse = client.SendRequest(testRequest);
            // check what was received in the proxy

            byte [] receivedRequestBytes = dataStore.LoadRequestData(0);
            if (receivedRequestBytes == null)
            {
                receivedRequest = null;
            }
            else
            {
                receivedRequest = new HttpRequestInfo(receivedRequestBytes);
            }
            mockProxy.Stop();
        }
        protected override void ActionFunction(TVRequestInfo curReqInfo)
        {
            int           index = -1;
            TVRequestInfo tvInfo;
            var           curReqData = _dataSource.LoadRequestData(curReqInfo.Id);

            if (curReqData == null)
            {
                ErrorBox.ShowDialog("No request data for selected request");
            }

            HttpRequestInfo curHttpReqInfo = new HttpRequestInfo(curReqData);


            while ((tvInfo = _dataSource.GetNext(ref index)) != null)
            {
                if (tvInfo.Id != curReqInfo.Id)
                {
                    //replicate the headers
                    byte[]          reqData = _dataSource.LoadRequestData(tvInfo.Id);
                    HttpRequestInfo reqInfo = new HttpRequestInfo(reqData);
                    reqInfo.Headers = new HTTPHeaders();
                    reqInfo.Cookies.Clear();
                    foreach (var header in curHttpReqInfo.Headers)
                    {
                        reqInfo.Headers.Add(header.Name, header.Values.ToArray());
                    }
                    _dataSource.SaveRequest(tvInfo.Id, reqInfo.ToArray(false));
                }
            }
        }
Exemplo n.º 4
0
        private void UpdateRequest(object sender, EventArgs e)
        {
            try
            {
                _curReqInfo.PathVariables.Clear();
                _curReqInfo.QueryVariables.Clear();
                _curReqInfo.BodyVariables.Clear();
                _curReqInfo.Cookies.Clear();
                _curReqInfo.Headers = new HTTPHeaders();

                var entities = _gridParameters.GetValues();
                foreach (var entity in entities)
                {
                    string[] values = entity.Split(Constants.VALUES_SEPARATOR.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    if (values.Length == 3)
                    {
                        if (values[0].Equals(RequestLocation.Path.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            _curReqInfo.PathVariables[values[1]] = Utils.UrlEncode(values[2]);
                        }
                        else if (values[0].Equals(RequestLocation.Query.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            _curReqInfo.QueryVariables[values[1]] = Utils.UrlEncode(values[2]);
                        }
                        else if (values[0].Equals(RequestLocation.Body.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            if (_curIsBodyEncoded)
                            {
                                values[2] = Utils.UrlEncode(values[2]);
                            }
                            _curReqInfo.BodyVariables[values[1]] = values[2];
                        }
                        else if (values[0].Equals(RequestLocation.Cookies.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            _curReqInfo.SetCookie(values[1], Utils.UrlEncode(values[2]));
                        }
                        else if (values[0].Equals(RequestLocation.Headers.ToString(), StringComparison.OrdinalIgnoreCase))
                        {
                            _curReqInfo.Headers[values[1]] = values[2];
                        }
                    }
                }

                _curAccessor.SaveRequest(_curReqId, _curReqInfo.ToArray());
            }
            catch (Exception ex)
            {
                ErrorBox.ShowDialog(ex.Message);
            }
        }
Exemplo n.º 5
0
        private void AddRequest(ITrafficDataAccessor currentFile, Uri uri, string fullQuery, string format)
        {
            string          request     = String.Format(format, uri.AbsolutePath, fullQuery, uri.Host, uri.Port);
            HttpRequestInfo requestInfo = new HttpRequestInfo(request);
            TVRequestInfo   tvReqInfo   = new TVRequestInfo();

            tvReqInfo.Description = Resources.UriParserDescription;
            tvReqInfo.RequestLine = HttpRequestInfo.GetRequestLine(request);
            tvReqInfo.ThreadId    = "N/A";
            tvReqInfo.RequestTime = DateTime.Now;
            tvReqInfo.IsHttps     = String.Compare(uri.Scheme, "https", true) == 0;
            currentFile.AddRequestInfo(tvReqInfo);
            currentFile.SaveRequest(tvReqInfo.Id, requestInfo.ToArray(false));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a request to the data accessor
        /// </summary>
        /// <param name="request"></param>
        private void AddHttpRequest(HttpRequestInfo request)
        {
            var reqInfo = new TVRequestInfo();

            reqInfo.Host    = request.Host;
            reqInfo.IsHttps = request.IsSecure;

            byte [] rawRequest = request.ToArray();

            reqInfo.RequestLine = HttpRequestInfo.GetRequestLine(rawRequest);
            reqInfo.Description = "Script Explore";

            var id = _curDataAccessor.AddRequestInfo(reqInfo);

            _curDataAccessor.SaveRequest(id, rawRequest);
        }
Exemplo n.º 7
0
 protected override HttpRequestInfo OnBeforeRequestToSite(HttpRequestInfo requestInfo)
 {
     requestInfo = base.OnBeforeRequestToSite(requestInfo);
     if (!_isNonEssential)
     {
         bool mutated;
         requestInfo = _parentProxy.HandleRequest(requestInfo, out mutated);
         if (mutated)
         {
             CurrDataStoreRequestInfo.Description = "Custom Test";
         }
         TrafficDataStore.SaveRequest(CurrDataStoreRequestInfo.Id, requestInfo.ToArray(false));
         TrafficDataStore.UpdateRequestInfo(CurrDataStoreRequestInfo);
     }
     return(requestInfo);
 }
Exemplo n.º 8
0
        private void AddRequest(ITrafficDataAccessor currentFile, Entry entry)
        {
            Uri uri = entry.Request.Url;
            //check exclusions
            Request         harRequest  = entry.Request;
            string          request     = String.Format("{0} {1} {2}\r\n\r\n", harRequest.Method, uri.PathAndQuery, harRequest.HttpVersion);
            HttpRequestInfo requestInfo = new HttpRequestInfo(request);

            //add the headers
            foreach (var header in harRequest.Headers)
            {
                if (!header.Name.ToLower().Equals("accept-encoding") &&
                    !header.Name.ToLower().Equals("if-modified-since") &&
                    !header.Name.ToLower().Equals("if-none-match"))
                {
                    requestInfo.Headers.Add(header.Name, header.Value);
                }
            }
            if (harRequest.PostData != null)
            {
                requestInfo.ContentData = Constants.DefaultEncoding.GetBytes(harRequest.PostData.Text);
            }
            TVRequestInfo tvReqInfo = new TVRequestInfo();

            tvReqInfo.Description = Resources.HarParserDescription;
            tvReqInfo.RequestLine = HttpRequestInfo.GetRequestLine(request);
            tvReqInfo.ThreadId    = "N/A";
            tvReqInfo.RequestTime = DateTime.Now;
            tvReqInfo.IsHttps     = String.Compare(uri.Scheme, "https", true) == 0;
            tvReqInfo.Host        = uri.Host;

            Response         harResponse  = entry.Response;
            string           responseHead = String.Format("{0} {1}\r\n\r\n", harResponse.HttpVersion, harResponse.Status);
            HttpResponseInfo respInfo     = new HttpResponseInfo(responseHead);

            foreach (var header in harResponse.Headers)
            {
                respInfo.Headers.Add(header.Name, header.Value);
            }
            if (harResponse.Content != null && !String.IsNullOrWhiteSpace(harResponse.Content.Text))
            {
                respInfo.ResponseBody.AddChunkReference(Constants.DefaultEncoding.GetBytes(harResponse.Content.Text));
            }

            currentFile.AddRequestInfo(tvReqInfo);
            currentFile.SaveRequestResponse(tvReqInfo.Id, requestInfo.ToArray(false), respInfo.ToArray());
        }
Exemplo n.º 9
0
        private void SendRequestAsync(object sender, DoWorkEventArgs e)
        {
            object[] args = e.Argument as object[];
            string   text = (string)args[0];

            text = text.Replace(Constants.SEQUENCE_VAR_PATTERN, DateTime.Now.Ticks.ToString());


            bool            https   = (bool)args[1];
            HttpRequestInfo reqInfo = new HttpRequestInfo(text, this._urlEncodeBeforeSending.Checked);

            if (this._urlEncodeBeforeSending.Checked)
            {
                if (reqInfo.BodyVariables != null && reqInfo.BodyVariables.MatchingDefinition.IsRegular && reqInfo.BodyVariables.Count > 0)
                {
                    List <string> paramNames = new List <string>(reqInfo.BodyVariables.Keys);
                    using (List <string> .Enumerator enumerator = paramNames.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            string name  = enumerator.Current;
                            string value = Utils.UrlDecode(reqInfo.BodyVariables[name]);
                            reqInfo.BodyVariables[name] = Utils.UrlEncode(value);
                        }
                        goto IL_D9;
                    }
                }
                reqInfo.ContentData = HttpUtility.UrlEncodeToBytes(reqInfo.ContentData);
            }
IL_D9:
            reqInfo.IsSecure = https;
            if (reqInfo.ContentData != null)
            {
                reqInfo.Headers["Content-Length"] = reqInfo.ContentData.Length.ToString();
            }
            HttpResponseInfo respInfo;
            int reqId;

            try
            {
                respInfo = TrafficViewer.Instance.MakeHttpClient().SendRequest(reqInfo);
                reqId    = TrafficViewer.Instance.TrafficViewerFile.AddRequestResponse(reqInfo.ToArray(), respInfo.ToArray(), https);
            }
            catch
            {
                respInfo = null;
                reqId    = TrafficViewer.Instance.TrafficViewerFile.AddRequestResponse(reqInfo.ToArray(), new byte[0], https);
            }
            e.Result = respInfo;
        }
Exemplo n.º 10
0
        private void TrackRequestContext(HttpRequestInfo requestInfo)
        {
            foreach (TrackingPattern pattern in _autoTrackingPatternList.Values)
            {
                string rawRequest = requestInfo.ToString();

                string needle = Utils.RegexFirstGroupValue(rawRequest, pattern.RequestPattern);

                if (String.IsNullOrWhiteSpace(needle))
                {
                    continue;
                }


                //first search for the path of the current request in responses
                LineMatches results = SearchParameterValue(needle);

                if (results.Count == 0)
                {
                    needle  = Utils.UrlDecode(needle);
                    results = SearchParameterValue(needle);
                }

                //if any of the two searches returned results
                if (results.Count != 0)
                {
                    //get the last match to extract the request context
                    var match = results[results.Count - 1];
                    CurrDataStoreRequestInfo.RefererId = match.RequestId;
                    //replace the path in the match
                    string requestContext = match.Line.Replace(needle, REQ_CONTEXT_ID);

                    if (requestContext.Length > MAX_REQUEST_CONTEXT_SIZE)
                    {
                        requestContext = TrimRequestContext(requestContext);
                    }

                    //also replace hexadecimal values
                    requestContext = Regex.Replace(requestContext, HEX_REGEX, HEX_VAL);

                    //escape the line
                    requestContext = Regex.Escape(requestContext);
                    //insert the group
                    requestContext = requestContext.Replace(REQ_CONTEXT_ID, RX_GROUP);
                    //insert the HEX regex
                    requestContext = requestContext.Replace(HEX_VAL, HEX_REGEX);

                    CurrDataStoreRequestInfo.RequestContext  = requestContext;
                    CurrDataStoreRequestInfo.TrackingPattern = pattern.Name;

                    TrafficDataStore.UpdateRequestInfo(CurrDataStoreRequestInfo);

                    string originalPath = requestInfo.Path;
                    CurrDataStoreRequestInfo.UpdatedPath = originalPath;

                    //change the path of the request
                    HttpRequestInfo newReq = new HttpRequestInfo(requestInfo.ToArray(false), false);

                    //we are only replacing the last portion of the path and the query string to prevent relative path issues and also cookie path issues
                    int lastIndexOfSlash = originalPath.LastIndexOf('/');
                    if (lastIndexOfSlash >= 0)
                    {
                        originalPath = originalPath.Substring(0, lastIndexOfSlash + 1);
                    }


                    newReq.Path = String.Format("{0}{1}{2}", originalPath, REQ_ID_STRING, CurrDataStoreRequestInfo.Id);

                    TrafficDataStore.SaveRequest(CurrDataStoreRequestInfo.Id, newReq.ToArray(false));

                    HttpServerConsole.Instance.WriteLine
                        ("Found request context for request '{0}' id: {1}, referer id:{2}",
                        requestInfo.Path, CurrDataStoreRequestInfo.Id, CurrDataStoreRequestInfo.RefererId);
                    HttpServerConsole.Instance.WriteLine
                        (requestContext);

                    return;                     //we can only have one tracking pattern per request
                }
            }
        }