Add option --localhost_only.

This commit is contained in:
voussoir 2020-09-30 15:15:49 -07:00
parent 59cc76f8d9
commit 5e6a666ca2
2 changed files with 13 additions and 1 deletions

View file

@ -42,6 +42,7 @@ site.jinja_env.trim_blocks = True
site.jinja_env.lstrip_blocks = True site.jinja_env.lstrip_blocks = True
jinja_filters.register_all(site) jinja_filters.register_all(site)
site.debug = True site.debug = True
site.localhost_only = False
session_manager = sessions.SessionManager(maxlen=10000) session_manager = sessions.SessionManager(maxlen=10000)
file_cache_manager = caching.FileCacheManager( file_cache_manager = caching.FileCacheManager(
@ -73,6 +74,12 @@ def decorate_and_route(*route_args, **route_kwargs):
return wrapper return wrapper
site.route = decorate_and_route site.route = decorate_and_route
@site.before_request
def before_request():
ip = request.remote_addr
if site.localhost_only and ip != '127.0.0.1':
flask.abort(403)
gzip_minimum_size = 500 gzip_minimum_size = 500
gzip_maximum_size = 5 * 2**20 gzip_maximum_size = 5 * 2**20
gzip_level = 3 gzip_level = 3

View file

@ -26,7 +26,7 @@ site = backend.site
HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https') HTTPS_DIR = pathclass.Path(__file__).parent.with_child('https')
def etiquette_flask_launch(create, port, use_https): def etiquette_flask_launch(create, port, localhost_only, use_https):
if use_https is None: if use_https is None:
use_https = port == 443 use_https = port == 443
@ -43,6 +43,9 @@ def etiquette_flask_launch(create, port, use_https):
application=site, application=site,
) )
if localhost_only:
site.localhost_only = True
backend.common.init_photodb(create=create) backend.common.init_photodb(create=create)
message = f'Starting server on port {port}' message = f'Starting server on port {port}'
@ -59,6 +62,7 @@ def etiquette_flask_launch_argparse(args):
return etiquette_flask_launch( return etiquette_flask_launch(
create=args.create, create=args.create,
port=args.port, port=args.port,
localhost_only=args.localhost_only,
use_https=args.use_https, use_https=args.use_https,
) )
@ -67,6 +71,7 @@ def main(argv):
parser.add_argument('port', nargs='?', type=int, default=5000) parser.add_argument('port', nargs='?', type=int, default=5000)
parser.add_argument('--dont_create', '--dont-create', '--no-create', dest='create', action='store_false', default=True) parser.add_argument('--dont_create', '--dont-create', '--no-create', dest='create', action='store_false', default=True)
parser.add_argument('--localhost_only', '--localhost-only', dest='localhost_only', action='store_true')
parser.add_argument('--https', dest='use_https', action='store_true', default=None) parser.add_argument('--https', dest='use_https', action='store_true', default=None)
parser.set_defaults(func=etiquette_flask_launch_argparse) parser.set_defaults(func=etiquette_flask_launch_argparse)