Пример #1
0
        /// <summary>
        /// Listen for incoming connections.
        /// </summary>
        public void Listen()
        {
            ApplyFilterSettings ();

            TcpListener listener = new TcpListener (new IPEndPoint (address, Port));

            bool retrying = false;

            while (true) {
                try {
                    listener.Start ();
                    Console.WriteLine ("HTTP Proxy listening on " + address + ", port " + Port);

                    while (true) {

                        //Cached connection cleaning
                        connectionManager.Cleanup ();

                        if (listener.Server.Poll (5000000, SelectMode.SelectRead) == false) {
                            GC.Collect ();
                            Thread.Sleep (50);
                            continue;
                        }

                        TcpClient c = listener.AcceptTcpClient ();

                        //Limit proxy connections
                        lock (proxyList) {
                            if (proxyList.Count > 1000) {
                                c.Close ();
                                continue;
                            }
                        }

                        ProxySession ps = new ProxySession (c.Client, this, connectionManager);
                        lock (proxyList) {
                            proxyList.Add (ps);
                        }
                        ps.Start ();
                    }
                } catch (SocketException e) {
                    if (e.ErrorCode == 10048) {
                        if (retrying == false) {
                            Console.Error.WriteLine ("Error: " + e.Message);
                            Console.Error.WriteLine ("Retrying...");
                        }
                        retrying = true;
                    } else

                        Console.Error.WriteLine ("Error: " + e.Message);

                    Thread.Sleep (500);
                } finally {
                    listener.Stop ();

                    foreach (ProxySession ps in SessionArray ()) {
                        ps.Stop ();
                    }
                }
            }
        }
Пример #2
0
 public void Remove(ProxySession session)
 {
     lock (proxyList) {
         proxyList.Remove (session);
     }
 }