private void HandleTCPReq(byte[] request, Pipe server2Client, Pipe client2Server)
        {
            string ip   = GetIP(request);
            int    port = GetPort(request);
            string reply;

            if (ip == null)
            {
                string str = Encoding.UTF8.GetString(request);
                return;
            }
            TcpClient tcpClient2Server;

            try
            {
                tcpClient2Server = new TcpClient(ip, port);
                reply            = "\u0005\u0000\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000";
                server2Client.Write(reply);
                client2Server.ClientTo   = tcpClient2Server;
                server2Client.ClientFrom = tcpClient2Server;
                client2Server.Flow();
                server2Client.Flow();
            }
            catch
            {
                CloseRequest(server2Client);
            }
        }
Esempio n. 2
0
        private void HandleClient(object clientObj)
        {
            TcpClient client2Client = clientObj as TcpClient;
            TcpClient client2Server = Connect2Server(ServerHost, ServerPort);

            if (client2Server == null)
            {
                return;
            }

            Pipe pipe0 = new Pipe(
                client2Client,
                client2Server
                );

            pipe0.EncryptTo = true;
            Pipe pipe1 = new Pipe(
                client2Server,
                client2Client
                );

            pipe0.EncryptTo   = true;
            pipe1.EncryptFrom = true;

            pipe0.Flow();
            pipe1.Flow();
        }
        protected override void HandleClient(Socket socket2Client)
        {
            if (socket2Client != null)
            {
                if (Authenticate(socket2Client, out string user))
                {
                    Pipe pipeClient2Server = new Pipe(socket2Client,
                                                      null, user);
                    pipeClient2Server.EncryptFrom = true;
                    Pipe pipeServer2Client = new Pipe(null,
                                                      socket2Client,
                                                      user);
                    pipeServer2Client.EncryptTo = true;

                    IPEndPoint reqIPEP = ReceiveReqEndPoint(socket2Client);
                    if (reqIPEP != null)
                    {
                        Socket socket2Server = CreateSocketConnect(reqIPEP);
                        if (socket2Server != null)
                        {
                            pipeClient2Server.SocketTo   = socket2Server;
                            pipeServer2Client.SocketFrom = socket2Server;

                            if (WorkFlow(pipeClient2Server, pipeServer2Client))
                            {
                                pipeClient2Server.Flow();
                                pipeServer2Client.Flow();
                            }
                            else
                            {
                                pipeClient2Server.Close();
                                pipeServer2Client.Close();
                            }
                        }
                        else
                        {
                            pipeClient2Server.Close();
                            pipeServer2Client.Close();
                        }
                    }
                    else
                    {
                        pipeClient2Server.Close();
                        pipeServer2Client.Close();
                    }
                }
                else
                {
                    socket2Client.Close();
                }
            }
        }
        protected override void HandleClient(Socket socket2Client)
        {
            DateTime now;

            if (Conf.IsDebug)
            {
                now = DateTime.Now;
            }

            if (socket2Client != null)
            {
                Socket socket2Server = CreateSocketConnect(
                    GetRemoteIPEndPoint());

                if (Authenticate(socket2Server))
                {
                    Pipe pipeClient2Server = new Pipe(socket2Client,
                                                      socket2Server);
                    pipeClient2Server.EncryptTo = true;
                    Pipe pipeServer2Client = new Pipe(socket2Server,
                                                      socket2Client);
                    pipeServer2Client.EncryptFrom = true;

                    if (WorkFlow(pipeClient2Server, pipeServer2Client))
                    {
                        pipeClient2Server.Flow();
                        pipeServer2Client.Flow();
                    }
                    else
                    {
                        pipeClient2Server.Close();
                        pipeServer2Client.Close();
                    }
                }
                else
                {
                    socket2Client.Close();
                }
            }

            if (Conf.IsDebug)
            {
                double sec = (DateTime.Now - now).TotalSeconds;
                if (sec > Conf.DebugTimeThreshold)
                {
                    Console.WriteLine("Time for ClientRelay.Handle_Client is {0} s", sec);
                }
            }
        }
        protected override void HandleClient(object clientObj)
        {
            TcpClient client2Client = clientObj as TcpClient;
            TcpClient client2Server = new TcpClient();

            try
            {
                int tmpIndex = indexOfRemoteAddresses++;
                if (tmpIndex >= remoteAddresses.Length)
                {
                    tmpIndex %= remoteAddresses.Length;
                }
                client2Server.Connect(remoteAddresses[tmpIndex]);
                if (!Authenticate(client2Server))
                {
                    client2Server.Close();
                    client2Client.Close();
                    return;
                }
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
            }


            Pipe pipe0 = new Pipe(
                client2Client,
                client2Server
                );

            pipe0.EncryptTo = true;
            Pipe pipe1 = new Pipe(
                client2Server,
                client2Client
                );

            pipe0.EncryptTo   = true;
            pipe1.EncryptFrom = true;

            pipe0.Flow();
            pipe1.Flow();
        }
        protected override void HandleClient(object clientObj)
        {
            TcpClient socket2Client = clientObj as TcpClient;

            Pipe pipe0;
            Pipe pipe1 = null;

            pipe0 = new Pipe(
                socket2Client,
                null
                );
            pipe0.EncryptFrom = true;
            try
            {
                byte[] buffer = pipe0.Read();
                if (buffer == null)
                {
                    return;
                }
                string request = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                string host    = GetHost(request);
                int    port    = GetPort(request);
                if (host == "" || port == 0)
                {
                    return;
                }
                IPHostEntry iphe = Dns.GetHostEntry(host);
                IPAddress   ipa  = null;
                foreach (IPAddress tmp in iphe.AddressList)
                {
                    if (tmp.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ipa = tmp;
                    }
                }
                if (ipa == null)
                {
                    return;
                }
                string ip = ipa.ToString();
                Console.WriteLine("connect to " + host + ":" + port);

                TcpClient client2Server = new TcpClient(ip, port);

                pipe1 = new Pipe(
                    client2Server,
                    socket2Client
                    );
                pipe1.EncryptTo = true;

                pipe0.ClientTo = client2Server;

                if (port == 443)
                {
                    string re443 = "HTTP/1.1 OK\r\n\r\n";
                    buffer = Encoding.UTF8.GetBytes(re443);
                    pipe1.Write(buffer);
                }
                else
                {
                    pipe0.Write(buffer);
                }

                pipe0.Flow();
                pipe1.Flow();
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
                pipe0.Close();
                if (pipe1 != null)
                {
                    pipe1.Close();
                }
            }
        }
 public void Flow()
 {
     pipeL2R.Flow();
     pipeR2L.Flow();
     IsOpening = false;
 }
Esempio n. 8
0
 public void Flow()
 {
     pipeL2R.Flow();
     pipeR2L.Flow();
 }