public MainWindow() { #if DEBUG SaveTrafficDataPath = Path.Combine(Environment.CurrentDirectory, "Temp"); #else if (string.IsNullOrEmpty(Properties.Settings.Default.SaveDataPath)) { SaveTrafficDataPath = Path.Combine("h:\\Temp", "TitaniumWebProxy"); } else { SaveTrafficDataPath = Properties.Settings.Default.SaveDataPath; } #endif _filterMatchInclusive = Properties.Settings.Default.filterMatchInclusive; _nodecryptSSLMatchInclusive = Properties.Settings.Default.nodecryptSSLMatchInclusive; _saveMatchInclusive = Properties.Settings.Default.saveMatchInclusive; FilterSettingsFile = "filter.config"; NodecryptSSLSettingsFile = "NodecryptSSL.config"; SaveFilterSettingsFile = "save_filter.config"; FilterExclusiveSettingsFile = "filterExclusive.config"; NodecryptSSLExclusiveSettingsFile = "NodecryptSSLExclusive.config"; SaveFilterExclusiveSettingsFile = "save_filterExclusive.config"; _filterMatchFinder = new Models.FilterMatchFinder(FilterSettingsFile); _nodecryptSSLMatchFinder = new Models.FilterMatchFinder(NodecryptSSLSettingsFile); _saveFilterMatchFinder = new Models.FilterMatchFinder(SaveFilterSettingsFile); _filterMatchFinderExclusive = new Models.FilterMatchFinder(FilterExclusiveSettingsFile); _nodecryptSSLMatchFinderExclusive = new Models.FilterMatchFinder(NodecryptSSLExclusiveSettingsFile); _saveFilterMatchFinderExclusive = new Models.FilterMatchFinder(SaveFilterExclusiveSettingsFile); #region TEST string testUrl = "http://www.youtube.com:443"; var res = _nodecryptSSLMatchFinder.HasMatches(testUrl); testUrl = "http://www.youtube.com:443/"; res = _nodecryptSSLMatchFinder.HasMatches(testUrl); testUrl = "https://www.youtube.com:443/"; res = _nodecryptSSLMatchFinder.HasMatches(testUrl); testUrl = "www.youtube.com:443"; res = _nodecryptSSLMatchFinder.HasMatches(testUrl); testUrl = "https://www.youtube.com/youtubei/v1/log_event?alt=json&key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8"; res = _filterMatchFinder.HasMatches(testUrl); #endregion TEST _nodecryptSSLMatchFinder = new Models.FilterMatchFinder(NodecryptSSLSettingsFile); _filterMatchFinder = new Models.FilterMatchFinder(FilterSettingsFile); proxyServer = new ProxyServer(); //proxyServer.CertificateManager.CertificateEngine = CertificateEngine.DefaultWindows; ////Set a password for the .pfx file //proxyServer.CertificateManager.PfxPassword = "******"; ////Set Name(path) of the Root certificate file //proxyServer.CertificateManager.PfxFilePath = @"C:\NameFolder\rootCert.pfx"; ////do you want Replace an existing Root certificate file(.pfx) if password is incorrect(RootCertificate=null)? yes====>true //proxyServer.CertificateManager.OverwritePfxFile = true; ////save all fake certificates in folder "crts"(will be created in proxy dll directory) ////if create new Root certificate file(.pfx) ====> delete folder "crts" //proxyServer.CertificateManager.SaveFakeCertificates = true; proxyServer.ForwardToUpstreamGateway = true; ////if you need Load or Create Certificate now. ////// "true" if you need Enable===> Trust the RootCertificate used by this proxy server //proxyServer.CertificateManager.EnsureRootCertificate(true); ////or load directly certificate(As Administrator if need this) ////and At the same time chose path and password ////if password is incorrect and (overwriteRootCert=true)(RootCertificate=null) ====> replace an existing .pfx file ////note : load now (if existed) //proxyServer.CertificateManager.LoadRootCertificate(@"C:\NameFolder\rootCert.pfx", "PfxPassword"); var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8000, true); proxyServer.AddEndPoint(explicitEndPoint); //proxyServer.UpStreamHttpProxy = new ExternalProxy //{ // HostName = "158.69.115.45", // Port = 3128, // UserName = "******", // Password = "******", //}; proxyServer.BeforeRequest += ProxyServer_BeforeRequest; proxyServer.BeforeResponse += ProxyServer_BeforeResponse; proxyServer.AfterResponse += ProxyServer_AfterResponse; explicitEndPoint.BeforeTunnelConnectRequest += ProxyServer_BeforeTunnelConnectRequest; explicitEndPoint.BeforeTunnelConnectResponse += ProxyServer_BeforeTunnelConnectResponse; proxyServer.ClientConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ClientConnectionCount = proxyServer.ClientConnectionCount; }); }; proxyServer.ServerConnectionCountChanged += delegate { Dispatcher.Invoke(() => { ServerConnectionCount = proxyServer.ServerConnectionCount; }); }; proxyServer.Start(); proxyServer.SetAsSystemProxy(explicitEndPoint, ProxyProtocolType.AllHttp); InitializeComponent(); }
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."); } } }