Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class phpServ
- {
- private $sock;
- private $clients;
- private $stdRead;
- private $stdWrite;
- private $stdErr;
- private $port;
- private $break = false;
- private $oneReady = 0;
- public function __construct($port)
- {
- $this -> port = $port;
- $this -> run();
- }
- private function run()
- {
- $this -> sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
- if(socket_bind($this -> sock, '127.0.0.1', $this -> port) && socket_listen($this -> sock))
- {
- $this -> clients = array($this -> sock);
- echo "[+] Binded\r\n";
- flush();
- while(!connection_aborted() && !$this -> break) $this -> listen();
- }
- if(sizeof($this -> clients))
- foreach($this -> clients as $no => $client)
- if($client != $this -> sock) socket_close($client);
- socket_close($this -> sock);
- exit;
- }
- private function listen()
- {
- $this -> stdRead = $this -> clients;
- if(socket_select($this -> stdRead, $this -> stdWrite, $this -> stdError, 0) < 1) return;
- if(in_array($this -> sock, $this -> stdRead))
- {
- $current = $this -> clients[] = socket_accept($this -> sock);
- socket_getpeername($current, $ip);
- echo "[+] New connection: $ip\n";
- flush();
- socket_write($current, "HELLO Master\r\n");
- $this -> clientInterpret($current);
- }
- foreach($this -> clients as $client) if($client != $this -> sock) $this -> clientInterpret($client);
- }
- private function disconnect($client, $msg = 'ERROR Undefined Error')
- {
- $key = array_search($client, $this -> clients);
- socket_write($client, $msg . "\r\n");
- socket_close($client);
- unset($this -> clients[$key]);
- }
- private function clientInterpret($client)
- {
- $data = '';
- while(($byte = socket_read($client, 1)) != "\n") $data .= $byte;
- if(!$data) return;
- echo "[+] Client send: $data\r\n";
- flush();
- $param = explode(' ', ltrim($data, "\r\n"));
- $cmd = $param[0];
- unset($param[0]);
- if(method_exists($this, 'proc' . $cmd))
- {
- $method = 'proc' . ucfirst(strtolower($cmd));
- echo "[+] Client called method $method\r\n";
- flush();
- if(sizeof($param)) $this -> $method($client, $param);
- else $this -> $method($client);
- }
- else $this -> disconnect($client, 'Protocol error');
- }
- private function procHello($client, $param = array())
- {
- echo '[+] Accepted client ' . @$param[0] . "\r\n";
- flush();
- socket_write($client, "PACKAGE ".strlen("zabeaty@gmail.com\r\nzbt@o2.pl")."\r\n");
- }
- private function procDone($client)
- {
- if($this -> oneReady > 1)
- {
- $this -> disconnect($client, 'LOGOUT madafaka !');
- return ($this -> break = true);
- }
- socket_write($client, "PACKAGE ".strlen("zabeaty@gmail.com\r\nzbt@o2.pl")."\r\n");
- $this -> oneReady++;
- }
- private function procReady($client, $param = array())
- {
- socket_write($client, "zabeaty@gmail.com\r\nzbt@o2.pl\r\n");
- }
- }
- new phpServ($argv[1]);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement