|
| 1 | +from docker_service import DockerService |
| 2 | +from proxy import ProxyType, ProxyServer |
| 3 | +import subprocess |
| 4 | +import json |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import time |
| 9 | +import argparse |
| 10 | + |
| 11 | +DOCKER_HOST_ENV = os.getenv("DOCKER_HOST") |
| 12 | +ACTIVE_PROXIES = [] |
| 13 | +# Configure Logging |
| 14 | +logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s', level=logging.DEBUG) |
| 15 | + |
| 16 | +logging.getLogger("urllib3").setLevel(logging.INFO) |
| 17 | +logging.getLogger("docker").setLevel(logging.INFO) |
| 18 | + |
| 19 | +def getDockerHostFromContext(): |
| 20 | + current_context_name_cmd = subprocess.run(["docker", "context", "show"], capture_output=True) |
| 21 | + current_context_name = current_context_name_cmd.stdout.decode().replace("\n", "") |
| 22 | + current_context_cmd = subprocess.run(["docker", "context", "inspect", current_context_name], capture_output=True) |
| 23 | + current_context_json = json.loads(current_context_cmd.stdout.decode()) |
| 24 | + return current_context_json[0]["Endpoints"]["docker"]["Host"] |
| 25 | + |
| 26 | +def manageProxies(remote_docker_host_name, published_ports, listen_system_ports): |
| 27 | + global ACTIVE_PROXIES |
| 28 | + # Get only tcp services because only tcp proxy implemented |
| 29 | + published_ports = list(x["published"] for x in published_ports if x["internal"].endswith("tcp")) |
| 30 | + if not listen_system_ports: |
| 31 | + published_ports = list(x for x in published_ports if x > 1024) |
| 32 | + new_active_proxies = [] |
| 33 | + # Add active proxy and stop inactive |
| 34 | + for proxy in ACTIVE_PROXIES: |
| 35 | + if(proxy.proxy_port in published_ports): |
| 36 | + new_active_proxies.append(proxy) |
| 37 | + published_ports.remove(proxy.proxy_port) |
| 38 | + else: |
| 39 | + proxy.stop() |
| 40 | + # Add and start new proxies |
| 41 | + for port in published_ports: |
| 42 | + proxy = ProxyServer(remote_docker_host_name, port, ProxyType.TCP) |
| 43 | + proxy.start() |
| 44 | + new_active_proxies.append(proxy) |
| 45 | + # Update proxy list |
| 46 | + ACTIVE_PROXIES = new_active_proxies |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +def mainLoop(docker_host, listen_system_ports): |
| 51 | + logging.info("Initialize docker remote proxy to `%s`" %docker_host) |
| 52 | + docker_service = DockerService(docker_host) |
| 53 | + remote_docker_host_name = docker_service.getRemoteHost() |
| 54 | + |
| 55 | + try: |
| 56 | + while True: |
| 57 | + try: |
| 58 | + published_ports = docker_service.getPublishedPorts() |
| 59 | + manageProxies(remote_docker_host_name, published_ports, listen_system_ports) |
| 60 | + except Exception as e: |
| 61 | + logging.error(e) |
| 62 | + raise e |
| 63 | + time.sleep(60) |
| 64 | + except KeyboardInterrupt: |
| 65 | + logging.info('Docker remote proxy stopped!') |
| 66 | + |
| 67 | +if __name__ == "__main__": |
| 68 | + logging.info('Starting docker remote proxy!') |
| 69 | + parser = argparse.ArgumentParser(description='Remote docker context proxy') |
| 70 | + parser.add_argument('--listen-system-ports', action=argparse.BooleanOptionalAction, default=False, help='Listen and Proxy system ports example: 22, 80, 443 etc.!') |
| 71 | + parser.add_argument('--host', type=str, default=None, help='Docker host uri') |
| 72 | + args = parser.parse_args() |
| 73 | + if DOCKER_HOST_ENV != None: |
| 74 | + mainLoop(DOCKER_HOST_ENV, args.listen_system_ports) |
| 75 | + elif args.host != None: |
| 76 | + mainLoop(args.host, args.listen_system_ports) |
| 77 | + else: |
| 78 | + mainLoop(getDockerHostFromContext(), args.listen_system_ports) |
0 commit comments