/// <summary> /// Creates certificate authority certificate if required. /// </summary> /// <param name="proxyServerConfiguration"></param> /// <param name="certificateGenerator"></param> /// <param name="certificateCache"></param> /// <returns></returns> private static CertificateManager GetCertificateManager(ProxyServerConfiguration proxyServerConfiguration, CertificateGenerator certificateGenerator, CertificateCache certificateCache) { var caCertificateFileName = Path.Combine(proxyServerConfiguration.CertificatePath, proxyServerConfiguration.CaCertificateFileName); if (!File.Exists(caCertificateFileName)) { CreateCaCertificate(certificateGenerator, proxyServerConfiguration); } AsymmetricCipherKeyPair caKeyPair = null; try { var caKeyPairText = File.ReadAllText(Path.Combine(proxyServerConfiguration.CertificatePath, proxyServerConfiguration.CaKeyPairFileName)); caKeyPair = certificateGenerator.ImportKeyPair(caKeyPairText); } catch (Exception ex) { throw new ConfigurationErrorsException(string.Format("Could not read the ca private key from file from {0}", proxyServerConfiguration.CaKeyPairFileName), ex); } X509Certificate caCertificate = null; try { var caCertificateText = File.ReadAllText(Path.Combine(proxyServerConfiguration.CertificatePath, proxyServerConfiguration.CaCertificateFileName)); caCertificate = certificateGenerator.ImportCertificate(caCertificateText); } catch (Exception ex) { throw new ConfigurationErrorsException(string.Format("Could not read the ca certificate from file from {0}", proxyServerConfiguration.CaCertificateFileName), ex); } var certificateManager = new CertificateManager(certificateCache, caKeyPair, caCertificate); return certificateManager; }
static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); var proxyServerConfiguration = new ProxyServerConfiguration() { DumpHeaders = false, DumpPostData = false, DumpResponseData = false }; if (args.Length > 0) { if (args.Length <= 3) { var argRegHelp = new Regex(@"^(/|-)\?$"); var argRexH = new Regex("^(/|-)h$"); var argRexP = new Regex("^(/|-)p$"); var argRexR = new Regex("^(/|-)r$"); foreach (var s in args) { if (argRexH.IsMatch(s.ToLower())) { proxyServerConfiguration.DumpHeaders = true; } else if (argRexP.IsMatch(s.ToLower())) { proxyServerConfiguration.DumpPostData = true; } else if (argRexR.IsMatch(s.ToLower())) { proxyServerConfiguration.DumpResponseData = true; } else { PrintUsageInfo(); return; } } } else if (args.Length > 4) { PrintUsageInfo(); return; } } var proxyCache = new ProxyCache(); var profileCache = new ProfileCache(); var profileManager = new ProfileManager(profileCache); var certificateGenerator = new CertificateGenerator(); var certificateCache = new CertificateCache(); var certificateManager = GetCertificateManager(proxyServerConfiguration, certificateGenerator, certificateCache); var proxyServer = new ProxyServer(proxyServerConfiguration, profileManager, proxyCache, certificateGenerator, certificateManager); if (proxyServer.Start()) { Console.WriteLine("Server started on {0}:{1}...Press enter key to end", proxyServerConfiguration.ListeningIpInterface, proxyServerConfiguration.ListeningPort); Console.ReadLine(); Console.WriteLine("Shutting down"); proxyServer.Stop(); Console.WriteLine("Server stopped..."); } Console.WriteLine("Press enter to exit"); Console.ReadLine(); }