#!/usr/bin/env bash # shellcheck shell=bash # `````````````````````````````````````````````````````````````````````````````` # Function name: CreateTorProcess() # # Description: # It creates tor processes. # # Usage: # CreateTorProcess "username" "num" "num" # # Examples: # CreateTorProcess ${user_name} ${socks_port_number} ${control_port_number} # function CreateTorProcess() { local _FUNCTION_ID="CreateTorProcess" local _STATE=0 local _arg_uname="$1" local _arg_socks="$2" local _arg_control="$3" # shellcheck disable=SC2154 local _proc_dir="${_multitor_directory}/${_arg_socks}" local _torrc_config="${_proc_dir}/${_arg_socks}.torrc" # shellcheck disable=SC2034 local _tport_state=0 # We create a directory for the new tor process. CreateTorDirectory # We save the hash of the password to the configuration file. # shellcheck disable=SC2154 echo "HashedControlPassword ${_pass_hash}" > "${_torrc_config}" _kstate="$?" if [[ $_kstate -eq 0 ]] ; then _logger "info" \ "${_FUNCTION_ID}()" \ "saved HashedControlPassword correctly" else _logger "stop" \ "${_FUNCTION_ID}()" \ "not saved HashedControlPassword correctly" fi # shellcheck disable=SC2154 chmod 0400 "${_torrc_config}" >>"$_log_stdout" 2>&1 && \ chown "${_arg_uname}:${_arg_uname}" "${_torrc_config}" >>"$_log_stdout" 2>&1 _kstate="$?" if [[ $_kstate -eq 0 ]] ; then _logger "info" \ "${_FUNCTION_ID}()" \ "change permission and owner correctly" else _logger "stop" \ "${_FUNCTION_ID}()" \ "not changed permission and owner correctly" fi # shellcheck disable=SC2024 sudo -u "$_arg_uname" tor -f "${_torrc_config}" \ --RunAsDaemon 1 \ --CookieAuthentication 0 \ --SocksPort "$_arg_socks" \ --ControlPort "$_arg_control" \ --PidFile "${_proc_dir}/${_arg_socks}.pid" \ --DataDirectory "${_proc_dir}" \ --SocksBindAddress 127.0.0.1 \ --NewCircuitPeriod 15 \ --MaxCircuitDirtiness 15 \ --NumEntryGuards 8 \ --CircuitBuildTimeout 5 \ --ExitRelay 0 \ --RefuseUnknownExits 0 \ --ClientOnly 1 \ --StrictNodes 1 \ --AllowSingleHopCircuits 1 \ >>"$_log_stdout" 2>&1 ; _kstate="$?" if [[ $_kstate -eq 0 ]] ; then _logger "info" \ "${_FUNCTION_ID}()" \ "create process: '${_arg_socks}.pid'" _tor_processes_done=$((_tor_processes_done + 1)) else _logger "warn" \ "${_FUNCTION_ID}()" \ "no process was created" _tor_processes_fail=$((_tor_processes_fail + 1)) fi unset _kstate return $_STATE }