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(object clientObj)
        {
            Connect   connect2Client = clientObj as Connect;
            TcpClient socket2Client  = connect2Client.client;

            Pipe client2Server = new Pipe(
                socket2Client,
                null,
                connect2Client.userFrom
                );
            Pipe server2Client = new Pipe(
                null,
                socket2Client,
                connect2Client.userFrom
                );

            client2Server.EncryptFrom = true;
            server2Client.EncryptTo   = true;
            try
            {
                byte[] request = client2Server.Read();
                if (request == null)
                {
                    return;
                }
                // not socket 5 request
                int version = request[0];
                if (version != '\u0005')
                {
                    return;
                }

                string reply = "\u0005\u0000";
                server2Client.Write(reply);

                request = client2Server.Read();
                if (request == null)
                {
                    return;
                }

                CMDType cmdType = (CMDType)request[1];

                if (cmdType == CMDType.Connect)
                {
                    HandleTCPReq(request, server2Client, client2Server);
                }
                else if (cmdType == CMDType.Udp)
                {
                    //HandleUDPReq(request, server2Client);
                }
            }
            catch (SocketException se)
            {
                Console.WriteLine(se.Message);
                client2Server.Close();
                server2Client.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 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 Close()
 {
     pipeL2R.Close();
     pipeR2L.Close();
     IsOpening = false;
 }
Exemple #6
0
 public void Close()
 {
     pipeL2R.Close();
     pipeR2L.Close();
 }