freedombone-app-searx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/bash
  2. #
  3. # .---. . .
  4. # | | |
  5. # |--- .--. .-. .-. .-.| .-. .--.--. |.-. .-. .--. .-.
  6. # | | (.-' (.-' ( | ( )| | | | )( )| | (.-'
  7. # ' ' --' --' -' - -' ' ' -' -' -' ' - --'
  8. #
  9. # Freedom in the Cloud
  10. #
  11. # Searx engine application
  12. #
  13. # License
  14. # =======
  15. #
  16. # Copyright (C) 2014-2016 Bob Mottram <bob@freedombone.net>
  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. VARIANTS=''
  31. IN_DEFAULT_INSTALL=0
  32. SHOW_ON_ABOUT=1
  33. SEARX_REPO="https://github.com/asciimoo/searx"
  34. SEARX_COMMIT='fee556c9904637051a9ba874ba7e71cd9f10789f'
  35. SEARX_PATH=/etc
  36. SEARX_ONION_PORT=8094
  37. SEARX_ONION_HOSTNAME=
  38. SEARX_LOGIN_TEXT=$"Search engine login"
  39. SEARX_PASSWORD=
  40. searx_variables=(SEARX_REPO
  41. SEARX_COMMIT
  42. SEARX_PATH
  43. SEARX_LOGIN_TEXT
  44. MY_USERNAME
  45. SYSTEM_TYPE)
  46. function install_interactive_searx {
  47. echo -n ''
  48. APP_INSTALLED=1
  49. }
  50. function change_password_searx {
  51. echo -n ''
  52. }
  53. function reconfigure_searx {
  54. echo -n ''
  55. }
  56. function upgrade_searx {
  57. set_repo_commit $SEARX_PATH/searx "Search engine commit" "$SEARX_COMMIT" $SEARX_REPO
  58. if grep "Search engine key" $COMPLETION_FILE; then
  59. if [ -f ${SEARX_PATH}/searx/searx/settings.yml ]; then
  60. # note: this might change to a --tor option in a later version
  61. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  62. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  63. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  64. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  65. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  66. fi
  67. SEARX_SECRET_KEY=$(get_completion_param "searx key")
  68. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  69. if [ -f /var/lib/tor/hidden_service_searx/hostname ]; then
  70. SEARX_ONION_HOSTNAME=$(echo /var/lib/tor/hidden_service_searx/hostname)
  71. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  72. fi
  73. fi
  74. fi
  75. }
  76. function backup_local_searx {
  77. echo -n ''
  78. }
  79. function restore_local_searx {
  80. echo -n ''
  81. }
  82. function backup_remote_searx {
  83. echo -n ''
  84. }
  85. function restore_remote_searx {
  86. echo -n ''
  87. }
  88. function remove_searx {
  89. systemctl stop searx
  90. systemctl disable searx
  91. rm /etc/systemd/system/searx.service
  92. function_check remove_onion_service
  93. remove_onion_service searx ${SEARX_ONION_PORT}
  94. userdel -r searx
  95. nginx_dissite searx
  96. if [ -f /etc/nginx/sites-available/searx ]; then
  97. rm /etc/nginx/sites-available/searx
  98. fi
  99. if [ -d ${SEARX_PATH}/searx ]; then
  100. rm -rf ${SEARX_PATH}/searx
  101. fi
  102. remove_completion_param install_searx
  103. sed -i '/Search engine /d' $COMPLETION_FILE
  104. sed -i '/Search Engine /d' /home/$MY_USERNAME/README
  105. sed -i '/Search engine /d' /home/$MY_USERNAME/README
  106. sed -i '/search engine /d' /home/$MY_USERNAME/README
  107. }
  108. function install_searx {
  109. # Note: currently socks5 outgoing proxies to other search engines does not work
  110. if [ ! -d /etc/nginx ]; then
  111. echo $'Webserver is not installed'
  112. exit 62429
  113. fi
  114. apt-get -yq install python-pip libyaml-dev python-werkzeug python-babel python-lxml apache2-utils
  115. apt-get -yq install git build-essential libxslt-dev python-dev python-virtualenv python-pybabel zlib1g-dev uwsgi uwsgi-plugin-python libapache2-mod-uwsgi
  116. pip install --upgrade pip
  117. pip install certifi
  118. if [ ! "$?" = "0" ]; then
  119. echo $'Failed to install certifi'
  120. exit 737692
  121. fi
  122. pip install pyyaml
  123. if [ ! "$?" = "0" ]; then
  124. echo $'Failed to install pyyaml'
  125. exit 469242
  126. fi
  127. pip install flask --upgrade
  128. if [ ! "$?" = "0" ]; then
  129. echo $'Failed to install flask'
  130. exit 888575
  131. fi
  132. pip install flask_restless --upgrade
  133. if [ ! "$?" = "0" ]; then
  134. echo $'Failed to install flask_restless'
  135. exit 54835
  136. fi
  137. pip install flask_babel --upgrade
  138. if [ ! "$?" = "0" ]; then
  139. echo $'Failed to install flask_babel'
  140. exit 63738
  141. fi
  142. if [ ! -d $SEARX_PATH ]; then
  143. mkdir -p $SEARX_PATH
  144. fi
  145. # clone the repo
  146. cd $SEARX_PATH
  147. function_check git_clone
  148. git_clone $SEARX_REPO searx
  149. git checkout $SEARX_COMMIT -b $SEARX_COMMIT
  150. set_completion_param "searx commit" "$SEARX_COMMIT"
  151. # create an onion service
  152. SEARX_ONION_HOSTNAME=$(add_onion_service searx 80 ${SEARX_ONION_PORT})
  153. # an unprivileged user to run as
  154. useradd -d ${SEARX_PATH}/searx/ -s /bin/false searx
  155. adduser searx debian-tor
  156. # daemon
  157. echo '[Unit]' > /etc/systemd/system/searx.service
  158. echo 'Description=Searx (search engine)' >> /etc/systemd/system/searx.service
  159. echo 'After=syslog.target' >> /etc/systemd/system/searx.service
  160. echo 'After=network.target' >> /etc/systemd/system/searx.service
  161. echo '' >> /etc/systemd/system/searx.service
  162. echo '[Service]' >> /etc/systemd/system/searx.service
  163. echo 'Type=simple' >> /etc/systemd/system/searx.service
  164. echo 'User=searx' >> /etc/systemd/system/searx.service
  165. echo 'Group=searx' >> /etc/systemd/system/searx.service
  166. echo "WorkingDirectory=${SEARX_PATH}/searx" >> /etc/systemd/system/searx.service
  167. echo "ExecStart=/usr/bin/python ${SEARX_PATH}/searx/searx/webapp.py" >> /etc/systemd/system/searx.service
  168. echo 'Restart=always' >> /etc/systemd/system/searx.service
  169. echo 'Environment="USER=searx"' >> /etc/systemd/system/searx.service
  170. echo '' >> /etc/systemd/system/searx.service
  171. echo '[Install]' >> /etc/systemd/system/searx.service
  172. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/searx.service
  173. # create a webserver file
  174. echo 'server {' > /etc/nginx/sites-available/searx
  175. echo " listen 127.0.0.1:${SEARX_ONION_PORT} default_server;" >> /etc/nginx/sites-available/searx
  176. echo " root ${SEARX_PATH}/searx;" >> /etc/nginx/sites-available/searx
  177. echo " server_name ${SEARX_ONION_HOSTNAME};" >> /etc/nginx/sites-available/searx
  178. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/searx
  179. echo " error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/searx
  180. echo '' >> /etc/nginx/sites-available/searx
  181. function_check nginx_disable_sniffing
  182. nginx_disable_sniffing searx
  183. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/searx
  184. echo '' >> /etc/nginx/sites-available/searx
  185. echo ' location / {' >> /etc/nginx/sites-available/searx
  186. function_check nginx_limits
  187. nginx_limits searx '1M'
  188. echo ' proxy_pass http://localhost:8888;' >> /etc/nginx/sites-available/searx
  189. echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/searx
  190. echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/searx
  191. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/searx
  192. echo ' proxy_set_header X-Remote-Port $remote_port;' >> /etc/nginx/sites-available/searx
  193. echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/searx
  194. echo ' proxy_redirect off;' >> /etc/nginx/sites-available/searx
  195. echo " auth_basic \"${SEARX_LOGIN_TEXT}\";" >> /etc/nginx/sites-available/searx
  196. echo ' auth_basic_user_file /etc/nginx/.htpasswd;' >> /etc/nginx/sites-available/searx
  197. echo ' }' >> /etc/nginx/sites-available/searx
  198. echo '' >> /etc/nginx/sites-available/searx
  199. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/searx
  200. echo '' >> /etc/nginx/sites-available/searx
  201. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/searx
  202. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/searx
  203. echo '' >> /etc/nginx/sites-available/searx
  204. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/searx
  205. echo ' allow all;' >> /etc/nginx/sites-available/searx
  206. echo ' log_not_found off;' >> /etc/nginx/sites-available/searx
  207. echo ' access_log /dev/null;' >> /etc/nginx/sites-available/searx
  208. echo ' }' >> /etc/nginx/sites-available/searx
  209. echo '}' >> /etc/nginx/sites-available/searx
  210. # replace the secret key
  211. if ! grep "searx key" $COMPLETION_FILE; then
  212. SEARX_SECRET_KEY="$(create_password 30)"
  213. fi
  214. set_completion_param "searx key" "${SEARX_SECRET_KEY}"
  215. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  216. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings_robot.yml
  217. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  218. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings_robot.yml
  219. # note: this might change to a --tor option in a later version
  220. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  221. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  222. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  223. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  224. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  225. fi
  226. chown -R searx:searx ${SEARX_PATH}/searx
  227. # enable the site
  228. nginx_ensite searx
  229. # restart the web server
  230. systemctl restart php5-fpm
  231. systemctl restart nginx
  232. # start the daemon
  233. systemctl enable searx.service
  234. systemctl daemon-reload
  235. systemctl start searx.service
  236. if ! grep -q "Your search engine password is" /home/$MY_USERNAME/README; then
  237. if [ ${#SEARX_PASSWORD} -lt 8 ]; then
  238. if [ -f $IMAGE_PASSWORD_FILE ]; then
  239. SEARX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  240. else
  241. SEARX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  242. fi
  243. fi
  244. echo "$SEARX_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd $MY_USERNAME
  245. echo '' >> /home/$MY_USERNAME/README
  246. echo '' >> /home/$MY_USERNAME/README
  247. echo $'# Search Engine' >> /home/$MY_USERNAME/README
  248. echo $"Search engine onion domain: ${SEARX_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  249. echo $"Your search engine password is: $SEARX_PASSWORD" >> /home/$MY_USERNAME/README
  250. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  251. chmod 600 /home/$MY_USERNAME/README
  252. fi
  253. APP_INSTALLED=1
  254. }
  255. # NOTE: deliberately no exit 0