void proxyServer_ClientReceiveCommand(FromClientCommand Command) { if (Command.Command == ClientActions.Receive) { if (CheckRequestAndSend(Command)) { HttpRequest request = null; try { request = HttpParse.Parse(clients[Command.Client]); } catch { } if (request != null) { /*// Текущий клиент остается висеть, до тех пор пока сам не отключится или его не отключат * Manager.SockClient.Client client = new Manager.SockClient.Client(); * client.ServerReceiveCommand += new OnServerReceiveCommand(client_ServerReceiveCommand); * client.SetConnectBuffer(Encoding.ASCII.GetBytes(clients[Command.Client]), 0, clients[Command.Client].Length); * client.UserToken = Command.Client; * if (request.Host.Split(new char[] { ':' }).Length == 2) * client.Connect(new System.Net.DnsEndPoint(request.Host.Split(new char[] { ':' })[0], int.Parse(request.Host.Split(new char[] { ':' })[1]))); * else * client.Connect(new System.Net.DnsEndPoint(request.Host, 80)); * */ Socket websocket = null; try { //if (sMessage.Substring(0, 7) != "CONNECT" && iHostNameStart != -1 && iHostNameEnd != -1 && iHostNameEndFirstSlash != -1) { //This is a socket to the real webserver with the webpage websocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (request.Host.Split(new char[] { ':' }).Length == 2) { websocket.Connect(new System.Net.DnsEndPoint(request.Host.Split(new char[] { ':' })[0], int.Parse(request.Host.Split(new char[] { ':' })[1]))); } else { websocket.Connect(new System.Net.DnsEndPoint(request.Host, 80)); } websocket.Send(Encoding.ASCII.GetBytes(clients[Command.Client]), clients[Command.Client].Length, 0); //Receive data from the webserver int ireceivedbyte = 0; bool timeout = false; for (int i = 0; i < 200; i++) { Thread.Sleep(10); if (websocket.Available != 0) { break; } if (i == 200) { timeout = true; } } if (timeout) { websocket.Shutdown(SocketShutdown.Both); websocket.Close(); //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } }); //if (clientSocket != null && clientSocket.Connected) // clientSocket.Close(); } while (websocket.Available != 0) { byte[] receivebuffer = new byte[websocket.Available]; ireceivedbyte = websocket.Receive(receivebuffer, receivebuffer.Length, 0); //Console.WriteLine("Received from webserver socket {0} bytes", +ireceivedbyte); //clientSocket.Send(receivebuffer, ireceivedbyte, 0); proxyServer.Send(new ToServerCommand() { Action = ServerCommands.Send, ReceiveBuffer = receivebuffer, ToClient = new List <OnServerClientConnection> { (OnServerClientConnection)Command.Client } }); } websocket.Shutdown(SocketShutdown.Both); websocket.Close(); //clientSocket.Receive(readbyte, 4096, 0); } //else //{ // ireadbytes = 0; //} } catch { } finally { //ireadbytes = 0; //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } }); //if (clientSocket != null && clientSocket.Connected) // clientSocket.Close(); } } else { //proxyServer.SendCommand(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List<OnServerClientConnection> { Command.Client } }); clients.Remove(Command.Client); } } } if (Command.Command == ClientActions.Disconnected) { clients.Remove(Command.Client); } if (Command.Command == ClientActions.Shutdown) { clients.Remove(Command.Client); } }
void ClientModule_ServerReceiveCommand(FromServerCommand Command) { if (Command.Action == ServerActions.AlreadyConnected) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.Connected) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.ConnectedOverProxy) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.ConnectedToProxy) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.Connecting) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.ConnectingToProxy) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.ConnectionFailed) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.Disconnected) { try { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); ServerModule.Send(new ToServerCommand() { Action = ServerCommands.Disconnect, ToClient = new List <OnServerClientConnection>() { ((OnServerClientConnection)Clients[(EndPoint)Command.UserToken][0]) } }); } catch { } } else if (Command.Action == ServerActions.ProxyAuthFailed) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.Receive) { byte[] buf = new byte[Command.ReceiveBufferLength]; Array.Copy(Command.ReceiveBuffer, 0, buf, 0, Command.ReceiveBufferLength); try { ServerModule.Send(new ToServerCommand() { Action = ServerCommands.Send, ReceiveBuffer = buf, ToClient = new List <OnServerClientConnection>() { ((OnServerClientConnection)Clients[(EndPoint)Command.UserToken][0]) } }); } catch { } TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.SendCompleted) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } else if (Command.Action == ServerActions.Shutdown) { TB_Messages.AppendText(Command.UserToken.ToString() + " Message: " + Command.Action + "\n"); } }