freedombone-app-searx 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 reconfigure_searx {
  39. echo -n ''
  40. }
  41. function upgrade_searx {
  42. if ! grep -Fxq "install_searx" $COMPLETION_FILE; then
  43. return
  44. fi
  45. set_repo_commit $SEARX_PATH/searx "Search engine commit" "$SEARX_COMMIT" $SEARX_REPO
  46. if grep "Search engine key" $COMPLETION_FILE; then
  47. if [ -f ${SEARX_PATH}/searx/searx/settings.yml ]; then
  48. # note: this might change to a --tor option in a later version
  49. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  50. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  51. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  52. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  53. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  54. fi
  55. SEARX_SECRET_KEY=$(cat $COMPLETION_FILE | grep "Search engine key" | awk -F ':' '{print $2}')
  56. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  57. if [ -f /var/lib/tor/hidden_service_searx/hostname ]; then
  58. SEARX_ONION_HOSTNAME=$(echo /var/lib/tor/hidden_service_searx/hostname)
  59. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  60. fi
  61. fi
  62. fi
  63. }
  64. function backup_local_searx {
  65. echo -n ''
  66. }
  67. function restore_local_searx {
  68. echo -n ''
  69. }
  70. function backup_remote_searx {
  71. echo -n ''
  72. }
  73. function restore_remote_searx {
  74. echo -n ''
  75. }
  76. function remove_searx {
  77. if ! grep -Fxq "install_searx" $COMPLETION_FILE; then
  78. return
  79. fi
  80. systemctl stop searx
  81. systemctl disable searx
  82. rm /etc/systemd/system/searx.service
  83. function_check remove_onion_service
  84. remove_onion_service searx ${SEARX_ONION_PORT}
  85. userdel -r searx
  86. nginx_dissite searx
  87. if [ -f /etc/nginx/sites-available/searx ]; then
  88. rm /etc/nginx/sites-available/searx
  89. fi
  90. if [ -d ${SEARX_PATH}/searx ]; then
  91. rm -rf ${SEARX_PATH}/searx
  92. fi
  93. sed -i '/install_searx/d' $COMPLETION_FILE
  94. sed -i '/Search engine /d' $COMPLETION_FILE
  95. }
  96. function install_searx {
  97. # Note: currently socks5 outgoing proxies to other search engines does not work
  98. if [[ $SYSTEM_TYPE == "$VARIANT_MESH" ]]; then
  99. return
  100. fi
  101. if [ ! -d /etc/nginx ]; then
  102. echo $'Webserver is not installed'
  103. exit 62429
  104. fi
  105. if grep -Fxq "install_searx" $COMPLETION_FILE; then
  106. return
  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. if ! grep "Search engine onion domain" $COMPLETION_FILE; then
  152. echo "Search engine onion domain:${SEARX_ONION_HOSTNAME}" >> $COMPLETION_FILE
  153. else
  154. sed -i "s|Search engine onion domain.*|Search engine onion domain:${SEARX_ONION_HOSTNAME}|g" $COMPLETION_FILE
  155. fi
  156. # an unprivileged user to run as
  157. useradd -d ${SEARX_PATH}/searx/ -s /bin/false searx
  158. adduser searx debian-tor
  159. # daemon
  160. echo '[Unit]' > /etc/systemd/system/searx.service
  161. echo 'Description=Searx (search engine)' >> /etc/systemd/system/searx.service
  162. echo 'After=syslog.target' >> /etc/systemd/system/searx.service
  163. echo 'After=network.target' >> /etc/systemd/system/searx.service
  164. echo '' >> /etc/systemd/system/searx.service
  165. echo '[Service]' >> /etc/systemd/system/searx.service
  166. echo 'Type=simple' >> /etc/systemd/system/searx.service
  167. echo 'User=searx' >> /etc/systemd/system/searx.service
  168. echo 'Group=searx' >> /etc/systemd/system/searx.service
  169. echo "WorkingDirectory=${SEARX_PATH}/searx" >> /etc/systemd/system/searx.service
  170. echo "ExecStart=/usr/bin/python ${SEARX_PATH}/searx/searx/webapp.py" >> /etc/systemd/system/searx.service
  171. echo 'Restart=always' >> /etc/systemd/system/searx.service
  172. echo 'Environment="USER=searx"' >> /etc/systemd/system/searx.service
  173. echo '' >> /etc/systemd/system/searx.service
  174. echo '[Install]' >> /etc/systemd/system/searx.service
  175. echo 'WantedBy=multi-user.target' >> /etc/systemd/system/searx.service
  176. # create a webserver file
  177. echo 'server {' > /etc/nginx/sites-available/searx
  178. echo " listen 127.0.0.1:${SEARX_ONION_PORT} default_server;" >> /etc/nginx/sites-available/searx
  179. echo " root ${SEARX_PATH}/searx;" >> /etc/nginx/sites-available/searx
  180. echo " server_name ${SEARX_ONION_HOSTNAME};" >> /etc/nginx/sites-available/searx
  181. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  182. echo " error_log /var/log/searx_error.log $WEBSERVER_LOG_LEVEL;" >> /etc/nginx/sites-available/searx
  183. echo '' >> /etc/nginx/sites-available/searx
  184. function_check nginx_limits
  185. nginx_limits searx '1M'
  186. function_check nginx_disable_sniffing
  187. nginx_disable_sniffing searx
  188. echo ' add_header Strict-Transport-Security max-age=0;' >> /etc/nginx/sites-available/searx
  189. echo '' >> /etc/nginx/sites-available/searx
  190. echo ' location / {' >> /etc/nginx/sites-available/searx
  191. echo ' proxy_pass http://localhost:8888;' >> /etc/nginx/sites-available/searx
  192. echo ' proxy_set_header Host $host;' >> /etc/nginx/sites-available/searx
  193. echo ' proxy_set_header X-Real-IP $remote_addr;' >> /etc/nginx/sites-available/searx
  194. echo ' proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' >> /etc/nginx/sites-available/searx
  195. echo ' proxy_set_header X-Remote-Port $remote_port;' >> /etc/nginx/sites-available/searx
  196. echo ' proxy_set_header X-Forwarded-Proto $scheme;' >> /etc/nginx/sites-available/searx
  197. echo ' proxy_redirect off;' >> /etc/nginx/sites-available/searx
  198. echo " auth_basic \"${SEARX_LOGIN_TEXT}\";" >> /etc/nginx/sites-available/searx
  199. echo ' auth_basic_user_file /etc/nginx/.htpasswd;' >> /etc/nginx/sites-available/searx
  200. echo ' }' >> /etc/nginx/sites-available/searx
  201. echo '' >> /etc/nginx/sites-available/searx
  202. echo ' fastcgi_buffers 64 4K;' >> /etc/nginx/sites-available/searx
  203. echo '' >> /etc/nginx/sites-available/searx
  204. echo ' error_page 403 /core/templates/403.php;' >> /etc/nginx/sites-available/searx
  205. echo ' error_page 404 /core/templates/404.php;' >> /etc/nginx/sites-available/searx
  206. echo '' >> /etc/nginx/sites-available/searx
  207. echo ' location = /robots.txt {' >> /etc/nginx/sites-available/searx
  208. echo ' allow all;' >> /etc/nginx/sites-available/searx
  209. echo ' log_not_found off;' >> /etc/nginx/sites-available/searx
  210. echo ' access_log off;' >> /etc/nginx/sites-available/searx
  211. echo ' }' >> /etc/nginx/sites-available/searx
  212. echo '}' >> /etc/nginx/sites-available/searx
  213. # replace the secret key
  214. if ! grep "Search engine key" $COMPLETION_FILE; then
  215. SEARX_SECRET_KEY="$(create_password 30)"
  216. echo "Search engine key:${SEARX_SECRET_KEY}" >> $COMPLETION_FILE
  217. else
  218. SEARX_SECRET_KEY=$(cat $COMPLETION_FILE | grep "Search engine key" | awk -F ':' '{print $2}')
  219. fi
  220. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings.yml
  221. sed -i "s|secret_key.*|secret_key : \"${SEARX_SECRET_KEY}\"|g" ${SEARX_PATH}/searx/searx/settings_robot.yml
  222. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings.yml
  223. sed -i '0,/RE/s/base_url.*/base_url : \"http://${SEARX_ONION_HOSTNAME}\/' ${SEARX_PATH}/searx/searx/settings_robot.yml
  224. # note: this might change to a --tor option in a later version
  225. if ! grep 'socks5://127.0.0.1:9050' ${SEARX_PATH}/searx/searx/settings.yml; then
  226. echo 'outgoing: # communication with search engines' >> ${SEARX_PATH}/searx/searx/settings.yml
  227. echo ' proxies:' >> ${SEARX_PATH}/searx/searx/settings.yml
  228. echo ' http : socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  229. echo ' https: socks5://127.0.0.1:9050' >> ${SEARX_PATH}/searx/searx/settings.yml
  230. fi
  231. chown -R searx:searx ${SEARX_PATH}/searx
  232. # enable the site
  233. nginx_ensite searx
  234. # restart the web server
  235. systemctl restart php5-fpm
  236. systemctl restart nginx
  237. # start the daemon
  238. systemctl enable searx.service
  239. systemctl daemon-reload
  240. systemctl start searx.service
  241. if ! grep -q "Your search engine password is" /home/$MY_USERNAME/README; then
  242. if [ ${#SEARX_PASSWORD} -lt 8 ]; then
  243. if [ -f $IMAGE_PASSWORD_FILE ]; then
  244. SEARX_PASSWORD="$(printf `cat $IMAGE_PASSWORD_FILE`)"
  245. else
  246. SEARX_PASSWORD="$(create_password ${MINIMUM_PASSWORD_LENGTH})"
  247. fi
  248. fi
  249. echo "$SEARX_PASSWORD" | htpasswd -i -s -c /etc/nginx/.htpasswd $MY_USERNAME
  250. echo '' >> /home/$MY_USERNAME/README
  251. echo '' >> /home/$MY_USERNAME/README
  252. echo $'Search Engine' >> /home/$MY_USERNAME/README
  253. echo '=============' >> /home/$MY_USERNAME/README
  254. echo $"Search engine onion domain: ${SEARX_ONION_HOSTNAME}" >> /home/$MY_USERNAME/README
  255. echo $"Your search engine password is: $SEARX_PASSWORD" >> /home/$MY_USERNAME/README
  256. chown $MY_USERNAME:$MY_USERNAME /home/$MY_USERNAME/README
  257. chmod 600 /home/$MY_USERNAME/README
  258. fi
  259. echo 'install_searx' >> $COMPLETION_FILE
  260. }
  261. # NOTE: deliberately no exit 0