private async Task ProxyServer_BeforeRequest(object sender, SessionEventArgs e)
        {
            SessionListItem item = null;
            await Dispatcher.InvokeAsync(() =>
            {
                item = AddSession(e);
            });

            if (e.WebSession.Request.HasBody)
            {
                item.RequestBody = await e.GetRequestBody();
            }
        }
        private SessionListItem CreateSessionListItem(SessionEventArgs e)
        {
            lastSessionNumber++;
            var item = new SessionListItem
            {
                Number      = lastSessionNumber,
                SessionArgs = e,
                // save the headers because TWP will set it to null in Dispose
                RequestHeaders  = e.WebSession.Request.RequestHeaders,
                ResponseHeaders = e.WebSession.Response.ResponseHeaders,
                Request         = e.WebSession.Request,
                Response        = e.WebSession.Response,
            };

            if (e is TunnelConnectSessionEventArgs || e.WebSession.Request.UpgradeToWebSocket)
            {
                e.DataReceived += (sender, args) =>
                {
                    var             session = (SessionEventArgs)sender;
                    SessionListItem li;
                    if (sessionDictionary.TryGetValue(session, out li))
                    {
                        li.ReceivedDataCount += args.Count;
                    }
                };

                e.DataSent += (sender, args) =>
                {
                    var             session = (SessionEventArgs)sender;
                    SessionListItem li;
                    if (sessionDictionary.TryGetValue(session, out li))
                    {
                        li.SentDataCount += args.Count;
                    }
                };
            }

            item.Update();
            return(item);
        }
Esempio n. 3
0
        private async Task ProxyServer_BeforeResponse(object sender, SessionEventArgs e)
        {
            SessionListItem item = null;
            await Dispatcher.InvokeAsync(() =>
            {
                SessionListItem item2;
                if (sessionDictionary.TryGetValue(e.WebSession, out item2))
                {
                    item2.Update();
                    item = item2;
                }
            });

            if (item != null)
            {
                if (e.WebSession.Response.HasBody)
                {
                    e.WebSession.Response.KeepBody = true;
                    await e.GetResponseBody();
                }
            }
        }
Esempio n. 4
0
        private SessionListItem CreateSessionListItem(SessionEventArgsBase e)
        {
            lastSessionNumber++;
            bool isTunnelConnect = e is TunnelConnectSessionEventArgs;
            var  item            = new SessionListItem
            {
                Number          = lastSessionNumber,
                WebSession      = e.WebSession,
                IsTunnelConnect = isTunnelConnect
            };

            if (!isTunnelConnect || e.WebSession.Request.UpgradeToWebSocket)
            {
                e.DataReceived -= ReceivedData;
                e.DataReceived += ReceivedData;

                e.DataSent -= SentData;
                e.DataSent += SentData;
            }

            item.Update();
            return(item);
        }
Esempio n. 5
0
        private SessionListItem CreateSessionListItem(SessionEventArgs e)
        {
            lastSessionNumber++;
            bool isTunnelConnect = e is TunnelConnectSessionEventArgs;
            var  item            = new SessionListItem
            {
                Number          = lastSessionNumber,
                WebSession      = e.WebSession,
                IsTunnelConnect = isTunnelConnect,
            };

            if (isTunnelConnect || e.WebSession.Request.UpgradeToWebSocket)
            {
                e.DataReceived += (sender, args) =>
                {
                    var             session = (SessionEventArgs)sender;
                    SessionListItem li;
                    if (sessionDictionary.TryGetValue(session.WebSession, out li))
                    {
                        li.ReceivedDataCount += args.Count;
                    }
                };

                e.DataSent += (sender, args) =>
                {
                    var             session = (SessionEventArgs)sender;
                    SessionListItem li;
                    if (sessionDictionary.TryGetValue(session.WebSession, out li))
                    {
                        li.SentDataCount += args.Count;
                    }
                };
            }

            item.Update();
            return(item);
        }
Esempio n. 6
0
        private async Task ProxyServer_BeforeRequest(object sender, SessionEventArgs e)
        {
            bool   isSave           = false;
            bool   isSaveByFilter   = false;
            string savePath         = string.Empty;
            bool   terminateSession = false;
            bool   filterTraffic    = false;

            SessionListItem item = null;
            await Dispatcher.InvokeAsync(() => {
                item = AddSession(e);



                isSave         = SaveTrafficDataToFile;
                isSaveByFilter = SaveByFilter;
                savePath       = SaveTrafficDataPath;
                filterTraffic  = FilterTrafficBySettings;
            });

            if (e.WebSession.Request.HasBody)
            {
                e.WebSession.Request.KeepBody = true;
                await e.GetRequestBody();
            }

            Models.MatchResult mresult = _filterMatchInclusive ?
                                         _filterMatchFinder.HasMatches(e.WebSession.Request.Url) :
                                         _filterMatchFinderExclusive.HasMatches(e.WebSession.Request.Url);
            if (_filterMatchInclusive)
            {
                if (mresult.IsMatch && filterTraffic)
                {
                    e.TerminateSession();
                    terminateSession = true;
                }
            }
            else
            {
                if (!mresult.IsMatch && filterTraffic)
                {
                    e.TerminateSession();
                    terminateSession = true;
                }
            }

            var smresult = _saveMatchInclusive ?
                           _saveFilterMatchFinder.HasMatches(e.WebSession.Request.Url) :
                           _saveFilterMatchFinderExclusive.HasMatches(e.WebSession.Request.Url);

            if (isSave)
            {
                if (isSaveByFilter && _saveMatchInclusive && !smresult.IsMatch)
                {
                    return;
                }
                if (isSaveByFilter && !_saveMatchInclusive && smresult.IsMatch)
                {
                    return;
                }


                string hostName       = e.WebSession.Request.Host.Replace(":", "_Port").Replace(".", "_");
                string fileNameHeader = string.Format("{0}\\{1:yyyy'-'MM'-'dd'-'HH'-'mm'-'ss'-'fff}_H_REQ_{2}.log",
                                                      savePath, DateTime.Now, hostName);
                string fileNameBody = string.Format("{0}\\{1:yyyy'-'MM'-'dd'-'HH'-'mm'-'ss'-'fff}_B_REQ_{2}.log",
                                                    savePath, DateTime.Now, hostName);

                if (terminateSession)
                {
                    if (_filterMatchInclusive)
                    {
                        WriteToLog(fileNameHeader, e.WebSession.Request.HeaderText, string.Format("Willbe termineted by {0}", mresult.MatchedWildCard));
                    }
                    else
                    {
                        WriteToLog(fileNameHeader, e.WebSession.Request.HeaderText, string.Format("Will be termineted by exclusive (no match any filter wildcards)"));
                    }
                }
                else
                {
                    WriteToLog(fileNameHeader, e.WebSession.Request.HeaderText);
                }
                e.UserData = terminateSession ? (object)mresult : e.WebSession.Request.HeaderText;

                if (e.WebSession.Request.IsBodyRead && e.WebSession.Request.HasBody)
                {
                    WriteToLog(fileNameBody, e.WebSession.Request.Body, e.WebSession.Request.Body.Length);
                }
                else if (!e.WebSession.Request.HasBody)
                {
                    WriteToLog(fileNameBody, "Titanium.Web.Proxy: Body not exist.");
                }
                else if (!e.WebSession.Request.IsBodyRead)
                {
                    WriteToLog(fileNameBody, "Titanium.Web.Proxy: Body not read yet.");
                }
            }
        }