diff --git a/.github/workflows/deploy_server.yml b/.github/workflows/deploy_server.yml index f15e8f2..4ad1347 100644 --- a/.github/workflows/deploy_server.yml +++ b/.github/workflows/deploy_server.yml @@ -6,10 +6,6 @@ on: type: boolean default: false description: Redeploy all (be careful!) - # redeploy_tcp_sockets: - # type: boolean - # default: false - # description: TCP Sockets redeploy_web_sockets: type: boolean default: false @@ -48,26 +44,13 @@ jobs: deploy_microservices: runs-on: ubuntu-latest steps: - # - name: Redeploy TCP Sockets - # if: ${{ inputs.redeploy_tcp_sockets || inputs.redeploy_all }} - # uses: appleboy/ssh-action@v1.2.0 - # with: - # host: ${{ secrets.GOOGLE_CLOUD_IP }} - # username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - # key: ${{ secrets.GOOGLE_PRIVATE_SSH }} - # script: | - # cd /root/ukraine_alarm_map/deploy/ - # git fetch --all - # git switch ${{ github.ref_name }} - # git pull - # bash redeploy_tcp_server.sh -m ${{ secrets.MEMCACHED_HOST }} - name: Redeploy WebSockets if: ${{ inputs.redeploy_web_sockets || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -78,9 +61,9 @@ jobs: if: ${{ inputs.redeploy_update_server || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -91,22 +74,22 @@ jobs: if: ${{ inputs.redeploy_web_server || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all git switch ${{ github.ref_name }} git pull - bash redeploy_web_server.sh -d ${{ secrets.WEB_TOKEN }} -p 80 -m ${{ secrets.MEMCACHED_HOST }} + bash redeploy_web_server.sh -d ${{ secrets.WEB_TOKEN }} -m ${{ secrets.MEMCACHED_HOST }} - name: Redeploy Weather if: ${{ inputs.redeploy_weather || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -117,9 +100,9 @@ jobs: if: ${{ inputs.redeploy_updater || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -130,9 +113,9 @@ jobs: if: ${{ inputs.redeploy_svg_generator || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -143,9 +126,9 @@ jobs: if: ${{ inputs.redeploy_etryvoga || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -156,9 +139,9 @@ jobs: if: ${{ inputs.redeploy_alerts || inputs.redeploy_all }} uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | cd /root/ukraine_alarm_map/deploy/ git fetch --all @@ -168,8 +151,8 @@ jobs: - name: Clear unused images uses: appleboy/ssh-action@v1.2.0 with: - host: ${{ secrets.GOOGLE_CLOUD_IP }} - username: ${{ secrets.GOOGLE_CLOUD_SSH_USER }} - key: ${{ secrets.GOOGLE_PRIVATE_SSH }} + host: ${{ secrets.CLOUD_SERVER_IP }} + username: ${{ secrets.CLOUD_SERVER_USER }} + key: ${{ secrets.CLOUD_SERVER_SSH_KEY }} script: | docker image prune -f \ No newline at end of file diff --git a/deploy/redeploy_alerts.sh b/deploy/redeploy_alerts.sh index bd76dc1..f0052df 100644 --- a/deploy/redeploy_alerts.sh +++ b/deploy/redeploy_alerts.sh @@ -60,7 +60,7 @@ docker rm map_alerts || true # Deploying the new container echo "Deploying new container..." -docker run --name map_alerts --restart unless-stopped -d --env ALERT_PERIOD="$ALERT_PERIOD" --env ALERT_TOKEN="$ALERT_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_alerts +docker run --name map_alerts --restart unless-stopped --network=jaam -d --env ALERT_PERIOD="$ALERT_PERIOD" --env ALERT_TOKEN="$ALERT_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_alerts echo "Container deployed successfully!" diff --git a/deploy/redeploy_etryvoga.sh b/deploy/redeploy_etryvoga.sh index ad84ff6..94fc075 100644 --- a/deploy/redeploy_etryvoga.sh +++ b/deploy/redeploy_etryvoga.sh @@ -72,7 +72,7 @@ docker rm map_etryvoga || true # Deploying the new container echo "Deploying new container..." -docker run --name map_etryvoga --restart unless-stopped -d --env ETRYVOGA_HOST="$ETRYVOGA_HOST" --env ETRYVOGA_DISTRICTS_HOST="$ETRYVOGA_DISTRICTS_HOST" --env ETRYVOGA_PERIOD="$ETRYVOGA_PERIOD" --env ETRYVOGA_DISTRICTS_PERIOD="$ETRYVOGA_DISTRICTS_PERIOD" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_etryvoga +docker run --name map_etryvoga --restart unless-stopped --network=jaam -d --env ETRYVOGA_HOST="$ETRYVOGA_HOST" --env ETRYVOGA_DISTRICTS_HOST="$ETRYVOGA_DISTRICTS_HOST" --env ETRYVOGA_PERIOD="$ETRYVOGA_PERIOD" --env ETRYVOGA_DISTRICTS_PERIOD="$ETRYVOGA_DISTRICTS_PERIOD" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_etryvoga echo "Container deployed successfully!" diff --git a/deploy/redeploy_svg_generator.sh b/deploy/redeploy_svg_generator.sh index 30d7e4c..d4719fc 100644 --- a/deploy/redeploy_svg_generator.sh +++ b/deploy/redeploy_svg_generator.sh @@ -52,7 +52,7 @@ docker rm map_svg_generator || true # Deploying the new container echo "Deploying new container..." -docker run --name map_svg_generator --restart unless-stopped -d -v /shared_data:/shared_data --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_svg_generator +docker run --name map_svg_generator --restart unless-stopped --network=jaam -d -v /shared_data:/shared_data --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_svg_generator echo "Container deployed successfully!" diff --git a/deploy/redeploy_tcp_server.sh b/deploy/redeploy_tcp_server.sh deleted file mode 100644 index a29ecb4..0000000 --- a/deploy/redeploy_tcp_server.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/bin/bash - -# Default values -MEMCACHED_HOST="" -TCP_PORT=12345 -LOGGING="INFO" - -# Check for arguments -while [[ $# -gt 0 ]]; do - case "$1" in - -a|--tcp-port) - TCP_PORT="$2" - shift 2 - ;; - -m|--memcached-host) - MEMCACHED_HOST="$2" - shift 2 - ;; - -l|--logging) - LOGGING="$2" - shift 2 - ;; - *) - echo "Unknown argument: $1" - exit 1 - ;; - esac -done - -echo "TCP SERVER" - -echo "MEMCACHED_HOST: $MEMCACHED_HOST" -echo "TCP_PORT: $TCP_PORT" -echo "LOGGING: $LOGGING" - - -# Updating the Git repo -echo "Updating Git repo..." -#cd /path/to/your/git/repo -git pull - -# Moving to the deployment directory -echo "Moving to deployment directory..." -cd tcp_server - -# Building Docker image -echo "Building Docker image..." -docker build -t map_tcp_server -f Dockerfile . - -# Stopping and removing the old container (if exists) -echo "Stopping and removing old container..." -docker stop map_tcp_server || true -docker rm map_tcp_server || true - -# Deploying the new container -echo "Deploying new container..." -docker run --name map_tcp_server --restart unless-stopped -d -p "$TCP_PORT":"$TCP_PORT" --env TCP_PORT="$TCP_PORT" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_tcp_server - -echo "Container deployed successfully!" - diff --git a/deploy/redeploy_update_server.sh b/deploy/redeploy_update_server.sh index c628f52..f819641 100644 --- a/deploy/redeploy_update_server.sh +++ b/deploy/redeploy_update_server.sh @@ -70,7 +70,7 @@ docker rm map_update_server || true # Deploying the new container echo "Deploying new container..." -docker run --name map_update_server --restart unless-stopped -d -p "$PORT":"$PORT" -v "$SHARED_PATH":/shared_data -v "$SHARED_BETA_PATH":/shared_beta_data --env PORT="$PORT" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_update_server +docker run --name map_update_server --restart unless-stopped --network=jaam -d -v "$SHARED_PATH":/shared_data -v "$SHARED_BETA_PATH":/shared_beta_data --env PORT="$PORT" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_update_server echo "Container deployed successfully!" diff --git a/deploy/redeploy_updater.sh b/deploy/redeploy_updater.sh index 1ca2b67..66df7fd 100644 --- a/deploy/redeploy_updater.sh +++ b/deploy/redeploy_updater.sh @@ -54,7 +54,7 @@ docker rm map_updater || true # Deploying the new container echo "Deploying new container..." -docker run --name map_updater --restart unless-stopped -d --env UPDATER_PERIOD="$UPDATER_PERIOD" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_updater +docker run --name map_updater --restart unless-stopped --network=jaam -d --env UPDATER_PERIOD="$UPDATER_PERIOD" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_updater echo "Container deployed successfully!" diff --git a/deploy/redeploy_weather.sh b/deploy/redeploy_weather.sh index 0a5259d..b8e9145 100644 --- a/deploy/redeploy_weather.sh +++ b/deploy/redeploy_weather.sh @@ -60,7 +60,7 @@ docker rm map_weather || true # Deploying the new container echo "Deploying new container..." -docker run --name map_weather --restart unless-stopped -d --env WEATHER_PERIOD="$WEATHER_PERIOD" --env WEATHER_TOKEN="$WEATHER_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_weather +docker run --name map_weather --restart unless-stopped --network=jaam -d --env WEATHER_PERIOD="$WEATHER_PERIOD" --env WEATHER_TOKEN="$WEATHER_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_weather echo "Container deployed successfully!" diff --git a/deploy/redeploy_web_server.sh b/deploy/redeploy_web_server.sh index 2dd1c0c..e5116c6 100644 --- a/deploy/redeploy_web_server.sh +++ b/deploy/redeploy_web_server.sh @@ -64,7 +64,7 @@ docker rm map_web_server || true # Deploying the new container echo "Deploying new container..." -docker run --name map_web_server --restart unless-stopped -d -p "$PORT":8080 -v /shared_data:/shared_data --env DATA_TOKEN="$DATA_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_web_server +docker run --name map_web_server --restart unless-stopped --network=jaam -d -v /shared_data:/shared_data --env PORT="$PORT" --env DATA_TOKEN="$DATA_TOKEN" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env LOGGING="$LOGGING" map_web_server echo "Container deployed successfully!" diff --git a/deploy/redeploy_websocket_server.sh b/deploy/redeploy_websocket_server.sh index 3d6a948..fc23d41 100644 --- a/deploy/redeploy_websocket_server.sh +++ b/deploy/redeploy_websocket_server.sh @@ -70,7 +70,7 @@ docker rm map_websocket_server || true # Deploying the new container echo "Deploying new container..." -docker run --name map_websocket_server --restart unless-stopped -d -p "$WEBSOCKET_PORT":"$WEBSOCKET_PORT" --env WEBSOCKET_PORT="$WEBSOCKET_PORT" --env API_SECRET="$API_SECRET" --env MEASUREMENT_ID="$MEASUREMENT_ID" --env PING_INTERVAL="$PING_INTERVAL" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env ENVIRONMENT="$ENVIRONMENT" --env LOGGING="$LOGGING" map_websocket_server +docker run --name map_websocket_server --restart unless-stopped --network=jaam -d --env WEBSOCKET_PORT="$WEBSOCKET_PORT" --env API_SECRET="$API_SECRET" --env MEASUREMENT_ID="$MEASUREMENT_ID" --env PING_INTERVAL="$PING_INTERVAL" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env ENVIRONMENT="$ENVIRONMENT" --env LOGGING="$LOGGING" map_websocket_server echo "Container deployed successfully!" diff --git a/deploy/redeploy_websocket_server_dev.sh b/deploy/redeploy_websocket_server_dev.sh index 70b5b19..86f2195 100644 --- a/deploy/redeploy_websocket_server_dev.sh +++ b/deploy/redeploy_websocket_server_dev.sh @@ -88,7 +88,7 @@ docker rm map_websocket_server_dev || true # Deploying the new container echo "Deploying new container..." -docker run --name map_websocket_server_dev --restart unless-stopped -d -p "$WEBSOCKET_PORT":"$WEBSOCKET_PORT" --env WEBSOCKET_PORT="$WEBSOCKET_PORT" --env API_SECRET="$API_SECRET" --env MEASUREMENT_ID="$MEASUREMENT_ID" --env PING_INTERVAL="$PING_INTERVAL" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env ENVIRONMENT="$ENVIRONMENT" --env RANDOM_MODE="$RANDOM_MODE" --env TEST_MODE="$TEST_MODE" --env MEMCACHE_FETCH_INTERVAL="$MEMCACHE_FETCH_INTERVAL" --env LOGGING="$LOGGING" map_websocket_server_dev +docker run --name map_websocket_server_dev --restart unless-stopped --network=jaam -d --env WEBSOCKET_PORT="$WEBSOCKET_PORT" --env API_SECRET="$API_SECRET" --env MEASUREMENT_ID="$MEASUREMENT_ID" --env PING_INTERVAL="$PING_INTERVAL" --env MEMCACHED_HOST="$MEMCACHED_HOST" --env ENVIRONMENT="$ENVIRONMENT" --env RANDOM_MODE="$RANDOM_MODE" --env TEST_MODE="$TEST_MODE" --env MEMCACHE_FETCH_INTERVAL="$MEMCACHE_FETCH_INTERVAL" --env LOGGING="$LOGGING" map_websocket_server_dev echo "Container deployed successfully!" diff --git a/deploy/tcp_server/Dockerfile b/deploy/tcp_server/Dockerfile deleted file mode 100644 index 4e6f075..0000000 --- a/deploy/tcp_server/Dockerfile +++ /dev/null @@ -1,7 +0,0 @@ -FROM python:3.12-slim -ENV TZ="Europe/Kyiv" -COPY tcp_server.py . -COPY GeoLite2-City.mmdb . -RUN --mount=type=bind,source=requirements.txt,target=/tmp/requirements.txt \ - python -m pip install --no-cache-dir -r /tmp/requirements.txt -CMD python tcp_server.py \ No newline at end of file diff --git a/deploy/tcp_server/GeoLite2-City.mmdb b/deploy/tcp_server/GeoLite2-City.mmdb deleted file mode 100644 index a1053d1..0000000 Binary files a/deploy/tcp_server/GeoLite2-City.mmdb and /dev/null differ diff --git a/deploy/tcp_server/requirements.txt b/deploy/tcp_server/requirements.txt deleted file mode 100644 index 07ed290..0000000 --- a/deploy/tcp_server/requirements.txt +++ /dev/null @@ -1,3 +0,0 @@ -aiomcache==0.8.1 -geoip2==4.8.0 - diff --git a/deploy/tcp_server/tcp_server.py b/deploy/tcp_server/tcp_server.py deleted file mode 100644 index 346b137..0000000 --- a/deploy/tcp_server/tcp_server.py +++ /dev/null @@ -1,179 +0,0 @@ -import json -import os -import asyncio -import logging -from aiomcache import Client -from geoip2 import database -from datetime import datetime -from time import time - -debug_level = os.environ.get("LOGGING") -memcached_host = os.environ.get("MEMCACHED_HOST") or "localhost" -tcp_port = os.environ.get("TCP_PORT") or 12345 - -logging.basicConfig(level=debug_level, format="%(asctime)s %(levelname)s : %(message)s") -logger = logging.getLogger(__name__) - - -class SharedData: - def __init__(self): - self.data = "Initial data" - self.clients = {} - self.blocked_ips = [] - - -async def handle_client(reader, writer, shared_data, geo): - logger.debug(f"New client connected from {writer.get_extra_info('peername')}") - data_from_client = False - - client_ip = writer.get_extra_info("peername")[0] - client_port = writer.get_extra_info("peername")[1] - - if client_ip in shared_data.blocked_ips: - logger.info(f"Blocked IP {client_ip} attempted to connect.") - writer.close() - await writer.wait_closed() - return - - response = geo.city(client_ip) - - if response.country.iso_code != "UA" and response.continent.code != "EU": - logger.info(f"Block IP {client_ip} from {response.country.name}.") - shared_data.blocked_ips.append(client_ip) - writer.close() - await writer.wait_closed() - return - - try: - data_from_client = await asyncio.wait_for(reader.read(100), timeout=2.0) - data_from_client = data_from_client.decode() - writer.firmware = data_from_client - - except asyncio.exceptions.TimeoutError as e: - writer.firmware = "unknown" - - shared_data.clients[f"{client_ip}_{client_port}"] = { - "firmware": writer.firmware, - "city": response.city.name or "unknown", - "region": response.subdivisions.most_specific.name or "unknown", - } - - logger.debug(shared_data.clients[f"{client_ip}_{client_port}"]) - - logger.debug(f"Received data from {client_ip}:{client_port}: {data_from_client}") - writer.data_sent = shared_data.data - writer.last_ping = int(time()) - writer.write(shared_data.data.encode()) - await writer.drain() - - try: - while True: - await asyncio.sleep(1) - current_timestamp = int(time()) - if (current_timestamp - writer.last_ping) > 1: - ping_data = "p" - logger.debug(f"Client {client_ip}:{client_port} ({writer.firmware}) ping") - writer.last_ping = current_timestamp - writer.write(ping_data.encode()) - shared_data.clients[f"{client_ip}_{client_port}"]["last_ping"] = current_timestamp - await writer.drain() - await asyncio.sleep(1) - - if shared_data.data != writer.data_sent: - writer.write(shared_data.data.encode()) - logger.debug(f"Data changed. Broadcasting to {client_ip}:{client_port}") - writer.data_sent = shared_data.data - shared_data.clients[f"{client_ip}_{client_port}"]["last_data"] = current_timestamp - await writer.drain() - - except asyncio.CancelledError: - pass - - finally: - logger.debug( - f"Client from {writer.get_extra_info('peername')[0]}:{writer.get_extra_info('peername')[1]} disconnected" - ) - del shared_data.clients[f"{client_ip}_{client_port}"] - writer.close() - await writer.wait_closed() - - -async def update_shared_data(shared_data, mc): - while True: - try: - await asyncio.sleep(1) - logger.debug("Memcache check") - data_from_memcached = await get_data_from_memcached(mc) - # data_from_memcached = '0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0:30.82,29.81,31.14,29.59,26.1,29.13,33.44,32.07,32.37,31.27,34.81,35.84,35.94,37.65,37.48,36.68,31.28,37.27,35.64,33.91,31.81,34.91,34.51,32.79,34.21,32.07' - - if data_from_memcached != shared_data.data: - shared_data.data = data_from_memcached - logger.debug(f"Data updated: {data_from_memcached}") - - except Exception as e: - logger.error(f"Error in update_shared_data: {e}") - - -async def print_clients(shared_data, mc): - while True: - try: - await asyncio.sleep(60) - logger.debug(f"Print clients: {len(shared_data.clients)}") - logger.debug(f"Print blocked: {shared_data.blocked_ips}") - - await mc.set(b"tcp_clients", json.dumps(shared_data.clients).encode("utf-8")) - - for client, data in shared_data.clients.items(): - logger.debug(f"{client}: {data['firmware']}") - - except Exception as e: - logger.error(f"Error in update_shared_data: {e}") - - -async def calculate_time_difference(timestamp1, timestamp2): - format_str = "%Y-%m-%dT%H:%M:%SZ" - - time1 = datetime.strptime(timestamp1, format_str) - time2 = datetime.strptime(timestamp2, format_str) - - time_difference = (time2 - time1).total_seconds() - return abs(time_difference) - - -async def get_data_from_memcached(mc): - tcp_cached = await mc.get(b"tcp") - - if tcp_cached: - tcp_cached_data = json.loads(tcp_cached.decode("utf-8")) - else: - tcp_cached_data = {} - - return tcp_cached_data - - -async def main(): - shared_data = SharedData() - mc = Client(memcached_host, 11211) - geo = database.Reader("GeoLite2-City.mmdb") - - updater_task = asyncio.create_task(update_shared_data(shared_data, mc)) - log_task = asyncio.create_task(print_clients(shared_data, mc)) - - server = await asyncio.start_server(lambda r, w: handle_client(r, w, shared_data, geo), "0.0.0.0", tcp_port) - - try: - async with server: - logger.info("Start") - await server.serve_forever() - except KeyboardInterrupt: - logger.debug("Server shutting down...") - finally: - logger.info("Shutdown") - updater_task.cancel() - await mc.close() - await updater_task - await log_task - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/deploy/web_server/web_server.py b/deploy/web_server/web_server.py index 744545b..cae07c8 100644 --- a/deploy/web_server/web_server.py +++ b/deploy/web_server/web_server.py @@ -18,6 +18,7 @@ debug_level = os.environ.get("LOGGING") debug = os.environ.get("DEBUG", False) +port = int(os.environ.get("PORT", 8080)) memcached_host = os.environ.get("MEMCACHED_HOST", "localhost") memcached_port = int(os.environ.get("MEMCACHED_PORT", 11211)) shared_path = os.environ.get("SHARED_PATH") or "/shared_data" @@ -564,4 +565,4 @@ async def stats(request): ) if __name__ == "__main__": - uvicorn.run(app, host="0.0.0.0", port=8080) + uvicorn.run(app, host="0.0.0.0", port=port)