Exemplo n.º 1
0
        public static void BandwidthThrottlingProxyEcho()
        {
            using (var echoServer = new EchoServer(new IPEndPoint(IPAddress.Loopback, 0)))
            {
                echoServer.Start();

                var listener = new ThrottlingListener(
                    SinkListener.Instance,
                    10 * 1024 /* 10 Kb/s */
                    );
                var connector = new SimpleConnector(echoServer.EndPoint, listener);

                using (var proxyServer = new ProxyServer(new IPEndPoint(IPAddress.Loopback, 0), connector))
                {
                    proxyServer.Start();

                    var block = Encoding.UTF8.GetBytes(new string('?', 1024));

                    var stopwatch = Stopwatch.StartNew();

                    using (var echoClient = new EchoPingClient(proxyServer.EndPoint, block))
                    {
                        echoClient.Start();

                        for (int i = 0; i < 100; i++)
                        {
                            echoClient.Ping();
                        }
                    }

                    Console.WriteLine(stopwatch.Elapsed);
                }
            }
        }
Exemplo n.º 2
0
        protected void Run(int port = 0)
        {
            var endpoint = GetServerEndpoint();

            IListener listener = Listener;

            if (ParseUtils.ParseSize(options.Bandwidth) is int bandwidth)
            {
                listener = new ThrottlingListener(listener, bandwidth);
            }

            IConnector connector = new SimpleConnector(endpoint, listener);

            if (options.Chaos)
            {
                var chaosConfiguration = new ChaosConfiguration
                {
                    Reject =
                    {
                        Percentage = 0.5
                    },
                    Abort =
                    {
                        Percentage      =                  1,
                        UpstreamBytes   = new Range <long>(0, 1024 * 1024 * 10),
                        DownstreamBytes = new Range <long>(0, 1024 * 1024 * 10)
                    }
                };
                var chaosConnector = new ChaosConnector(chaosConfiguration, connector);

                //chaosConnector.Rejected += (s, e) => Console.WriteLine("REJECTED");
                //chaosConnector.Aborted += (s, e) => Console.WriteLine($"ABORTED reason {e.Reason}, upstream {e.UpstreamTransferred}, downstream {e.DownstreamTransferred}");

                connector = chaosConnector;
            }

            proxy = new ProxyServer(new IPEndPoint(IPAddress.Loopback, port), connector);
            proxy.ExceptionOccured += (s, e) => Console.WriteLine($"EXCEPTION: {e.Exception.Message} ({e.Exception.GetType().FullName})");
            proxy.Start();

            Running(proxy.EndPoint);
        }