diff --git a/.travis.yml b/.travis.yml index d7d8f4b..0f4f357 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,39 +1,39 @@ -language: php -php: - - '5.4' - - '5.5' - - '5.6' - - '7.0' - - '7.1' - - '7.2' - - nightly -# - hhvm - -# https://docs.travis-ci.com/user/reference/trusty#PHP-images -matrix: - include: - - php: 5.2 - dist: precise - - php: 5.3.3 - dist: precise -# matrix: -# allow_failures: -# - php: hhvm - -before_script: - - chmod 777 -R ./ - - php -m - - cd build - -script: - - php build -n holy_lance - - php -l holy_lance.php - # - php -S 0.0.0.0:8080 & - # - curl -s http://localhost:8080/holy_lance.php?file=init.php - # - curl -s http://localhost:8080/holy_lance.php?file=api.php - - php-cgi -f holy_lance.php file=init.php - - php-cgi -f holy_lance.php file=api.php - - php-cgi -f holy_lance.php file=test_disk.php - - php-cgi -f holy_lance.php file=test_pi.php - - php-cgi -f holy_lance.php file=test_ping.php ip=8.8.8.8 +language: php +php: + - '5.4' + - '5.5' + - '5.6' + - '7.0' + - '7.1' + - '7.2' + - nightly +# - hhvm + +# https://docs.travis-ci.com/user/reference/trusty#PHP-images +matrix: + include: + - php: 5.2 + dist: precise + - php: 5.3.3 + dist: precise +# matrix: +# allow_failures: +# - php: hhvm + +before_script: + - chmod 777 -R ./ + - php -m + - cd build + +script: + - php build -n holy_lance + - php -l holy_lance.php + # - php -S 0.0.0.0:8080 & + # - curl -s http://localhost:8080/holy_lance.php?file=init.php + # - curl -s http://localhost:8080/holy_lance.php?file=api.php + - php-cgi -f holy_lance.php file=init.php + - php-cgi -f holy_lance.php file=api.php + - php-cgi -f holy_lance.php file=test_disk.php + - php-cgi -f holy_lance.php file=test_pi.php + - php-cgi -f holy_lance.php file=test_ping.php ip=8.8.8.8 - php-cgi -f holy_lance.php \ No newline at end of file diff --git a/build/build b/build/build index f357baf..20f2393 100644 --- a/build/build +++ b/build/build @@ -1,122 +1,122 @@ - $old_file_name_value) { - $file_buffer[$old_file_name]['content'] = str_replace($old_file_name_value, $new_file_name_list[$file_name_key], $file_buffer[$old_file_name]['content']); - } - $file_buffer[$old_file_name]['content'] = str_replace(" ", "", $file_buffer[$old_file_name]['content']); - } -} - -function listDir($dir) -{ - global $ignore_dir, $ignore_file, $old_file_name_list, $new_file_name_list; - if (is_dir($dir)) { - if ($dh = opendir($dir)) { - while (($file = readdir($dh)) !== false) { - if ((is_dir($dir . "/" . $file)) && $file != "." && $file != "..") { - if (!in_array($file, $ignore_dir)) { - // echo $file . "\n"; - listDir($dir . $file . "/"); - } - } else { - if ($file != "." && $file != "..") { - $file_name = $dir . $file; - if (!in_array($file_name, $ignore_file)) { - $old_file_name_list[$file_name] = str_replace("../", "", $file_name); - $new_file_name_list[$file_name] = BUILD_FILE_NAME . "?file=" . urlencode($old_file_name_list[$file_name]); - echo $file_name . "\n"; - } - } - } - } - closedir($dh); - } - } -} - - -listDir("../"); - -foreach ($old_file_name_list as $file_name => $old_file_name) { - add_file_to_buffer($file_name); -} - -$fp = fopen(BUILD_FILE_NAME, 'w'); -$entry_file = !empty($file_buffer[ENTER_FILE_NAME]) ? $file_buffer[ENTER_FILE_NAME] : ''; -if ($entry_file === '') { - echo 'Entry file not found! '; - exit(1); -} -unset($file_buffer[ENTER_FILE_NAME]); -$common_file = !empty($file_buffer[COMMON_FILE_NAME]) ? $file_buffer[COMMON_FILE_NAME] : ''; -if ($common_file === '') { - echo 'Common file not found! '; - exit(2); -} -fwrite($fp, ''); -$common_file['content'] = str_replace("define('HOLY_LANCE_PASSWORD', '');", "define('HOLY_LANCE_PASSWORD', '" . $password . "');", $common_file['content']); -fwrite($fp, $common_file['content']); -unset($file_buffer[COMMON_FILE_NAME]); -foreach ($file_buffer as $file_name => $file) { - fwrite($fp, ''); - fwrite($fp, $file['content']); - fwrite($fp, ''); -} - -fwrite($fp, $entry_file['content']); -fclose($fp); - + $old_file_name_value) { + $file_buffer[$old_file_name]['content'] = str_replace($old_file_name_value, $new_file_name_list[$file_name_key], $file_buffer[$old_file_name]['content']); + } + $file_buffer[$old_file_name]['content'] = str_replace(" ", "", $file_buffer[$old_file_name]['content']); + } +} + +function listDir($dir) +{ + global $ignore_dir, $ignore_file, $old_file_name_list, $new_file_name_list; + if (is_dir($dir)) { + if ($dh = opendir($dir)) { + while (($file = readdir($dh)) !== false) { + if ((is_dir($dir . "/" . $file)) && $file != "." && $file != "..") { + if (!in_array($file, $ignore_dir)) { + // echo $file . "\n"; + listDir($dir . $file . "/"); + } + } else { + if ($file != "." && $file != "..") { + $file_name = $dir . $file; + if (!in_array($file_name, $ignore_file)) { + $old_file_name_list[$file_name] = str_replace("../", "", $file_name); + $new_file_name_list[$file_name] = BUILD_FILE_NAME . "?file=" . urlencode($old_file_name_list[$file_name]); + echo $file_name . "\n"; + } + } + } + } + closedir($dh); + } + } +} + + +listDir("../"); + +foreach ($old_file_name_list as $file_name => $old_file_name) { + add_file_to_buffer($file_name); +} + +$fp = fopen(BUILD_FILE_NAME, 'w'); +$entry_file = !empty($file_buffer[ENTER_FILE_NAME]) ? $file_buffer[ENTER_FILE_NAME] : ''; +if ($entry_file === '') { + echo 'Entry file not found! '; + exit(1); +} +unset($file_buffer[ENTER_FILE_NAME]); +$common_file = !empty($file_buffer[COMMON_FILE_NAME]) ? $file_buffer[COMMON_FILE_NAME] : ''; +if ($common_file === '') { + echo 'Common file not found! '; + exit(2); +} +fwrite($fp, ''); +$common_file['content'] = str_replace("define('HOLY_LANCE_PASSWORD', '');", "define('HOLY_LANCE_PASSWORD', '" . $password . "');", $common_file['content']); +fwrite($fp, $common_file['content']); +unset($file_buffer[COMMON_FILE_NAME]); +foreach ($file_buffer as $file_name => $file) { + fwrite($fp, ''); + fwrite($fp, $file['content']); + fwrite($fp, ''); +} + +fwrite($fp, $entry_file['content']); +fclose($fp); + echo "\n\n" . BUILD_FILE_NAME . " has been generated! \npssword: " . $password . "\n\n"; \ No newline at end of file diff --git a/common.php b/common.php index 4c64024..2729139 100644 --- a/common.php +++ b/common.php @@ -1,141 +1,141 @@ -= 1024 && $i < 4; $i++) - $size /= 1024; - return round($size, $precision) . $units[$i]; -} - -function format_number($number) -{ - return number_format($number, '0', '.', ' '); -} - - -function check_permission($file_name) -{ - $fp = @fopen($file_name, 'w'); - if (!$fp) { - return false; - } else { - fclose($fp); - @unlink($file_name); - return true; - } -} - -function get_cpu_info_map($cpu_info_val) -{ - $result = array(); - foreach (explode("\n", $cpu_info_val) as $value) { - if ($value) { - $item = array_map("trim", explode(":", $value)); - $result[str_replace(" ", "_", $item[0])] = $item[1]; - } - } - return $result; -} - -function get_mem_info_map($mem_info) -{ - $result = array(); - foreach ($mem_info as $value) { - $value = str_ireplace(")", "", str_ireplace("(", "_", str_ireplace("kB", "", $value))); - $item = array_map("trim", explode(":", $value)); - $result[str_replace(" ", "_", $item[0])] = $item[1]; - } - return $result; -} - -function convert_timestamp_2_string($timestamp) -{ - $timestamp = intval($timestamp); - return intval($timestamp / 86400) . ":" - . sprintf("%02d", $timestamp % 86400 / 3600) . ":" - . sprintf("%02d", $timestamp % 3600 / 60) . ":" - . sprintf("%02d", $timestamp % 60); -} - -function check_password() -{ - if (HOLY_LANCE_PASSWORD !== '' && (!isset($_POST['password']) || $_POST['password'] !== HOLY_LANCE_PASSWORD)) { - echo '{"status":false}'; - exit(1); - } - return true; -} - - -// 创建row socket 需要root权限,所以用root账户在CLI下运行可以成功,用www用户在fpm下运行可能会失败,但是不会报错 -// 需要root权限运行则要php-fpm -R运行 -// 目前针对没有root权限做了一套临时的兼容方案 -function ping($host, $port = 80) -{ - $protocolNumber = getprotobyname('icmp'); - $socket = socket_create(AF_INET, SOCK_RAW, $protocolNumber); - if ($socket === false) {// 没有Root权限,开启Raw socket失败,用TCP协议ping - return ping_without_root($host, $port); - } - socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0)); - socket_connect($socket, $host, 0); - $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67"; - socket_send($socket, $package, strlen($package), 0); - $ts1 = microtime(true); - if (socket_read($socket, 255) !== false) { - $ts2 = microtime(true); - $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; - } else { - $result = socket_strerror(socket_last_error($socket)); - } - socket_close($socket); - return $result; -} - -function ping_without_root($host, $port) -{ - try { - $err_no = null; - $err_str = null; - $ts1 = microtime(true); - $fp = stream_socket_client("tcp://" . $host . ":" . $port, $err_no, $err_str, 3); - $ts2 = microtime(true); - $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; - if ($fp === false) { - $result = 'Timeout'; - } - fclose($fp); - } catch (Exception $exception) { - $result = 'Timeout'; - } - return $result; -} += 1024 && $i < 4; $i++) + $size /= 1024; + return round($size, $precision) . $units[$i]; +} + +function format_number($number) +{ + return number_format($number, '0', '.', ' '); +} + + +function check_permission($file_name) +{ + $fp = @fopen($file_name, 'w'); + if (!$fp) { + return false; + } else { + fclose($fp); + @unlink($file_name); + return true; + } +} + +function get_cpu_info_map($cpu_info_val) +{ + $result = array(); + foreach (explode("\n", $cpu_info_val) as $value) { + if ($value) { + $item = array_map("trim", explode(":", $value)); + $result[str_replace(" ", "_", $item[0])] = $item[1]; + } + } + return $result; +} + +function get_mem_info_map($mem_info) +{ + $result = array(); + foreach ($mem_info as $value) { + $value = str_ireplace(")", "", str_ireplace("(", "_", str_ireplace("kB", "", $value))); + $item = array_map("trim", explode(":", $value)); + $result[str_replace(" ", "_", $item[0])] = $item[1]; + } + return $result; +} + +function convert_timestamp_2_string($timestamp) +{ + $timestamp = intval($timestamp); + return intval($timestamp / 86400) . ":" + . sprintf("%02d", $timestamp % 86400 / 3600) . ":" + . sprintf("%02d", $timestamp % 3600 / 60) . ":" + . sprintf("%02d", $timestamp % 60); +} + +function check_password() +{ + if (HOLY_LANCE_PASSWORD !== '' && (!isset($_POST['password']) || $_POST['password'] !== HOLY_LANCE_PASSWORD)) { + echo '{"status":false}'; + exit(1); + } + return true; +} + + +// 创建row socket 需要root权限,所以用root账户在CLI下运行可以成功,用www用户在fpm下运行可能会失败,但是不会报错 +// 需要root权限运行则要php-fpm -R运行 +// 目前针对没有root权限做了一套临时的兼容方案 +function ping($host, $port = 80) +{ + $protocolNumber = getprotobyname('icmp'); + $socket = socket_create(AF_INET, SOCK_RAW, $protocolNumber); + if ($socket === false) {// 没有Root权限,开启Raw socket失败,用TCP协议ping + return ping_without_root($host, $port); + } + socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 1, 'usec' => 0)); + socket_connect($socket, $host, 0); + $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67"; + socket_send($socket, $package, strlen($package), 0); + $ts1 = microtime(true); + if (socket_read($socket, 255) !== false) { + $ts2 = microtime(true); + $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; + } else { + $result = socket_strerror(socket_last_error($socket)); + } + socket_close($socket); + return $result; +} + +function ping_without_root($host, $port) +{ + try { + $err_no = null; + $err_str = null; + $ts1 = microtime(true); + $fp = stream_socket_client("tcp://" . $host . ":" . $port, $err_no, $err_str, 3); + $ts2 = microtime(true); + $result = round(($ts2 - $ts1) * 1000, 2) . ' ms'; + if ($fp === false) { + $result = 'Timeout'; + } + fclose($fp); + } catch (Exception $exception) { + $result = 'Timeout'; + } + return $result; +} ?> \ No newline at end of file diff --git a/test_disk.php b/test_disk.php index df6b462..85c53c6 100644 --- a/test_disk.php +++ b/test_disk.php @@ -1,49 +1,49 @@ - -{ - "status": false, - "message": "chown -R www ./", - "result": { - "disk_write_512k" :"×", - "disk_read_512k" :"×", - "disk_write_4k" :"×", - "disk_read_4k" :"×" - } -} -&1 |awk \'/copied/ {print $8 " " $9}\'')); - $disk_read_512k = trim(system('dd if=' . $file_name . ' of=/dev/null bs=524288 iflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); - unlink($file_name); - // Write 128 MiB - $disk_write_4k = trim(system('dd if=/dev/zero of=' . $file_name . ' bs=4096 count=32768 conv=fdatasync oflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); - $disk_read_4k = trim(system('dd if=' . $file_name . ' of=/dev/null bs=4096 iflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); - unlink($file_name); - ob_end_clean(); -?> -{ - "status": true, - "message": "", - "result": { - "disk_write_512k" :"", - "disk_read_512k" :"", - "disk_write_4k" :"", - "disk_read_4k" :"" - } -} - +{ + "status": false, + "message": "chown -R www ./", + "result": { + "disk_write_512k" :"×", + "disk_read_512k" :"×", + "disk_write_4k" :"×", + "disk_read_4k" :"×" + } +} +&1 |awk \'/copied/ {print $8 " " $9}\'')); + $disk_read_512k = trim(system('dd if=' . $file_name . ' of=/dev/null bs=524288 iflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); + unlink($file_name); + // Write 128 MiB + $disk_write_4k = trim(system('dd if=/dev/zero of=' . $file_name . ' bs=4096 count=32768 conv=fdatasync oflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); + $disk_read_4k = trim(system('dd if=' . $file_name . ' of=/dev/null bs=4096 iflag=direct,nonblock 2>&1 |awk \'/copied/ {print $8 " " $9}\'')); + unlink($file_name); + ob_end_clean(); +?> +{ + "status": true, + "message": "", + "result": { + "disk_write_512k" :"", + "disk_read_512k" :"", + "disk_write_4k" :"", + "disk_read_4k" :"" + } +} + \ No newline at end of file diff --git a/test_ping.php b/test_ping.php index fec2341..53dbc8b 100644 --- a/test_ping.php +++ b/test_ping.php @@ -1,35 +1,35 @@ - true, 'ip' => $ip, 'result' => ping($ip, $port))); -} else { - echo json_encode(array('status' => false, 'result' => 'Invalid IP')); -} + true, 'ip' => $ip, 'result' => ping($ip, $port))); +} else { + echo json_encode(array('status' => false, 'result' => 'Invalid IP')); +} ?> \ No newline at end of file