Exemple #1
0
        public Task Start()
        {
            _listener.Start();

            return(Task.Factory.StartNew(async() =>
            {
                var bs = new byte[short.MaxValue];
                while (true)
                {
                    try
                    {
                        var client = await _listener.AcceptTcpClientAsync();
                        _acceptClients.Add(client.Client.RemoteEndPoint, client);
                        var stream = client.GetStream();

                        Console.WriteLine("Accept client!");

                        var read = await stream.ReadAsync(bs, 0, bs.Length);
                        var str = Encoding.Default.GetString(bs, 0, read);
                        if (IsConnectMethod(str))
                        {
                            var url = ExtractUrl(str);

                            var isConnect = await CanConnect(url);
                            if (isConnect)
                            {
                                Console.WriteLine("Success to connect!");

                                var ret = Encoding.UTF8.GetBytes("HTTP/1.1 200 OK");
                                await stream.WriteAsync(ret, 0, ret.Length);
                                var proxy = ProxyClient.NewClient(client, url);
                                if (_proxies.ContainsKey(client.Client.RemoteEndPoint))
                                {
                                    _proxies.Remove(client.Client.RemoteEndPoint);
                                }
                                _proxies.Add(client.Client.RemoteEndPoint, proxy);
                            }
                            else
                            {
                                Console.WriteLine("Fail to connect!");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                    ThreadUtil.SleepLoop();
                }
            }));
        }
Exemple #2
0
        private async void ReceiveFromClient()
        {
            var failCount = 0;

            var bs = new byte[short.MaxValue];

            while (true)
            {
                try
                {
                    if (!_client.Connected)
                    {
                        Console.WriteLine("Loss client connection");
                        return;
                    }

                    var read = await _toClientStream.ReadAsync(bs, 0, bs.Length);

                    var str = Encoding.Default.GetString(bs, 0, read);

                    var ret = Encoding.Default.GetString(bs, 0, read);
                    if (!string.IsNullOrEmpty(ret))
                    {
                        await WriteAsync(bs, 0, read);
                    }

                    failCount = 0;
                    ThreadUtil.SleepLoop();
                }
                catch (Exception ex)
                {
                    failCount++;
                    Console.WriteLine(ex);

                    if (failCount > 5 && !_client.Connected)
                    {
                        Console.WriteLine("Disconnect client!");
                        return;
                    }
                }
            }
        }
Exemple #3
0
        private async void ReceiveFromOrigin()
        {
            var failCount = 0;

            var bs = new byte[short.MaxValue];

            while (true)
            {
                if (!_toOriginClient.Connected)
                {
                    Console.WriteLine("Loss origin client connection");
                    return;
                }

                try
                {
                    var read = -1;
                    while ((read = await _toOriginRemoteStream.ReadAsync(bs, 0, bs.Length)) > 0)
                    {
                        await _toClientStream.WriteAsync(bs, 0, read);
                    }
                    failCount = 0;
                }
                catch (Exception ex)
                {
                    failCount++;
                    Console.WriteLine(ex);

                    if (failCount > 5 && !_client.Connected)
                    {
                        Console.WriteLine("Disconnect origin server!");
                        return;
                    }
                }

                ThreadUtil.SleepLoop();
            }
        }