From d1e95a46cd6c64c33907deacedba9199fea3c4a9 Mon Sep 17 00:00:00 2001 From: zhaoke Date: Fri, 2 Dec 2022 06:51:11 +0000 Subject: [PATCH] * Finish task 77678. --- module/zahost/control.php | 13 +++++++- module/zahost/lang/de.php | 1 + module/zahost/lang/en.php | 1 + module/zahost/lang/fr.php | 1 + module/zahost/lang/vi.php | 6 ++++ module/zahost/lang/zh-cn.php | 1 + module/zahost/model.php | 64 ++++++++++++++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 1 deletion(-) diff --git a/module/zahost/control.php b/module/zahost/control.php index 0cfbc6e931..b73a38351e 100644 --- a/module/zahost/control.php +++ b/module/zahost/control.php @@ -88,6 +88,10 @@ class zahost extends control { return $this->send(array('result' => 'fail', 'message' => dao::getError())); } + elseif($hostID === false) + { + return $this->send(array('result' => 'fail', 'message' => array("extranet" => array($this->lang->zahost->netError)))); + } $initLink = $this->createLink('zahost', 'inithost', "hostID=$hostID"); return print(""); @@ -111,7 +115,14 @@ class zahost extends control if($_POST) { $changes = $this->zahost->update($hostID); - if(dao::isError()) return $this->send(array('result' => 'fail', 'message' => dao::getError())); + if(dao::isError()) + { + return $this->send(array('result' => 'fail', 'message' => dao::getError())); + } + elseif($hostID === false) + { + return $this->send(array('result' => 'fail', 'message' => array("extranet" => array($this->lang->zahost->netError)))); + } if(!empty($changes)) { diff --git a/module/zahost/lang/de.php b/module/zahost/lang/de.php index e16b0e18ed..95aa835f71 100644 --- a/module/zahost/lang/de.php +++ b/module/zahost/lang/de.php @@ -40,6 +40,7 @@ $lang->zahost->createZanode = 'Create Zanode'; $lang->zahost->initHostNotice = 'Save successfully, initialize the ZAhost or return list'; $lang->zahost->createZanodeNotice = 'Initialization successful, ready to create the zanode'; $lang->zahost->downloadImageNotice = 'Initialization successful, download the image to create zanode'; +$lang->zahost->netError = 'Unable to connect to the host, please check the network and try again.'; $lang->zahost->initHost = new stdclass; $lang->zahost->initHost->checkStatus = "Check Service Status"; diff --git a/module/zahost/lang/en.php b/module/zahost/lang/en.php index 1be9a4cd11..4d2a57326b 100644 --- a/module/zahost/lang/en.php +++ b/module/zahost/lang/en.php @@ -40,6 +40,7 @@ $lang->zahost->createZanode = 'Create Zanode'; $lang->zahost->initHostNotice = 'Save successfully, initialize the ZAhost or return list'; $lang->zahost->createZanodeNotice = 'Initialization successful, ready to create the zanode'; $lang->zahost->downloadImageNotice = 'Initialization successful, download the image to create zanode'; +$lang->zahost->netError = 'Unable to connect to the host, please check the network and try again.'; $lang->zahost->initHost = new stdclass; $lang->zahost->initHost->checkStatus = "Check Service Status"; diff --git a/module/zahost/lang/fr.php b/module/zahost/lang/fr.php index 14e553f7ea..50e57ad928 100644 --- a/module/zahost/lang/fr.php +++ b/module/zahost/lang/fr.php @@ -40,6 +40,7 @@ $lang->zahost->createZanode = 'Create Zanode'; $lang->zahost->initHostNotice = 'Save successfully, initialize the ZAhost or return list'; $lang->zahost->createZanodeNotice = 'Initialization successful, ready to create the zanode'; $lang->zahost->downloadImageNotice = 'Initialization successful, download the image to create zanode'; +$lang->zahost->netError = 'Unable to connect to the host, please check the network and try again.'; $lang->zahost->initHost = new stdclass; $lang->zahost->initHost->checkStatus = "Check Service Status"; diff --git a/module/zahost/lang/vi.php b/module/zahost/lang/vi.php index e3e5b1da37..03737e60c7 100644 --- a/module/zahost/lang/vi.php +++ b/module/zahost/lang/vi.php @@ -39,6 +39,12 @@ $lang->zahost->osVersion = 'Os Version No'; $lang->zahost->osLang = 'Language'; $lang->zahost->imageName = 'Image File'; +$lang->zahost->createZanode = 'Create Zanode'; +$lang->zahost->initHostNotice = 'Save successfully, initialize the ZAhost or return list'; +$lang->zahost->createZanodeNotice = 'Initialization successful, ready to create the zanode'; +$lang->zahost->downloadImageNotice = 'Initialization successful, download the image to create zanode'; +$lang->zahost->netError = 'Unable to connect to the host, please check the network and try again.'; + $lang->zahost->initHost = new stdclass; $lang->zahost->initHost->checkStatus = "Check Service Status"; $lang->zahost->initHost->not_install = "Not installed"; diff --git a/module/zahost/lang/zh-cn.php b/module/zahost/lang/zh-cn.php index 1223b3231e..6d5d951979 100644 --- a/module/zahost/lang/zh-cn.php +++ b/module/zahost/lang/zh-cn.php @@ -40,6 +40,7 @@ $lang->zahost->createZanode = '创建执行节点'; $lang->zahost->initHostNotice = '保存成功,请您初始化宿主机或返回列表。'; $lang->zahost->createZanodeNotice = '初始化成功,您现在可以创建执行节点了。'; $lang->zahost->downloadImageNotice = '初始化成功,请下载镜像用于创建执行节点。'; +$lang->zahost->netError = '无法连接到宿主机,请检查网络后重试。'; $lang->zahost->initHost = new stdclass; $lang->zahost->initHost->statusTitle = "服务状态"; diff --git a/module/zahost/model.php b/module/zahost/model.php index 6987f9acbd..9d02e57675 100644 --- a/module/zahost/model.php +++ b/module/zahost/model.php @@ -49,6 +49,12 @@ class zahostModel extends model ->autoCheck(); if(dao::isError()) return false; + $ping = $this->checkAddress($hostInfo->extranet); + if(!$ping) + { + return false; + } + $this->dao->insert(TABLE_ZAHOST)->data($hostInfo)->autoCheck()->exec(); $hostID = $this->dao->lastInsertID(); if(!dao::isError()) @@ -79,6 +85,12 @@ class zahostModel extends model ->batchCheck('diskSize,memory', 'float'); if(dao::isError()) return false; + $ping = $this->checkAddress($hostInfo->extranet); + if(!$ping) + { + return false; + } + $this->dao->update(TABLE_ZAHOST)->data($hostInfo, 'name')->autoCheck() ->batchCheck('cpuCores,diskSize', 'gt', 0) ->batchCheck('diskSize,memory', 'float') @@ -86,6 +98,58 @@ class zahostModel extends model return common::createChanges($oldHost, $hostInfo); } + /** + * Ping ip/domain. + * + * @param string $address + * @access public + * @return bool + */ + public function ping($address) + { + if (strcasecmp(PHP_OS, 'WINNT') === 0) + { + exec("ping -n 1 {$address}", $outcome, $status); + } + elseif (strcasecmp(PHP_OS, 'Linux') === 0) + { + exec("ping -c 1 {$address}", $outcome, $status); + } + + if (0 == $status) + { + $status = true; + } + else + { + $status = false; + } + + return $status; + } + + /** + * Telnet ip/domain. + * + * @param string $address + * @access public + * @return bool + */ + public function checkAddress($address) + { + $address = str_replace(array('https://', 'http://'), '', $address); + + if ($this->ping($address)) return true; + + foreach(array(80, 443, 8086) as $port) + { + $fp = fsockopen($address, $port, $errno, $errstr, 3); + if ($fp) return true; + } + + return false; + } + /** * Get image by ID. *