freedombone-app-searx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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@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. 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. }
  105. function install_searx {
  106. # Note: currently socks5 outgoing proxies to other search engines does not work
  107. if [ ! -d /etc/nginx ]; then
  108. echo $'Webserver is not installed'
  109. exit 62429
  110. fi
  111. apt-get -y install python-pip libyaml-dev python-werkzeug python-babel python-lxml apache2-utils
  112. apt-get -y install git build-essential libxslt-dev python-dev python-virtualenv python-pybabel zlib1g-dev uwsgi uwsgi-plugin-python libapache2-mod-uwsgi
  113. pip install --upgrade pip
  114. pip install certifi
  115. if [ ! "$?" = "0" ]; then
  116. echo $'Failed to install certifi'
  117. exit 737692
  118. fi
  119. pip install pyyaml
  120. if [ ! "$?" = "0" ]; then
  121. echo $'Failed to install pyyaml'
  122. exit 469242
  123. fi
  124. pip install flask --upgrade
  125. if [ ! "$?" = "0" ]; then
  126. echo $'Failed to install flask'
  127. exit 888575
  128. fi
  129. pip install flask_restless --upgrade
  130. if [ ! "$?" = "0" ]; then
  131. echo $'Failed to install flask_restless'
  132. exit 54835
  133. fi
  134. pip install flask_babel --upgrade
  135. if [ ! "$?" = "0" ]; then
  136. echo $'Failed to install flask_babel'
  137. exit 63738
  138. fi
  139. if [ ! -d $SEARX_PATH ]; then
  140. mkdir -p $SEARX_PATH
  141. fi
  142. # clone the repo
  143. cd $SEARX_PATH
  144. function_check git_clone
  145. git_clone $SEARX_REPO searx
  146. git checkout $SEARX_COMMIT -b $SEARX_COMMIT
  147. set_completion_param "searx commit" "$SEARX_COMMIT"
  148. # create an onion service
  149. SEARX_ONION_HOSTNAME=$(add_onion_service searx 80 ${SEARX_ONION_PORT})
  150. # an unprivileged user to run as
  151. useradd -d ${SEARX_PATH}/searx/ -s /bin/false searx
  152. adduser searx debian-tor
  153. # daemon
  154. echo '[Unit]' > /etc/systemd/system/searx.service
  155. echo 'Description=Searx (search engine)' >> /etc/systemd/system/searx.service
  156. echo 'After=syslog.target' >> /etc/systemd/system/searx.service
  157. echo 'After=network.target' >> /etc/systemd/system/searx.service
  158. echo '' >> /etc/systemd/system/searx.service
  159. echo '[Service]' >> /etc/systemd/system/searx.service
  160. echo 'Type=simple' >> /etc/systemd/system/searx.service
  161. echo 'User=searx' >> /etc/systemd/system/searx.service
  162. echo 'Group=searx' >> /etc/systemd/system/searx.service
  163. echo "WorkingDirectory=${SEARX_PATH}/searx" >> /etc/systemd/system/searx.service
  164. echo "ExecStart=/usr/bin/python ${SEARX_PATH}/searx/searx/webapp.py" >> /etc/systemd/system/searx.service
  165. echo 'Restart=always' >> /etc/systemd/system/searx.service
  166. echo 'Environment="USER=searx"' >> /etc/systemd/system/searx.service
  167. echo '' >> /etc/systemd/system/searx.service
  168. echo '[Install]' >> /etc/systemd/system/searx.service
  169. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/searx.service
  170. # create a webserver file
  171. echo 'server {' > /etc/nginx/sites-available/searx
  172. echo " listen 127.0.0.1:${SEARX_ONION_PORT} default_server;" >> /etc/nginx/sites-available/searx
  173. echo " root ${SEARX_PATH}/searx;" >> /etc/nginx/sites-available/searx
  174. echo " server_name ${SEARX_ONION_HOSTNAME};" >> /etc/nginx/sites-available/searx
  175. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  176. echo " error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/searx
  177. echo '' >> /etc/nginx/sites-available/searx
  178. function_check nginx_disable_sniffing
  179. nginx_disable_sniffing searx
  180. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/searx
  181. echo '' >> /etc/nginx/sites-available/searx
  182. echo ' location / {' >> /etc/nginx/sites-available/searx
  183. function_check nginx_limits
  184. nginx_limits searx '1M'
  185. echo ' proxy_pass http://localhost:8888;' >> /etc/nginx/sites-available/searx
  186. echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/searx
  187. echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/searx
  188. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/searx
  189. echo ' proxy_set_header X-Remote-Port $remote_port;' >> /etc/nginx/sites-available/searx
  190. echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/searx
  191. echo ' proxy_redirect off;' >> /etc/nginx/sites-available/searx
  192. echo " auth_basic \"${SEARX_LOGIN_TEXT}\";" >> /etc/nginx/sites-available/searx
  193. echo ' auth_basic_user_file /etc/nginx/.htpasswd;' >> /etc/nginx/sites-available/searx
  194. echo ' }' >> /etc/nginx/sites-available/searx
  195. echo '' >> /etc/nginx/sites-available/searx
  196. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/searx
  197. echo '' >> /etc/nginx/sites-available/searx
  198. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/searx
  199. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/searx
  200. echo '' >> /etc/nginx/sites-available/searx
  201. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/searx
  202. echo ' allow all;' >> /etc/nginx/sites-available/searx
  203. echo ' log_not_found off;' >> /etc/nginx/sites-available/searx
  204. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  205. echo ' }' >> /etc/nginx/sites-available/searx
  206. echo '}' >> /etc/nginx/sites-available/searx
  207. # replace the secret key
  208. if ! grep "searx key" $COMPLETION_FILE; then
  209. SEARX_SECRET_KEY="$(create_password 30)"
  210. fi
  211. set_completion_param "searx key" "${SEARX_SECRET_KEY}"
  212. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  213. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings_robot.yml
  214. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  215. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings_robot.yml
  216. # note: this might change to a --tor option in a later version
  217. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  218. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  219. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  220. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  221. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  222. fi
  223. chown -R searx:searx ${SEARX_PATH}/searx
  224. # enable the site
  225. nginx_ensite searx
  226. # restart the web server
  227. systemctl restart php5-fpm
  228. systemctl restart nginx
  229. # start the daemon
  230. systemctl enable searx.service
  231. systemctl daemon-reload
  232. systemctl start searx.service
  233. if ! grep -q "Your search engine password is" /home/$MY_USERNAME/README; then
  234. if [ ${#SEARX_PASSWORD} -lt 8 ]; then
  235. if [ -f $IMAGE_PASSWORD_FILE ]; then
  236. SEARX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  237. else
  238. SEARX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  239. fi
  240. fi
  241. echo "$SEARX_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd $MY_USERNAME
  242. echo '' >> /home/$MY_USERNAME/README
  243. echo '' >> /home/$MY_USERNAME/README
  244. echo $'Search Engine' >> /home/$MY_USERNAME/README
  245. echo '=============' >> /home/$MY_USERNAME/README
  246. echo $"Search engine onion domain: ${SEARX_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  247. echo $"Your search engine password is: $SEARX_PASSWORD" >> /home/$MY_USERNAME/README
  248. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  249. chmod 600 /home/$MY_USERNAME/README
  250. fi
  251. APP_INSTALLED=1
  252. }
  253. # NOTE: deliberately no exit 0