freedombone-app-searx 12KB

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