admin.php 1.6 KB

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