freedombone-app-searx 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 change_password_searx {
  39. echo -n ''
  40. }
  41. function reconfigure_searx {
  42. echo -n ''
  43. }
  44. function upgrade_searx {
  45. if ! grep -Fxq "install_searx" $COMPLETION_FILE; then
  46. return
  47. fi
  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 ! grep -Fxq "install_searx" $COMPLETION_FILE; 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. if grep -Fxq "install_searx" $COMPLETION_FILE; then
  109. return
  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. if ! grep -q "Search engine commit" $COMPLETION_FILE; then
  148. echo "Search engine commit:$SEARX_COMMIT" >> $COMPLETION_FILE
  149. else
  150. sed -i "s/Search engine commit.*/Search engine commit:$SEARX_COMMIT/g" $COMPLETION_FILE
  151. fi
  152. # create an onion service
  153. SEARX_ONION_HOSTNAME=$(add_onion_service searx 80 ${SEARX_ONION_PORT})
  154. if ! grep "Search engine onion domain" $COMPLETION_FILE; then
  155. echo "Search engine onion domain:${SEARX_ONION_HOSTNAME}" >> $COMPLETION_FILE
  156. else
  157. sed -i "s|Search engine onion domain.*|Search engine onion domain:${SEARX_ONION_HOSTNAME}|g" $COMPLETION_FILE
  158. fi
  159. # an unprivileged user to run as
  160. useradd -d ${SEARX_PATH}/searx/ -s /bin/false searx
  161. adduser searx debian-tor
  162. # daemon
  163. echo '[Unit]' > /etc/systemd/system/searx.service
  164. echo 'Description=Searx (search engine)' >> /etc/systemd/system/searx.service
  165. echo 'After=syslog.target' >> /etc/systemd/system/searx.service
  166. echo 'After=network.target' >> /etc/systemd/system/searx.service
  167. echo '' >> /etc/systemd/system/searx.service
  168. echo '[Service]' >> /etc/systemd/system/searx.service
  169. echo 'Type=simple' >> /etc/systemd/system/searx.service
  170. echo 'User=searx' >> /etc/systemd/system/searx.service
  171. echo 'Group=searx' >> /etc/systemd/system/searx.service
  172. echo "WorkingDirectory=${SEARX_PATH}/searx" >> /etc/systemd/system/searx.service
  173. echo "ExecStart=/usr/bin/python ${SEARX_PATH}/searx/searx/webapp.py" >> /etc/systemd/system/searx.service
  174. echo 'Restart=always' >> /etc/systemd/system/searx.service
  175. echo 'Environment="USER=searx"' >> /etc/systemd/system/searx.service
  176. echo '' >> /etc/systemd/system/searx.service
  177. echo '[Install]' >> /etc/systemd/system/searx.service
  178. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/searx.service
  179. # create a webserver file
  180. echo 'server {' > /etc/nginx/sites-available/searx
  181. echo " listen 127.0.0.1:${SEARX_ONION_PORT} default_server;" >> /etc/nginx/sites-available/searx
  182. echo " root ${SEARX_PATH}/searx;" >> /etc/nginx/sites-available/searx
  183. echo " server_name ${SEARX_ONION_HOSTNAME};" >> /etc/nginx/sites-available/searx
  184. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  185. echo " error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/searx
  186. echo '' >> /etc/nginx/sites-available/searx
  187. function_check nginx_limits
  188. nginx_limits searx '1M'
  189. function_check nginx_disable_sniffing
  190. nginx_disable_sniffing searx
  191. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/searx
  192. echo '' >> /etc/nginx/sites-available/searx
  193. echo ' location / {' >> /etc/nginx/sites-available/searx
  194. echo ' proxy_pass http://localhost:8888;' >> /etc/nginx/sites-available/searx
  195. echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/searx
  196. echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/searx
  197. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/searx
  198. echo ' proxy_set_header X-Remote-Port $remote_port;' >> /etc/nginx/sites-available/searx
  199. echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/searx
  200. echo ' proxy_redirect off;' >> /etc/nginx/sites-available/searx
  201. echo " auth_basic \"${SEARX_LOGIN_TEXT}\";" >> /etc/nginx/sites-available/searx
  202. echo ' auth_basic_user_file /etc/nginx/.htpasswd;' >> /etc/nginx/sites-available/searx
  203. echo ' }' >> /etc/nginx/sites-available/searx
  204. echo '' >> /etc/nginx/sites-available/searx
  205. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/searx
  206. echo '' >> /etc/nginx/sites-available/searx
  207. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/searx
  208. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/searx
  209. echo '' >> /etc/nginx/sites-available/searx
  210. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/searx
  211. echo ' allow all;' >> /etc/nginx/sites-available/searx
  212. echo ' log_not_found off;' >> /etc/nginx/sites-available/searx
  213. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  214. echo ' }' >> /etc/nginx/sites-available/searx
  215. echo '}' >> /etc/nginx/sites-available/searx
  216. # replace the secret key
  217. if ! grep "Search engine key" $COMPLETION_FILE; then
  218. SEARX_SECRET_KEY="$(create_password 30)"
  219. echo "Search engine key:${SEARX_SECRET_KEY}" >> $COMPLETION_FILE
  220. else
  221. SEARX_SECRET_KEY=$(cat $COMPLETION_FILE | grep "Search engine key" | awk -F ':' '{print $2}')
  222. fi
  223. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  224. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings_robot.yml
  225. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  226. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings_robot.yml
  227. # note: this might change to a --tor option in a later version
  228. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  229. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  230. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  231. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  232. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  233. fi
  234. chown -R searx:searx ${SEARX_PATH}/searx
  235. # enable the site
  236. nginx_ensite searx
  237. # restart the web server
  238. systemctl restart php5-fpm
  239. systemctl restart nginx
  240. # start the daemon
  241. systemctl enable searx.service
  242. systemctl daemon-reload
  243. systemctl start searx.service
  244. if ! grep -q "Your search engine password is" /home/$MY_USERNAME/README; then
  245. if [ ${#SEARX_PASSWORD} -lt 8 ]; then
  246. if [ -f $IMAGE_PASSWORD_FILE ]; then
  247. SEARX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  248. else
  249. SEARX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  250. fi
  251. fi
  252. echo "$SEARX_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd $MY_USERNAME
  253. echo '' >> /home/$MY_USERNAME/README
  254. echo '' >> /home/$MY_USERNAME/README
  255. echo $'Search Engine' >> /home/$MY_USERNAME/README
  256. echo '=============' >> /home/$MY_USERNAME/README
  257. echo $"Search engine onion domain: ${SEARX_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  258. echo $"Your search engine password is: $SEARX_PASSWORD" >> /home/$MY_USERNAME/README
  259. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  260. chmod 600 /home/$MY_USERNAME/README
  261. fi
  262. echo 'install_searx' >> $COMPLETION_FILE
  263. }
  264. # NOTE: deliberately no exit 0