Exemple #1
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.");
                }
            }
        }
Exemple #2
0
        private async Task ProxyServer_AfterResponse(object sender, SessionEventArgs e)
        {
            bool   isSave         = false;
            bool   isSaveByFilter = false;
            string savePath       = string.Empty;
            await Dispatcher.InvokeAsync(() =>
            {
                SessionListItem item;
                if (sessionDictionary.TryGetValue(e.WebSession, out item))
                {
                    item.Exception = e.Exception;
                }
                isSave         = SaveTrafficDataToFile;
                isSaveByFilter = SaveByFilter;
                savePath       = SaveTrafficDataPath;
            });

            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_RES_{2}.log",
                                                      savePath, DateTime.Now, hostName);
                string fileNameBody = string.Format("{0}\\{1:yyyy'-'MM'-'dd'-'HH'-'mm'-'ss'-'fff}_B_RES_{2}.log",
                                                    savePath, DateTime.Now, hostName);

                if (e.UserData is Models.MatchResult)
                {
                    Models.MatchResult res = e.UserData as Models.MatchResult;
                    if (_filterMatchInclusive)
                    {
                        WriteToLog(
                            fileNameHeader,
                            e.WebSession.Response.HeaderText,
                            string.Format("Session Terminated. By wildcard {0}. Url {1}", res.MatchedWildCard, res.ProcessingString));
                    }
                    else
                    {
                        WriteToLog(
                            fileNameHeader,
                            e.WebSession.Response.HeaderText,
                            string.Format("Session Terminated. By wildcard exclusive. Url {0}", res.ProcessingString));
                    }
                }
                else
                {
                    WriteToLog(fileNameHeader, e.WebSession.Response.HeaderText, e.UserData.ToString());
                }


                if (e.WebSession.Response.IsBodyRead && e.WebSession.Response.HasBody)
                {
                    WriteToLog(fileNameBody, e.WebSession.Response.Body, e.WebSession.Response.Body.Length);
                }
                else if (!e.WebSession.Response.HasBody)
                {
                    WriteToLog(fileNameBody, "Titanium.Web.Proxy: Body not exist.");
                }
                else if (!e.WebSession.Response.IsBodyRead)
                {
                    WriteToLog(fileNameBody, "Titanium.Web.Proxy: Body not read yet.");
                }
            }
        }
Exemple #3
0
        private async Task ProxyServer_BeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
        {
            bool   isSave           = false;
            bool   isSaveByFilter   = false;
            string savePath         = string.Empty;
            string hostname         = e.WebSession.Request.RequestUri.Host;
            bool   terminateSession = false;
            bool   filterTraffic    = false;

            var matchres = _nodecryptSSLMatchInclusive ?
                           _nodecryptSSLMatchFinder.HasMatches(e.WebSession.Request.RequestUri.AbsoluteUri) :
                           _nodecryptSSLMatchFinderExclusive.HasMatches(e.WebSession.Request.RequestUri.AbsoluteUri);

            if (_nodecryptSSLMatchInclusive)
            {
                //No decrypt if matched one of the wildcard in settings
                if (matchres.IsMatch)
                {
                    e.DecryptSsl = false;
                }
            }
            else
            {
                //No decrypt if no matched one of the wildcard in settings
                if (!matchres.IsMatch)
                {
                    e.DecryptSsl = false;
                }
            }


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

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

            if (!filterTraffic)
            {
                e.DecryptSsl = false;
            }


            Models.MatchResult mresult = _filterMatchInclusive ? _filterMatchFinder.HasMatches(e.WebSession.Request.Url) : _filterMatchFinderExclusive.HasMatches(e.WebSession.Request.Url);
            if (_filterMatchInclusive)
            {
                if (mresult.IsMatch && filterTraffic)
                {
                    e.TerminateSession();
                    e.DenyConnect    = true;
                    e.DecryptSsl     = false;//need to set for quick drop connection
                    terminateSession = true;
                }
            }
            else
            {
                if (!mresult.IsMatch && filterTraffic)
                {
                    e.TerminateSession();
                    e.DenyConnect    = true;
                    e.DecryptSsl     = false;//need to set for quick drop connection
                    terminateSession = true;
                }
            }
            //Is the same?
            //if ((mresult.IsMatch && _filterMatchInclusive || !mresult.IsMatch && !_filterMatchInclusive) && 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_TREQ_{2}.log",
                                                      savePath, DateTime.Now, hostName);
                string fileNameBody = string.Format("{0}\\{1:yyyy'-'MM'-'dd'-'HH'-'mm'-'ss'-'fff}_B_TREQ_{2}.log",
                                                    savePath, DateTime.Now, hostName);

                if (terminateSession)
                {
                    if (_filterMatchInclusive)
                    {
                        WriteToLog(fileNameHeader, e.WebSession.Request.HeaderText, string.Format("Will be 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.");
                }
            }
        }