CreateTorProcess 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env bash
  2. # shellcheck shell=bash
  3. # ``````````````````````````````````````````````````````````````````````````````
  4. # Function name: CreateTorProcess()
  5. #
  6. # Description:
  7. # It creates tor processes.
  8. #
  9. # Usage:
  10. # CreateTorProcess "username" "num" "num"
  11. #
  12. # Examples:
  13. # CreateTorProcess ${user_name} ${socks_port_number} ${control_port_number}
  14. #
  15. function CreateTorProcess() {
  16. local _FUNCTION_ID="CreateTorProcess"
  17. local _STATE=0
  18. local _arg_uname="$1"
  19. local _arg_socks="$2"
  20. local _arg_control="$3"
  21. # shellcheck disable=SC2154
  22. local _proc_dir="${_multitor_directory}/${_arg_socks}"
  23. local _torrc_config="${_proc_dir}/${_arg_socks}.torrc"
  24. # shellcheck disable=SC2034
  25. local _tport_state=0
  26. # We create a directory for the new tor process.
  27. CreateTorDirectory
  28. # We save the hash of the password to the configuration file.
  29. # shellcheck disable=SC2154
  30. echo "HashedControlPassword ${_pass_hash}" > "${_torrc_config}"
  31. _kstate="$?"
  32. if [[ $_kstate -eq 0 ]] ; then
  33. _logger "info" \
  34. "${_FUNCTION_ID}()" \
  35. "saved HashedControlPassword correctly"
  36. else
  37. _logger "stop" \
  38. "${_FUNCTION_ID}()" \
  39. "not saved HashedControlPassword correctly"
  40. fi
  41. # shellcheck disable=SC2154
  42. chmod 0400 "${_torrc_config}" >>"$_log_stdout" 2>&1 && \
  43. chown "${_arg_uname}:${_arg_uname}" "${_torrc_config}" >>"$_log_stdout" 2>&1
  44. _kstate="$?"
  45. if [[ $_kstate -eq 0 ]] ; then
  46. _logger "info" \
  47. "${_FUNCTION_ID}()" \
  48. "change permission and owner correctly"
  49. else
  50. _logger "stop" \
  51. "${_FUNCTION_ID}()" \
  52. "not changed permission and owner correctly"
  53. fi
  54. # shellcheck disable=SC2024
  55. sudo -u "$_arg_uname" tor -f "${_torrc_config}" \
  56. --RunAsDaemon 1 \
  57. --CookieAuthentication 0 \
  58. --SocksPort "$_arg_socks" \
  59. --ControlPort "$_arg_control" \
  60. --PidFile "${_proc_dir}/${_arg_socks}.pid" \
  61. --DataDirectory "${_proc_dir}" \
  62. --SocksBindAddress 127.0.0.1 \
  63. --NewCircuitPeriod 15 \
  64. --MaxCircuitDirtiness 15 \
  65. --NumEntryGuards 8 \
  66. --CircuitBuildTimeout 5 \
  67. --ExitRelay 0 \
  68. --RefuseUnknownExits 0 \
  69. --ClientOnly 1 \
  70. --StrictNodes 1 \
  71. --AllowSingleHopCircuits 1 \
  72. >>"$_log_stdout" 2>&1 ; _kstate="$?"
  73. if [[ $_kstate -eq 0 ]] ; then
  74. _logger "info" \
  75. "${_FUNCTION_ID}()" \
  76. "create process: '${_arg_socks}.pid'"
  77. _tor_processes_done=$((_tor_processes_done + 1))
  78. else
  79. _logger "warn" \
  80. "${_FUNCTION_ID}()" \
  81. "no process was created"
  82. _tor_processes_fail=$((_tor_processes_fail + 1))
  83. fi
  84. unset _kstate
  85. return $_STATE
  86. }