Changed ping from socket to exec function

As there are a lot of problems with permissions and ping, the function now uses exec(). Resolved #405, #576, #614, #748.

Co-Authored-By: Sean Perryman <se@nperryman.com>
Co-Authored-By: Luke C <crawford.luke@outlook.com>
pull/812/merge
TimZ99 2020-01-14 19:59:08 +01:00
parent d7536d209e
commit 6ba4b59239
No known key found for this signature in database
GPG Key ID: 4D8268DC68E8339D
1 changed files with 21 additions and 17 deletions

View File

@ -165,40 +165,44 @@ class StatusUpdater
} }
/** /**
* Check the current servers ping status - Code from http://stackoverflow.com/a/20467492 * Check the current servers ping status
* @param int $max_runs * @param int $max_runs
* @param int $run * @param int $run
* @return boolean * @return boolean
*/ */
protected function updatePing($max_runs, $run = 1) protected function updatePing($max_runs, $run = 1)
{ {
// save response time if ($max_runs == null || $max_runs > 1) {
$starttime = microtime(true); $max_runs = 1;
// set ping payload }
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost"; $txt = exec("ping -c " . $max_runs . " " . $this->server['ip']);
// Non-greedy match on filler
$re1 = '.*?';
// Uninteresting: float
$re2 = '[+-]?\\d*\\.\\d+(?![-+0-9\\.])';
// Non-greedy match on filler
$re3 = '.*?';
// Float 1
$re4 = '([+-]?\\d*\\.\\d+)(?![-+0-9\\.])';
$socket = socket_create(AF_INET, SOCK_RAW, 1); if ($c = preg_match_all("/" . $re1 . $re2 . $re3 . $re4 . "/is", $txt, $matches)) {
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0)); $result = $matches[1][0];
socket_connect($socket, $this->server['ip'], null); } else {
$result = null;
}
socket_send($socket, $package, strLen($package), 0); if (!is_null($result)) {
if (socket_read($socket, 255)) {
$status = true; $status = true;
} else { } else {
$status = false; $status = false;
// set error message
$errorcode = socket_last_error();
$this->error = "Couldn't create socket [" . $errorcode . "]: " . socket_strerror($errorcode);
} }
$this->rtime = microtime(true) - $starttime; //Divide by a thousand to convert to milliseconds
socket_close($socket); $this->rtime = $result / 1000;
// check if server is available and rerun if asked. // check if server is available and rerun if asked.
if (!$status && $run < $max_runs) { if (!$status && $run < $max_runs) {
return $this->updatePing($max_runs, $run + 1); return $this->updatePing($max_runs, $run + 1);
} }
return $status; return $status;
} }