freedombone-app-search 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Search engine application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@robotics.uk.to>
  17. #
  18. # This program is free software: you can redistribute it and/or modify
  19. # it under the terms of the GNU Affero General Public License as published by
  20. # the Free Software Foundation, either version 3 of the License, or
  21. # (at your option) any later version.
  22. #
  23. # This program is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU Affero General Public License for more details.
  27. #
  28. # You should have received a copy of the GNU Affero General Public License
  29. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. SEARCH_ENGINE_REPO="https://github.com/asciimoo/searx"
  31. SEARCH_ENGINE_COMMIT='fee556c9904637051a9ba874ba7e71cd9f10789f'
  32. SEARCH_ENGINE_PATH=/etc
  33. SEARCH_ENGINE_ONION_PORT=8094
  34. SEARCH_ENGINE_ONION_HOSTNAME=
  35. SEARCH_ENGINE_LOGIN_TEXT=$"Search engine login"
  36. SEARCH_ENGINE_PASSWORD=
  37. function backup_search_engine {
  38. echo ''
  39. }
  40. function remove_search_engine {
  41. if ! grep -Fxq "install_search_engine" $COMPLETION_FILE; then
  42. return
  43. fi
  44. systemctl stop searx
  45. systemctl disable searx
  46. rm /etc/systemd/system/searx.service
  47. function_check remove_onion_service
  48. remove_onion_service searx ${SEARCH_ENGINE_ONION_PORT}
  49. userdel -r searx
  50. nginx_dissite searx
  51. if [ -f /etc/nginx/sites-available/searx ]; then
  52. rm /etc/nginx/sites-available/searx
  53. fi
  54. if [ -d ${SEARCH_ENGINE_PATH}/searx ]; then
  55. rm -rf ${SEARCH_ENGINE_PATH}/searx
  56. fi
  57. sed -i '/install_search_engine/d' $COMPLETION_FILE
  58. sed -i '/Search engine /d' $COMPLETION_FILE
  59. }
  60. function install_search_engine {
  61. # Note: currently socks5 outgoing proxies to other search engines does not work
  62. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  63. return
  64. fi
  65. if [ ! -d /etc/nginx ]; then
  66. echo $'Webserver is not installed'
  67. exit 62429
  68. fi
  69. # update to a new commit if needed
  70. set_repo_commit $SEARCH_ENGINE_PATH/searx "Search engine commit" "$SEARCH_ENGINE_COMMIT" $SEARCH_ENGINE_REPO
  71. if grep "Search engine key" $COMPLETION_FILE; then
  72. if [ -f ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml ]; then
  73. # note: this might change to a --tor option in a later version
  74. if ! grep 'socks5://127.0.0.1:9050' ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml; then
  75. echo 'outgoing: # communication with search engines' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  76. echo ' proxies:' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  77. echo ' http : socks5://127.0.0.1:9050' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  78. echo ' https: socks5://127.0.0.1:9050' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  79. fi
  80. SEARCH_ENGINE_SECRET_KEY=$(cat $COMPLETION_FILE | grep "Search engine key" | awk -F ':' '{print $2}')
  81. sed -i "s|secret_key.*|secret_key : \"${SEARCH_ENGINE_SECRET_KEY}\"|g" ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  82. if [ -f /var/lib/tor/hidden_service_searx/hostname ]; then
  83. SEARCH_ENGINE_ONION_HOSTNAME=$(echo /var/lib/tor/hidden_service_searx/hostname)
  84. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARCH_ENGINE_ONION_HOSTNAME}\/' ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  85. fi
  86. fi
  87. fi
  88. if grep -Fxq "install_search_engine" $COMPLETION_FILE; then
  89. return
  90. fi
  91. apt-get -y install python-pip libyaml-dev python-werkzeug python-babel python-lxml apache2-utils
  92. apt-get -y install git build-essential libxslt-dev python-dev python-virtualenv python-pybabel zlib1g-dev uwsgi uwsgi-plugin-python libapache2-mod-uwsgi
  93. pip install --upgrade pip
  94. pip install certifi
  95. if [ ! "$?" = "0" ]; then
  96. echo $'Failed to install certifi'
  97. exit 737692
  98. fi
  99. pip install pyyaml
  100. if [ ! "$?" = "0" ]; then
  101. echo $'Failed to install pyyaml'
  102. exit 469242
  103. fi
  104. pip install flask --upgrade
  105. if [ ! "$?" = "0" ]; then
  106. echo $'Failed to install flask'
  107. exit 888575
  108. fi
  109. pip install flask_restless --upgrade
  110. if [ ! "$?" = "0" ]; then
  111. echo $'Failed to install flask_restless'
  112. exit 54835
  113. fi
  114. pip install flask_babel --upgrade
  115. if [ ! "$?" = "0" ]; then
  116. echo $'Failed to install flask_babel'
  117. exit 63738
  118. fi
  119. if [ ! -d $SEARCH_ENGINE_PATH ]; then
  120. mkdir -p $SEARCH_ENGINE_PATH
  121. fi
  122. # clone the repo
  123. cd $SEARCH_ENGINE_PATH
  124. function_check git_clone
  125. git_clone $SEARCH_ENGINE_REPO searx
  126. git checkout $SEARCH_ENGINE_COMMIT -b $SEARCH_ENGINE_COMMIT
  127. if ! grep -q "Search engine commit" $COMPLETION_FILE; then
  128. echo "Search engine commit:$SEARCH_ENGINE_COMMIT" >> $COMPLETION_FILE
  129. else
  130. sed -i "s/Search engine commit.*/Search engine commit:$SEARCH_ENGINE_COMMIT/g" $COMPLETION_FILE
  131. fi
  132. # create an onion service
  133. SEARCH_ENGINE_ONION_HOSTNAME=$(add_onion_service searx 80 ${SEARCH_ENGINE_ONION_PORT})
  134. if ! grep "Search engine onion domain" $COMPLETION_FILE; then
  135. echo "Search engine onion domain:${SEARCH_ENGINE_ONION_HOSTNAME}" >> $COMPLETION_FILE
  136. else
  137. sed -i "s|Search engine onion domain.*|Search engine onion domain:${SEARCH_ENGINE_ONION_HOSTNAME}|g" $COMPLETION_FILE
  138. fi
  139. # an unprivileged user to run as
  140. useradd -d ${SEARCH_ENGINE_PATH}/searx/ -s /bin/false searx
  141. adduser searx debian-tor
  142. # daemon
  143. echo '[Unit]' > /etc/systemd/system/searx.service
  144. echo 'Description=Searx (search engine)' >> /etc/systemd/system/searx.service
  145. echo 'After=syslog.target' >> /etc/systemd/system/searx.service
  146. echo 'After=network.target' >> /etc/systemd/system/searx.service
  147. echo '' >> /etc/systemd/system/searx.service
  148. echo '[Service]' >> /etc/systemd/system/searx.service
  149. echo 'Type=simple' >> /etc/systemd/system/searx.service
  150. echo 'User=searx' >> /etc/systemd/system/searx.service
  151. echo 'Group=searx' >> /etc/systemd/system/searx.service
  152. echo "WorkingDirectory=${SEARCH_ENGINE_PATH}/searx" >> /etc/systemd/system/searx.service
  153. echo "ExecStart=/usr/bin/python ${SEARCH_ENGINE_PATH}/searx/searx/webapp.py" >> /etc/systemd/system/searx.service
  154. echo 'Restart=always' >> /etc/systemd/system/searx.service
  155. echo 'Environment="USER=searx"' >> /etc/systemd/system/searx.service
  156. echo '' >> /etc/systemd/system/searx.service
  157. echo '[Install]' >> /etc/systemd/system/searx.service
  158. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/searx.service
  159. # create a webserver file
  160. echo 'server {' > /etc/nginx/sites-available/searx
  161. echo " listen 127.0.0.1:${SEARCH_ENGINE_ONION_PORT} default_server;" >> /etc/nginx/sites-available/searx
  162. echo " root ${SEARCH_ENGINE_PATH}/searx;" >> /etc/nginx/sites-available/searx
  163. echo " server_name ${SEARCH_ENGINE_ONION_HOSTNAME};" >> /etc/nginx/sites-available/searx
  164. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  165. echo " error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/searx
  166. echo '' >> /etc/nginx/sites-available/searx
  167. function_check nginx_limits
  168. nginx_limits searx '1M'
  169. function_check nginx_disable_sniffing
  170. nginx_disable_sniffing searx
  171. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/searx
  172. echo '' >> /etc/nginx/sites-available/searx
  173. echo ' location / {' >> /etc/nginx/sites-available/searx
  174. echo ' proxy_pass http://localhost:8888;' >> /etc/nginx/sites-available/searx
  175. echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/searx
  176. echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/searx
  177. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/searx
  178. echo ' proxy_set_header X-Remote-Port $remote_port;' >> /etc/nginx/sites-available/searx
  179. echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/searx
  180. echo ' proxy_redirect off;' >> /etc/nginx/sites-available/searx
  181. echo " auth_basic \"${SEARCH_ENGINE_LOGIN_TEXT}\";" >> /etc/nginx/sites-available/searx
  182. echo ' auth_basic_user_file /etc/nginx/.htpasswd;' >> /etc/nginx/sites-available/searx
  183. echo ' }' >> /etc/nginx/sites-available/searx
  184. echo '' >> /etc/nginx/sites-available/searx
  185. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/searx
  186. echo '' >> /etc/nginx/sites-available/searx
  187. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/searx
  188. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/searx
  189. echo '' >> /etc/nginx/sites-available/searx
  190. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/searx
  191. echo ' allow all;' >> /etc/nginx/sites-available/searx
  192. echo ' log_not_found off;' >> /etc/nginx/sites-available/searx
  193. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  194. echo ' }' >> /etc/nginx/sites-available/searx
  195. echo '}' >> /etc/nginx/sites-available/searx
  196. # replace the secret key
  197. if ! grep "Search engine key" $COMPLETION_FILE; then
  198. SEARCH_ENGINE_SECRET_KEY="$(create_password 30)"
  199. echo "Search engine key:${SEARCH_ENGINE_SECRET_KEY}" >> $COMPLETION_FILE
  200. else
  201. SEARCH_ENGINE_SECRET_KEY=$(cat $COMPLETION_FILE | grep "Search engine key" | awk -F ':' '{print $2}')
  202. fi
  203. sed -i "s|secret_key.*|secret_key : \"${SEARCH_ENGINE_SECRET_KEY}\"|g" ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  204. sed -i "s|secret_key.*|secret_key : \"${SEARCH_ENGINE_SECRET_KEY}\"|g" ${SEARCH_ENGINE_PATH}/searx/searx/settings_robot.yml
  205. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARCH_ENGINE_ONION_HOSTNAME}\/' ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  206. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARCH_ENGINE_ONION_HOSTNAME}\/' ${SEARCH_ENGINE_PATH}/searx/searx/settings_robot.yml
  207. # note: this might change to a --tor option in a later version
  208. if ! grep 'socks5://127.0.0.1:9050' ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml; then
  209. echo 'outgoing: # communication with search engines' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  210. echo ' proxies:' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  211. echo ' http : socks5://127.0.0.1:9050' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  212. echo ' https: socks5://127.0.0.1:9050' >> ${SEARCH_ENGINE_PATH}/searx/searx/settings.yml
  213. fi
  214. chown -R searx:searx ${SEARCH_ENGINE_PATH}/searx
  215. # enable the site
  216. nginx_ensite searx
  217. # restart the web server
  218. systemctl restart php5-fpm
  219. systemctl restart nginx
  220. # start the daemon
  221. systemctl enable searx.service
  222. systemctl daemon-reload
  223. systemctl start searx.service
  224. if ! grep -q "Your search engine password is" /home/$MY_USERNAME/README; then
  225. if [ ${#SEARCH_ENGINE_PASSWORD} -lt 8 ]; then
  226. if [ -f $IMAGE_PASSWORD_FILE ]; then
  227. SEARCH_ENGINE_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  228. else
  229. SEARCH_ENGINE_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  230. fi
  231. fi
  232. echo "$SEARCH_ENGINE_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd $MY_USERNAME
  233. echo '' >> /home/$MY_USERNAME/README
  234. echo '' >> /home/$MY_USERNAME/README
  235. echo $'Search Engine' >> /home/$MY_USERNAME/README
  236. echo '=============' >> /home/$MY_USERNAME/README
  237. echo $"Search engine onion domain: ${SEARCH_ENGINE_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  238. echo $"Your search engine password is: $SEARCH_ENGINE_PASSWORD" >> /home/$MY_USERNAME/README
  239. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  240. chmod 600 /home/$MY_USERNAME/README
  241. fi
  242. echo 'install_search_engine' >> $COMPLETION_FILE
  243. }
  244. # NOTE: deliberately no exit 0