2021-06-04 18:09:30 +03:00
|
|
|
from app.filter import clean_query
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 03:47:42 +03:00
|
|
|
from app.request import send_tor_signal
|
2022-12-05 22:14:14 +03:00
|
|
|
from app.utils.session import generate_key
|
2021-03-08 20:22:04 +03:00
|
|
|
from app.utils.bangs import gen_bangs_json
|
2022-02-02 01:29:22 +03:00
|
|
|
from app.utils.misc import gen_file_hash, read_config_bool
|
2022-08-29 22:36:40 +03:00
|
|
|
from base64 import b64encode
|
2022-02-14 22:19:02 +03:00
|
|
|
from datetime import datetime, timedelta
|
2020-01-21 23:26:49 +03:00
|
|
|
from flask import Flask
|
2020-12-18 00:39:35 +03:00
|
|
|
import json
|
2021-04-09 16:26:16 +03:00
|
|
|
import logging.config
|
2020-01-21 23:26:49 +03:00
|
|
|
import os
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 03:47:42 +03:00
|
|
|
from stem import Signal
|
2022-01-25 22:28:06 +03:00
|
|
|
import threading
|
2021-03-28 20:27:08 +03:00
|
|
|
from dotenv import load_dotenv
|
2020-01-21 23:26:49 +03:00
|
|
|
|
2022-04-22 20:07:36 +03:00
|
|
|
from werkzeug.middleware.proxy_fix import ProxyFix
|
|
|
|
|
2022-09-20 20:11:27 +03:00
|
|
|
from app.utils.misc import read_config_bool
|
2022-11-26 02:24:38 +03:00
|
|
|
from app.version import __version__
|
2022-09-20 20:11:27 +03:00
|
|
|
|
2020-12-18 00:06:47 +03:00
|
|
|
app = Flask(__name__, static_folder=os.path.dirname(
|
|
|
|
os.path.abspath(__file__)) + '/static')
|
2021-03-28 20:27:08 +03:00
|
|
|
|
2022-04-22 20:07:36 +03:00
|
|
|
app.wsgi_app = ProxyFix(app.wsgi_app)
|
|
|
|
|
2022-08-29 22:36:40 +03:00
|
|
|
dot_env_path = (
|
|
|
|
os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
|
|
|
'../whoogle.env'))
|
|
|
|
|
2021-03-28 20:27:08 +03:00
|
|
|
# Load .env file if enabled
|
2022-09-20 20:11:27 +03:00
|
|
|
if read_config_bool('WHOOGLE_DOTENV'):
|
2022-08-29 22:36:40 +03:00
|
|
|
load_dotenv(dot_env_path)
|
2021-03-28 20:27:08 +03:00
|
|
|
|
2022-12-05 22:14:14 +03:00
|
|
|
app.enc_key = generate_key()
|
2021-11-21 02:34:37 +03:00
|
|
|
|
2022-09-20 20:11:27 +03:00
|
|
|
if read_config_bool('HTTPS_ONLY'):
|
2021-11-21 02:34:37 +03:00
|
|
|
app.config['SESSION_COOKIE_NAME'] = '__Secure-session'
|
|
|
|
app.config['SESSION_COOKIE_SECURE'] = True
|
|
|
|
|
2022-11-26 02:24:38 +03:00
|
|
|
app.config['VERSION_NUMBER'] = __version__
|
2020-12-18 00:06:47 +03:00
|
|
|
app.config['APP_ROOT'] = os.getenv(
|
|
|
|
'APP_ROOT',
|
|
|
|
os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
app.config['STATIC_FOLDER'] = os.getenv(
|
|
|
|
'STATIC_FOLDER',
|
|
|
|
os.path.join(app.config['APP_ROOT'], 'static'))
|
2021-07-01 02:00:01 +03:00
|
|
|
app.config['BUILD_FOLDER'] = os.path.join(
|
|
|
|
app.config['STATIC_FOLDER'], 'build')
|
|
|
|
app.config['CACHE_BUSTING_MAP'] = {}
|
2021-03-21 04:21:41 +03:00
|
|
|
app.config['LANGUAGES'] = json.load(open(
|
2021-08-31 02:23:19 +03:00
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/languages.json'),
|
|
|
|
encoding='utf-8'))
|
2021-03-21 04:21:41 +03:00
|
|
|
app.config['COUNTRIES'] = json.load(open(
|
2021-12-22 00:11:55 +03:00
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/countries.json'),
|
|
|
|
encoding='utf-8'))
|
2022-12-21 23:24:27 +03:00
|
|
|
app.config['TIME_PERIODS'] = json.load(open(
|
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/time_periods.json'),
|
|
|
|
encoding='utf-8'))
|
2021-05-25 00:03:02 +03:00
|
|
|
app.config['TRANSLATIONS'] = json.load(open(
|
2021-12-22 00:11:55 +03:00
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/translations.json'),
|
|
|
|
encoding='utf-8'))
|
2021-06-28 17:26:51 +03:00
|
|
|
app.config['THEMES'] = json.load(open(
|
2021-12-22 00:11:55 +03:00
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/themes.json'),
|
|
|
|
encoding='utf-8'))
|
2022-02-07 20:47:25 +03:00
|
|
|
app.config['HEADER_TABS'] = json.load(open(
|
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'settings/header_tabs.json'),
|
|
|
|
encoding='utf-8'))
|
2020-12-18 00:06:47 +03:00
|
|
|
app.config['CONFIG_PATH'] = os.getenv(
|
|
|
|
'CONFIG_VOLUME',
|
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'config'))
|
|
|
|
app.config['DEFAULT_CONFIG'] = os.path.join(
|
|
|
|
app.config['CONFIG_PATH'],
|
|
|
|
'config.json')
|
2022-02-02 01:29:22 +03:00
|
|
|
app.config['CONFIG_DISABLE'] = read_config_bool('WHOOGLE_CONFIG_DISABLE')
|
2020-12-18 00:06:47 +03:00
|
|
|
app.config['SESSION_FILE_DIR'] = os.path.join(
|
|
|
|
app.config['CONFIG_PATH'],
|
|
|
|
'session')
|
2022-06-16 20:50:13 +03:00
|
|
|
app.config['MAX_SESSION_SIZE'] = 4000 # Sessions won't exceed 4KB
|
2020-12-18 00:06:47 +03:00
|
|
|
app.config['BANG_PATH'] = os.getenv(
|
|
|
|
'CONFIG_VOLUME',
|
|
|
|
os.path.join(app.config['STATIC_FOLDER'], 'bangs'))
|
|
|
|
app.config['BANG_FILE'] = os.path.join(
|
|
|
|
app.config['BANG_PATH'],
|
|
|
|
'bangs.json')
|
2022-02-14 22:19:02 +03:00
|
|
|
|
2022-08-29 22:36:40 +03:00
|
|
|
# Ensure all necessary directories exist
|
|
|
|
if not os.path.exists(app.config['CONFIG_PATH']):
|
|
|
|
os.makedirs(app.config['CONFIG_PATH'])
|
|
|
|
|
|
|
|
if not os.path.exists(app.config['SESSION_FILE_DIR']):
|
|
|
|
os.makedirs(app.config['SESSION_FILE_DIR'])
|
|
|
|
|
|
|
|
if not os.path.exists(app.config['BANG_PATH']):
|
|
|
|
os.makedirs(app.config['BANG_PATH'])
|
|
|
|
|
|
|
|
if not os.path.exists(app.config['BUILD_FOLDER']):
|
|
|
|
os.makedirs(app.config['BUILD_FOLDER'])
|
|
|
|
|
|
|
|
# Session values
|
|
|
|
app_key_path = os.path.join(app.config['CONFIG_PATH'], 'whoogle.key')
|
|
|
|
if os.path.exists(app_key_path):
|
|
|
|
app.config['SECRET_KEY'] = open(app_key_path, 'r').read()
|
|
|
|
else:
|
|
|
|
app.config['SECRET_KEY'] = str(b64encode(os.urandom(32)))
|
|
|
|
with open(app_key_path, 'w') as key_file:
|
|
|
|
key_file.write(app.config['SECRET_KEY'])
|
|
|
|
key_file.close()
|
|
|
|
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=365)
|
|
|
|
|
|
|
|
# NOTE: SESSION_COOKIE_SAMESITE must be set to 'lax' to allow the user's
|
|
|
|
# previous session to persist when accessing the instance from an external
|
|
|
|
# link. Setting this value to 'strict' causes Whoogle to revalidate a new
|
|
|
|
# session, and fail, resulting in cookies being disabled.
|
|
|
|
app.config['SESSION_COOKIE_SAMESITE'] = 'Lax'
|
|
|
|
|
2022-02-14 22:19:02 +03:00
|
|
|
# Config fields that are used to check for updates
|
2021-11-02 19:35:40 +03:00
|
|
|
app.config['RELEASES_URL'] = 'https://github.com/' \
|
|
|
|
'benbusby/whoogle-search/releases'
|
2022-02-14 22:19:02 +03:00
|
|
|
app.config['LAST_UPDATE_CHECK'] = datetime.now() - timedelta(hours=24)
|
|
|
|
app.config['HAS_UPDATE'] = ''
|
2021-06-15 17:14:42 +03:00
|
|
|
|
|
|
|
# The alternative to Google Translate is treated a bit differently than other
|
|
|
|
# social media site alternatives, in that it is used for any translation
|
|
|
|
# related searches.
|
2022-03-21 19:08:52 +03:00
|
|
|
translate_url = os.getenv('WHOOGLE_ALT_TL', 'https://farside.link/lingva')
|
2021-06-15 17:14:42 +03:00
|
|
|
if not translate_url.startswith('http'):
|
|
|
|
translate_url = 'https://' + translate_url
|
|
|
|
app.config['TRANSLATE_URL'] = translate_url
|
|
|
|
|
2021-03-07 22:04:05 +03:00
|
|
|
app.config['CSP'] = 'default-src \'none\';' \
|
2021-06-15 17:14:42 +03:00
|
|
|
'frame-src ' + translate_url + ';' \
|
2021-03-21 02:52:06 +03:00
|
|
|
'manifest-src \'self\';' \
|
2021-05-05 19:51:11 +03:00
|
|
|
'img-src \'self\' data:;' \
|
2021-03-07 22:04:05 +03:00
|
|
|
'style-src \'self\' \'unsafe-inline\';' \
|
|
|
|
'script-src \'self\';' \
|
|
|
|
'media-src \'self\';' \
|
2021-08-31 16:57:50 +03:00
|
|
|
'connect-src \'self\';'
|
2020-06-02 21:54:47 +03:00
|
|
|
|
2022-08-29 22:36:40 +03:00
|
|
|
# Generate DDG bang filter
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 03:47:42 +03:00
|
|
|
if not os.path.exists(app.config['BANG_FILE']):
|
2022-01-25 22:28:06 +03:00
|
|
|
json.dump({}, open(app.config['BANG_FILE'], 'w'))
|
|
|
|
bangs_thread = threading.Thread(
|
|
|
|
target=gen_bangs_json,
|
|
|
|
args=(app.config['BANG_FILE'],))
|
|
|
|
bangs_thread.start()
|
2020-10-10 22:55:14 +03:00
|
|
|
|
2021-07-01 02:00:01 +03:00
|
|
|
# Build new mapping of static files for cache busting
|
|
|
|
cache_busting_dirs = ['css', 'js']
|
|
|
|
for cb_dir in cache_busting_dirs:
|
|
|
|
full_cb_dir = os.path.join(app.config['STATIC_FOLDER'], cb_dir)
|
|
|
|
for cb_file in os.listdir(full_cb_dir):
|
|
|
|
# Create hash from current file state
|
|
|
|
full_cb_path = os.path.join(full_cb_dir, cb_file)
|
|
|
|
cb_file_link = gen_file_hash(full_cb_dir, cb_file)
|
|
|
|
build_path = os.path.join(app.config['BUILD_FOLDER'], cb_file_link)
|
2021-07-02 22:28:27 +03:00
|
|
|
|
|
|
|
try:
|
|
|
|
os.symlink(full_cb_path, build_path)
|
|
|
|
except FileExistsError:
|
|
|
|
# Symlink hasn't changed, ignore
|
|
|
|
pass
|
2021-07-01 02:00:01 +03:00
|
|
|
|
|
|
|
# Create mapping for relative path urls
|
|
|
|
map_path = build_path.replace(app.config['APP_ROOT'], '')
|
|
|
|
if map_path.startswith('/'):
|
|
|
|
map_path = map_path[1:]
|
|
|
|
app.config['CACHE_BUSTING_MAP'][cb_file] = map_path
|
|
|
|
|
|
|
|
# Templating functions
|
|
|
|
app.jinja_env.globals.update(clean_query=clean_query)
|
|
|
|
app.jinja_env.globals.update(
|
|
|
|
cb_url=lambda f: app.config['CACHE_BUSTING_MAP'][f])
|
|
|
|
|
Add tor and http/socks proxy support (#137)
* Add tor and http/socks proxy support
Allows users to enable/disable tor from the config menu, which will
forward all requests through Tor.
Also adds support for setting environment variables for alternative
proxy support. Setting the following variables will forward requests
through the proxy:
- WHOOGLE_PROXY_USER (optional)
- WHOOGLE_PROXY_PASS (optional)
- WHOOGLE_PROXY_TYPE (required)
- Can be "http", "socks4", or "socks5"
- WHOOGLE_PROXY_LOC (required)
- Format: "<ip address>:<port>"
See #30
* Refactor acquire_tor_conn -> acquire_tor_identity
Also updated travis CI to set up tor
* Add check for Tor socket on init, improve Tor error handling
Initializing the app sends a heartbeat request to Tor to check for
availability, and updates the home page config options accordingly. This
heartbeat is sent on every request, to ensure Tor support can be
reconfigured without restarting the entire app.
If Tor support is enabled, and a subsequent request fails, then a new
TorError exception is raised, and the Tor feature is disabled until a
valid connection is restored.
The max attempts has been updated to 10, since 5 seemed a bit too low
for how quickly the attempts go by.
* Change send_tor_signal arg type, update function doc
send_tor_signal now accepts a stem.Signal arg (a bit cleaner tbh). Also
added the doc string for the "disable" attribute in TorError.
* Fix tor identity logic in Request.send
* Update proxy init, change proxyloc var name
Proxy is now only initialized if both type and location are specified,
as neither have a default fallback and both are required. I suppose the
type could fall back to http, but seems safer this way.
Also refactored proxyurl -> proxyloc for the runtime args in order to
match the Dockerfile args.
* Add tor/proxy support for Docker builds, fix opensearch/init
The Dockerfile is now updated to include support for Tor configuration,
with a working torrc file included in the repo.
An issue with opensearch was fixed as well, which was uncovered during
testing and was simple enough to fix here. Likewise, DDG bang gen was
updated to only ever happen if the file didn't exist previously, as
testing with the file being regenerated every time was tedious.
* Add missing "@" for socks proxy requests
2020-10-29 03:47:42 +03:00
|
|
|
# Attempt to acquire tor identity, to determine if Tor config is available
|
|
|
|
send_tor_signal(Signal.HEARTBEAT)
|
|
|
|
|
2020-12-18 00:06:47 +03:00
|
|
|
from app import routes # noqa
|
2021-04-09 16:26:16 +03:00
|
|
|
|
|
|
|
# Disable logging from imported modules
|
|
|
|
logging.config.dictConfig({
|
|
|
|
'version': 1,
|
|
|
|
'disable_existing_loggers': True,
|
|
|
|
})
|