diff --git a/.dockerignore b/.dockerignore index 957739e..b731fe6 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,5 @@ /venv +/.venv /.git /.gitignore /.dockerignore @@ -7,4 +8,4 @@ __pycache__/ /README.md /Dockerfile-staging /README.md - +.run_data/ diff --git a/nginx_proxy/Host.py b/nginx_proxy/Host.py index a795962..41e9012 100644 --- a/nginx_proxy/Host.py +++ b/nginx_proxy/Host.py @@ -95,8 +95,9 @@ def __repr__(self): "port": self.port}) def __str__(self): - hostname= "%s:%s" % ( - self.hostname if self.hostname else '?', - str(self.port) if self.port is not None else '?') + return self.__repr__() + # hostname= "%s:%s" % ( + # self.hostname if self.hostname else '?', + # str(self.port) if self.port is not None else '?') diff --git a/nginx_proxy/Location.py b/nginx_proxy/Location.py index 52f9810..1c4945a 100644 --- a/nginx_proxy/Location.py +++ b/nginx_proxy/Location.py @@ -1,4 +1,4 @@ -from typing import Dict, Any +from typing import Dict, Any, Set from . import Container @@ -12,7 +12,7 @@ def __init__(self, name, is_websocket_backend=False, is_http_backend=True): self.http = is_http_backend self.websocket = is_websocket_backend self.name = name - self.containers = set() + self.containers :Set[Container.Container] = set() self.extras: Dict[str, Any] = {} def update_extras(self, extras: Dict[str, Any]): @@ -28,13 +28,13 @@ def update_extras(self, extras: Dict[str, Any]): else: self.extras[x] = extras[x] - def add(self, container: Container): + def add(self, container: Container.Container): self.containers.add(container) def isempty(self): return len(self.containers) == 0 - def remove(self, container: Container): + def remove(self, container: Container.Container): if container in self.containers: self.containers.remove(container) return True diff --git a/nginx_proxy/ProxyConfigData.py b/nginx_proxy/ProxyConfigData.py index 29e73c8..0686d1d 100644 --- a/nginx_proxy/ProxyConfigData.py +++ b/nginx_proxy/ProxyConfigData.py @@ -1,6 +1,7 @@ from typing import Dict, Set, Generator, Tuple, Union from nginx_proxy.Host import Host +from nginx_proxy.Location import Location class ProxyConfigData: @@ -73,22 +74,33 @@ def __len__(self): def print(self): for host in self.host_list(): - if host.port != 80: - url = "- " + ("https" if host.secured else "http") + "://" + host.hostname + ":" + str(host.port) - else: - url = "- " + ("https" if host.secured else "http") + "://" + host.hostname + postfix="://" + host.hostname + def host_url(isWebsocket=False): + if host.secured: + return "- " + ("wss" if isWebsocket else "https") + postfix + (":" + str(host.port) if host.port!=443 else '') + else: + return "- " + ("ws" if isWebsocket else "http") + postfix + (":" + str(host.port) if host.port!=80 else '') + if host.isredirect(): - print(url) + print(host_url()) print(" redirect : ", host.full_redirect) else: if len(host.extras): - print(url) + print(host_url()) self.printextra(" ", host.extras) for location in host.locations.values(): - print(url + location.name) - print(" Type: ", "Websocket" if location.websocket else "Http") + print(host_url(location.websocket)+location.name) + for container in location.containers: + print(" -> ", (container.scheme) + "://"+container.address + (":"+ str(container.port) if container.port else '' )+container.path) + if len(location.extras): - self.printextra(" ", location.extras) + self.printextra(" ", location.extras) + + # self.address: str = address + # self.port: int = port + # self.path: Union[str, None] = path + # self.scheme: str = scheme + # self.networks = @staticmethod def printextra(gap, extra): diff --git a/nginx_proxy/WebServer.py b/nginx_proxy/WebServer.py index 13be26f..c885e46 100644 --- a/nginx_proxy/WebServer.py +++ b/nginx_proxy/WebServer.py @@ -4,6 +4,7 @@ import sys import time from typing import List +import json import requests from docker import DockerClient @@ -87,7 +88,7 @@ def learn_yourself(self): file=sys.stderr) print("Falling back to default network", file=sys.stderr) network = self.client.networks.get("frontend") - self.networks[network.id] = "frontend" + self.networks[network.id] = network.id def _register_container(self, container: DockerContainer): """ @@ -151,7 +152,6 @@ def reload(self, forced=False) -> bool: else: response = self.nginx.update_config(output) return response - def disconnect(self, network, container, scope): if self.id is not None and container == self.id: diff --git a/nginx_proxy/pre_processors/virtual_host_processor.py b/nginx_proxy/pre_processors/virtual_host_processor.py index f6feed5..7cb8cd2 100644 --- a/nginx_proxy/pre_processors/virtual_host_processor.py +++ b/nginx_proxy/pre_processors/virtual_host_processor.py @@ -56,7 +56,7 @@ def _parse_host_entry(entry_string: str): external, internal = (split_url(external), split_url(internal)) c = Container(None, scheme=list(internal['scheme'])[0] if len(internal['scheme']) else 'http', - address=None, + address=internal["host"] if internal["host"] else None, port=internal["port"] if internal["port"] else None, path=internal["location"] if internal["location"] else "/") h = Host( @@ -83,7 +83,8 @@ def host_generator(container: DockerContainer, service_id: str = None, known_net # List all the environment variables with VIRTUAL_HOST and list them. virtual_hosts = [x[1] for x in env_map.items() if x[0].startswith("VIRTUAL_HOST")] - if len(virtual_hosts) is 0: + static_hosts = [x[1] for x in env_map.items() if x[0].startswith("STATIC_VIRTUAL_HOST")] + if len(virtual_hosts) == 0 and len(static_hosts) == 0: raise NoHostConiguration() # Instead of directly processing container details, check whether or not it's accessible through known networks. @@ -102,6 +103,16 @@ def host_generator(container: DockerContainer, service_id: str = None, known_net else: raise UnreachableNetwork() + for host_config in static_hosts: + host, location, container_data, extras = _parse_host_entry(host_config) + container_data.id = container.id + host.secured = 'https' in host.scheme or 'wss' in host.scheme or host.port == 443 + if host.port is None : + host.port = 443 if host.secured else 80 + if container_data.port is None : + container_data.port = 443 if ('https' in container_data.scheme or 'wss' in container_data.scheme) else 80 + yield (host, location, container_data, extras) + override_ssl = False override_port = None if len(virtual_hosts) == 1: diff --git a/vhosts_template/default.conf.jinja2 b/vhosts_template/default.conf.jinja2 index 6bd70e2..38fa6c3 100644 --- a/vhosts_template/default.conf.jinja2 +++ b/vhosts_template/default.conf.jinja2 @@ -39,8 +39,14 @@ server{ proxy_pass {{ location.container.scheme }}://{{ location.upstream }}{{location.container.path}};{% else %} proxy_pass {{location.container.scheme }}://{{ location.container.address }}:{{ location.container.port }}{{ location.container.path }};{% endif %}{% if location.name != '/' %} proxy_redirect $scheme://$http_host{{ location.container.path if location.container.path else '/' }} $scheme://$http_host{{location.name}};{% endif %} {% if location.websocket and location.http %} + proxy_set_header Host $http_host; + proxy_set_header Connection $connection_upgrade; proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade;{% elif location.websocket %} + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; + proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl; + proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;{% elif location.websocket %} proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; @@ -58,8 +64,14 @@ server{ proxy_pass {{ location.container.scheme }}://{{ location.upstream }}{{location.container.path}};{% else %} proxy_pass {{location.container.scheme }}://{{ location.container.address }}:{{ location.container.port }}{{ location.container.path }};{% endif %}{% if location.name != '/' %} proxy_redirect $scheme://$http_host{{ location.container.path if location.container.path else '/' }} $scheme://$http_host{{location.name}};{% endif %} {% if location.websocket and location.http %} + proxy_set_header Host $http_host; + proxy_set_header Connection $connection_upgrade; proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection $connection_upgrade;{% elif location.websocket %} + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto; + proxy_set_header X-Forwarded-Ssl $proxy_x_forwarded_ssl; + proxy_set_header X-Forwarded-Port $proxy_x_forwarded_port;{% elif location.websocket %} proxy_http_version 1.1; proxy_read_timeout 1h; proxy_send_timeout 1h;