diff --git a/.travis.yml b/.travis.yml index 0cc9ad80e..87ba08836 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,8 +28,8 @@ install: - sudo add-apt-repository -y ppa:nginx/stable - sudo apt-get update - sudo apt-get install -y --force-yes nginx-extras - - travisCI/install-nginx.sh script: + - travis-ci/install-nginx.sh - sudo nginx -t &> /dev/stdout - curl -vsf 'http://localhost:8080/nginx.php' &> /dev/stdout - curl -A "googlebot" http://localhost:8080/nginx.php &> /dev/stdout diff --git a/travisCI/bad-referrer-words.tpl.conf b/travisCI/_OLD/bad-referrer-words.tpl.conf similarity index 100% rename from travisCI/bad-referrer-words.tpl.conf rename to travisCI/_OLD/bad-referrer-words.tpl.conf diff --git a/travisCI/blacklist-ips.tpl.conf b/travisCI/_OLD/blacklist-ips.tpl.conf similarity index 100% rename from travisCI/blacklist-ips.tpl.conf rename to travisCI/_OLD/blacklist-ips.tpl.conf diff --git a/travisCI/blacklist-user-agents.tpl.conf b/travisCI/_OLD/blacklist-user-agents.tpl.conf similarity index 100% rename from travisCI/blacklist-user-agents.tpl.conf rename to travisCI/_OLD/blacklist-user-agents.tpl.conf diff --git a/travisCI/blockbots.tpl.conf b/travisCI/_OLD/blockbots.tpl.conf similarity index 100% rename from travisCI/blockbots.tpl.conf rename to travisCI/_OLD/blockbots.tpl.conf diff --git a/travisCI/botblocker-nginx-settings.tpl.conf b/travisCI/_OLD/botblocker-nginx-settings.tpl.conf similarity index 100% rename from travisCI/botblocker-nginx-settings.tpl.conf rename to travisCI/_OLD/botblocker-nginx-settings.tpl.conf diff --git a/travisCI/custom-bad-referrers.tpl.conf b/travisCI/_OLD/custom-bad-referrers.tpl.conf similarity index 100% rename from travisCI/custom-bad-referrers.tpl.conf rename to travisCI/_OLD/custom-bad-referrers.tpl.conf diff --git a/travisCI/ddos.tpl.conf b/travisCI/_OLD/ddos.tpl.conf similarity index 100% rename from travisCI/ddos.tpl.conf rename to travisCI/_OLD/ddos.tpl.conf diff --git a/travisCI/default-site.tpl.conf b/travisCI/_OLD/default-site.tpl.conf similarity index 100% rename from travisCI/default-site.tpl.conf rename to travisCI/_OLD/default-site.tpl.conf diff --git a/travisCI/deploy.sh b/travisCI/_OLD/deploy.sh similarity index 100% rename from travisCI/deploy.sh rename to travisCI/_OLD/deploy.sh diff --git a/travisCI/fastcgi.tpl.conf b/travisCI/_OLD/fastcgi.tpl.conf similarity index 100% rename from travisCI/fastcgi.tpl.conf rename to travisCI/_OLD/fastcgi.tpl.conf diff --git a/travisCI/globalblacklist.tpl.conf b/travisCI/_OLD/globalblacklist.tpl.conf similarity index 100% rename from travisCI/globalblacklist.tpl.conf rename to travisCI/_OLD/globalblacklist.tpl.conf diff --git a/travisCI/_OLD/install-nginx.sh-old b/travisCI/_OLD/install-nginx.sh-old new file mode 100755 index 000000000..c94138ba2 --- /dev/null +++ b/travisCI/_OLD/install-nginx.sh-old @@ -0,0 +1,56 @@ +#!/bin/bash +# Travis CI Code to Configure Nginx +set -e +set -x + +DIR=$(realpath $(dirname "$0")) +USER=$(whoami) +PHP_VERSION=$(phpenv version-name) +ROOT=$(realpath "$DIR/..") +PORT=9000 +SERVER="/tmp/php.sock" + +function tpl { + sed \ + -e "s|{DIR}|$DIR|g" \ + -e "s|{USER}|$USER|g" \ + -e "s|{PHP_VERSION}|$PHP_VERSION|g" \ + -e "s|{ROOT}|$ROOT|g" \ + -e "s|{PORT}|$PORT|g" \ + -e "s|{SERVER}|$SERVER|g" \ + < $1 > $2 +} + +# Make some working directories. +mkdir "$DIR/nginx" +mkdir "$DIR/nginx/sites-enabled" +mkdir "$DIR/nginx/bots.d" +mkdir "$DIR/var" + +# Configure the PHP handler. + PHP_FPM_BIN="$HOME/.phpenv/versions/$PHP_VERSION/sbin/php-fpm" + PHP_FPM_CONF="$DIR/nginx/php-fpm.conf" + + # Build the php-fpm.conf. + tpl "$DIR/php-fpm.tpl.conf" "$PHP_FPM_CONF" + + # Start php-fpm + "$PHP_FPM_BIN" --fpm-config "$PHP_FPM_CONF" + +# Build the default nginx config files. +tpl "$DIR/nginx.tpl.conf" "$DIR/nginx/nginx.conf" +tpl "$DIR/fastcgi.tpl.conf" "$DIR/nginx/fastcgi.conf" +tpl "$DIR/ddos.tpl.conf" "$DIR/nginx/ddos.conf" +tpl "$DIR/blockbots.tpl.conf" "$DIR/nginx/blockbots.conf" +tpl "$DIR/whitelist-ips.tpl.conf" "$DIR/nginx/bots.d/whitelist-ips.conf" +tpl "$DIR/whitelist-domains.tpl.conf" "$DIR/nginx/bots.d/whitelist-domains.conf" +tpl "$DIR/blacklist-user-agents.tpl.conf" "$DIR/nginx/bots.d/blacklist-user-agents.conf" +tpl "$DIR/bad-referrer-words.tpl.conf" "$DIR/nginx/bots.d/bad-referrer-words.conf" +tpl "$DIR/custom-bad-referrers.tpl.conf" "$DIR/nginx/bots.d/custom-bad-referrers.conf" +tpl "$DIR/blacklist-ips.tpl.conf" "$DIR/nginx/bots.d/blacklist-ips.conf" +tpl "$DIR/botblocker-nginx-settings.tpl.conf" "$DIR/nginx/botblocker-nginx-settings.conf" +tpl "$DIR/globalblacklist.tpl.conf" "$DIR/nginx/globalblacklist.conf" +tpl "$DIR/default-site.tpl.conf" "$DIR/nginx/sites-enabled/default-site.conf" + +# Start nginx. +nginx -c "$DIR/nginx/nginx.conf" \ No newline at end of file diff --git a/travisCI/nginx.tpl.conf b/travisCI/_OLD/nginx.tpl.conf similarity index 100% rename from travisCI/nginx.tpl.conf rename to travisCI/_OLD/nginx.tpl.conf diff --git a/travisCI/php-fpm.tpl.conf b/travisCI/_OLD/php-fpm.tpl.conf similarity index 100% rename from travisCI/php-fpm.tpl.conf rename to travisCI/_OLD/php-fpm.tpl.conf diff --git a/travisCI/whitelist-domains.tpl.conf b/travisCI/_OLD/whitelist-domains.tpl.conf similarity index 100% rename from travisCI/whitelist-domains.tpl.conf rename to travisCI/_OLD/whitelist-domains.tpl.conf diff --git a/travisCI/whitelist-ips.tpl.conf b/travisCI/_OLD/whitelist-ips.tpl.conf similarity index 100% rename from travisCI/whitelist-ips.tpl.conf rename to travisCI/_OLD/whitelist-ips.tpl.conf diff --git a/www/nginx.php b/travisCI/_OLD/www/nginx.php similarity index 100% rename from www/nginx.php rename to travisCI/_OLD/www/nginx.php diff --git a/travisCI/change-file.sh b/travisCI/change-file.sh new file mode 100755 index 000000000..01919f941 --- /dev/null +++ b/travisCI/change-file.sh @@ -0,0 +1,31 @@ +#!/bin/bash +YEAR=$(date +"%Y") +MONTH=$(date +"%m") +cd $TRAVIS_BUILD_DIR + +#Remove Remotes Added by TravisCI +git remote rm origin + +#Add Remote with Secure Key +git remote add origin https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git + +#List Remotes ONLY DURING testing - do not do this on live repo / possible key leak +#git remote -v + +# Set Git Variables +git config --global user.email "${GIT_EMAIL}" +git config --global user.name "${GIT_NAME}" +git config --global push.default simple + +# Make sure we have master branch checked out in Git +git checkout master + +# Modify our file and make sure Travis is owner +sudo $TRAVIS_BUILD_DIR/travis-ci/modify-globalblacklist.sh +sudo chown -R travis:travis $TRAVIS_BUILD_DIR/* + +# Add the modified file to the and commit it +git add $TRAVIS_BUILD_DIR/globalblacklist.conf +git commit -am "V3.$YEAR.$MONTH.$TRAVIS_BUILD_NUMBER [ci skip]" + +# Travis now moves to the before_deploy: section of .travis.yml \ No newline at end of file diff --git a/travisCI/default.vhost b/travisCI/default.vhost new file mode 100644 index 000000000..4e68cb8c8 --- /dev/null +++ b/travisCI/default.vhost @@ -0,0 +1,11 @@ +server { + listen *:9000; + + root /var/www/html; + server_name localhost; + charset UTF-8; + + location / { + root /var/www/html/; + } +} diff --git a/travisCI/deploy-package.sh b/travisCI/deploy-package.sh new file mode 100755 index 000000000..24b397602 --- /dev/null +++ b/travisCI/deploy-package.sh @@ -0,0 +1,20 @@ +#!/bin/bash +YEAR=$(date +"%Y") +MONTH=$(date +"%m") + +# Make Sure we are in the Build Directory +cd $TRAVIS_BUILD_DIR + +# Create our Version Number +export GIT_TAG=V3.$YEAR-$MONTH.$TRAVIS_BUILD_NUMBER + +# Tag our release +git tag $GIT_TAG -a -m "V3.$YEAR.$MONTH.$TRAVIS_BUILD_NUMBER" + +# Push our commit and tags back to the repo +sudo git push origin master && git push origin master --tags + +# Uncomment to list all git folders and modified files etc +#ls -aR + +# Now TravisCI moves into the deploy: section of TravisCI - see .travis.yml diff --git a/travisCI/index.php b/travisCI/index.php new file mode 100644 index 000000000..1081dac02 --- /dev/null +++ b/travisCI/index.php @@ -0,0 +1,5 @@ + $2 -} +# Start Getting Nginx Ready for Testing the Nginx Bad Bot Blocker -# Make some working directories. -mkdir "$DIR/nginx" -mkdir "$DIR/nginx/sites-enabled" -mkdir "$DIR/nginx/bots.d" -mkdir "$DIR/var" +# Delete default site created by Nginx Installation +sudo rm /etc/nginx/sites-available/default -# Configure the PHP handler. - PHP_FPM_BIN="$HOME/.phpenv/versions/$PHP_VERSION/sbin/php-fpm" - PHP_FPM_CONF="$DIR/nginx/php-fpm.conf" +# Download the Nginx Bad Bot Blocker files from the Live Repository +sudo wget https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master/install-ngxblocker -O /usr/sbin/install-ngxblocker +sudo wget https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master/setup-ngxblocker -O /usr/sbin/setup-ngxblocker +sudo wget https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master/update-ngxblocker -O /usr/sbin/update-ngxblocker - # Build the php-fpm.conf. - tpl "$DIR/php-fpm.tpl.conf" "$PHP_FPM_CONF" +# Set our install and setup scripts to be executable +sudo chmod +x /usr/sbin/install-ngxblocker +sudo chmod +x /usr/sbin/setup-ngxblocker +sudo chmod +x /usr/sbin/update-ngxblocker - # Start php-fpm - "$PHP_FPM_BIN" --fpm-config "$PHP_FPM_CONF" +# Run Install-NgxBlocker +cd /usr/sbin +sudo ./install-ngxblocker -x -# Build the default nginx config files. -tpl "$DIR/nginx.tpl.conf" "$DIR/nginx/nginx.conf" -tpl "$DIR/fastcgi.tpl.conf" "$DIR/nginx/fastcgi.conf" -tpl "$DIR/ddos.tpl.conf" "$DIR/nginx/ddos.conf" -tpl "$DIR/blockbots.tpl.conf" "$DIR/nginx/blockbots.conf" -tpl "$DIR/whitelist-ips.tpl.conf" "$DIR/nginx/bots.d/whitelist-ips.conf" -tpl "$DIR/whitelist-domains.tpl.conf" "$DIR/nginx/bots.d/whitelist-domains.conf" -tpl "$DIR/blacklist-user-agents.tpl.conf" "$DIR/nginx/bots.d/blacklist-user-agents.conf" -tpl "$DIR/bad-referrer-words.tpl.conf" "$DIR/nginx/bots.d/bad-referrer-words.conf" -tpl "$DIR/custom-bad-referrers.tpl.conf" "$DIR/nginx/bots.d/custom-bad-referrers.conf" -tpl "$DIR/blacklist-ips.tpl.conf" "$DIR/nginx/bots.d/blacklist-ips.conf" -tpl "$DIR/botblocker-nginx-settings.tpl.conf" "$DIR/nginx/botblocker-nginx-settings.conf" -tpl "$DIR/globalblacklist.tpl.conf" "$DIR/nginx/globalblacklist.conf" -tpl "$DIR/default-site.tpl.conf" "$DIR/nginx/sites-enabled/default-site.conf" +# Copy our default.vhost file into Nginx /sites-available/ +sudo cp $TRAVIS_BUILD_DIR/travis-ci/default.vhost /etc/nginx/sites-available/default.vhost -# Start nginx. -nginx -c "$DIR/nginx/nginx.conf" \ No newline at end of file +# Link the vhost file into Nginx /sites-enabled/ and reload nginx +sudo ln -s /etc/nginx/sites-available/default.vhost /etc/nginx/sites-enabled/default.vhost +#sudo service nginx reload + +# Run setup-ngxblocker +cd /usr/sbin +sudo ./setup-ngxblocker -x + +# NOTE: for Verbose Testing of any shell scripts use below format adding sh -x before running the script +# this helps a lot inside the TravisCI environment to see where a shell script may be failing +#sudo sh -x ./setup-ngxblocker -x + +# Load our Nginx.conf file and reload Nginx +sudo nginx -c /etc/nginx/nginx.conf +#sudo service nginx reload + +# Copy our index.php file into the default site's root folder +sudo cp $TRAVIS_BUILD_DIR/travis-ci/index.php /var/www/html/index.php + +# Run update-ngxblocker test +cd /usr/sbin +sudo ./update-ngxblocker + +# Reload nginx - no need to do this as update-ngxblocker does this for us +#sudo service nginx reload + +# Set all our other setup and deploy scripts to be executable +sudo chmod +x $TRAVIS_BUILD_DIR/travis-ci/modify-globalblacklist.sh +sudo chmod +x $TRAVIS_BUILD_DIR/travis-ci/deploy-package.sh +sudo chmod +x $TRAVIS_BUILD_DIR/travis-ci/change-file.sh + +# Travis now goes into the rest of the tests in the script: section of .travis.yml \ No newline at end of file diff --git a/travisCI/modify-globalblacklist.sh b/travisCI/modify-globalblacklist.sh new file mode 100755 index 000000000..d437a7b46 --- /dev/null +++ b/travisCI/modify-globalblacklist.sh @@ -0,0 +1,81 @@ +#!/bin/bash +# Write Build Number into globalblacklist.conf +# Created by: https://github.com/mitchellkrogza (mitchellkrog@gmail.com) +# Copyright: Mitchell Krog - https://github.com/mitchellkrogza + +# Start time of script generation +start=$(date +%s.%N) +versionyear=$(date +%Y) +versionmonth=$(date +%m) +MY_GIT_TAG=V3.$versionyear.$versionmonth.$TRAVIS_BUILD_NUMBER + +# Temporary database files we create +_inputdbA=/tmp/lastupdated.db + +# Declare Nginx template and temp variables +#_nginx=/home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/conf.d/globalblacklist.conf +#_nginx2=/home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/Engintron_for_cPanel_WHM_Configuration_Example/etc/nginx/conf.d/globalblacklist.conf + + +_tmpnginxA=tmpnginxA +_tmpnginxB=tmpnginxB + +# Start and End Strings to Search for to do inserts into template +_startmarker="### Version Information #" +_endmarker="### Version Information ##" + +# PRINT VERSION INFORMATION INTO GLOBALBLACKLIST FILE 1 +# ***************************************************** +LASTUPDATEIFS=$IFS +IFS=$'\n' +now="$(date)" +end=$(date +%s.%N) +echo $_startmarker >> $_tmpnginxA +runtime=$(python -c "print(${end} - ${start})") +printf "############################################\n### Version: "$MY_GIT_TAG"\n### Updated: "$now"\n### Generated In: "$runtime" seconds\n############################################\n" >> $_tmpnginxA +echo $_endmarker >> $_tmpnginxA +IFS=$LASTUPDATEIFS +mv $_tmpnginxA $_inputdbA +ed -s $_inputdbA<<\IN +1,/### Version Information #/d +/### Version Information ##/,$d +,d +.r /home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/conf.d/globalblacklist.conf +/### Version Information #/x +.t. +.,/### Version Information ##/-d +#,p +#,p used to print output replaced with w below to write +w /home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/conf.d/globalblacklist.conf +q +IN +rm $_inputdbA + +# PRINT VERSION INFORMATION INTO GLOBALBLACKLIST FILE 2 +# ***************************************************** +LASTUPDATE2IFS=$IFS +IFS=$'\n' +now="$(date)" +end=$(date +%s.%N) +echo $_startmarker >> $_tmpnginxB +runtime=$(python -c "print(${end} - ${start})") +printf "############################################\n### Version: "$MY_GIT_TAG"\n### Updated: "$now"\n### Generated In: "$runtime" seconds\n############################################\n" >> $_tmpnginxB +echo $_endmarker >> $_tmpnginxB +IFS=$LASTUPDATE2IFS +mv $_tmpnginxB $_inputdbA +ed -s $_inputdbA<<\IN +1,/### Version Information #/d +/### Version Information ##/,$d +,d +.r /home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/Engintron_for_cPanel_WHM_Configuration_Example/etc/nginx/conf.d/globalblacklist.conf +/### Version Information #/x +.t. +.,/### Version Information ##/-d +#,p +#,p used to print output replaced with w below to write +w /home/travis/build/mitchellkrogza/nginx-ultimate-bad-bot-blocker/Engintron_for_cPanel_WHM_Configuration_Example/etc/nginx/conf.d/globalblacklist.conf +q +IN +rm $_inputdbA + +exit 0