onionsman.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/bash
  2. # Onion services manager
  3. # acetone at mail.i2p
  4. # 2022
  5. TORRC="/etc/tor/torrc"
  6. TORDIR="/var/lib/tor"
  7. ###############################
  8. ###############################
  9. ######################### funcs
  10. tor_install() {
  11. apt update
  12. apt install tor -y
  13. systemctl restart tor
  14. systemctl enable tor
  15. }
  16. tor_restart() {
  17. systemctl restart tor
  18. counter=0
  19. if [[ $? != 0 ]]; then
  20. let counter++
  21. echo "Tor service restarting failed, retrying ($counter)"
  22. sleep 3
  23. tor_restart
  24. fi;
  25. }
  26. service_del() {
  27. if [[ "$(cat $TORRC | grep "### BEGIN $1 ###")" == "" ]]; then
  28. echo -e "Service '$1' not existed"
  29. exit 1
  30. fi
  31. sed "/### BEGIN $1 ###/,$ d" $TORRC > /tmp/onionsman_tmp_1
  32. sed "1,/### END $1 ###/ d" $TORRC > /tmp/onionsman_tmp_2
  33. cat /tmp/onionsman_tmp_1 > $TORRC
  34. cat /tmp/onionsman_tmp_2 >> $TORRC
  35. tor_restart
  36. echo "Deleted: $1"
  37. }
  38. service_create() {
  39. if [[ "$(cat $TORRC | grep "### BEGIN $1 ###")" != "" ]]; then
  40. echo "'$1' looks like already existed. Use another name."
  41. exit 1
  42. fi
  43. echo -e "### BEGIN $1 ###" >> $TORRC
  44. echo -e "HiddenServiceDir $TORDIR/$1/" >> $TORRC
  45. echo -e "HiddenServicePort $2 $3" >> $TORRC
  46. echo -e "### END $1 ###" >> $TORRC
  47. tor_restart
  48. sleep 2
  49. address=$(cat $TORDIR/$1/hostname)
  50. if [[ address == "" ]]; then
  51. echo -e "Service creating failed..."
  52. exit 1
  53. fi
  54. echo -e "Service created! Address:\n$address"
  55. }
  56. service_print() {
  57. address=$(cat $TORDIR/$1/hostname)
  58. if [[ $? != 0 ]]; then
  59. echo -e "Service not existed or invalid"
  60. echo -e "Check it manually: $TORDIR/$1/hostname"
  61. exit 1
  62. fi
  63. echo $address
  64. }
  65. service_list() {
  66. all=($(cat $TORRC | grep "### BEGIN" | cut -f3 -d' ' | cut -f1 -d' '))
  67. enabled=($(cat $TORRC | grep "^### BEGIN" | cut -f3 -d' ' | cut -f1 -d' '))
  68. disabled=($(cat $TORRC | grep "#### BEGIN" | cut -f3 -d' ' | cut -f1 -d' '))
  69. echo -e "All: ${#all[@]}"
  70. if [[ ${enabled[@]} > 0 ]]; then
  71. echo -e " Enabled (${#enabled[@]}):"
  72. for j in "${!enabled[@]}"
  73. do
  74. name=${enabled[j]}
  75. echo -e " [$name] $(sed -n "/### BEGIN $name ###/,+2p" $TORRC | tail -n 1 | cut -f2,3 -d' ')"
  76. done
  77. fi
  78. if [[ ${disabled[@]} > 0 ]]; then
  79. echo -e " Disabled (${#disabled[@]}):"
  80. for i in "${!disabled[@]}"
  81. do
  82. name=${disabled[i]}
  83. echo -e " [$name] $(sed -n "/### BEGIN $name ###/,+2p" $TORRC | tail -n 1 | cut -f2,3 -d' ')"
  84. done
  85. fi
  86. }
  87. service_stop() {
  88. if [[ "$(cat $TORRC | grep "#### BEGIN $1 ###")" != "" ]]; then
  89. echo "Can not disable '$1': already stopped"
  90. exit 1
  91. fi
  92. if [[ "$(cat $TORRC | grep "### BEGIN $1 ###")" == "" ]]; then
  93. echo "Can not disable '$1': service not exist"
  94. exit 1
  95. fi
  96. str1=$(sed -n "/### BEGIN $1 ###/,+2p" $TORRC | tail -n 2 | head -n 1)
  97. str2=$(sed -n "/### BEGIN $1 ###/,+2p" $TORRC | tail -n 1)
  98. begin=$(sed -n "/^### BEGIN $1 ###/=" $TORRC)
  99. for ((i=begin; begin<i+4; begin++)); do
  100. sed -i "${begin}s/^/#/" $TORRC
  101. done
  102. tor_restart
  103. echo -e "\033[1m-\033[0m\t$str1"
  104. echo -e "\033[1m-\033[0m\t$str2"
  105. }
  106. service_start() {
  107. if [[ "$(cat $TORRC | grep "### BEGIN $1 ###")" == "" ]]; then
  108. echo "Can not start '$1': not exist"
  109. exit 1
  110. fi
  111. if [[ "$(cat $TORRC | grep "#### BEGIN $1 ###")" == "" ]]; then
  112. echo "Can not start '$1': already enabled"
  113. exit 1
  114. fi
  115. begin=$(sed -n "/^#### BEGIN $1 ###/=" $TORRC)
  116. for ((i=begin; begin<i+4; begin++)); do
  117. sed -i "${begin}s/^#//" $TORRC
  118. done
  119. str1=$(sed -n "/### BEGIN $1 ###/,+2p" $TORRC | tail -n 2 | head -n 1)
  120. str2=$(sed -n "/### BEGIN $1 ###/,+2p" $TORRC | tail -n 1)
  121. tor_restart
  122. echo -e "\033[1m+\033[0m\t$str1"
  123. echo -e "\033[1m+\033[0m\t$str2"
  124. }
  125. service_status() {
  126. if [[ "$(cat $TORRC | grep "#### BEGIN $1 ###")" != "" ]]; then
  127. echo "'$1' is disabled"
  128. else if [[ "$(cat $TORRC | grep "### BEGIN $1 ###")" != "" ]]; then
  129. echo "'$1' is enabled"
  130. else
  131. echo "Error: '$1' not exist"
  132. fi
  133. fi
  134. }
  135. ###############################
  136. ###############################
  137. ######################### calls
  138. if [[ $1 == "help" ]]; then
  139. echo -e "Usage:"
  140. echo -e "\033[1mcreate\033[0m <name> <tor port> <address> example: create testservice 80 127.0.0.1:8080"
  141. echo -e "\033[1mremove\033[0m <name> example: remove testservice"
  142. echo -e "\033[1mlist\033[0m print name list of existed services"
  143. echo -e "\033[1mprint\033[0m <name> print onion address for passed name"
  144. echo -e "\033[1mstop\033[0m <name> disable active hidden service"
  145. echo -e "\033[1mstart\033[0m <name> enable disabled hidden service"
  146. echo -e "\033[1mstatus\033[0m <name> print status of hidden service (off/on)"
  147. echo -e "\nonionsman: Onion services manager"
  148. exit 0
  149. fi
  150. if [ "$EUID" -ne 0 ]
  151. then echo "Please run as root"
  152. exit
  153. fi
  154. if [[ $1 == "install" ]]; then
  155. tor_install
  156. exit 0
  157. fi
  158. if [[ $1 == "restart" ]]; then
  159. tor_restart
  160. exit 0
  161. fi
  162. systemctl status tor &> /dev/null
  163. if [[ $? != "0" ]]; then
  164. echo "TOR seems not installed. Use \"install\" or \"restart\" parameter."
  165. exit 1
  166. fi
  167. if [[ $1 == "create" ]]; then
  168. service_create $2 $3 $4
  169. exit 0
  170. fi
  171. if [[ $1 == "remove" ]]; then
  172. service_del $2
  173. exit 0
  174. fi
  175. if [[ $1 == "print" ]]; then
  176. service_print $2
  177. exit 0
  178. fi
  179. if [[ $1 == "list" ]]; then
  180. service_list
  181. exit 0
  182. fi
  183. if [[ $1 == "stop" ]]; then
  184. service_stop $2
  185. exit 0
  186. fi
  187. if [[ $1 == "start" ]]; then
  188. service_start $2
  189. exit 0
  190. fi
  191. if [[ $1 == "status" ]]; then
  192. service_status $2
  193. exit 0
  194. fi
  195. echo "No known arguments. Use 'help'"
  196. exit 1