1#!/bin/bash
2# Curl Testing Script for Nginx Ultimate Bad Bot Blocker
3# Created by: Mitchell Krog (mitchellkrog@gmail.com)
4# Copyright: Mitchell Krog - https://github.com/mitchellkrogza
5# Repo Url: https://github.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker
6
7##############################################################################
8#       _  __     _                                                          #
9#      / |/ /__ _(_)__ __ __                                                 #
10#     /    / _ `/ / _ \\ \ /                                                 #
11#    /_/|_/\_, /_/_//_/_\_\                                                  #
12#       __/___/      __   ___       __     ___  __         __                #
13#      / _ )___ ____/ /  / _ )___  / /_   / _ )/ /__  ____/ /_____ ____      #
14#     / _  / _ `/ _  /  / _  / _ \/ __/  / _  / / _ \/ __/  '_/ -_) __/      #
15#    /____/\_,_/\_,_/  /____/\___/\__/  /____/_/\___/\__/_/\_\\__/_/         #
16#                                                                            #
17##############################################################################
18
19# ------------------------------------------------------------------------------
20# MIT License
21# ------------------------------------------------------------------------------
22# Copyright (c) 2017 Mitchell Krog - mitchellkrog@gmail.com
23# https://github.com/mitchellkrogza
24# ------------------------------------------------------------------------------
25# Permission is hereby granted, free of charge, to any person obtaining a copy
26# of this software and associated documentation files (the "Software"), to deal
27# in the Software without restriction, including without limitation the rights
28# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29# copies of the Software, and to permit persons to whom the Software is
30# furnished to do so, subject to the following conditions:
31# ------------------------------------------------------------------------------
32# The above copyright notice and this permission notice shall be included in all
33# copies or substantial portions of the Software.
34# ------------------------------------------------------------------------------
35# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
41# SOFTWARE.
42# ------------------------------------------------------------------------------
43
44# ------------------------
45# Set Terminal Font Colors
46# ------------------------
47
48bold=$(tput bold)
49red=$(tput setaf 1)
50green=$(tput setaf 2)
51yellow=$(tput setaf 3)
52blue=$(tput setaf 4)
53magenta=$(tput setaf 5)
54cyan=$(tput setaf 6)
55white=$(tput setaf 7)
56defaultcolor=$(tput setaf default)
57thisip=$(dig +short myip.opendns.com @resolver1.opendns.com)
58
59# ---------
60# FUNCTIONS
61# ---------
62
63reloadNginX () {
64echo "${bold}${green}---------------"
65echo "${bold}${green}Reloading Nginx"
66echo "${bold}${green}---------------"
67sudo nginx -t && sudo nginx -s reload
68}
69
70waitforReload () {
71echo "${bold}${yellow}-----------------------------------------------------------------------"
72echo "${bold}${yellow}Sleeping for 10 seconds to allow Nginx to Properly Reload inside Travis"
73echo "${bold}${yellow}-----------------------------------------------------------------------"
74printf "\n"
75sleep 10s
76}
77
78run_curltest1 () {
79if curl http://localhost:9000 2>&1 | grep -i '(52)'; then
80   echo "${bold}${green}PASSED - ${bold}${red}blacklist own ip is WORKING"
81else
82   echo "${bold}${red}FAILED - blacklist own ip is NOT working"
83   exit 1
84fi
85}
86
87run_curltest2 () {
88if curl http://localhost:9000 2>&1 | grep -i 'Welcome'; then
89   echo "${bold}${green}PASSED - whitelist own ip is WORKING"
90else
91   echo "${bold}${red}FAILED - whitelist own ip is NOT working"
92   curl http://localhost:9000
93   exit 1
94fi
95}
96
97backupConfFiles () {
98printf "\n"
99echo "${bold}${green}------------------------------------------------------------"
100echo "${bold}${green}Make Backup all conf files and folders used during this test"
101echo "${bold}${green}------------------------------------------------------------"
102printf "\n"
103sudo cp /etc/nginx/bots.d/* ${TRAVIS_BUILD_DIR}/.dev-tools/test1_conf_files_ip_whitelist/bots.d/
104sudo cp /etc/nginx/conf.d/* ${TRAVIS_BUILD_DIR}/.dev-tools/test1_conf_files_ip_whitelist/conf.d/
105sudo cp /etc/nginx/sites-available/default.vhost ${TRAVIS_BUILD_DIR}/.dev-tools/test1_conf_files_ip_whitelist/default.vhost
106sudo cp /etc/nginx/nginx.conf ${TRAVIS_BUILD_DIR}/.dev-tools/test1_conf_files_ip_whitelist/nginx.conf
107}
108
109blacklistOwnIP () {
110sudo truncate -s 0 ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/blacklist-ips.conf
111printf '%s\t%s\n' "${thisip}" "1;" > ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/blacklist-ips.conf
112printf '%s\t%s\n' "127.0.0.1" "1;" >> ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/blacklist-ips.conf
113sudo cp ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/blacklist-ips.conf /etc/nginx/bots.d/blacklist-ips.conf
114sudo truncate -s 0 ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf
115sudo cp ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf /etc/nginx/bots.d/whitelist-ips.conf
116}
117
118whitelistOwnIP () {
119sudo truncate -s 0 ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf
120printf '%s\t%s\n' "${thisip}" "0;" > ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf
121printf '%s\t%s\n' "127.0.0.1" "0;" >> ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf
122sudo cp ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/whitelist-ips.conf /etc/nginx/bots.d/whitelist-ips.conf
123# TEST ANY CHANGES TO botblocker-nginx-settings.conf
124sudo cp ${TRAVIS_BUILD_DIR}/.dev-tools/test_units/botblocker-nginx-settings.conf /etc/nginx/conf.d/botblocker-nginx-settings.conf
125}
126
127# -----------
128# Start Tests
129# -----------
130
131echo "${bold}${green}--------------------------"
132echo "${bold}${green}Whitelist IP Test Starting"
133echo "${bold}${green}--------------------------"
134printf "\n"
135
136echo "${bold}${green}-------------------------"
137echo "${bold}${green}Blacklisting own IP First"
138echo "${bold}${green}-------------------------"
139printf "\n"
140
141blacklistOwnIP
142reloadNginX
143waitforReload
144run_curltest1
145
146echo "${bold}${green}--------------------"
147echo "${bold}${green}Now Whitelist own IP"
148echo "${bold}${green}--------------------"
149printf "\n"
150
151whitelistOwnIP
152reloadNginX
153waitforReload
154run_curltest2
155
156echo "${bold}${green}-----------------------------"
157echo "${bold}${green}Whitelisting IP Test Complete"
158echo "${bold}${green}-----------------------------"
159printf "\n"
160
161# *************************************************************
162# Copy all .conf files used in Testing to a folder for checking
163# *************************************************************
164
165backupConfFiles
166
167# **********************
168# Exit With Error Number
169# **********************
170
171exit ${?}
172
173# ------------------------------------------------------------------------------
174# MIT License
175# ------------------------------------------------------------------------------
176# Copyright (c) 2017 Mitchell Krog - mitchellkrog@gmail.com
177# https://github.com/mitchellkrogza
178# ------------------------------------------------------------------------------
179# Permission is hereby granted, free of charge, to any person obtaining a copy
180# of this software and associated documentation files (the "Software"), to deal
181# in the Software without restriction, including without limitation the rights
182# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
183# copies of the Software, and to permit persons to whom the Software is
184# furnished to do so, subject to the following conditions:
185# ------------------------------------------------------------------------------
186# The above copyright notice and this permission notice shall be included in all
187# copies or substantial portions of the Software.
188# ------------------------------------------------------------------------------
189# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
190# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
191# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
192# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
193# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
194# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
195# SOFTWARE.
196# ------------------------------------------------------------------------------
197