admin.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Statistics\Modules;
  3. use Statistics\Lib\Cache;
  4. function admin()
  5. {
  6. $act = isset($_GET['act'])? $_GET['act'] : 'home';
  7. switch($act)
  8. {
  9. case 'detect_server':
  10. // 创建udp socket
  11. $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
  12. socket_set_option($socket, SOL_SOCKET, SO_BROADCAST, 1);
  13. $buffer = json_encode(array('cmd'=>'REPORT_IP'))."\n";
  14. // 广播
  15. socket_sendto($socket, $buffer, strlen($buffer), 0, '255.255.255.255', \Statistics\Web\Config::$ProviderPort);
  16. // 超时相关
  17. $time_start = microtime(true);
  18. $global_timeout = 1;
  19. $ip_list = array();
  20. $recv_timeout = array('sec'=>0,'usec'=>8000);
  21. socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$recv_timeout);
  22. // 循环读数据
  23. while(microtime(true) - $time_start < $global_timeout)
  24. {
  25. $buf = $host = $port = '';
  26. if(@socket_recvfrom($socket, $buf, 65535, 0, $host, $port))
  27. {
  28. $ip_list[$host] = $host;
  29. }
  30. }
  31. break;
  32. }
  33. $ip_list_str = '';
  34. foreach($ip_list as $ip)
  35. {
  36. if(!isset(Cache::$ServerIpList[$ip]))
  37. {
  38. $ip_list_str .= $ip."\r\n";
  39. }
  40. }
  41. include ST_ROOT . '/Views/header.tpl.php';
  42. include ST_ROOT . '/Views/admin.tpl.php';
  43. include ST_ROOT . '/Views/footer.tpl.php';
  44. }