Skip to content

Commit

Permalink
Check if proxy server is host-able before running
Browse files Browse the repository at this point in the history
Log differences in origins when updated
Load repeated timers into a list
  • Loading branch information
dormant-user committed Apr 25, 2024
1 parent 8075784 commit 05bdc4d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
8 changes: 8 additions & 0 deletions pyfilebrowser/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
import multiprocessing
import os
import socket
import subprocess
import time
import warnings
Expand Down Expand Up @@ -176,6 +177,13 @@ def background_tasks(self, auth_map: Dict[str, str]) -> None:
if self.proxy:
assert proxy_settings.port != int(self.env.config_settings.server.port), \
f"\n\tProxy server can't run on the same port [{proxy_settings.port}] as the server!!"
try:
with socket.socket() as sock:
sock.bind((proxy_settings.host, proxy_settings.port))
except OSError as error:
self.logger.error(error)
self.logger.critical("Cannot initiate proxy server")
raise
log_config = struct.LoggerConfig(self.logger).get()
if proxy_settings.debug:
log_config = struct.update_log_level(log_config, logging.DEBUG)
Expand Down
18 changes: 14 additions & 4 deletions pyfilebrowser/proxy/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import difflib
import json
import logging
import time
Expand All @@ -12,6 +13,7 @@

LOGGER = logging.getLogger('proxy')
CLIENT = httpx.Client()
DIFFER = difflib.Differ()

epoch = lambda: int(time.time()) # noqa: E731

Expand All @@ -25,11 +27,19 @@ def refresh_allowed_origins() -> None:
- | The background task runs only when the env vars, ``private_ip`` or ``public_ip`` is set to True,
| as they are the only dynamic values.
"""
LOGGER.debug("Refreshing allowed origins")
allowed_origins = set()
allowed_origins.update(settings.env_config.origins)
allowed_origins.update(settings.allowance())

# Extract elements that are only in one of the sets
if difference := [item for item in list(DIFFER.compare(sorted(settings.session.allowed_origins),
sorted(allowed_origins)))
if item.startswith('- ') or item.startswith('+ ')]:
LOGGER.warning("Changes in allowed origins: %s", difference)

settings.session.allowed_origins.clear()
settings.session.allowed_origins.update(settings.env_config.origins)
settings.session.allowed_origins.update(settings.allowance())
LOGGER.debug("Next refresh - %s",
settings.session.allowed_origins = allowed_origins
LOGGER.debug("Refreshed allowed origins. Next refresh - %s",
(datetime.now() + timedelta(seconds=settings.env_config.origin_refresh)).strftime('%c'))


Expand Down
18 changes: 10 additions & 8 deletions pyfilebrowser/proxy/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,21 @@ def run_in_parallel(self, logger: logging.Logger) -> None:
uvicorn_access.disabled = True
uvicorn_access.propagate = False
assert logger.name == "proxy"
timer = None
timers = []
if settings.env_config.origin_refresh and \
(settings.env_config.private_ip or settings.env_config.public_ip):
logger.info("Initiating background task to refresh allowed origins every %d seconds",
settings.env_config.origin_refresh)
timer = repeated_timer.RepeatedTimer(
timers.append(repeated_timer.RepeatedTimer(
function=main.refresh_allowed_origins, interval=settings.env_config.origin_refresh
)
))
for timer in timers:
logger.info("Initiating the background task '%s' with interval %d seconds",
timer.function.__name__, timer.interval.real)
timer.start()
try:
self.run()
except KeyboardInterrupt:
if timer:
logger.info("Stopping background task to refresh allowed origins")
for timer in timers:
logger.info("Stopping the background task '%s'", timer.function.__name__)
timer.stop()
finally:
logger.info("Proxy service terminated")
Expand All @@ -75,7 +76,8 @@ def proxy_server(server: str,

settings.destination.url = server
settings.destination.auth_config = auth_map
main.refresh_allowed_origins()
settings.session.allowed_origins.update(settings.env_config.origins)
settings.session.allowed_origins.update(settings.allowance())

# noinspection HttpUrlsUsage
logger.info("Starting proxy engine on http://%s:%s with %s workers",
Expand Down

0 comments on commit 05bdc4d

Please sign in to comment.