SHARE
TWEET

Untitled

a guest Jul 20th, 2017 902 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. class phpServ
  4. {
  5.     private $sock;
  6.     private $clients;
  7.     private $stdRead;
  8.     private $stdWrite;
  9.     private $stdErr;
  10.     private $port;
  11.     private $break = false;
  12.     private $oneReady = 0;
  13.    
  14.     public function __construct($port)
  15.     {
  16.         $this -> port = $port;
  17.  
  18.         $this -> run();
  19.     }
  20.  
  21.    
  22.     private function run()
  23.     {
  24.         $this -> sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  25.  
  26.         if(socket_bind($this -> sock, '127.0.0.1', $this -> port) && socket_listen($this -> sock))
  27.         {
  28.             $this -> clients = array($this -> sock);
  29.  
  30.             echo "[+] Binded\r\n";
  31.             flush();
  32.  
  33.             while(!connection_aborted() && !$this -> break) $this -> listen();
  34.         }
  35.  
  36.         if(sizeof($this -> clients))
  37.                 foreach($this -> clients as $no => $client)
  38.                     if($client != $this -> sock) socket_close($client);
  39.                    
  40.         socket_close($this -> sock);
  41.  
  42.         exit;
  43.     }
  44.  
  45.  
  46.     private function listen()
  47.     {
  48.         $this -> stdRead = $this -> clients;
  49.  
  50.         if(socket_select($this -> stdRead, $this -> stdWrite, $this -> stdError, 0) < 1) return;
  51.  
  52.         if(in_array($this -> sock, $this -> stdRead))
  53.         {
  54.             $current = $this -> clients[] = socket_accept($this -> sock);
  55.             socket_getpeername($current, $ip);
  56.  
  57.             echo "[+] New connection: $ip\n";
  58.             flush();
  59.  
  60.             socket_write($current, "HELLO Master\r\n");
  61.             $this -> clientInterpret($current);
  62.         }
  63.  
  64.         foreach($this -> clients as $client) if($client != $this -> sock) $this -> clientInterpret($client);
  65.     }
  66.  
  67.  
  68.     private function disconnect($client, $msg = 'ERROR Undefined Error')
  69.     {
  70.         $key = array_search($client, $this -> clients);
  71.  
  72.         socket_write($client, $msg . "\r\n");
  73.         socket_close($client);
  74.  
  75.         unset($this -> clients[$key]);
  76.     }
  77.  
  78.     private function clientInterpret($client)
  79.     {
  80.         $data = '';
  81.  
  82.         while(($byte = socket_read($client, 1)) != "\n") $data .= $byte;
  83.  
  84.         if(!$data) return;
  85.         echo "[+] Client send: $data\r\n";
  86.         flush();
  87.        
  88.         $param = explode(' ', ltrim($data, "\r\n"));
  89.         $cmd = $param[0];
  90.         unset($param[0]);
  91.        
  92.         if(method_exists($this, 'proc' . $cmd))
  93.         {
  94.             $method = 'proc' . ucfirst(strtolower($cmd));
  95.            
  96.             echo "[+] Client called method $method\r\n";
  97.             flush();
  98.            
  99.             if(sizeof($param)) $this -> $method($client, $param);
  100.             else $this -> $method($client);
  101.         }
  102.  
  103.         else $this -> disconnect($client, 'Protocol error');
  104.     }
  105.  
  106.  
  107.     private function procHello($client, $param = array())
  108.     {
  109.         echo '[+] Accepted client ' . @$param[0] . "\r\n";
  110.         flush();
  111.  
  112.         socket_write($client, "PACKAGE ".strlen("zabeaty@gmail.com\r\nzbt@o2.pl")."\r\n");
  113.     }
  114.  
  115.  
  116.     private function procDone($client)
  117.     {
  118.         if($this -> oneReady > 1)
  119.         {
  120.             $this -> disconnect($client, 'LOGOUT madafaka !');
  121.             return ($this -> break = true);
  122.         }
  123.  
  124.         socket_write($client, "PACKAGE ".strlen("zabeaty@gmail.com\r\nzbt@o2.pl")."\r\n");
  125.         $this -> oneReady++;
  126.     }
  127.  
  128.     private function procReady($client, $param = array())
  129.     {
  130.         socket_write($client, "zabeaty@gmail.com\r\nzbt@o2.pl\r\n");
  131.     }
  132. }
  133.  
  134. new phpServ($argv[1]);
  135.  
  136. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top