Ipv6 and small updates

More ipv6 check and functions
Small update to servercontroller
pull/92/head
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_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_URL, $href); curl_setopt($ch, CURLOPT_URL, $href);
if($add_agent) { if($add_agent) {
curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; phpservermon/'.PSM_VERSION.'; +http://www.phpservermonitor.org)'); curl_setopt ($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; phpservermon/'.PSM_VERSION.'; +http://www.phpservermonitor.org)');
} }
@ -536,7 +537,9 @@ function psm_is_cli() {
* @return boolean * @return boolean
*/ */
function psm_validate_ipv6($ip) { 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)){ if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
return true; return true;
} else { } else {

View File

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

View File

@ -82,7 +82,7 @@ class StatusUpdater {
$this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array( $this->server = $this->db->selectRow(PSM_DB_PREFIX . 'servers', array(
'server_id' => $server_id, 'server_id' => $server_id,
), array( ), 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)) { if(empty($this->server)) {
return false; return false;
@ -171,10 +171,22 @@ class StatusUpdater {
protected function updateWebsite($max_runs, $run = 1) { protected function updateWebsite($max_runs, $run = 1) {
$starttime = microtime(true); $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! // We're only interested in the header, because that should tell us plenty!
// unless we have a pattern to search for! // unless we have a pattern to search for!
$curl_result = psm_curl_get( $curl_result = psm_curl_get(
$this->server['ip'], $this->server['ip'].':'.$this->server['port'],
true, true,
($this->server['pattern'] == '' ? false : true) ($this->server['pattern'] == '' ? false : true)
); );
@ -235,21 +247,23 @@ class StatusUpdater {
// save response time // save response time
$starttime = microtime(true); $starttime = microtime(true);
/* Only run if is cron /**
* Only run if is cron
* socket_create() need to run as root :( * socket_create() need to run as root :(
* ugly cli hack i know * ugly cli hack i know
* might be a better way still have not found a solution when updating true website * might be a better way still have not found a solution when updating true website
*/ */
//if(psm_is_cli()) { if(psm_is_cli()) {
// IPv6 ready // if ipv6 we have to use AF_INET6
if (psm_validate_ipv6($this->server['ip'])) { 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); $socket = socket_create(AF_INET6, SOCK_RAW, 1);
} else { } else {
$socket = socket_create(AF_INET, SOCK_RAW, 1); $socket = socket_create(AF_INET, SOCK_RAW, 1);
} }
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0)); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
socket_connect($socket, $this->server['ip'], null); socket_connect($socket, $this->server['ip'], null);
socket_send($socket, $package, strLen($package), 0); socket_send($socket, $package, strLen($package), 0);
@ -266,7 +280,14 @@ class StatusUpdater {
} }
return $status; 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;
}
} }
/** /**