12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040 |
- #!/usr/bin/env bash
- # shellcheck shell=bash
- ################################################################################
- ######################### Main function (script init) ##########################
- ################################################################################
- function __main__() {
- local _FUNCTION_ID="__main__"
- local _STATE="0"
- # Stores the current date.
- readonly _cdate=$(date +%Y%m%d)
- # Variables related to the log file. Divided into three parts due
- # to the better possibility of manipulation for the user.
- # shellcheck disable=SC2154
- readonly _log_directory="${_rel}/log"
- # shellcheck disable=SC2154
- readonly _log_file="${_init_name}.${_cdate}.log"
- readonly _log_stdout="${_log_directory}/stdout.log"
- readonly _log_path="${_log_directory}/${_log_file}"
- # We check if we are a root user.
- if [[ "$EUID" -ne 0 ]] ; then
- printf "EUID is not equal 0 (no root user)\\n"
- _exit_ "1"
- fi
- # Path to import file.
- # shellcheck disable=SC2154
- readonly _import_path="${_src}/import"
- # External configuration file (-c|--config script param).
- config=""
- load_state="0"
- # Declaration of output variables (--debug and --verbose params).
- stdout_mode=""
- verbose_mode="false"
- # Enable/disable output colors.
- # shellcheck disable=SC2034
- s_color="true"
- # Declaration of total execution time.
- time_tracking="false"
- export _cmdtime_state="0"
- # Create multitor directory.
- readonly _multitor_directory="/var/lib/multitor"
- # We place here used commands at script runtime, as strings to anything
- # unnecessarily run.
- readonly commands=(basename dirname stat ps date grep cut sed awk chown \
- chmod mkdir pidof tor sudo nc kill \
- haproxy polipo privoxy hpts)
- # If you intend to specify the full path to the command we do it like:
- # readonly exec_gzip="/bin/gzip"
- # Stores the names of the missing commands.
- missing_hash=()
- missing_counter="0"
- for i in "${commands[@]}" ; do
- if [[ ! -z "$i" ]] ; then
- hash "$i" >/dev/null 2>&1 ; state="$?"
- # If the command was not found put it in the array
- if [[ "$state" -ne 0 ]] ; then
- missing_hash+=("$i")
- ((missing_counter++))
- fi
- fi
- done
- # It is a good idea to terminate the script at this stage
- # with information for the user to fix the errors if at least one
- # of the required commands in the commands array is not found.
- if [[ "$missing_counter" -gt 0 ]] ; then
- printf "not found in PATH: %s\\n" "${missing_hash[*]}" >&2
- _exit_ "1"
- fi
- if [[ "$time_tracking" == "true" ]] ; then
- _begtime=$(date +%s) ; fi
- # shellcheck disable=SC2154
- _logger "init" \
- "init '${_init_name}' in '${_init_directory}'" && \
- _logger "info" \
- "__init_params[] = (${__init_params[*]})," \
- "__script_params[] = (${__script_params[*]})"
- # Include import file.
- _load "null" "$_import_path"
- # Specifies the call parameters of the script, the exact description
- # can be found in _help_ and file README.md.
- local _short_opt="i:ksnu:"
- local _long_opt="help,debug,verbose,init:,kill,show-id,new-id,user:,socks-port:,control-port:,proxy:,haproxy"
- _GETOPT_PARAMS=$(getopt -o "${_short_opt}" --long "${_long_opt}" \
- -n "${_init_name}" -- "${__script_params[@]}")
- # With this structure, in the case of problems with the parameters placed
- # in the _GETOPT_PARAMS variable we finish the script. Keep this in mind
- # because it has some consequences - the __main __() function will not be
- # executed.
- # Ends an error if the parameter or its argument is not valid.
- _getopt_state="$?"
- if [ "$_getopt_state" != 0 ] ; then
- _exit_ "1"
- # Ends if no parameter is specified.
- elif [[ "${#__script_params[@]}" -eq 0 ]] ; then
- _exit_ "0"
- fi
- eval set -- "$_GETOPT_PARAMS"
- while true ; do
- case $1 in
- --help)
- _help_
- shift ; _exit_ "0" ;;
- --debug)
- export stdout_mode="debug"
- shift ;;
- --verbose)
- export verbose_mode="true"
- shift ;;
- -i|--init)
- export init_state="1"
- export init_number="${2}"
- shift 2 ;;
- -k|--kill)
- export kill_state="1"
- export kill_status="0"
- shift ;;
- -s|--show-id)
- export show_id_state="1"
- shift ;;
- -n|--new-id)
- export new_id_state="1"
- shift ;;
- -u|--user)
- export user_state="1"
- export user_name="${2}"
- shift 2 ;;
- --socks-port)
- export socks_port_state=1
- export socks_port_number="${2}"
- shift 2 ;;
- --control-port)
- export control_port_state=1
- export control_port_number="${2}"
- shift 2 ;;
- --proxy)
- export proxy_state=1
- export proxy_type="${2}"
- shift 2 ;;
- --haproxy)
- export frontend_state=1
- export frontend_type="haproxy"
- shift 2 ;;
- *)
- if [[ "$2" == "-" ]] || [[ ! -z "$2" ]] ; then
- printf "%s: invalid option -- '%s'\\n" "$_init_name" "$2"
- _exit_ "1"
- # elif [[ -z "$2" ]] ; then break ; fi
- else break ; fi
- ;;
- esac
- done
- # If you run the script in debug mode, the information
- # will be displayed on the screen from this point.
- if [[ "$stdout_mode" == "debug" ]] ; then
- _logger "info" \
- "${_FUNCTION_ID}()" \
- "starting debug mode"
- fi
- # Running tasks before start user functions.
- _before_init
- ################################# USER SPACE #################################
- # ````````````````````````````````````````````````````````````````````````````
- # Put here all your variable declarations, function calls
- # and all the other code blocks.
- # In this section we add external file (for -c|--config script param).
- if [[ "$load_state" -eq 1 ]] ; then _load "head" "$config" ; fi
- # shellcheck disable=SC2034
- # Generate random value.
- _random=$(date +"%s")
- # Array that stores the names of variables used that are part of the script
- # call parameters (_GETOPT_PARAMS). Useful when checking whether all
- # or selected parameters without which the script can not work properly
- # have been used. Do not add the load_state variable to the _opt_values array,
- # which is supported above.
- _opt_values=()
- local _tproc=""
- local _tor_processes_done=0
- local _tor_processes_fail=0
- local _tor_processes=0
- local _pass_gen
- local _pass_gen_ha
- local _pass_hash
- local _num='^[0-9]+$'
- local _proxy_list=("polipo" "privoxy" "hpts" "node")
- # Checking whether the variable value is a numeric value.
- if [[ "$init_number" =~ $_num ]] || \
- [[ "$socks_port_number" =~ $_num ]] || \
- [[ "$socks_port_number" == "all" ]] || \
- [[ "$control_port_number" =~ $_num ]] || \
- [[ "$control_port_number" == "all" ]] ; then
- # shellcheck disable=SC2034
- _ports_limit=$((65536 - init_number))
- # shellcheck disable=SC2154
- if [[ "$init_state" -eq 1 ]] && [[ "$init_number" -le 0 ]] ; then
- _sprintf "stop" "init_number is less or equal 0"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "init_number is less then or equal 0"
- elif [[ "$socks_port_number" -ne "all" ]] || \
- [[ "$control_port_number" -ne "all" ]] ; then
- if [[ "$socks_port_state" -eq 1 && "$socks_port_number" -le 1023 ]] ; then
- _sprintf "stop" "value is less then or equal 1023"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "value is less or equal 1023"
- elif [[ "$control_port_state" -eq 1 && "$control_port_number" -le 1023 ]] ; then
- _sprintf "stop" "value is less then or equal 1023"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "value is less or equal 1023"
- elif [[ "$socks_port_state" -eq 1 && "$socks_port_number" -ge 65536 ]] ; then
- _sprintf "stop" "value is equal or grather then 65536"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "value is equal or grather then 65536"
- elif [[ "$socks_port_state" -eq 1 && "$control_port_number" -ge 65536 ]] ; then
- _sprintf "stop" "value is equal or grather then 65536"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "value is equal or grather then 65536"
- elif [[ "$socks_port_number" -gt "$_ports_limit" ]] || \
- [[ "$control_port_number" -gt "$_ports_limit" ]] ; then
- _sprintf "stop" "too many set processes"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "too many set processes"
- elif [[ "$socks_port_number" -eq "$control_port_number" ]] ; then
- _sprintf "stop" "value mismatch"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "value mismatch"
- fi
- fi
- else
- if [[ "$kill_state" -eq 1 ]] ; then
- true
- else
- _sprintf "stop" "no numeric value"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "no numeric value"
- fi
- fi
- # Checking if the user exists.
- if [[ "$user_state" -eq 1 ]] ; then
- if ! id "$user_name" >/dev/null 2>&1 ; then
- _sprintf "stop" "incorrect user"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "incorrect user"
- fi
- fi
- # Checking if proxy is set.
- if [[ "$proxy_state" -eq 1 ]] ; then
- if [[ "$proxy_type" == "socks" ]] || \
- [[ " ${_proxy_list[*]} " =~ $proxy_type ]] ; then
- _logger "info" \
- "${_FUNCTION_ID}()" \
- "correct proxy type: '$proxy_type'"
- else
- _sprintf "stop" "incorrect proxy type"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "incorrect proxy type"
- fi
- fi
- # Checking if proxy is set for '--haproxy' param.
- if [[ "$frontend_state" -eq 1 ]] ; then
- if [[ "$proxy_state" -ne 1 ]] ; then
- _sprintf "stop" "not set '--proxy' param"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "not set '--proxy' param"
- fi
- fi
- # We set the value of the variable depending on the call option.
- if [[ "$init_state" -eq 1 ]] ; then
- local _opt_values=("init_state" "init_number" \
- "user_state" "user_name" \
- "socks_port_state" "socks_port_number" \
- "control_port_state" "control_port_number")
- elif [[ "$kill_state" -eq 1 ]] ; then
- local _opt_values=("kill_state")
- elif [[ "$show_id_state" -eq 1 ]] || [[ "$new_id_state" -eq 1 ]] ; then
- local _opt_values=("socks_port_state" "socks_port_number")
- else
- _sprintf "stop" "one of the required parameters was not found"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "one of the required parameters was not found"
- fi
- # Checking the value of the variables (if they are unset or empty):
- # - variables for call parameters
- # - variables from the additional configuration files
- if [[ "${#_opt_values[@]}" -ne 0 ]] ; then
- for i in "${_opt_values[@]}" ; do
- _i="" ; eval _i='$'"$i"
- _logger "info" \
- "${_FUNCTION_ID}()" \
- "$i: '$_i'"
- if [[ -z "$_i" ]] ; then
- _sprintf "stop" "error of argument value: '$i' is unset or empty"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "error of argument value: '$i' is unset or empty"
- fi
- done
- fi
- if [[ "$init_state" -eq 1 ]] ; then
- # First, we remove current processes.
- # _tor_processes=($(pidof tor))
- IFS=" " read -r -a _tor_processes <<< "$(pidof "tor")"
- if [[ ${#_tor_processes[@]} -ne 0 ]] ; then
- for _tpr in "${_tor_processes[@]}" ; do
- # In this case, we can search for a specific identifier (${_tpr}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$_tpr")
- if [[ -z "$_tproc" ]] ; then
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found or unknown tor process"
- else
- DestroyProcess "${_tpr}"
- fi
- done
- else
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found tor processes: ${_tpr}"
- fi
- # Removed all multitor process data directory.
- find ${_multitor_directory}/* -delete >>"$_log_stdout" 2>&1
- # Generate random password.
- # Does storing the password in this form is security issue?
- # shellcheck disable=SC2034
- _pass_gen=$(< /dev/urandom tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
- # shellcheck disable=SC2034
- _pass_hash=$(sudo -u "$user_name" tor --hash-password "$_pass_gen" | grep "16:")
- if [[ "$proxy_state" -eq 1 ]] ; then
- # shellcheck disable=SC2034,SC2154
- local _tml_ha0="${_tml}/haproxy-template.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_po0="${_tml}/${proxy_type}-template.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_ha1="${_etc}/haproxy.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_po1="${_etc}/${proxy_type}.cfg"
- for _tfd in "$_tml_ha1" "$_tml_po1" ; do
- # shellcheck disable=SC2154
- if [[ -e "$_tfd" ]] ; then
- rm -fr "${_tfd}"
- fi
- done
- # Updated auth for HAProxy template configuration file.
- # shellcheck disable=SC2154
- cp "${_tml_ha0}" "${_tml_ha1}"
- # shellcheck disable=SC2034
- _pass_gen_ha=$(< /dev/urandom tr -dc 'a-zA-Z0-9' | fold -w 18 | head -n 1)
- sed -i "s/__PASSWORD__/$_pass_gen_ha/g" "${_tml_ha1}"
- unset _pass_gen_ha
- _proxy_ports=()
- else
- # Flush etc/ directory.
- rm -fr "${_etc:?}"/*
- fi
- for i in $(seq 0 $((init_number - 1))) ; do
- _logger "info" \
- "${_FUNCTION_ID}()" \
- "socks_port_number: '$socks_port_number', control_port_number: '$control_port_number'"
- CreateTorProcess "${user_name}" "${socks_port_number}" "${control_port_number}"
- # For proxy:
- _proxy_ports+=("$socks_port_number")
- # In this case, we can search for a specific identifier (${socks_port_number}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$socks_port_number")
- # We get process information and display it on the screen.
- GetTorProcess
- # shellcheck disable=SC2154
- if [[ "$socks_port_number" == "all" ]] ; then
- _tor_pid_status="true"
- if [[ "$verbose_mode" == "true" ]] ; then
- echo
- OutputGen
- fi
- elif [[ "$socks_port_number" -eq "$_tor_proc_socks" ]] ; then
- _tor_pid_status="true"
- if [[ "$verbose_mode" == "true" ]] ; then
- echo
- OutputGen
- fi
- else
- _tor_pid_status="false"
- printf " \\e[1;30msocks_port\\e[m: \\e[1;39m%s\\e[m\\n" "$_tor_proc_socks"
- printf " \\e[1;30mstatus\\e[m: \\e[1;31m%s\\e[m\\n" "$_tor_pid_status"
- fi
- socks_port_number=$((socks_port_number + 1))
- control_port_number=$((control_port_number + 1))
- done
- _proxy_processes_list=()
- # shellcheck disable=SC2034
- # Manages proxy processes stack.
- for _proc in "haproxy" "${_proxy_list[@]}" ; do
- CheckProxyProcess "${_proc}"
- done
- # shellcheck disable=SC2154
- for _hproc in "${_proxy_processes_list[@]}" ; do
- DestroyProcess "${_hproc}"
- done
- if [[ "$proxy_state" -eq 1 ]] ; then
- CreateProxyProcess
- fi
- # shellcheck disable=SC2034
- for _proc in "haproxy" "${_proxy_list[@]}" ; do
- CheckProxyProcess "${_proc}"
- done
- printf "\\n Set processes: \\e[0;39m%d\\e[m\\n" "$init_number"
- printf " Created: \\e[0;32m%d\\e[m\\n" "$_tor_processes_done"
- printf " Not created: \\e[0;31m%s\\e[m\\n" "$_tor_processes_fail"
- printf " Control password: \\e[2;38m%s\\e[m\\n\\n" "$_pass_gen"
- # shellcheck disable=SC2154
- if [[ ${#_proxy_processes_list[@]} -ne 0 ]] && [[ "$proxy_state" -eq 1 ]] ; then
- if [[ "$proxy_type" == "socks" ]] ; then
- printf " Proxy state: \\e[2;32m%s\\e[m (haproxy » %s)\\n\\n" "running" "$proxy_type"
- elif [[ "$frontend_type" == "haproxy" ]] ; then
- printf " Proxy state: \\e[2;32m%s\\e[m (haproxy » %s » socks)\\n\\n" "running" "$proxy_type"
- else
- printf " Proxy state: \\e[2;32m%s\\e[m (%s » haproxy » socks)\\n\\n" "running" "$proxy_type"
- fi
- else
- printf " Proxy state: \\e[2;31m%s\\e[m (only tor)\\n\\n" "disable"
- fi
- # Unset password variables.
- unset _pass_gen ; unset _pass_hash
- elif [[ "$kill_state" -eq 1 ]] ; then
- # First, we remove current processes.
- # _tor_processes=($(pidof tor))
- IFS=" " read -r -a _tor_processes <<< "$(pidof "tor")"
- if [[ ${#_tor_processes[@]} -ne 0 ]] ; then
- for _tpr in "${_tor_processes[@]}" ; do
- # In this case, we can search for a specific identifier (${_tpr}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$_tpr")
- if [[ -z "$_tproc" ]] ; then
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found or unknown tor process"
- else
- DestroyProcess "${_tpr}"
- kill_status="1"
- fi
- done
- else
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found tor processes: ${_tpr}"
- fi
- # Removed all multitor process data directory.
- find ${_multitor_directory}/* -delete >>"$_log_stdout" 2>&1
- # shellcheck disable=SC2034,SC2154
- local _tml_ha0="${_tml}/haproxy-template.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_po0="${_tml}/${proxy_type}-template.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_ha1="${_etc}/haproxy.cfg"
- # shellcheck disable=SC2034,SC2154
- local _tml_po1="${_etc}/${proxy_type}.cfg"
- for _tfd in "$_tml_ha1" "$_tml_po1" ; do
- # shellcheck disable=SC2154
- if [[ -e "$_tfd" ]] ; then
- rm -fr "${_tfd}"
- kill_status="1"
- fi
- done
- # Flush etc/ directory.
- rm -fr "${_etc:?}"/*
- # shellcheck disable=SC2034
- # Manages proxy processes stack.
- for _proc in "haproxy" "${_proxy_list[@]}" ; do
- CheckProxyProcess "${_proc}"
- done
- # shellcheck disable=SC2154
- for _hproc in "${_proxy_processes_list[@]}" ; do
- DestroyProcess "${_hproc}"
- kill_status="1"
- done
- if [[ "$kill_status" -eq 1 ]] ; then
- printf "Multitor processes: \\e[1;31m%s\\e[m\\n" "stop"
- fi
- elif [[ "$show_id_state" -eq 1 ]] ; then
- IFS=" " read -r -a _tor_processes <<< "$(pidof "tor")"
- if [[ ${#_tor_processes[@]} -ne 0 ]] ; then
- if [[ "$socks_port_number" == "all" ]] ; then
- for _tpr in "${_tor_processes[@]}" ; do
- # In this case, we can search for a specific identifier (${_tpr}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$_tpr")
- IFS=" " read -r -a _proc_socks_num_tmp <<< "$(echo "$_tproc" | awk '{print $13}')"
- _proc_socks_num+=("${_proc_socks_num_tmp[@]}")
- done
- # IFS=$'\n' read -r -a _proc_socks_num_sorted <<< "$(sort <<<"${_proc_socks_num[*]}")"
- # shellcheck disable=SC2207
- _proc_socks_num_sorted=( $( printf "%s\\n" "${_proc_socks_num[@]}" | sort -n ) )
- unset IFS
- for _tpr in "${_proc_socks_num_sorted[@]}" ; do
- # In this case, we can search for a specific identifier (${_tpr}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$_tpr")
- echo
- # We get process information and display it on the screen.
- GetTorProcess
- if [[ "$socks_port_number" == "all" ]] ; then
- _tor_pid_status="true"
- OutputGen
- elif [[ "$socks_port_number" -eq "$_tor_proc_socks" ]] ; then
- _tor_pid_status="true"
- OutputGen
- else
- _tor_pid_status="false"
- printf " \\e[1;30msocks_port\\e[m: \\e[1;39m%s\\e[m\\n" "$_tor_proc_socks"
- printf " \\e[1;30mstatus\\e[m: \\e[1;31m%s\\e[m\\n" "$_tor_pid_status"
- fi
- done
- else
- # In this case, we can search for a specific identifier (${socks_port_number}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$socks_port_number")
- if [[ -z "$_tproc" ]] ; then
- _sprintf "stop" "not found tor process"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "not found tor process"
- else
- echo
- # We get process information and display it on the screen.
- GetTorProcess
- if [[ "$socks_port_number" == "all" ]] ; then
- _tor_pid_status="true"
- OutputGen
- elif [[ "$socks_port_number" -eq "$_tor_proc_socks" ]] ; then
- _tor_pid_status="true"
- OutputGen
- else
- _tor_pid_status="false"
- _sprintf "stop" "not found tor process"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "not found tor process"
- fi
- fi
- fi
- else
- _sprintf "stop" "not found tor process"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "not found tor process"
- fi
- echo
- elif [[ "$new_id_state" -eq 1 ]] ; then
- _output_tparams=()
- if [[ "$socks_port_number" == "all" ]] ; then
- IFS=" " read -r -a _tor_processes <<< "$(pidof "tor")"
- else
- _tor_processes=("$socks_port_number")
- fi
- # Password required at this stage.
- read -rsp "Enter password: " _pass_in
- echo
- if [[ ${#_tor_processes[@]} -ne 0 ]] ; then
- for _tpr in "${_tor_processes[@]}" ; do
- # In this case, we can search for a specific identifier (${_tpr}).
- # shellcheck disable=SC2009
- _tproc=$(ps -ax | grep "multitor" | grep ".pid" | grep -v "grep" | grep "$_tpr")
- if [[ -z "$_tproc" ]] ; then
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found or unknown tor process"
- else
- # We get process information.
- GetTorProcess
- if [[ "$socks_port_number" == "all" ]] ; then
- _tor_pid_status="true"
- elif [[ "$socks_port_number" -eq "$_tor_proc_socks" ]] ; then
- _tor_pid_status="true"
- else
- _tor_pid_status="false"
- fi
- # shellcheck disable=SC2154
- _newnym=$(printf "AUTHENTICATE \"%s\"\\r\\nSIGNAL NEWNYM\\nQUIT\\n" "${_pass_in}" \
- | nc 127.0.0.1 "$_tor_proc_control")
- if echo "$_newnym" | grep -qi "250 closing connection" ; then
- _identity_state="true"
- elif echo "$_newnym" | grep -q "Authentication failed" ; then
- _identity_state="false"
- else
- _identity_state="false"
- fi
- _output_tparams+=("$_tor_proc_id":"$_tor_proc_socks":"$_tor_proc_control":"$_identity_state")
- fi
- done
- else
- _sprintf "stop" "not found tor process"
- _logger "stop" \
- "${_FUNCTION_ID}()" \
- "not found tor process"
- fi
- unset _pass_in
- for i in "${_output_tparams[@]}" ; do
- # shellcheck disable=SC2034
- _key_id=$(echo "$i" | awk -v FS="(:|:)" '{print $1}')
- # shellcheck disable=SC2034
- _key_socks=$(echo "$i" | awk -v FS="(:|:)" '{print $2}')
- # shellcheck disable=SC2034
- _key_control=$(echo "$i" | awk -v FS="(:|:)" '{print $3}')
- # shellcheck disable=SC2034
- _key_state=$(echo "$i" | awk -v FS="(:|:)" '{print $4}')
- if [[ "$_key_state" == "true" ]] ; then
- printf "New identity for \\e[1;39m%d\\e[m: \\e[0;32m%s\\e[m\\n" "$_key_id" "regenerated"
- else
- if [[ "$_tor_pid_status" == "false" ]] ; then
- _logger "warn" \
- "${_FUNCTION_ID}()" \
- "not found or unknown tor process"
- printf "New identity for \\e[1;39m%d\\e[m: \\e[0;31m%s\\e[m\\n" "$_key_id" "unknown"
- else
- printf "New identity for \\e[1;39m%d\\e[m: \\e[0;31m%s\\e[m\\n" "$_key_id" "failed"
- fi
- fi
- done
- fi
- # ````````````````````````````````````````````````````````````````````````````
- if [[ "$time_tracking" == "true" ]] ; then
- # Counting the execution time.
- _endtime=$(date +%s)
- _totaltime=$((_endtime - _begtime))
- # Print time header.
- printf '\\e[m\\e[1;39mTOTAL TIME: %dh:%dm:%ds\\e[m\n' \
- $((_totaltime/3600)) $((_totaltime%3600/60)) $((_totaltime%60))
- fi
- return "$_STATE"
- }
|