|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Copyright (C) 2021 Hasan CALISIR <hasan.calisir@psauxit.com> |
| 4 | +# Distributed under the GNU General Public License, version 2.0. |
| 5 | +# |
| 6 | +# This program is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU General Public License as published by |
| 8 | +# the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# This program is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU General Public License |
| 17 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | +# |
| 19 | +# What is doing this script exactly? |
| 20 | +# -Maven build, deploy, jvm control helper script for pspenit app. |
| 21 | + |
| 22 | +# app info |
| 23 | +prog_name="JVM Tools | PSAUXIT-PSPENIT" |
| 24 | +script_name="$0" |
| 25 | + |
| 26 | +# prod path | app name |
| 27 | +app_name="psauxit.jar" |
| 28 | +prod_path="$HOME/psauxitwebtools" |
| 29 | +preprod_path="$HOME/webnettools" |
| 30 | + |
| 31 | +# my jar is exist |
| 32 | +find_prod_app () { |
| 33 | + prod_app=`find "${prod_path:?}/" -name \*"${app_name}" -print0 | tr -d '\0' 2>/dev/null` |
| 34 | +} |
| 35 | + |
| 36 | +# Set color |
| 37 | +setup_terminal () { |
| 38 | + green="$(tput setaf 2)"; red="$(tput setaf 1)"; reset="$(tput sgr 0)" |
| 39 | + cyan="$(tput setaf 6)"; magenta="$(tput setaf 5)" |
| 40 | + TPUT_BOLD="$(tput bold)"; TPUT_BGRED="$(tput setab 1)" |
| 41 | + TPUT_WHITE="$(tput setaf 7)"; |
| 42 | +} |
| 43 | +setup_terminal |
| 44 | + |
| 45 | +# wait for process exit code |
| 46 | +my_wait () { |
| 47 | + local my_pid=$! |
| 48 | + local result |
| 49 | + # Force kill bg process if script exits |
| 50 | + trap "kill -9 $my_pid 2>/dev/null" EXIT |
| 51 | + # Wait stylish while process is alive |
| 52 | + spin='-\|/' |
| 53 | + mi=0 |
| 54 | + while kill -0 $my_pid 2>/dev/null # (ps | grep $my_pid) is alternative |
| 55 | + do |
| 56 | + mi=$(( (mi+1) %4 )) |
| 57 | + printf "\r${m_tab}${green}[ ${spin:$mi:1} ]${magenta} ${1}${reset}" |
| 58 | + sleep .1 |
| 59 | + done |
| 60 | + # Get bg process exit code |
| 61 | + wait $my_pid |
| 62 | + [[ $? -eq 0 ]] && result=ok |
| 63 | + # Need for tput cuu1 |
| 64 | + echo "" |
| 65 | + # Reset trap to normal exit |
| 66 | + trap - EXIT |
| 67 | + # Return status of function according to bg process status |
| 68 | + [[ "${result}" ]] && return 0 |
| 69 | + return 1 |
| 70 | +} |
| 71 | + |
| 72 | +fatal () { |
| 73 | + printf >&2 "\n${m_tab}%s ABORTED %s %s \n\n" "${TPUT_BGRED}${TPUT_WHITE}${TPUT_BOLD}" "${reset}" "${*}" |
| 74 | + exit 1 |
| 75 | +} |
| 76 | + |
| 77 | +pretty_suc () { |
| 78 | + echo "${m_tab}${TPUT_BOLD}${green}[ ✓ ] ${cyan}${1}${reset}" |
| 79 | +} |
| 80 | + |
| 81 | +pretty_fail () { |
| 82 | + echo "${m_tab}${TPUT_BOLD}${red}[ x ] ${red}${1}${reset}" |
| 83 | +} |
| 84 | + |
| 85 | +replace_suc () { |
| 86 | + tput cuu 1 |
| 87 | + echo "${m_tab}${TPUT_BOLD}${green}[ ✓ ] ${cyan}${1}${reset}" |
| 88 | +} |
| 89 | + |
| 90 | +replace_fail () { |
| 91 | + tput cuu 1 |
| 92 | + echo "${m_tab}${TPUT_BOLD}${red}[ x ] ${red}${1}${reset}" |
| 93 | +} |
| 94 | + |
| 95 | +# deployment process |
| 96 | +deploy () { |
| 97 | + local preprod_app=`find "${preprod_path:?}/" -name \*-runner.jar -print0 | tr -d '\0' 2>/dev/null` |
| 98 | + |
| 99 | + # check this is a clean deploy |
| 100 | + if [[ -d "${prod_path:?}" ]]; then |
| 101 | + find_prod_app |
| 102 | + [[ $prod_app ]] && clean=0 || clean=1 |
| 103 | + else |
| 104 | + clean=1 |
| 105 | + fi |
| 106 | + |
| 107 | + # Start deployment |
| 108 | + if [[ -d "${preprod_path:?}" ]]; then |
| 109 | + if [[ $preprod_app ]]; then |
| 110 | + if [[ "${clean}" -eq 0 ]]; then |
| 111 | + if [[ "$(md5sum "${preprod_app}" | awk '{print $1}')" != "$(md5sum "${prod_app}" | awk '{print $1}')" ]]; then |
| 112 | + { rm -rf "${prod_path:?}"; mkdir -p "${prod_path}"/lib; cp "${preprod_app}" "${prod_path}"/"${prod_app##*/}"; cp -r "${preprod_path}"/target/lib/* "${prod_path}"/lib/; } >/dev/null 2>&1 |
| 113 | + else |
| 114 | + pretty_suc "Application up to date. Deployment SKIPPED!" |
| 115 | + return 1 |
| 116 | + fi |
| 117 | + else |
| 118 | + { mkdir -p "${prod_path}"/lib; cp "${preprod_app}" "${prod_path}"/"${app_name}"; cp -r "${preprod_path}"/target/lib/* "${prod_path}"/lib/; find_prod_app; } >/dev/null 2>&1 |
| 119 | + fi |
| 120 | + else |
| 121 | + pretty_fail "Cannot start deployment process, deployment jar cannot found!" |
| 122 | + return 1 |
| 123 | + fi |
| 124 | + else |
| 125 | + pretty_fail "Cannot start deployment process, preprod path not found!" |
| 126 | + return 1 |
| 127 | + fi |
| 128 | + return 0 |
| 129 | +} |
| 130 | + |
| 131 | +# clean frontend |
| 132 | +clean () { |
| 133 | + arrb=("node" "node_modules" "package-lock.json") |
| 134 | + arrf=("vendor" "build") |
| 135 | + |
| 136 | + for d in "${arrb[@]}"; do |
| 137 | + if [[ -e "${preprod_path}/src/main/frontend/${d}" ]]; then |
| 138 | + print_b=1 |
| 139 | + rm -rf "${preprod_path:?}/src/main/frontend/${d}" &>/dev/null & |
| 140 | + fi |
| 141 | + done |
| 142 | + |
| 143 | + if [[ "${print_b}" ]]; then |
| 144 | + my_wait "Cleaning node,node_modules,lock.json.." && replace_suc "Cleaning node,node_modules,lock.json COMPLETED!" || { replace_fail "Cleaning node,node_modules,lock.json FAILED!"; fatal "QUIT"; } |
| 145 | + fi |
| 146 | + |
| 147 | + for d in "${arrf[@]}"; do |
| 148 | + if [[ -d "${preprod_path}/src/main/frontend/public/${d}" ]]; then |
| 149 | + print_f=1 |
| 150 | + rm -r "${preprod_path}/src/main/frontend/public/${d}" &>/dev/null & |
| 151 | + fi |
| 152 | + done |
| 153 | + |
| 154 | + if [[ "${print_f}" ]]; then |
| 155 | + my_wait "Cleaning previous frontend build.." && replace_suc "Cleaning previous frontend build COMPLETED!" || { replace_fail "Cleaning previous frontend build FAILED!"; fatal "QUIT"; } |
| 156 | + fi |
| 157 | + |
| 158 | + if [[ $1 == all ]] ; then |
| 159 | + cd "${preprod_path}" |
| 160 | + mvn clean &>/dev/null & |
| 161 | + my_wait "Cleaning previous backend build.." && replace_suc "Cleaning previous backend build COMPLETED!" || { replace_fail "Cleaning previous backend build FAILED!"; fatal "QUIT"; } |
| 162 | + fi |
| 163 | +} |
| 164 | + |
| 165 | +# Build jar via maven |
| 166 | +mvn_build () { |
| 167 | + if [[ -d "${preprod_path:?}" ]]; then |
| 168 | + cd "${preprod_path}" |
| 169 | + mvn clean &>/dev/null & |
| 170 | + my_wait "Cleaning previous backend build.." && replace_suc "Cleaning previous backend build COMPLETED!" || { replace_fail "Cleaning previous backend build FAILED!"; fatal "QUIT"; } |
| 171 | + clean |
| 172 | + mvn package &>/dev/null & |
| 173 | + my_wait "Building application.." && replace_suc "Building application COMPLETED! READY TO DEPLOYMENT!" || { replace_fail "Building application FAILED!"; fatal "QUIT"; } |
| 174 | + else |
| 175 | + fatal "Preprod path not exist!" |
| 176 | + fi |
| 177 | +} |
| 178 | + |
| 179 | +# Application global vars |
| 180 | +global_vars () { |
| 181 | + export PATH=/home/black/.local/bin:/home/black/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:$PATH |
| 182 | + export AVAILABLE_TOOLS=testssl,ping,dig,dnsrecon,wpscan,ddec,amass,whois,host,rustscan,cidr,mtr,wapiti,asn,xsstrike,nmap |
| 183 | + export RATE_LIMIT=60000 |
| 184 | + export CA_DIR=/etc/ssl/certs/ |
| 185 | + export PORT=8080 |
| 186 | + export INTRO_TEXT='<div class="accept"><p class="accept-p"><span style="font-size:14px">By using this free service, I accept the <a href="https://tools.psauxit.com/page/terms-and-conditions" onclick="window.open(this.href, ">Terms And Conditions</a> and <a href="https://tools.psauxit.com/page/privacy-policy" onclick="window.open(this.href, ">Privacy Policy</a> of this website (<a href="https://www.psauxit.com/" onclick="window.open( this.href, "><span style="color:#e74c3c"><em>psauxit.com</em></span></a> ). I declare that <strong>I will only scan my own network and domain names I own</strong> for vulnerability testing purposes.</span></p></div>' |
| 187 | +} |
| 188 | + |
| 189 | +# Test application is working after (re)start in background for 5 sec |
| 190 | +# To:do add 'try loop' if not suc after 5 sec |
| 191 | +nohup_java () { |
| 192 | + startwait=5 |
| 193 | + pid=$! |
| 194 | + spin='-\|/' |
| 195 | + mi=0 |
| 196 | + for i in $(seq $startwait); do |
| 197 | + if ! kill -0 $pid 2>/dev/null; then |
| 198 | + wait $! |
| 199 | + exitcode=$? |
| 200 | + replace_fail "Application cannot (re)started, EXITCODE:$exitcode" >&2 |
| 201 | + break |
| 202 | + fi |
| 203 | + mi=$(( (mi+1) %4 )) |
| 204 | + printf "\r${m_tab}${green}[ ${spin:$mi:1} ]${magenta} ${1}${reset}" |
| 205 | + sleep 1 |
| 206 | + done |
| 207 | + if kill -0 $pid; then |
| 208 | + echo "" |
| 209 | + replace_suc "Application (re)started successfully!" |
| 210 | + fi |
| 211 | +} |
| 212 | + |
| 213 | +# run in background |
| 214 | +my_run () { |
| 215 | + nohup java "-Dquarkus.http.host=10.0.0.3" "-Dquarkus.http.port=${PORT}" "-Djava.util.logging.manager=org.jboss.logmanager.LogManager" -jar "${prod_app}" >/dev/null 2>&1 & |
| 216 | +} |
| 217 | + |
| 218 | +# run in foreground |
| 219 | +my_run_f () { |
| 220 | + java "-Dquarkus.http.host=10.0.0.3" "-Dquarkus.http.port=${PORT}" "-Djava.util.logging.manager=org.jboss.logmanager.LogManager" -jar "${prod_app}" |
| 221 | +} |
| 222 | + |
| 223 | +# stop jvm |
| 224 | +stop_jvm () { |
| 225 | + if kill -9 $(ps aux | grep -v grep | grep "${app_name}" | awk '{print $2}') >/dev/null 2>&1; then |
| 226 | + pretty_suc "Application stopped!" |
| 227 | + else |
| 228 | + pretty_suc "Application already stopped!" |
| 229 | + fi |
| 230 | +} |
| 231 | + |
| 232 | +# start jvm |
| 233 | +start_jvm () { |
| 234 | + if ! ps aux | grep -v grep | grep "${app_name}" >/dev/null 2>&1; then |
| 235 | + find_prod_app |
| 236 | + [[ $prod_app ]] && { global_vars; my_run; nohup_java "Re-starting..."; } || pretty_fail "App not found. Cannot start application!" |
| 237 | + else |
| 238 | + pretty_suc "Application already running!" |
| 239 | + fi |
| 240 | +} |
| 241 | + |
| 242 | +# start {python wapiti virtual env,application} on reboot |
| 243 | +boot () { |
| 244 | + start_jvm |
| 245 | +} |
| 246 | + |
| 247 | +# restart jvm |
| 248 | +restart_jvm () { |
| 249 | + stop_jvm |
| 250 | + start_jvm |
| 251 | +} |
| 252 | + |
| 253 | +# carry process to foreground |
| 254 | +run_foreground () { |
| 255 | + stop_jvm |
| 256 | + find_prod_app |
| 257 | + [[ $prod_app ]] && { global_vars; my_run_f; } || pretty_fail "App not found. Cannot start application!" |
| 258 | +} |
| 259 | + |
| 260 | +help () { |
| 261 | + echo -e "\n${m_tab}${cyan}# Script Help" |
| 262 | + echo -e "${m_tab}# ---------------------------------------------------------------------------------" |
| 263 | + echo -e "${m_tab}#${m_tab}--start start jvm" |
| 264 | + echo -e "${m_tab}#${m_tab}--stop stop jvm" |
| 265 | + echo -e "${m_tab}#${m_tab}--restart restart jvm" |
| 266 | + echo -e "${m_tab}#${m_tab}--foreground start jvm on foreground" |
| 267 | + echo -e "${m_tab}#${m_tab}--deploy start deployment" |
| 268 | + echo -e "${m_tab}#${m_tab}--build build app" |
| 269 | + echo -e "${m_tab}#${m_tab}--build-deploy build app and start deploy" |
| 270 | + echo -e "${m_tab}#${m_tab}--clean clean previous frontend build" |
| 271 | + echo -e "${m_tab}#${m_tab}--clean-all clean previous frontend & backend build" |
| 272 | + echo -e "${m_tab}#${m_tab}--boot start jvm on boot" |
| 273 | + echo -e "${m_tab}#${m_tab}--help display help" |
| 274 | + echo -e "${m_tab}# ---------------------------------------------------------------------------------${reset}\n" |
| 275 | +} |
| 276 | + |
| 277 | +deployment () { |
| 278 | + if deploy; then |
| 279 | + # Restart application |
| 280 | + [[ $clean -eq 0 ]] && pretty_suc "OK! Deployment COMPLETED!" || pretty_suc "DONE! Clean deployment COMPLETED!" |
| 281 | + pretty_suc "Will try to (re)start application..." |
| 282 | + restart_jvm |
| 283 | + elif ! ps aux | grep -v grep | grep "${prod_app##*/}" >/dev/null 2>&1; then |
| 284 | + pretty_fail "Application is not working currently.." |
| 285 | + pretty_suc "Trying to start application..." |
| 286 | + start_jvm |
| 287 | + else |
| 288 | + pretty_suc "Application is running." |
| 289 | + fi |
| 290 | +} |
| 291 | + |
| 292 | +# Build app and deploy it immediately |
| 293 | +build_deploy () { |
| 294 | + mvn_build |
| 295 | + deployment |
| 296 | +} |
| 297 | + |
| 298 | +inv_opt () { |
| 299 | + printf "%s\\n" "${red}${prog_name}: Invalid option '$1'${reset}" |
| 300 | + printf "%s\\n" "${cyan}Try '${script_name} --help' for more information.${reset}" |
| 301 | + exit 1 |
| 302 | +} |
| 303 | + |
| 304 | +# Script management |
| 305 | +main () { |
| 306 | + if [[ "$#" -eq 0 || "$#" -gt 1 ]]; then |
| 307 | + printf "%s\\n" "${red}${prog_name}: Argument required or too many argument${reset}" |
| 308 | + printf "%s\\n" "${cyan}Try '${script_name} --help' for more information.${reset}" |
| 309 | + exit 1 |
| 310 | + fi |
| 311 | + |
| 312 | + # set script arguments |
| 313 | + while [[ "$#" -gt 0 ]]; do |
| 314 | + case "$1" in |
| 315 | + -s | --start ) start_jvm ;; |
| 316 | + -t | --stop ) stop_jvm ;; |
| 317 | + -r | --restart ) restart_jvm ;; |
| 318 | + -f | --foreground ) run_foreground ;; |
| 319 | + -d | --deploy ) deployment ;; |
| 320 | + -c | --build ) mvn_build ;; |
| 321 | + -cd | --build-deploy ) build_deploy ;; |
| 322 | + -e | --clean ) clean ;; |
| 323 | + -ca | --clean-all ) clean all ;; |
| 324 | + -b | --boot ) boot ;; |
| 325 | + -h | --help ) help ;; |
| 326 | + -- | -* | * ) inv_opt ;; |
| 327 | + esac |
| 328 | + break |
| 329 | + done |
| 330 | +} |
| 331 | + |
| 332 | +# Call main |
| 333 | +main "${@}" |
0 commit comments