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(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(); } } }