From 5b8d2f9c5dd894bba3ea92e7867bb034debf7c4a Mon Sep 17 00:00:00 2001 From: LoveesYe <109154433+LoveesYe@users.noreply.github.com> Date: Thu, 14 Jul 2022 20:58:16 +0200 Subject: [PATCH] Add files via upload --- web/api.php | 16 ++++++ web/config.php | 22 ++++++++ web/index.php | 62 ++++++++++++++++++++++ web/monitor.php | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ web/update.php | 30 +++++++++++ 5 files changed, 265 insertions(+) create mode 100644 web/api.php create mode 100644 web/config.php create mode 100644 web/index.php create mode 100644 web/monitor.php create mode 100644 web/update.php diff --git a/web/api.php b/web/api.php new file mode 100644 index 0000000..67db44d --- /dev/null +++ b/web/api.php @@ -0,0 +1,16 @@ +0){ + foreach($param as $target){ + $result[] = node_monitor_local('1', $target); + } +} +$data = ['msg'=>$result]; +echo json_encode($data); \ No newline at end of file diff --git a/web/config.php b/web/config.php new file mode 100644 index 0000000..8e6ec70 --- /dev/null +++ b/web/config.php @@ -0,0 +1,22 @@ + [ + ['node_id'=>'1', 'type'=>'local'], //本地节点 + // ['node_id'=>'2', 'type'=>'remote', 'api'=>'http://www.example.com/api.php'], //远程节点,需填写监控接口地址 + ], + + //国外组 + 'group_2' => [ + ['node_id'=>'1', 'type'=>'local'], //本地节点 + // ['node_id'=>'2', 'type'=>'remote', 'api'=>'http://www.example.com/api.php'], //远程节点,需填写监控接口地址 + ], +]; \ No newline at end of file diff --git a/web/index.php b/web/index.php new file mode 100644 index 0000000..18c0dd5 --- /dev/null +++ b/web/index.php @@ -0,0 +1,62 @@ +time(), 'rnd'=>$param['rnd']]; + echo generate_output($data); +} +elseif(strpos($url, '/auth') !== false && $method=='POST'){ + $param = parse_input(); + $data = ['nodes'=>10000, 'machine_code'=>$param['machine_code'], 'end_at'=>time()+3600*24*365]; + echo generate_output($data); +} +elseif(strpos($url, '/check') !== false && $method=='POST'){ + require 'config.php'; + require 'monitor.php'; + $post = file_get_contents('php://input'); + $param = json_decode($post, true); + $result = node_monitor_all($param); + $data = ['msg'=>$result]; + echo json_encode($data); +} +elseif(strpos($url, '/common/datetime') !== false && $method=='GET'){ + return date('Y-m-d H:i:s'); +} +elseif(strpos($url, '/master/upgrades') !== false){ + $version_data = file_get_contents('version.json'); + if(!$version_data) exit(json_encode(['code'=>-1, 'data'=>[], 'ip'=>$_SERVER['REMOTE_ADDR'], 'msg'=>'版本信息文件不存在'])); + $version_info = json_decode($version_data, true); + if(!$version_info) exit(json_encode(['code'=>-1, 'data'=>[], 'ip'=>$_SERVER['REMOTE_ADDR'], 'msg'=>'解析版本信息文件失败'])); + $data = ['code'=>0, 'count'=>1, 'data'=>[$version_info], 'ip'=>$_SERVER['REMOTE_ADDR']]; + echo json_encode($data); +} + + +function parse_input(){ + $post = file_get_contents('php://input'); + $param = json_decode(text_decrypt($post), true); + return $param; +} +function generate_output($data){ + $cipher = text_encrypt(json_encode($data)); + $data = ['code'=>0, 'data'=>$cipher, 'msg'=>'']; + return json_encode($data); +} +function text_encrypt($data){ + return openssl_encrypt($data, 'aes-128-cbc', AES_KEY, 0, AES_KEY); +} +function text_decrypt($data){ + return openssl_decrypt($data, 'aes-128-cbc', AES_KEY, 0, AES_KEY); +} diff --git a/web/monitor.php b/web/monitor.php new file mode 100644 index 0000000..a38595b --- /dev/null +++ b/web/monitor.php @@ -0,0 +1,135 @@ + 0 && count($target_group_1) > 0){ + foreach($monitor_config['group_1'] as $node){ + if($node['type'] == 'local'){ + foreach($target_group_1 as $target){ + $result[] = node_monitor_local($node['node_id'], $target); + } + }elseif($node['type'] == 'remote'){ + $result = array_merge($result, node_monitor_remote($node['node_id'], $node['api'], $target_group_1)); + } + } + } + if(count($monitor_config['group_2']) > 0 && count($target_group_2) > 0){ + foreach($monitor_config['group_2'] as $node){ + if($node['type'] == 'local'){ + foreach($target_group_2 as $target){ + $result[] = node_monitor_local($node['node_id'], $target); + } + }elseif($node['type'] == 'remote'){ + $result = array_merge($result, node_monitor_remote($node['node_id'], $node['api'], $target_group_2)); + } + } + } + return $result; +} + +//批量执行远程监控 +function node_monitor_remote($node_id, $apiurl, $targets){ + $json = json_encode($targets); + $data = send_request($apiurl, $json); + $arr = json_decode($data, true); + if(!$arr || !isset($arr['msg'])) return []; + $result = []; + foreach($arr['msg'] as $target){ + $target['node_id'] = $node_id; + $result[] = $target; + } + return $result; +} + +//单个执行本地监控 +function node_monitor_local($node_id, $target){ + $status = false; + if($target['type'] == 'http'){ + $status = check_http($target['target'], $target['port'], $target['path'], $target['host'], $target['timeout']); + }elseif($target['type'] == 'ping'){ + $status = check_ping($target['target'], $target['port'], $target['timeout']); + }else{ //tcp + $status = check_tcp($target['target'], $target['port'], $target['timeout']); + } + return ['node_id'=>$node_id, 'success'=>$status?1:0, 'target'=>$target['target']]; +} + +function check_http($target, $port, $path, $host = '', $timeout = 3){ + if($timeout > 3) $timeout = 3; + if(!$port) $port = 80; + if(!$path) $path = '/'; + if($port == 80){ + $url = 'http://'.$target.$path; + }else{ + $url = 'http://'.$target.':'.$port.$path; + } + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + $httpheader[] = "Accept: */*"; + $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8"; + $httpheader[] = "Connection: close"; + if(!empty($host)){ + $httpheader[] = "Host: ".$host; + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader); + curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); + curl_exec($ch); + $errno = curl_errno($ch); + if($errno) { + curl_close($ch); + return false; + } + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + if($httpcode>=200 && $httpcode<400){ + return true; + } + return false; +} + +function check_tcp($target, $port, $timeout = 3){ + if($timeout > 3) $timeout = 3; + if(!$port) $port = 80; + $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); + socket_set_nonblock($sock); + socket_connect($sock, $target, $port); + socket_set_block($sock); + $status = socket_select($r = array($sock), $w = array($sock), $f = array($sock), $timeout); + return $status === 1; +} + +function check_ping($target, $port, $timeout = 2){ + if(!function_exists('exec') || PHP_OS == 'WINNT') return check_tcp($target, $port, $timeout); + if($timeout > 2) $timeout = 2; + exec('ping -c 1 -w '.$timeout.' '.$target.' > /dev/null', $output, $return_var); + if($return_var === 0) return true; + return false; +} + +function send_request($url, $json){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $json); + curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json; charset=utf-8']); + $ret = curl_exec($ch); + curl_close($ch); + return $ret; +} \ No newline at end of file diff --git a/web/update.php b/web/update.php new file mode 100644 index 0000000..710f5a8 --- /dev/null +++ b/web/update.php @@ -0,0 +1,30 @@ +