예제 #1
0
        public Client(TcpClient client, EndPoint remote, IPPool ipPool)
        {
            lock (_lock)
            {
                id = nextId++;
            }

            if (remote == null || !(remote is IPEndPoint))
            {
                int port;
                if (Settings.PatchingOptions.Value.HasFlag(Settings.PatchingFlags.UseHttps))
                {
                    port = 443;
                }
                else if (remote != null && remote is DnsEndPoint)
                {
                    port = ((DnsEndPoint)remote).Port;
                }
                else
                {
                    port = 80;
                }
                doIPPool    = true;
                this.ipPool = ipPool;
                remote      = new IPEndPoint(ipPool.GetIP(), port);
            }

            remoteEP = (IPEndPoint)remote;

            clientIn = client;
            clientIn.ReceiveTimeout = CONNECTION_KEEPALIVE_TIMEOUT_SECONDS * 1000;
            clientIn.SendTimeout    = CONNECTION_TIMEOUT_SECONDS * 1000;
            clientIn.SendBufferSize = BUFFER_LENGTH;
            clientIn.NoDelay        = true;
        }
예제 #2
0
        public Client(TcpClient client, IPEndPoint remote, IPPool ipPool)
        {
            lock (_lock)
            {
                id = nextId++;
            }

            if (remote == null)
            {
                doIPPool    = true;
                this.ipPool = ipPool;
                remote      = new IPEndPoint(ipPool.GetIP(), Settings.PatchingUseHttps.Value ? 443 : 80);
            }

            remoteEP = remote;

            clientIn = client;
            clientIn.ReceiveTimeout = CONNECTION_KEEPALIVE_TIMEOUT_SECONDS * 1000;
            clientIn.SendTimeout    = CONNECTION_TIMEOUT_SECONDS * 1000;
            clientIn.SendBufferSize = BUFFER_LENGTH;
            clientIn.NoDelay        = true;
        }
예제 #3
0
        private void OnBeginAcceptTcpClient(IAsyncResult r)
        {
            var       l      = (TcpListener)r.AsyncState;
            TcpClient accept = null;

            try
            {
                accept = l.EndAcceptTcpClient(r);

                lock (this)
                {
                    if ((remoteEP == null || remoteEP is DnsEndPoint) && (ipPool == null || !ipPool.IsAlive()))
                    {
                        IPAddress[] ips;
                        try
                        {
                            string hostname;
                            if (remoteEP is DnsEndPoint)
                            {
                                hostname = ((DnsEndPoint)remoteEP).Host;
                            }
                            else
                            {
                                hostname = ProxyServer.PATCH_SERVER;
                            }
                            ips = Dns.GetHostAddresses(hostname);
                            if (ips.Length == 0)
                            {
                                throw new IndexOutOfRangeException();
                            }
                        }
                        catch (Exception ex)
                        {
                            Util.Logging.Log(ex);

                            throw new Exception("DNS lookup failed for " + PATCH_SERVER);
                        }

                        ipPool = new IPPool(ips);
                    }
                }

                var client = new Client(accept, remoteEP, ipPool);

                lock (this)
                {
                    //clients++;
                    clients.Add(client);

                    if (cancelToken != null)
                    {
                        using (cancelToken)
                        {
                            cancelToken.Cancel();
                            cancelToken = null;
                        }
                    }
                }

                if (ClientConnected != null)
                {
                    ClientConnected(this, client);
                }

                client.Closed += client_Closed;

                if (ResponseHeaderReceived != null)
                {
                    client.ResponseHeaderReceived += client_ResponseHeaderReceived;
                }
                if (RequestHeaderReceived != null)
                {
                    client.RequestHeaderReceived += client_RequestHeaderReceived;
                }
                if (ResponseDataReceived != null)
                {
                    client.ResponseDataReceived += client_ResponseDataReceived;
                }
                if (RequestDataReceived != null)
                {
                    client.RequestDataReceived += client_RequestDataReceived;
                }
                if (ClientError != null)
                {
                    client.Error += client_Error;
                }

                client.Start();
            }
            catch (Exception e)
            {
                Util.Logging.Log(e);

                if (accept != null)
                {
                    accept.Close();
                    using (accept.Client) { }
                }

                var server = l.Server;
                if (server == null || !server.IsBound)
                {
                    using (server) { }
                    return;
                }
            }

            try
            {
                l.BeginAcceptTcpClient(OnBeginAcceptTcpClient, l);
            }
            catch { }
        }
예제 #4
0
        private async void DoListener()
        {
            while (isActive)
            {
                TcpClient accept = null;

                try
                {
                    accept = await listener.AcceptTcpClientAsync();

                    if (remoteEP == null && ipPool == null)
                    {
                        IPAddress[] ips;
                        try
                        {
                            ips = Dns.GetHostAddresses(ProxyServer.PATCH_SERVER);
                            if (ips.Length == 0)
                            {
                                throw new IndexOutOfRangeException();
                            }
                        }
                        catch (Exception ex)
                        {
                            Util.Logging.Log(ex);

                            throw new Exception("DNS lookup failed for " + PATCH_SERVER);
                        }

                        ipPool = new IPPool(ips);
                    }

                    var client = new Client(accept, remoteEP, ipPool);

                    lock (this)
                    {
                        //clients++;
                        clients.Add(client);

                        if (cancelToken != null)
                        {
                            using (cancelToken)
                            {
                                cancelToken.Cancel();
                                cancelToken = null;
                            }
                        }
                    }

                    if (ClientConnected != null)
                    {
                        ClientConnected(this, client);
                    }

                    client.Closed += client_Closed;

                    if (ResponseHeaderReceived != null)
                    {
                        client.ResponseHeaderReceived += client_ResponseHeaderReceived;
                    }
                    if (RequestHeaderReceived != null)
                    {
                        client.RequestHeaderReceived += client_RequestHeaderReceived;
                    }
                    if (ResponseDataReceived != null)
                    {
                        client.ResponseDataReceived += client_ResponseDataReceived;
                    }
                    if (RequestDataReceived != null)
                    {
                        client.RequestDataReceived += client_RequestDataReceived;
                    }
                    if (ClientError != null)
                    {
                        client.Error += client_Error;
                    }

                    client.Start();
                }
                catch (Exception e)
                {
                    Util.Logging.Log(e);

                    if (accept != null)
                    {
                        accept.Close();
                    }

                    var server = listener.Server;
                    if (server == null || !server.IsBound)
                    {
                        if (server != null)
                        {
                            server.Dispose();
                        }
                        return;
                    }
                }
            }
        }