run_server.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/bin/bash
  2. cd "$(dirname "$0")";
  3. . src/ask.sh #import the ask()
  4. . i/template/tor.html.sh #import tor template page | tor.html()
  5. . i/template/standalone.sh #import standalone html page | print_standalone()
  6. . bin/config.sh &> /dev/null #import config
  7. chmod 770 bin/*
  8. chmod 770 i/template/*
  9. chmod 770 src/*
  10. #help page
  11. function purge_guide() {
  12. cat <<EOF
  13. -p
  14. --delete (it does not include the static contents)
  15. --delete-all (purge all files, it does include the static contents)
  16. --web (delete web/ files)
  17. EOF
  18. }
  19. function guide() {
  20. echo "usage: ./`basename $0` [-h] [-n] [-b|-c] [-g] [-r] [-t|-s] | [ -p --delete | --delete-all | --web ]"
  21. echo "-h help page"
  22. echo "-n generate new tor address"
  23. echo "-b build html."
  24. echo "-c create static contents (as image etc.)"
  25. echo "-g generate the web site as use created files"
  26. echo "-r run tor server/container"
  27. echo "-t view tor adress"
  28. echo "-s status"
  29. purge_guide;
  30. }
  31. #generate new tor adress [-n] option
  32. function new_address() {
  33. function run_new_address() {
  34. rm -rf web/;
  35. mkdir web;
  36. docker run -it --rm -v $(pwd)/web:/web tor-docker generate ^copy && echo "tor address is generated";
  37. }
  38. if ask "do you want the generate new tor address?" N; then
  39. run_new_address;
  40. else
  41. echo "okay."
  42. fi
  43. }
  44. #build html files [-b] option
  45. function build() {
  46. rm -rf i/out/*;
  47. mkdir -p i/out/{copy,pdf,rss};
  48. mkdir -p .tmp/;
  49. bin/generate_author_pages.sh -gl
  50. bin/generate_copy_files.sh -egl
  51. bin/generate_websites_index.sh && echo "websites index page generated"
  52. }
  53. #create static contents [-c] option
  54. function create() {
  55. mkdir -p i/listen/copy
  56. bin/generate_pdf_files.sh -iem #-c option is delete already created files
  57. }
  58. #generate the web site as use created files [-g option]
  59. function main() {
  60. mkdir -p public/;
  61. chmod 0755 public/;
  62. find public/free -type l -exec unlink {} \; &> /dev/null && echo "symbolic links removed"
  63. rm -rf public/* &> /dev/null && echo "pages deleted"
  64. mkdir public/free &> /dev/null && echo "pdf raw dir created"
  65. cp -r i/out/* public/ && echo "i/out/* files are copied."
  66. cp -r i/listen/copy/ public/ && echo "i/listen/copy/ dir copied"
  67. cp -r i/contents/* public/ && echo "i/contents/* are copied"
  68. ln -s $pdf_dir/* public/free && echo "/var/pdf/ symbolic link created"
  69. tor.html > public/tor.html && echo "tor.html created"
  70. print_standalone > public/print.html && echo "print.html created"
  71. mkdir public/index && echo "index page creating.."
  72. mkdir public/author-index && echo "pagination index page creating.."
  73. nohup bash src/index.sh > public/index/index.html &
  74. nohup bash src/index.sh.pagination > public/author-index/index.html &
  75. find public/ -type f -exec chmod 0644 {} \; &> /dev/null && echo "files permissions setted"
  76. find public/ -type d -exec chmod 0755 {} \; &> /dev/null && echo "directory permissions setted"
  77. }
  78. #run tor server/container [-r] option
  79. function tor_server() {
  80. function start_tor_server() {
  81. docker rm hiddensite -f &> /dev/null; #for security deleting the old container
  82. docker run -d --restart=always -h "onion-service" --network="host" --name hiddensite -v $(pwd)/web:/web tor-docker;
  83. }
  84. start_tor_server && echo "tor server is running.. hostname: $(grep -oP "server_name\s+\K\w+" web/site.conf).onion"
  85. }
  86. #purge everything [-p] option
  87. function purge() {
  88. function delete_default() {
  89. rm -rf i/out/* && echo "i/out/* are deleted";
  90. rm -rf public/* && echo "public/* are deleted";
  91. rm -rf .tmp/* && echo ".tmp/* are deleted";
  92. find -path '.tmp/.*' -delete && echo ".tmp/.* are deleted";
  93. }
  94. function delete_static() {
  95. rm -rf i/listen/copy/* && echo "i/listen/* are deleted";
  96. }
  97. case $purge_arg in
  98. --delete)
  99. delete_default
  100. ;;
  101. --delete-all)
  102. delete_default
  103. if ask "do you want the delete static files?" N; then
  104. delete_static;
  105. else
  106. echo "--delete-all option is bypassed"
  107. fi
  108. ;;
  109. --web)
  110. if ask "do you want the delete web files?" N; then
  111. rm -rf web/ && echo "web/ directory is deleted";
  112. else
  113. echo "--web option is bypassed";
  114. fi
  115. ;;
  116. *)
  117. echo "unknown option: $purge_arg"
  118. purge_guide;
  119. ;;
  120. esac
  121. }
  122. while getopts ":hnbcgrp:ts" opt; do
  123. case ${opt} in
  124. h ) guide;
  125. ;;
  126. n ) new_address;
  127. ;;
  128. b ) build;
  129. ;;
  130. c ) create;
  131. ;;
  132. g ) main;
  133. ;;
  134. r ) tor_server;
  135. ;;
  136. p )
  137. purge_arg=${OPTARG}
  138. purge
  139. ;;
  140. t )
  141. if [ -f "web/site.conf" ]; then
  142. echo -e "$(grep -oP "server_name\s+\K\w+" web/site.conf).onion"
  143. else
  144. echo -e "\033[31mplease first generate new tor adress!\e[0m ./`basename $0` -n "
  145. fi
  146. ;;
  147. s )
  148. docker exec -it hiddensite service nginx status
  149. docker exec -it hiddensite service tor status
  150. docker exec -it hiddensite ps aux|grep tor
  151. ;;
  152. : )
  153. echo "Missing option argument for -$OPTARG [ --default | --all ]"
  154. purge_guide
  155. ;;
  156. esac
  157. done
  158. if [ "$#" -lt 1 ]; then
  159. guide;
  160. fi