Guest User

get_tw_master_new-example

a guest
Jan 22nd, 2014
261
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /******************************************************************\
  3. |* Copyright (c) by Starlex.de
  4. |*
  5. |* Visit:
  6. |*      http://teeworlds.com
  7. |*      http://teeworlds.de
  8. |*      http://teeworlds-community.de
  9. |*
  10. |* Special Thanks:
  11. |*      east - annoyingeast.info
  12. |*      m!nus - mnus.de and teeworlds.com
  13. |*
  14. |* License:
  15. |*      http://creativecommons.org/licenses/by-nc-sa/3.0/deed.de
  16. |*
  17. \******************************************************************/
  18.  
  19. $masters = Array(
  20.     "master1.teeworlds.com:8300",
  21.     "master2.teeworlds.com:8300",
  22.     "master3.teeworlds.com:8300",
  23.     "master4.teeworlds.com:8300");
  24.  
  25. foreach ($masters as $ms) {
  26.     if($servers = get_tw_master_new($ms))
  27.     {
  28.         foreach ($servers as $server)
  29.         {
  30.             echo $server."<br>";
  31.         }
  32.     }
  33. }
  34.  
  35.  
  36. function get_tw_master_new($server) {  
  37.     $socket = stream_socket_client('udp://'.$server , $errno, $errstr, 1);
  38.     stream_set_timeout($socket, 1);
  39.     fwrite($socket, "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xffreq2");
  40.     $servers = Array();
  41.     $response = fread($socket, 2048);
  42.    
  43.     if ($response)
  44.     {
  45.         while (strlen($response) >= 1)
  46.         {
  47.             $response = substr($response, 14);
  48.             for ($i = 0; $i <= (strlen($response)-1) ; $i += 18) {
  49.            
  50.                 if(substr($response, $i, 12) == str_repeat("\x00", 10)."\xff\xff")
  51.                 {   // --- IPv4
  52.                     $serveraddresse = inet_ntop(substr($response, $i+12, 4));
  53.                    
  54.                 } else { // --- IPv6 - NOT TESTED !!!
  55.                     $serveraddresse = "[".inet_ntop(substr($response, $i, 16))."]";
  56.                 }
  57.                
  58.                 // Add the Port
  59.                 $serveraddresse .= ":".strToint($response{$i+16}.$response{$i+17});
  60.  
  61.             $servers[] = $serveraddresse;
  62.             }
  63.            
  64.             $response = fread($socket, 2048);
  65.         }
  66.         return $servers;
  67.     } else {
  68.         return FALSE;
  69.     }  
  70. }
  71. function strToint($string)
  72. {
  73.     $hex = "";
  74.     for ($i=0; $i < strlen($string); $i++)
  75.     {
  76.         $hex .= dechex(ord($string[$i]));
  77.     }
  78.     $int = hexdec($hex);
  79.     return $int;
  80. }
  81. ?>
RAW Paste Data