diff --git a/README.md b/README.md index 83c7f71..e09cfe4 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,9 @@ curl -X DELETE \ - [x] header to setup .host file instead of in archive - [x] ignore .gitignore/.host etc at root - [x] cerbot install in container + path env/arg -- [ ] redirect /.well-known/acme-challenge to specific path +- [x] redirect /.well-known/acme-challenge to specific path - [ ] certbot/self-signed create/renew in specific dir +- [ ] better logger - [ ] renew command - [ ] https mode w/ multiple hosts - [ ] restart command (on new/deleted host) diff --git a/data/.certbot/.gitignore b/data/.certbot/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/data/.certbot/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/data/.gitignore b/data/.gitignore index c96a04f..267f3f0 100644 --- a/data/.gitignore +++ b/data/.gitignore @@ -1,2 +1,3 @@ * -!.gitignore \ No newline at end of file +!.gitignore +!.certbot \ No newline at end of file diff --git a/src/handler.py b/src/handler.py index 1ceee5e..f129046 100644 --- a/src/handler.py +++ b/src/handler.py @@ -11,6 +11,7 @@ from . import project, params, registry, data_dir class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler): protocol_version = "HTTP/2.0" server_version = "StaplerServer/" + project.get_version() + CERTBOT_CHALLENGE_PATH = "/.well-known/acme-challenge" def __init__( self, *args, params: params.Parameters, registry: registry.Registry, **kwargs @@ -20,6 +21,7 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler): self.data_dir = data_dir.DataDir(params.data_dir) self.max_size_bytes = params.max_size_bytes self.registry = registry + self.certbot_www = os.path.realpath(params.certbot_www) super().__init__(*args, directory=params.data_dir, **kwargs) def list_directory(self, *_, **__): @@ -27,6 +29,8 @@ class StaplerRequestHandler(http.server.SimpleHTTPRequestHandler): self.send_error(http.HTTPStatus.NOT_FOUND, "File not found") def translate_path(self, path: str) -> str: + if path.startswith(self.CERTBOT_CHALLENGE_PATH): + return self.certbot_www + path.removeprefix(self.CERTBOT_CHALLENGE_PATH) if (page := self.registry.get_from_host(self.get_host())) is not None: path = f"/{page.path}" + path path = super().translate_path(path) diff --git a/src/server.py b/src/server.py index d28f430..91094ed 100644 --- a/src/server.py +++ b/src/server.py @@ -1,13 +1,13 @@ import http.server +import os -from . import params, handler, registry +from . import params, handler, registry, project class StaplerServer: def __init__(self, params: params.Parameters): - self.default_host = params.host - self.registry = registry.Registry(params) self.params = params + self.registry = registry.Registry(params) self.server = http.server.ThreadingHTTPServer( (params.bind, params.port), self.request_handler, @@ -18,10 +18,21 @@ class StaplerServer: *args, params=self.params, registry=self.registry ) - def start(self): + def __repr__(self): + return f"StaplerServer ({project.get_version()})" + + def __init_certbot_www(self): + os.makedirs(self.params.certbot_www, exist_ok=True) + + def __startup(self): + print(f"{self}: starting up...") self.registry.load_pages() + self.__init_certbot_www() + + def start(self): + self.__startup() print( - f"{handler.StaplerRequestHandler.server_version} serving on http://{self.default_host}:{self.server.server_port}..." + f"{self}: serving on http://{self.params.host}:{self.server.server_port}..." ) try: self.server.serve_forever()