Exemple #1
0
        private static void StartDefault()
        {
            if (CertificateUtil.FindRootCertificate() == default)
            {
                CertificateUtil.InstallNewRootCertificate();
            }
            //CertificateUtil.UninstallRootCertificates();
            //CertificateUtil.UninstallAllServerCertificatesByIssuer();
            //var ca = CertificateUtil.FindRootCertificate();
            //CertificateUtil.UninstallFromRootStore(ca);

            var engine = DefaultEngine.Create(new ListeningConfig(8080));

            engine.DecryptConfig.IsDecrypt = true;
            //engine.DecryptConfig.HostFilterHandler = host
            //    => host.EndsWith("cat-ears.net");
            // engine.DecryptConfig.RootCertificateResolver = store => store.FindRootCertificate("Titanium Root Certificate Authority");
            engine.DecryptConfig.CacheLocations
                = new[] {   // 定義順が読み取り優先順
                CertificateCacheLocation.Memory,
                CertificateCacheLocation.Custom,
                CertificateCacheLocation.Store,
                };
            engine.DecryptConfig.ServerCertificateCacheResolver = host =>
            {
                Console.WriteLine($"### ServerCertificateCacheResolver: {host}");
                return(null);
                //var path = host.Replace("*", "$x$") + ".pfx";
                //if (!File.Exists(path)) return null;
                //var bytes = File.ReadAllBytes(path);
                //return new X509Certificate2(bytes);
            };
            engine.ServerCertificateCreated += (_, c) =>
            {
                Console.WriteLine($"### ServerCertificateCreated: {c.Certificate.Subject}");
                //var path = c.Certificate.Subject.Replace("CN=", "").Replace("*", "$x$") + ".pfx";
                //if (!File.Exists(path))
                //    File.WriteAllBytes(path, c.Certificate.RawData);
            };

            engine.UpstreamProxyConfig = null;
            engine.ConnectionAdded    += (_, c) => Console.WriteLine($"### Connection Added. Connections Count: {c.Count}");
            engine.ConnectionRemoved  += (_, c) => Console.WriteLine($"### Connection Removed. Connections Count: {c.Count}");


            var proxy = HttpProxyFactory.Create(engine);

            proxy.FatalException   += (_, e) => Console.WriteLine(e.Exception);
            proxy.HttpResponseSent += (_, s) =>
            {
                if (IsOutputHeaders(s.Session))
                {
                    Console.WriteLine($"{s.Session.GetHost()}: {s.Session.Request}\r\n" +
                                      $"{s.Session.Response.StatusLine }{s.Session.Response.Headers}");
                }
                else if (s.Session.Request.RequestLine.Method.Method != "CONNECT")
                {
                    Console.WriteLine($"{s.Session.GetHost()}: {s.Session.Request.RequestLine}" +
                                      $"{s.Session.Response.StatusLine}");
                }
            };
            proxy.ClientWebSocketMessageSent += (_, m) =>
            {
                var host = m.Message.HandshakeSession.GetHost();
                if (m.Message.Opcode == WebSocketOpcode.Text)
                {
                    Console.WriteLine(
                        $@"ClientWebSocketMessage: {host}
Text: {Encoding.UTF8.GetString(m.Message.PayloadData.ToArray())}");
                }
                else
                {
                    //                    Console.WriteLine(
                    //$@"ClientWebSocketMessage: {host}
                    //{m.Message.Opcode}: {m.Message.PayloadData.Count} bytes.");
                }
            };
            proxy.ServerWebSocketMessageSent += (_, m) =>
            {
                var host = m.Message.HandshakeSession.GetHost();
                if (m.Message.Opcode == WebSocketOpcode.Text)
                {
                    Console.WriteLine(
                        $@"ServerWebSocketMessage: {host}
Text: {Encoding.UTF8.GetString(m.Message.PayloadData.ToArray())}");
                }
                else
                {
                    //                    Console.WriteLine(
                    //$@"ServerWebSocketMessage: {host}
                    //{m.Message.Opcode}: {m.Message.PayloadData.Count} bytes.");
                }
            };

            try
            {
                proxy.Start();
                while (true)
                {
                    Task.Delay(1000).Wait();
                }
            }
            finally
            {
                proxy.Stop();
            }
        }