Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.21 KB | None | 0 0
  1. <?php
  2.  
  3. function partition( $list, $p ) {
  4.         $listlen = count( $list );
  5.         $partlen = floor( $listlen / $p );
  6.         $partrem = $listlen % $p;
  7.         $partition = array();
  8.         $mark = 0;
  9.         for ($px = 0; $px < $p; $px++) {
  10.                 $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
  11.                 $partition[$px] = array_slice( $list, $mark, $incr );
  12.                 $mark += $incr;
  13.         }
  14.         return $partition;
  15. }
  16.  
  17. if($argc != 5)
  18. {
  19.     exit("Usage: filter.php [INPUT] [OUTPOUT] [THREADS] [SIZE]\r\n");
  20. }
  21. $childcount = $argv[3];
  22. $part = array();
  23. $array = file($argv[1], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  24. $array = array_unique($array);
  25. if($childcount < 1) exit("Invalid thread count!\r\n");
  26. echo "Count before: ".count($array)."\r\n";
  27. if($childcount > count($array)) $childcount = count($array);
  28. $part = partition($array, $childcount);
  29. file_put_contents($argv[2], "");
  30.  
  31. for($i = 0; $i < $childcount; $i ++)
  32. {
  33.         $pid = pcntl_fork();
  34.         if ($pid == -1) {
  35.                 echo "failed to fork on loop $i of forking\n";
  36.                 exit;
  37.         } else if ($pid) {
  38.                 continue;
  39.         } else {
  40.                 $buf = "a";
  41.                     foreach($part[$i] as $ip)
  42.                     {
  43.                 list($ip, $oldsize) = explode(" ", $ip);
  44.                             $fp = fsockopen("udp://".$ip, 1900);
  45.                             socket_set_timeout($fp, 2);
  46.                             fwrite($fp, $buf);
  47.                             $size = 0;
  48.                             while(($data = fgets($fp, 4096)) != "")
  49.                             {
  50.                                     $size += strlen($data);
  51.                                 }
  52.                                 if($size > $argv[4])
  53.                                 {
  54.                                         file_put_contents($argv[2], $ip." ".$size."\r\n", FILE_APPEND);
  55.                             }
  56.             }
  57.             $q++;
  58.                 die;
  59.         }
  60. }
  61.  
  62. for($j = 0; $j < $childcount; $j++)
  63. {
  64.         $pid = pcntl_wait($status);
  65. }
  66.  
  67. $after = count(file($argv[2], FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
  68. echo "Count after: ".$after."\r\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement