From 9a5b095305e132c54c9f5844e06accef8c7d5533 Mon Sep 17 00:00:00 2001 From: Shimul Date: Sun, 28 Mar 2021 22:57:08 +0530 Subject: [PATCH] Allow setting environment variables in whoogle.env (#237) This allows the user to enable their preferred settings in a variety of ways, depending on their deployment preference. Values added to whoogle.env can be enabled using WHOOGLE_DOTENV=1, in which case all values in the env var file will overwrite defaults or user provided settings. Co-authored-by: Ben Busby --- Dockerfile | 4 ++++ README.md | 5 ++++- app/__init__.py | 8 ++++++++ app/utils/results.py | 3 +++ docker-compose.yml | 2 ++ requirements.txt | 1 + run | 2 +- whoogle.env | 14 ++++++++++++++ 8 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 whoogle.env diff --git a/Dockerfile b/Dockerfile index 6d0f2d3..3e3854c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,6 +37,9 @@ ENV WHOOGLE_PROXY_TYPE=$proxytype ARG proxyloc='' ENV WHOOGLE_PROXY_LOC=$proxyloc +ARG whoogle_dotenv='' +ENV WHOOGLE_DOTENV=$whoogle_dotenv + ARG use_https='' ENV HTTPS_ONLY=$use_https @@ -59,6 +62,7 @@ COPY misc/tor/torrc /etc/tor/torrc COPY misc/tor/start-tor.sh misc/tor/start-tor.sh COPY app/ app/ COPY run . +COPY whoogle.env . EXPOSE $EXPOSE_PORT diff --git a/README.md b/README.md index 95a9450..ee2b3cc 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,8 @@ Description=Whoogle #Environment=WHOOGLE_ALT_YT=invidious.snopyta.org #Environment=WHOOGLE_ALT_IG=bibliogram.art/u #Environment=WHOOGLE_ALT_RD=libredd.it +# Load values from dotenv only +#Environment=WHOOGLE_DOTENV=1 Type=simple User=root WorkingDirectory= @@ -233,10 +235,11 @@ Depending on your preferences, you can also deploy the app yourself on your own - A bit more experience or willingness to work through issues ## Environment Variables -There are a few optional environment variables available for customizing a Whoogle instance: +There are a few optional environment variables available for customizing a Whoogle instance. These can be set manually, or copied into `whoogle.env` and enabled by setting `WHOOGLE_DOTENV=1`. | Variable | Description | | ------------------ | -------------------------------------------------------------- | +| WHOOGLE_DOTENV | Load environment variables in `whoogle.env` | | WHOOGLE_USER | The username for basic auth. WHOOGLE_PASS must also be set if used. | | WHOOGLE_PASS | The password for basic auth. WHOOGLE_USER must also be set if used. | | WHOOGLE_PROXY_USER | The username of the proxy server. | diff --git a/app/__init__.py b/app/__init__.py index 9d965b2..23422c3 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -6,9 +6,17 @@ from flask_session import Session import json import os from stem import Signal +from dotenv import load_dotenv app = Flask(__name__, static_folder=os.path.dirname( os.path.abspath(__file__)) + '/static') + +# Load .env file if enabled +if os.getenv("WHOOGLE_DOTENV", ''): + dotenv_path = '../whoogle.env' + load_dotenv(os.path.join(os.path.dirname(os.path.abspath(__file__)), + dotenv_path)) + app.user_elements = {} app.default_key_set = generate_user_keys() app.no_cookie_ips = [] diff --git a/app/utils/results.py b/app/utils/results.py index 2a9e60e..7db53ed 100644 --- a/app/utils/results.py +++ b/app/utils/results.py @@ -3,6 +3,7 @@ import os import urllib.parse as urlparse from urllib.parse import parse_qs + SKIP_ARGS = ['ref_src', 'utm'] SKIP_PREFIX = ['//www.', '//mobile.', '//m.'] GOOG_STATIC = 'www.gstatic.com' @@ -12,6 +13,7 @@ BLANK_B64 = ('data:image/png;base64,' 'iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAQAAAAnOwc2AAAAD0lEQVR42mNkw' 'AIYh7IgAAVVAAuInjI5AAAAAElFTkSuQmCC') + # Ad keywords BLACKLIST = [ 'ad', 'anuncio', 'annuncio', 'annonce', 'Anzeige', '广告', '廣告', 'Reklama', @@ -70,6 +72,7 @@ def get_site_alt(link: str) -> str: str: An updated (or ignored) result link """ + for site_key in SITE_ALTS.keys(): if site_key not in link: continue diff --git a/docker-compose.yml b/docker-compose.yml index 3f48604..f2e8797 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,6 +37,8 @@ services: #- WHOOGLE_ALT_YT=invidious.snopyta.org #- WHOOGLE_ALT_IG=bibliogram.art/u #- WHOOGLE_ALT_RD=libredd.it + # Load environment variables from whoogle.env + #- WHOOGLE_DOTENV=1 ports: - 5000:5000 restart: unless-stopped diff --git a/requirements.txt b/requirements.txt index aeeaad1..0e4cdf0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -31,3 +31,4 @@ urllib3==1.25.9 waitress==1.4.3 wcwidth==0.1.9 Werkzeug==0.16.0 +python-dotenv==0.16.0 diff --git a/run b/run index 4ace594..f4eac50 100755 --- a/run +++ b/run @@ -22,5 +22,5 @@ else mkdir -p "$STATIC_FOLDER" python3 -um app \ --host "${ADDRESS:-0.0.0.0}" \ - --port "${PORT:-"${EXPOSE_PORT:-5000}"}" + --port "${PORT:-"${EXPOSE_PORT:-5000}"}" --debug fi diff --git a/whoogle.env b/whoogle.env new file mode 100644 index 0000000..a817360 --- /dev/null +++ b/whoogle.env @@ -0,0 +1,14 @@ +# You can set Whoogle environment variables here, but must set +# WHOOGLE_DOTENV=1 in your deployment to enable these values + +#WHOOGLE_ALT_TW=nitter.net +#WHOOGLE_ALT_YT=invidious.snopyta.org +#WHOOGLE_ALT_IG=bibliogram.art/u +#WHOOGLE_ALT_RD=libredd.it +#WHOOGLE_USER="" +#WHOOGLE_PASS="" +#WHOOGLE_PROXY_USER="" +#WHOOGLE_PROXY_PASS="" +#WHOOGLE_PROXY_TYPE="" +#WHOOGLE_PROXY_LOC="" +#HTTPS_ONLY=1