From 71487ccf65d147960c4230282124abd3f89b0c74 Mon Sep 17 00:00:00 2001 From: ZhymabekRoman Date: Tue, 6 Aug 2024 07:24:19 +0500 Subject: [PATCH] cloudflare warp proxy: make auto restart on connection issues --- DockerfileHealthcare | 9 ++++ docker-compose.yml | 21 +++++++++ scripts/service_up.py | 105 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 135 insertions(+) create mode 100644 DockerfileHealthcare create mode 100644 scripts/service_up.py diff --git a/DockerfileHealthcare b/DockerfileHealthcare new file mode 100644 index 0000000..e0b5ddb --- /dev/null +++ b/DockerfileHealthcare @@ -0,0 +1,9 @@ +FROM python:3.12.3 + +RUN pip install docker requests[socks] loguru --no-cache-dir + +COPY scripts/ /scripts/ + +# RUN chmod +x /scripts/service_up.py + +CMD [ "python3", "/scripts/service_up.py" ] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 56701d4..259cff3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -32,6 +32,7 @@ services: wgcf1: image: neilpang/wgcf-docker:latest + container_name: wgcf1 volumes: # - ./wgcf:/wgcf - /lib/modules:/lib/modules @@ -55,6 +56,7 @@ services: dante_1: image: shturman/dante:1.4.2 + container_name: dante_1 # build: # context: ./ # dockerfile: ./DockerfileDante @@ -74,6 +76,25 @@ services: # timeout: 2s # retries: 5 + wgcf1_healthcare_service: + build: + context: . + dockerfile: DockerfileHealthcare + environment: + - TG_TOKEN=${TG_TOKEN} + - TG_CHAT_ID=${TG_CHAT_ID} + - PROXY_HOST=wgcf1 + - PROXY_PORT=1080 + depends_on: + - dante_1 + networks: + - caddy_net + volumes: + - /var/run/docker.sock:/var/run/docker.sock + - /etc/localtime:/etc/localtime:ro + - ./scripts/service_up.py:/scripts/service_up.py + # network_mode: "host" + web: build: context: ./ diff --git a/scripts/service_up.py b/scripts/service_up.py new file mode 100644 index 0000000..494a33b --- /dev/null +++ b/scripts/service_up.py @@ -0,0 +1,105 @@ +import os +import sys +import time +from loguru import logger +import docker +import requests + +logger.remove() +logger.add(sys.stdout, level="DEBUG") + +error_msg = "[Cloudflare proxy] Freedium proxy is not working, restarting containers..." + +USER_NAME = os.getenv("USER_NAME") +USER_PASSWORD = os.getenv("USER_PASSWORD") +PROXY_HOST = os.getenv("PROXY_HOST", "localhost") +PROXY_PORT = os.getenv("PROXY_PORT", "9681") # 1080 + +_AUTH_DATA = [USER_NAME, USER_PASSWORD] +AUTH_FORMATED = ":".join(filter(bool, _AUTH_DATA)) + +if not PROXY_HOST or not PROXY_PORT: + raise ValueError("PROXY_HOST and PROXY POER must be set") + +containers_to_restart = ["dante_1", "wgcf1"] + + +def restart_containers(): + # client = docker.from_env() + client = docker.DockerClient(base_url="unix://var/run/docker.sock") + + for container_name in containers_to_restart: + try: + container = client.containers.get(container_name) + logger.info(f"Restarting {container_name}...") + container.restart() + logger.info(f"{container_name} restarted successfully.") + except docker.errors.NotFound: + logger.error(f"Container {container_name} not found.") + except Exception as e: + logger.error(f"Error restarting {container_name}: {str(e)}") + + +def send_msg(text: str, max_retries: int = 3) -> bool: + for attempt in range(max_retries): + try: + tg_token = os.getenv("TG_TOKEN") + chat_id = os.getenv("TG_CHAT_ID") + url_req = "https://api.telegram.org/bot" + tg_token + "/sendMessage" + "?chat_id=" + chat_id + "&text=" + text + results = requests.get(url_req) + logger.debug(results.json()) + except Exception as e: + logger.exception(e) + logger.error(f"Error sending message: {str(e)}") + if attempt < max_retries - 1: + logger.info(f"Retrying in 5 seconds...") + time.sleep(5) + else: + logger.info("Message sent successfully.") + return True + + logger.error("All retry attempts failed. Message not sent.") + return False + + +def check_proxy(max_retries=3, retry_delay=5) -> bool: + auth_str = f"{AUTH_FORMATED}@" if AUTH_FORMATED else "" + + proxy_url = f"socks5://{auth_str}{PROXY_HOST}:{PROXY_PORT}" + proxy_test_url = "http://ipinfo.io" + logger.info(f"Checking proxy: {proxy_url}") + for attempt in range(max_retries): + try: + resp = requests.get( + proxy_test_url, + proxies=dict( + http=proxy_url, + https=proxy_url, + ), + timeout=5, + ) + logger.info(f"Proxy is working: {resp.status_code}") + return True + except Exception as e: + logger.exception(e) + logger.error(f"Error checking proxy (attempt {attempt + 1}/{max_retries}): {str(e)}") + if attempt < max_retries - 1: + logger.info(f"Retrying in {retry_delay} seconds...") + time.sleep(retry_delay) + + logger.error("All retry attempts failed. Proxy is not working.") + return False + + +if __name__ == "__main__": + while True: + time.sleep(75) + # time.sleep(10) + try: + logger.info("Checking proxy...") + if not check_proxy(): + logger.info(error_msg) + send_msg(error_msg) + restart_containers() + except Exception as e: + logger.error(f"Error checking proxy: {str(e)}")