2# -*- coding: utf-8; mode: sh indent-tabs-mode: nil -*-
3# SPDX-License-Identifier: AGPL-3.0-or-later
4# shellcheck disable=SC2119,SC2001
6# shellcheck source=utils/lib.sh
7source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
8# shellcheck source=utils/brand.env
9source "${REPO_ROOT}/utils/brand.env"
11source "${REPO_ROOT}/utils/lxc-searx.env"
12in_container && lxc_set_suite_env
14# ----------------------------------------------------------------------------
16# ----------------------------------------------------------------------------
18PUBLIC_URL="${PUBLIC_URL:-http://$(uname -n)/searx}"
19PUBLIC_HOST="${PUBLIC_HOST:-$(echo "$PUBLIC_URL" | sed -e 's/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/')}"
21FILTRON_URL_PATH="${FILTRON_URL_PATH:-$(echo "${PUBLIC_URL}" \
22| sed -e 's,^.*://[^/]*\(/.*\),\1,g')}"
23[[ "${FILTRON_URL_PATH}" == "${PUBLIC_URL}" ]] && FILTRON_URL_PATH=/
25FILTRON_ETC="/etc/filtron"
26FILTRON_RULES="$FILTRON_ETC/rules.json"
28FILTRON_API="${FILTRON_API:-127.0.0.1:4005}"
29FILTRON_LISTEN="${FILTRON_LISTEN:-127.0.0.1:4004}"
30FILTRON_TARGET="${FILTRON_TARGET:-127.0.0.1:8888}"
33SERVICE_USER="${SERVICE_USER:-${SERVICE_NAME}}"
34SERVICE_HOME_BASE="${SERVICE_HOME_BASE:-/usr/local}"
35SERVICE_HOME="${SERVICE_HOME_BASE}/${SERVICE_USER}"
36SERVICE_SYSTEMD_UNIT="${SYSTEMD_UNITS}/${SERVICE_NAME}.service"
37# shellcheck disable=SC2034
38SERVICE_GROUP="${SERVICE_USER}"
40# shellcheck disable=SC2034
41SERVICE_GROUP="${SERVICE_USER}"
43GO_ENV="${SERVICE_HOME}/.go_env"
44GO_PKG_URL="https://dl.google.com/go/go1.13.5.linux-amd64.tar.gz"
45GO_TAR=$(basename "$GO_PKG_URL")
47APACHE_FILTRON_SITE="searx.conf"
48NGINX_FILTRON_SITE="searx.conf"
50# shellcheck disable=SC2034
53 "${SERVICE_SYSTEMD_UNIT}"
56# ----------------------------------------------------------------------------
58# ----------------------------------------------------------------------------
60 # shellcheck disable=SC1117
63 $(basename "$0") shell
64 $(basename "$0") install [all|user|rules]
65 $(basename "$0") update [filtron]
66 $(basename "$0") remove [all]
67 $(basename "$0") activate [service]
68 $(basename "$0") deactivate [service]
69 $(basename "$0") inspect [service]
70 $(basename "$0") option [debug-on|debug-off]
71 $(basename "$0") apache [install|remove]
72 $(basename "$0") nginx [install|remove]
75 start interactive shell from user ${SERVICE_USER}
77 :all: complete setup of filtron service
78 :user: add/remove service user '$SERVICE_USER' ($SERVICE_HOME)
79 :rules: reinstall filtron rules $FILTRON_RULES
81 Update filtron installation ($SERVICE_HOME)
83 activate and start service daemon (systemd unit)
85 stop and deactivate service daemon (systemd unit)
87 show service status and log
89 set one of the available options
91 :install: apache site with a reverse proxy (ProxyPass)
92 :remove: apache site ${APACHE_FILTRON_SITE}
94 :install: nginx site with a reverse proxy (ProxyPass)
95 :remove: nginx site ${NGINX_FILTRON_SITE}
97filtron rules: ${FILTRON_RULES}
99If needed, set PUBLIC_URL of your WEB service in the '${DOT_CONFIG#"$REPO_ROOT/"}' file::
100 PUBLIC_URL : ${PUBLIC_URL}
101 PUBLIC_HOST : ${PUBLIC_HOST}
102 SERVICE_USER : ${SERVICE_USER}
103 FILTRON_TARGET : ${FILTRON_TARGET}
104 FILTRON_API : ${FILTRON_API}
105 FILTRON_LISTEN : ${FILTRON_LISTEN}
107 if in_container; then
108 # in containers the service is listening on 0.0.0.0 (see lxc-searx.env)
109 for ip in $(global_IPs) ; do
110 if [[ $ip =~ .*:.* ]]; then
111 echo " container URL (IPv6): http://[${ip#*|}]:4005/"
114 echo " container URL (IPv4): http://${ip#*|}:4005/"
118 [[ -n ${1} ]] && err_msg "$1"
123 sudo install git wget curl \
126 local _usage="unknown or missing $1 command $2"
129 --getenv) var="$2"; echo "${!var}"; exit 0;;
130 -h|--help) usage; exit 0;;
134 interactive_shell "${SERVICE_USER}"
142 *) usage "$_usage"; exit 42;;
145 rst_title "$SERVICE_NAME" part
151 rst_title "Re-Install filtron rules"
153 install_template --no-eval "$FILTRON_RULES" root root 644
154 systemd_restart_service "${SERVICE_NAME}"
156 *) usage "$_usage"; exit 42;;
161 filtron) update_filtron ;;
162 *) usage "$_usage"; exit 42;;
168 user) drop_service_account "${SERVICE_USER}" ;;
169 *) usage "$_usage"; exit 42;;
174 service) systemd_activate_service "${SERVICE_NAME}" ;;
175 *) usage "$_usage"; exit 42;;
180 service) systemd_deactivate_service "${SERVICE_NAME}" ;;
181 *) usage "$_usage"; exit 42;;
186 install) install_apache_site ;;
187 remove) remove_apache_site ;;
188 *) usage "$_usage"; exit 42;;
193 install) install_nginx_site ;;
194 remove) remove_nginx_site ;;
195 *) usage "$_usage"; exit 42;;
200 debug-on) echo; enable_debug ;;
201 debug-off) echo; disable_debug ;;
202 *) usage "$_usage"; exit 42;;
205 *) usage "unknown or missing command $1"; exit 42;;
210 rst_title "Install $SERVICE_NAME (service)"
213 install_go "${GO_PKG_URL}" "${GO_TAR}" "${SERVICE_USER}"
217 systemd_install_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"
220 if ! service_is_available "http://${FILTRON_LISTEN}" ; then
221 err_msg "Filtron does not listening on: http://${FILTRON_LISTEN}"
223 if apache_is_installed; then
224 info_msg "Apache is installed on this host."
225 if ask_yn "Do you want to install a reverse proxy (ProxyPass)" Yn; then
228 elif nginx_is_installed; then
229 info_msg "nginx is installed on this host."
230 if ask_yn "Do you want to install a reverse proxy (ProxyPass)" Yn; then
234 if ask_yn "Do you want to inspect the installation?" Ny; then
241 rst_title "De-Install $SERVICE_NAME (service)"
244It goes without saying that this script can only be used to remove
245installations that were installed with this script."
247 if ! systemd_remove_service "${SERVICE_NAME}" "${SERVICE_SYSTEMD_UNIT}"; then
250 drop_service_account "${SERVICE_USER}"
251 rm -r "$FILTRON_ETC" 2>&1 | prefix_stdout
252 if service_is_available "${PUBLIC_URL}"; then
253 MSG="** Don't forget to remove your public site! (${PUBLIC_URL}) **" wait_key 10
258 rst_title "user $SERVICE_USER" section
260 tee_stderr 1 <<EOF | bash | prefix_stdout
261useradd --shell /bin/bash --system \
262 --home-dir "$SERVICE_HOME" \
263 --comment 'Reverse HTTP proxy to filter requests' $SERVICE_USER
265chown -R "$SERVICE_GROUP:$SERVICE_GROUP" "$SERVICE_HOME"
268 SERVICE_HOME="$(sudo -i -u "$SERVICE_USER" echo \$HOME)"
270 echo "export SERVICE_HOME=$SERVICE_HOME"
272 cat > "$GO_ENV" <<EOF
273export GOPATH=\$HOME/go-apps
274export PATH=\$PATH:\$HOME/local/go/bin:\$GOPATH/bin
276 echo "Environment $GO_ENV has been setup."
278 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER"
279grep -qFs -- 'source $GO_ENV' ~/.profile || echo 'source $GO_ENV' >> ~/.profile
283filtron_is_installed() {
284 [[ -f $SERVICE_HOME/go-apps/bin/filtron ]]
287_svcpr=" ${_Yellow}|${SERVICE_USER}|${_creset} "
290 rst_title "Install filtron in user's ~/go-apps" section
292 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
293go get -v -u github.com/asciimoo/filtron
295 install_template --no-eval "$FILTRON_RULES" root root 644
299 rst_title "Update filtron" section
301 tee_stderr <<EOF | sudo -i -u "$SERVICE_USER" 2>&1 | prefix_stdout "$_svcpr"
302go get -v -u github.com/asciimoo/filtron
308 rst_title "service status & log"
312sourced ${DOT_CONFIG#"$REPO_ROOT/"} :
314 PUBLIC_URL : ${PUBLIC_URL}
315 PUBLIC_HOST : ${PUBLIC_HOST}
316 FILTRON_URL_PATH : ${FILTRON_URL_PATH}
317 FILTRON_API : ${FILTRON_API}
318 FILTRON_LISTEN : ${FILTRON_LISTEN}
319 FILTRON_TARGET : ${FILTRON_TARGET}
323 if service_account_is_available "$SERVICE_USER"; then
324 info_msg "service account $SERVICE_USER available."
326 err_msg "service account $SERVICE_USER not available!"
328 if go_is_available "$SERVICE_USER"; then
329 info_msg "~$SERVICE_USER: go is installed"
331 err_msg "~$SERVICE_USER: go is not installed"
333 if filtron_is_installed; then
334 info_msg "~$SERVICE_USER: filtron app is installed"
336 err_msg "~$SERVICE_USER: filtron app is not installed!"
339 if ! service_is_available "http://${FILTRON_API}"; then
340 err_msg "API not available at: http://${FILTRON_API}"
343 if ! service_is_available "http://${FILTRON_LISTEN}" ; then
344 err_msg "Filtron does not listening on: http://${FILTRON_LISTEN}"
347 if service_is_available "http://${FILTRON_TARGET}" ; then
348 info_msg "Filtron's target is available at: http://${FILTRON_TARGET}"
351 if ! service_is_available "${PUBLIC_URL}"; then
352 warn_msg "Public service at ${PUBLIC_URL} is not available!"
353 if ! in_container; then
354 warn_msg "Check if public name is correct and routed or use the public IP from above."
358 if in_container; then
361 info_msg "public URL --> ${PUBLIC_URL}"
362 info_msg "internal URL --> http://${FILTRON_LISTEN}"
367 if ask_yn "Enable filtron debug mode?"; then
372 systemctl --no-pager -l status "${SERVICE_NAME}"
375 info_msg "public URL --> ${PUBLIC_URL}"
376 # shellcheck disable=SC2059
377 printf "// use ${_BCyan}CTRL-C${_creset} to stop monitoring the log"
382 journalctl -f -u "${SERVICE_NAME}"
385 if [[ $_debug_on == 1 ]]; then
393 info_msg "try to enable debug mode ..."
398 u'name': u'debug request'
402 , u'actions': [{u'name': u'log'}]
405with open('$FILTRON_RULES') as rules:
409for i in range(len(j)):
410 if j[i].get('name') == 'debug request':
417with open('$FILTRON_RULES', 'w') as rules:
418 json.dump(j, rules, indent=2, sort_keys=True)
421 systemctl restart "${SERVICE_NAME}.service"
425 info_msg "try to disable debug mode ..."
428with open('$FILTRON_RULES') as rules:
432for i in range(len(j)):
433 if j[i].get('name') == 'debug request':
438 with open('$FILTRON_RULES', 'w') as rules:
439 json.dump(j, rules, indent=2, sort_keys=True)
441 systemctl restart "${SERVICE_NAME}.service"
444install_apache_site() {
446 rst_title "Install Apache site $APACHE_FILTRON_SITE"
449This installs a reverse proxy (ProxyPass) into apache site (${APACHE_FILTRON_SITE})"
451 ! apache_is_installed && info_msg "Apache is not installed."
453 if ! ask_yn "Do you really want to continue?" Yn; then
459 "${REPO_ROOT}/utils/searx.sh" install uwsgi
461 apache_install_site --variant=filtron "${APACHE_FILTRON_SITE}"
463 info_msg "testing public url .."
464 if ! service_is_available "${PUBLIC_URL}"; then
465 err_msg "Public service at ${PUBLIC_URL} is not available!"
469remove_apache_site() {
471 rst_title "Remove Apache site $APACHE_FILTRON_SITE"
474This removes apache site ${APACHE_FILTRON_SITE}."
476 ! apache_is_installed && err_msg "Apache is not installed."
478 if ! ask_yn "Do you really want to continue?" Yn; then
482 apache_remove_site "$APACHE_FILTRON_SITE"
486install_nginx_site() {
488 rst_title "Install nginx site $NGINX_FILTRON_SITE"
491This installs a reverse proxy (ProxyPass) into nginx site (${NGINX_FILTRON_SITE})"
493 ! nginx_is_installed && info_msg "nginx is not installed."
495 if ! ask_yn "Do you really want to continue?" Yn; then
501 "${REPO_ROOT}/utils/searx.sh" install uwsgi
503 # shellcheck disable=SC2034
504 SEARX_SRC=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_SRC)
505 # shellcheck disable=SC2034
506 SEARX_URL_PATH=$("${REPO_ROOT}/utils/searx.sh" --getenv SEARX_URL_PATH)
507 nginx_install_app --variant=filtron "${NGINX_FILTRON_SITE}"
509 info_msg "testing public url .."
510 if ! service_is_available "${PUBLIC_URL}"; then
511 err_msg "Public service at ${PUBLIC_URL} is not available!"
517 rst_title "Remove nginx site $NGINX_FILTRON_SITE"
520This removes nginx site ${NGINX_FILTRON_SITE}."
522 ! nginx_is_installed && err_msg "nginx is not installed."
524 if ! ask_yn "Do you really want to continue?" Yn; then
528 nginx_remove_site "$FILTRON_FILTRON_SITE"
535 eval "echo \"$(< "${REPO_ROOT}/docs/build-templates/filtron.rst")\""
537 echo -e "\n.. START install systemd unit"
541 .. group-tab:: systemd
546 eval "echo \"$(< "${TEMPLATES}/${SERVICE_SYSTEMD_UNIT}")\"" | prefix_stdout " "
547 echo -e "\n.. END install systemd unit"
549 # for DIST_NAME in ubuntu-20.04 arch fedora centos; do
551 # DIST_ID=${DIST_NAME%-*}
552 # DIST_VERS=${DIST_NAME#*-}
553 # [[ $DIST_VERS =~ $DIST_ID ]] && DIST_VERS=
559# ----------------------------------------------------------------------------
561# ----------------------------------------------------------------------------
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list