Ipv6 and small updates

More ipv6 check and functions
Small update to servercontroller
This commit is contained in:
michael@ilovecode.dk 2014-04-27 09:46:54 +02:00
parent ce5e1d8bc6
commit 11907c7047
3 changed files with 44 additions and 18 deletions

View File

@ -277,6 +277,7 @@ function psm_curl_get($href, $header = false, $body = true, $timeout = 10, $add_
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_URL, $href);
if($add_agent) {
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; phpservermon/'.PSM_VERSION.'; +http://www.phpservermonitor.org)');
}
@ -536,9 +537,11 @@ function psm_is_cli() {
* @return boolean
*/
function psm_validate_ipv6($ip) {
// if $ip is a valid ipv6 address it returns true
// Need to remove [] on ipv6 address before we can test
$ip = trim($ip, '[]');
if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
return true;
return true;
} else {
return false;
}

View File

@ -108,7 +108,7 @@ class ServerController extends AbstractServerController {
$servers[$x]['type_icon'] = 'icon-globe';
// add link to label
$ip = $servers[$x]['ip'];
if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80)) {
if(!empty($servers[$x]['port']) && ($servers[$x]['port'] != 80) && ($servers[$x]['port'] != 443)) {
$ip .= ' : ' . $servers[$x]['port'];
}
$servers[$x]['ip'] = '<a href="'.$servers[$x]['ip'].'" target="_blank">'.$ip.'</a>';
@ -215,10 +215,12 @@ class ServerController extends AbstractServerController {
'email' => in_array($_POST['email'], array('yes', 'no')) ? $_POST['email'] : 'no',
'sms' => in_array($_POST['sms'], array('yes', 'no')) ? $_POST['sms'] : 'no',
);
// make sure websites start with http://
if($clean['type'] == 'website' && substr($clean['ip'], 0, 4) != 'http') {
$clean['ip'] = 'http://' . $clean['ip'];
// Make sure websites start with http:// or https:// if port is 443
if($clean['type'] == 'website' && !preg_match('#^http(s)?://#', $clean['ip'])) {
$clean['ip'] = ($clean['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip'];
}
// check for edit or add
if($this->server_id > 0) {

View File

@ -82,7 +82,7 @@ class StatusUpdater {
$this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array(
'server_id' => $server_id,
), array(
'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status', 'active', 'warning_threshold', 'warning_threshold_counter',
'server_id', 'ip', 'port', 'label', 'type', 'pattern', 'status','rtime', 'active', 'warning_threshold', 'warning_threshold_counter',
));
if(empty($this->server)) {
return false;
@ -146,7 +146,7 @@ class StatusUpdater {
$errno = 0;
// save response time
$starttime = microtime(true);
$fp = fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10);
$status = ($fp === false) ? false : true;
@ -171,10 +171,22 @@ class StatusUpdater {
protected function updateWebsite($max_runs, $run = 1) {
$starttime = microtime(true);
// Removes http(s) from ip address so we can test if its an ipv6 adress
$clean['ip'] = preg_replace('#^http(s)?://#i', '', rtrim($this->server['ip'],'/')); // removes http(s) from ip address
// Test if ip is ipv6 or ipv4
if (psm_validate_ipv6($clean['ip'])) {
$clean['ip'] = '['. $clean['ip'] .']';
}
// Add http(s) again
$this->server['ip'] = ($this->server['port'] == 443 ? 'https' : 'http') . '://' . $clean['ip'];
// We're only interested in the header, because that should tell us plenty!
// unless we have a pattern to search for!
$curl_result = psm_curl_get(
$this->server['ip'],
$this->server['ip'].':'.$this->server['port'],
true,
($this->server['pattern'] == '' ? false : true)
);
@ -235,21 +247,23 @@ class StatusUpdater {
// save response time
$starttime = microtime(true);
/* Only run if is cron
* socket_create() need to run as root :(
* ugly cli hack i know
* might be a better way still have not found a solution when updating true website
/**
* Only run if is cron
* socket_create() need to run as root :(
* ugly cli hack i know
* might be a better way still have not found a solution when updating true website
*/
//if(psm_is_cli()) {
// IPv6 ready
if(psm_is_cli()) {
// if ipv6 we have to use AF_INET6
if (psm_validate_ipv6($this->server['ip'])) {
// Need to remove [] on ipv6 address
$this->server['ip'] = trim($this->server['ip'], '[]');
$socket = socket_create(AF_INET6, SOCK_RAW, 1);
} else {
$socket = socket_create(AF_INET, SOCK_RAW, 1);
}
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $this->server['ip'], null);
socket_send($socket, $package, strLen($package), 0);
@ -266,7 +280,14 @@ class StatusUpdater {
}
return $status;
//}
// If state on last update was 'on' and the update request is comming from the website
} elseif ($this->server['status'] == 'on') {
// need to set rtime to the value from last update, if not the latency will be 0
$this->rtime = $this->server['rtime'];
return true;
} else {
return false;
}
}
/**