diff --git a/api/v1/entries/hostheartbeat.php b/api/v1/entries/hostheartbeat.php index b042d5e71b..cc61905008 100644 --- a/api/v1/entries/hostheartbeat.php +++ b/api/v1/entries/hostheartbeat.php @@ -35,27 +35,34 @@ class hostHeartbeatEntry extends baseEntry $conditionField = $secret ? 'secret' : 'tokenSN'; $conditionValue = $secret ? $secret : $token; + + $this->dao = $this->loadModel('common')->dao; + $hostInfo = $this->dao->select('id,tokenSN')->from(TABLE_ZAHOST) + ->beginIF($secret)->where('secret')->eq($secret)->fi() + ->beginIF(!$secret)->where('tokenSN')->eq($token) + ->andWhere('tokenTime')->gt($now)->fi() + ->fetch(); + if(empty($hostInfo->id)) + { + if(empty($token)) return $this->sendError(400, 'Secret error.'); + $hostInfo = $this->dao->select('id,tokenSN')->from(TABLE_ZAHOST) + ->where('oldTokenSN')->eq($token) + ->andWhere('tokenTime')->gt(date(DT_DATETIME1, strtotime($now) - 30))->fi() + ->fetch(); + if(empty($hostInfo->id)) return $this->sendError(400, 'Secret error.'); + } + $host = new stdclass(); $host->status = $status; if($secret) { - $host->tokenSN = md5($secret . $now); - $host->tokenTime = date('Y-m-d H:i:s', time() + 7200); + $host->tokenSN = md5($secret . $now); + $host->tokenTime = date('Y-m-d H:i:s', time() + 7200); + $host->oldTokenSN = $hostInfo['tokenSN']; } - - $this->dao = $this->loadModel('common')->dao; - $id = $this->dao->select('id')->from(TABLE_ZAHOST) - ->beginIF($secret)->where('secret')->eq($secret)->fi() - ->beginIF(!$secret)->where('tokenSN')->eq($token) - ->andWhere('tokenTime')->gt($now)->fi() - ->fetch('id'); - if(!$id) return $this->sendError(400, 'Secret error.'); - + $host->heartbeat = $now; + $host->zap = $zap; $this->dao->update(TABLE_ZAHOST)->data($host)->where($conditionField)->eq($conditionValue)->exec(); - $this->dao->update(TABLE_ZAHOST) - ->set('heartbeat')->eq($now) - ->set('zap')->eq($zap) - ->where('id')->eq($id)->exec(); if($vms) { @@ -81,6 +88,7 @@ class hostHeartbeatEntry extends baseEntry $host->tokenTimeUnix = strtotime($host->tokenTime); unset($host->status); unset($host->tokenTime); + unset($host->oldTokenSN); return $this->send(200, $host); } } diff --git a/db/update18.0.beta1.sql b/db/update18.0.beta1.sql index bea7765f2b..6b6fedabbd 100644 --- a/db/update18.0.beta1.sql +++ b/db/update18.0.beta1.sql @@ -35,6 +35,7 @@ MODIFY COLUMN `secret` varchar(50) NOT NULL DEFAULT '' AFTER `status`, ADD COLUMN `desc` text NOT NULL AFTER `secret`, CHANGE COLUMN `token` `tokenSN` varchar(50) NOT NULL DEFAULT '' AFTER `desc`, CHANGE COLUMN `expiredDate` `tokenTime` datetime NOT NULL AFTER `tokenSN`, +ADD COLUMN `oldTokenSN` varchar(50) NOT NULL DEFAULT '' AFTER `tokenTime`, CHANGE COLUMN `virtualSoftware` `vsoft` varchar(30) NOT NULL DEFAULT '' AFTER `tokenTime`, CHANGE COLUMN `heartbeatTime` `heartbeat` datetime NOT NULL AFTER `vsoft`, CHANGE COLUMN `agentPort` `zap` varchar(10) NOT NULL AFTER `heartbeat`, diff --git a/db/zentao.sql b/db/zentao.sql index 0db3c05f8c..8ab0e46dc1 100755 --- a/db/zentao.sql +++ b/db/zentao.sql @@ -7266,6 +7266,7 @@ CREATE TABLE `zt_host` ( `desc` text NOT NULL, `tokenSN` varchar(50) NOT NULL DEFAULT '', `tokenTime` datetime NOT NULL, + `oldTokenSN` varchar(50) NOT NULL DEFAULT '', `vsoft` varchar(30) NOT NULL DEFAULT '', `heartbeat` datetime NOT NULL, `zap` varchar(10) NOT NULL,