From 0afeefd9644b4ed32f74bb2f0cc52fc2d47f2e79 Mon Sep 17 00:00:00 2001 From: cytopia Date: Sun, 7 May 2017 15:51:12 +0200 Subject: [PATCH] REL-0.9 Fixes #9 #10 DNS checks and custom domain suffix --- .../etc/apache-2.2/01-vhost-default.conf | 4 +- .devilbox/etc/apache-2.2/02-vhost-mass.conf | 4 ++ .../etc/apache-2.4/01-vhost-default.conf | 4 +- .devilbox/etc/apache-2.4/02-vhost-mass.conf | 4 ++ .devilbox/etc/nginx-mainline/00-defaults.conf | 3 -- .../etc/nginx-mainline/01-vhost-default.conf | 8 +-- .../etc/nginx-mainline/02-vhost-mass.conf | 6 +++ .devilbox/etc/nginx-stable/00-defaults.conf | 3 -- .../etc/nginx-stable/01-vhost-default.conf | 8 +-- .devilbox/etc/nginx-stable/02-vhost-mass.conf | 6 +++ .devilbox/www/htdocs/index.php | 4 +- .devilbox/www/htdocs/vhosts.php | 50 ++++++++++++++++--- .devilbox/www/include/lib/Docker.php | 30 ++--------- .devilbox/www/include/lib/Php.php | 4 ++ docker-compose.yml | 6 +++ env-example | 32 ++++++++++++ 16 files changed, 124 insertions(+), 52 deletions(-) diff --git a/.devilbox/etc/apache-2.2/01-vhost-default.conf b/.devilbox/etc/apache-2.2/01-vhost-default.conf index d0b297c4..ce722d1e 100644 --- a/.devilbox/etc/apache-2.2/01-vhost-default.conf +++ b/.devilbox/etc/apache-2.2/01-vhost-default.conf @@ -11,8 +11,8 @@ NameVirtualHost *:80 ServerAdmin root@localhost - ErrorLog /var/log/apache-2.2/localhost-error.log - CustomLog /var/log/apache-2.2/localhost-access.log combined + ErrorLog /var/log/apache-2.2/devilbox-error.log + CustomLog /var/log/apache-2.2/devilbox-access.log combined DirectoryIndex index.php index.html diff --git a/.devilbox/etc/apache-2.2/02-vhost-mass.conf b/.devilbox/etc/apache-2.2/02-vhost-mass.conf index 6e809cd2..c18ac2dc 100644 --- a/.devilbox/etc/apache-2.2/02-vhost-mass.conf +++ b/.devilbox/etc/apache-2.2/02-vhost-mass.conf @@ -11,6 +11,10 @@ ServerAlias *.* ServerAdmin root@localhost + # Required for DNS checking + SetEnvIf Origin "http(s)?://(domain1.org|localhost|127.0.0.1|httpd)$" AccessControlAllowOrigin=$0$1 + Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin + LogFormat "%V;%h;%l;%u;%t;\"%r\";%>s;%b;\"%{Referer}i\";\"%{User-agent}i\"" vcommon CustomLog "/var/log/apache-2.2/projects-access.log" vcommon ErrorLog /var/log/apache-2.2/projects-error.log diff --git a/.devilbox/etc/apache-2.4/01-vhost-default.conf b/.devilbox/etc/apache-2.4/01-vhost-default.conf index d607737f..0a899c9b 100644 --- a/.devilbox/etc/apache-2.4/01-vhost-default.conf +++ b/.devilbox/etc/apache-2.4/01-vhost-default.conf @@ -11,8 +11,8 @@ ServerAdmin root@localhost - ErrorLog /var/log/apache-2.4/localhost-error.log - CustomLog /var/log/apache-2.4/localhost-access.log combined + ErrorLog /var/log/apache-2.4/devilbox-error.log + CustomLog /var/log/apache-2.4/devilbox-access.log combined DirectoryIndex index.php index.html diff --git a/.devilbox/etc/apache-2.4/02-vhost-mass.conf b/.devilbox/etc/apache-2.4/02-vhost-mass.conf index a797d622..c8fe32bb 100644 --- a/.devilbox/etc/apache-2.4/02-vhost-mass.conf +++ b/.devilbox/etc/apache-2.4/02-vhost-mass.conf @@ -11,6 +11,10 @@ ServerAlias *.* ServerAdmin root@localhost + # Required for DNS checking + SetEnvIf Origin "http(s)?://(domain1.org|localhost|127.0.0.1|httpd)$" AccessControlAllowOrigin=$0$1 + Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin + LogFormat "%V;%h;%l;%u;%t;\"%r\";%>s;%b;\"%{Referer}i\";\"%{User-agent}i\"" vcommon CustomLog "/var/log/apache-2.4/projects-access.log" vcommon ErrorLog /var/log/apache-2.4/projects-error.log diff --git a/.devilbox/etc/nginx-mainline/00-defaults.conf b/.devilbox/etc/nginx-mainline/00-defaults.conf index bd16e922..f4c3c90f 100644 --- a/.devilbox/etc/nginx-mainline/00-defaults.conf +++ b/.devilbox/etc/nginx-mainline/00-defaults.conf @@ -1,6 +1,3 @@ -access_log /var/log/nginx-mainline/access.log main; -error_log /var/log/nginx-mainline/error.log warn; - charset utf-8; send_timeout 60; diff --git a/.devilbox/etc/nginx-mainline/01-vhost-default.conf b/.devilbox/etc/nginx-mainline/01-vhost-default.conf index 2dc74075..1296b88c 100644 --- a/.devilbox/etc/nginx-mainline/01-vhost-default.conf +++ b/.devilbox/etc/nginx-mainline/01-vhost-default.conf @@ -6,17 +6,17 @@ server { listen 80 default_server; server_name _; server_name localhost; - server_name 127\.0\.0\.1; + server_name 127.0.0.1; server_name httpd; - server_name 172\.16\.238\..*; + server_name 172.16.238.*; # Root directive root /var/www/default/htdocs; index index.php; - access_log /var/log/nginx-mainline/localhost-access.log main; - error_log /var/log/nginx-mainline/localhost-error.log warn; + access_log /var/log/nginx-mainline/devilbox-access.log main; + error_log /var/log/nginx-mainline/devilbox-error.log warn; # Front-controller pattern as recommended by the nginx docs diff --git a/.devilbox/etc/nginx-mainline/02-vhost-mass.conf b/.devilbox/etc/nginx-mainline/02-vhost-mass.conf index e9ae4629..5de8e293 100644 --- a/.devilbox/etc/nginx-mainline/02-vhost-mass.conf +++ b/.devilbox/etc/nginx-mainline/02-vhost-mass.conf @@ -21,6 +21,12 @@ server { # PHP FPM location ~ \.php?$ { + + # Required for DNS checking + if ( $http_origin ~* (https?://(localhost|127\.0\.0\.1|httpd)$) ) { + add_header "Access-Control-Allow-Origin" "$http_origin"; + } + # PHP.INI: # --------------------------- # // Find this: diff --git a/.devilbox/etc/nginx-stable/00-defaults.conf b/.devilbox/etc/nginx-stable/00-defaults.conf index 5ac4551b..f4c3c90f 100644 --- a/.devilbox/etc/nginx-stable/00-defaults.conf +++ b/.devilbox/etc/nginx-stable/00-defaults.conf @@ -1,6 +1,3 @@ -access_log /var/log/nginx-stable/access.log main; -error_log /var/log/nginx-stable/error.log warn; - charset utf-8; send_timeout 60; diff --git a/.devilbox/etc/nginx-stable/01-vhost-default.conf b/.devilbox/etc/nginx-stable/01-vhost-default.conf index ad9f45a9..1fb0720f 100644 --- a/.devilbox/etc/nginx-stable/01-vhost-default.conf +++ b/.devilbox/etc/nginx-stable/01-vhost-default.conf @@ -6,16 +6,16 @@ server { listen 80 default_server; server_name _; server_name localhost; - server_name 127\.0\.0\.1; + server_name 127.0.0.1; server_name httpd; - server_name 172\.16\.238\..*; + server_name 172.16.238.*; # Root directive root /var/www/default/htdocs; index index.php; - access_log /var/log/nginx-stable/localhost-access.log main; - error_log /var/log/nginx-stable/localhost-error.log warn; + access_log /var/log/nginx-stable/devilbox-access.log main; + error_log /var/log/nginx-stable/devilbox-error.log warn; # Front-controller pattern as recommended by the nginx docs diff --git a/.devilbox/etc/nginx-stable/02-vhost-mass.conf b/.devilbox/etc/nginx-stable/02-vhost-mass.conf index a237a137..d4625c57 100644 --- a/.devilbox/etc/nginx-stable/02-vhost-mass.conf +++ b/.devilbox/etc/nginx-stable/02-vhost-mass.conf @@ -21,6 +21,12 @@ server { # PHP FPM location ~ \.php?$ { + + # Required for DNS checking + if ( $http_origin ~* (https?://(localhost|127\.0\.0\.1|httpd)$) ) { + add_header "Access-Control-Allow-Origin" "$http_origin"; + } + # PHP.INI: # --------------------------- # // Find this: diff --git a/.devilbox/www/htdocs/index.php b/.devilbox/www/htdocs/index.php index d6d4161f..9bbf8c19 100644 --- a/.devilbox/www/htdocs/index.php +++ b/.devilbox/www/htdocs/index.php @@ -53,7 +53,7 @@ $connection['Httpd'][$host] = array( 'host' => $host, 'succ' => $succ ); -$host = 'random.loc'; +$host = 'random.'.loadClass('Php')->getTldSuffix(); $succ = \devilbox\Httpd::testConnection($error, $host); $connection['Httpd'][$host] = array( 'error' => $error, @@ -406,7 +406,7 @@ function getCirle($name) { vHost TLD - *.loc + *.getTldSuffix(); ?> DNS diff --git a/.devilbox/www/htdocs/vhosts.php b/.devilbox/www/htdocs/vhosts.php index 6190d45e..3c1c866e 100644 --- a/.devilbox/www/htdocs/vhosts.php +++ b/.devilbox/www/htdocs/vhosts.php @@ -39,7 +39,7 @@ - getEnv('HOST_PATH_TO_WWW_DOCROOTS');?>//htdocs + getEnv('HOST_PATH_HTTPD_DATADIR');?>//htdocs     @@ -49,8 +49,8 @@

No projects here.

-

Simply create a folder in getEnv('HOST_PATH_TO_WWW_DOCROOTS');?> (on your host computer - not inside the docker).

-

Example:
getEnv('HOST_PATH_TO_WWW_DOCROOTS');?>/my_project

+

Simply create a folder in getEnv('HOST_PATH_HTTPD_DATADIR');?> (on your host computer - not inside the docker).

+

Example:
getEnv('HOST_PATH_HTTPD_DATADIR');?>/my_project

@@ -83,9 +83,7 @@ el_valid.innerHTML = 'ERR'; el_href.innerHTML = error; } else { - el_valid.className += ' bg-success'; - el_valid.innerHTML = 'OK'; - el_href.innerHTML = ''+vhost+'.getTld().$Docker->getPort();?>'; + checkDns(vhost); } } }; @@ -93,6 +91,46 @@ xhttp.send(); } + /** + * Check if DNS record is set in /etc/hosts (or via attached DNS server) + * for TLD_SUFFIX + */ + function checkDns(vhost) { + var xhttp = new XMLHttpRequest(); + // Timeout after 1 seconds and mark it invalid DNS + xhttp.timeout = getEnv('DNS_CHECK_TIMEOUT');?>000; + + var el_valid = document.getElementById('valid-' + vhost); + var el_href = document.getElementById('href-' + vhost); + var error = this.responseText; + + xhttp.onreadystatechange = function(e) { + if (this.readyState == 4 && this.status == 200) { + //clearTimeout(xmlHttpTimeout); + el_valid.className += ' bg-success'; + el_valid.innerHTML = 'OK'; + el_href.innerHTML = ''+vhost+'.getTldSuffix().$Docker->getPort();?>'; + } + } + xhttp.ontimeout = function(e) { + el_valid.className += ' bg-danger'; + el_valid.innerHTML = 'ERR'; + el_href.innerHTML = 'No DNS record found: 127.0.0.1 '+vhost+'.getTldSuffix();?>'; + } + //xhttp.abort = function(e) { + // el_valid.className += ' bg-danger'; + // el_valid.innerHTML = 'ERR'; + // el_href.innerHTML = 'No DNS record found: 127.0.0.1 '+vhost+'.getTldSuffix();?>'; + //} + xhttp.open('GET', 'http://'+vhost+'.getTldSuffix();?>', true); + xhttp.send(); + // Timeout to abort in 1 second + //var xmlHttpTimeout=setTimeout(ajaxTimeout,20000); + //function ajaxTimeout(){ + // xhttp.abort(); + //} + } + var vhosts = document.getElementsByName('vhost[]'); diff --git a/.devilbox/www/include/lib/Docker.php b/.devilbox/www/include/lib/Docker.php index 3df5db97..94a82790 100644 --- a/.devilbox/www/include/lib/Docker.php +++ b/.devilbox/www/include/lib/Docker.php @@ -47,13 +47,6 @@ class Docker */ private $_env = array(); - /** - * Domain suffix. - * - * @var string - */ - private $_tld = 'loc'; - /** * Document root path in PHP docker @@ -83,12 +76,6 @@ class Docker $tmp = explode('=', $var); $this->_env[$tmp[0]] = $tmp[1]; } - - // Set the TLD suffix (domain ending) for virtual hosts - // Note: If this is changed it currently also needs to be changed - // in each webserver's configuration file in .devilbox//02-vhost-mass.conf - $this->_tld = $GLOBALS['TLD_SUFFIX']; - } @@ -113,15 +100,6 @@ class Docker return $this->_env[$variable]; } - /** - * Get tld suffix. - * - * @return string - */ - public function getTld() - { - return $this->_tld; - } /** * Get HTTP port. @@ -227,8 +205,8 @@ class Docker $vhosts[] = array( 'name' => $directory, - 'domain' => $directory .'.' . $this->_tld, - 'href' => 'http://' . $directory . '.' . $this->_tld + 'domain' => $directory .'.' . $this->getEnv('TLD_SUFFIX'), + 'href' => 'http://' . $directory . '.' . $this->getEnv('TLD_SUFFIX') ); } } @@ -246,14 +224,14 @@ class Docker { $docRoot = $this->_doc_root; $htdocs = $docRoot . DIRECTORY_SEPARATOR . $vhost . DIRECTORY_SEPARATOR . 'htdocs'; - $domain = $vhost . '.' . $this->_tld; + $domain = $vhost . '.' . $this->getEnv('TLD_SUFFIX'); $url = 'http://'.$domain; $error = array(); // 1. Check htdocs folder if (!$this->_is_valid_dir($htdocs)) { - $error[] = 'Missing htdocs directory in: '.$this->getEnv('HOST_PATH_TO_WWW_DOCROOTS').'/'.$vhost.'/'; + $error[] = 'Missing htdocs directory in: '.$this->getEnv('HOST_PATH_HTTPD_DATADIR').'/'.$vhost.'/'; } if ($GLOBALS['ENABLE_VHOST_DNS_CHECK']) { diff --git a/.devilbox/www/include/lib/Php.php b/.devilbox/www/include/lib/Php.php index 7a979419..b79bb9c0 100644 --- a/.devilbox/www/include/lib/Php.php +++ b/.devilbox/www/include/lib/Php.php @@ -151,6 +151,10 @@ class Php extends _Base implements _iBase return $this->egrep('/[0-9.]+/', isset($output[0]) ? $output[0] : ''); } + public function getTldSuffix() + { + return getenv('TLD_SUFFIX'); + } /********************************************************************************* diff --git a/docker-compose.yml b/docker-compose.yml index c7e7033b..0effc7e6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -57,6 +57,12 @@ services: ## - TIMEZONE + ## + ## TLD SUffix + ## + - TLD_SUFFIX=${TLD_SUFFIX:-loc} + - DNS_CHECK_TIMEOUT=${DNS_CHECK_TIMEOUT:-1} + ## ## PHP Xdebug ## diff --git a/env-example b/env-example index 0401d2c6..9ecaf85d 100644 --- a/env-example +++ b/env-example @@ -47,6 +47,38 @@ DEVILBOX_PATH=. LOCAL_LISTEN_ADDR=127.0.0.1: +### +### This is the domain suffix your projects will be made available +### with mass-virtual-hosting. +### It is also required for the internal DNS server to be setup. +### +### Note: Only ALPHA ([a-zA-Z]+) characters are supported. +### +### Example: +### TLD_SUFFIX=loc +### Makes your project available under xxxx.loc +### +### Example: +### TLD_SUFFIX=local +### Makes your project available under xxxx.local +### +TLD_SUFFIX=loc + + +### +### TLD_SUFFIX domains are checked if they are set in the +### host computer /etc/hosts or available via attached DNS server. +### Timeout is done on vhosts.php (intranet) via ajax calls. +### In order to keep performance, set this to a low value. +### DNS checks might not succeed in time on slow machines. +### If DNS is valid, but timeout is expired, set this to a higher value. +### +### DNS_CHECK_TIMEOUT value is how many seconds to time out +### Default is to timeout after 1 second (DNS_CHECK_TIMEOUT=1) +### +DNS_CHECK_TIMEOUT=1 + + ################################################################################ ###