diff options
| -rw-r--r-- | README.md | 20 | ||||
| -rw-r--r-- | deploy/ip.mfamily.org.nginx | 72 | ||||
| -rwxr-xr-x | ip-echo-daemon | 13 | ||||
| -rw-r--r-- | tests/test_he_dyndns.py | 18 |
4 files changed, 107 insertions, 16 deletions
@@ -120,7 +120,10 @@ systemctl reload nginx ``` The nginx server block redirects HTTP to HTTPS, listens on IPv4 and IPv6, and -proxies only `/`: +proxies only `/`. It serves `/.well-known/` directly from `/var/www/html` for +ACME challenges. All service error bodies contain only the numeric status code +and a newline, never HTML; see the full configuration in +`deploy/ip.mfamily.org.nginx` for the error mappings: ``` server { @@ -128,7 +131,13 @@ server { listen [::]:80; server_name ip.mfamily.org; - return 301 https://$host$request_uri; + location ^~ /.well-known/ { + root /var/www/html; + } + + location / { + return 301 https://$host$request_uri; + } } server { @@ -139,16 +148,21 @@ server { ssl_certificate /etc/letsencrypt/live/ip.mfamily.org/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ip.mfamily.org/privkey.pem; + location ^~ /.well-known/ { + root /var/www/html; + } + location = / { proxy_pass http://127.0.0.1:8080/; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass_request_body off; proxy_set_header Content-Length ""; + proxy_intercept_errors on; } location / { - return 404; + return 404 "404\n"; } } ``` diff --git a/deploy/ip.mfamily.org.nginx b/deploy/ip.mfamily.org.nginx index 08cbeb5..1060a48 100644 --- a/deploy/ip.mfamily.org.nginx +++ b/deploy/ip.mfamily.org.nginx @@ -1,18 +1,50 @@ +#vim: syntax=nginx ts=4 sw=4 sts=4 sr noet + server { - listen 80; - listen [::]:80; + listen 443 ssl; + listen 443 quic; + listen [::]:443 ssl; + listen [::]:443 quic; + http2 on; + add_header Alt-Svc 'h3=":$server_port"; ma=86400'; server_name ip.mfamily.org; - return 301 https://$host$request_uri; -} + root /var/www/html-ip; + ssl_certificate /etc/letsencrypt/live/ip.mfamily.org/fullchain.pem; # managed by Certbot + ssl_certificate_key /etc/letsencrypt/live/ip.mfamily.org/privkey.pem; # managed by Certbot + include /etc/letsencrypt/options-ssl-nginx.conf; + ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; -server { - listen 443 ssl; - listen [::]:443 ssl; - server_name ip.mfamily.org; + access_log /var/log/nginx/ip-mf-access.log; + error_log /var/log/nginx/ip-mf-error.log; + + # Security headers + add_header X-Frame-Options "SAMEORIGIN" always; + add_header X-Content-Type-Options "nosniff" always; + add_header Strict-Transport-Security "max-age=15552000" always; - ssl_certificate /etc/letsencrypt/live/ip.mfamily.org/fullchain.pem; - ssl_certificate_key /etc/letsencrypt/live/ip.mfamily.org/privkey.pem; + default_type text/plain; + + error_page 400 = @empty_400; + error_page 401 = @empty_401; + error_page 403 = @empty_403; + error_page 404 = @empty_404; + error_page 405 = @empty_405; + error_page 408 = @empty_408; + error_page 413 = @empty_413; + error_page 414 = @empty_414; + error_page 415 = @empty_415; + error_page 429 = @empty_429; + error_page 500 = @empty_500; + error_page 501 = @empty_501; + error_page 502 = @empty_502; + error_page 503 = @empty_503; + error_page 504 = @empty_504; + error_page 505 = @empty_505; + + location ^~ /.well-known/ { + root /var/www/html-ip; + } location = / { proxy_pass http://127.0.0.1:8080/; @@ -20,9 +52,27 @@ server { proxy_set_header X-Real-IP $remote_addr; proxy_pass_request_body off; proxy_set_header Content-Length ""; + proxy_intercept_errors on; } location / { - return 404; + return 404 "404\n"; } + + location @empty_400 { return 400 "400\n"; } + location @empty_401 { return 401 "401\n"; } + location @empty_403 { return 403 "403\n"; } + location @empty_404 { return 404 "404\n"; } + location @empty_405 { return 405 "405\n"; } + location @empty_408 { return 408 "408\n"; } + location @empty_413 { return 413 "413\n"; } + location @empty_414 { return 414 "414\n"; } + location @empty_415 { return 415 "415\n"; } + location @empty_429 { return 429 "429\n"; } + location @empty_500 { return 500 "500\n"; } + location @empty_501 { return 501 "501\n"; } + location @empty_502 { return 502 "502\n"; } + location @empty_503 { return 503 "503\n"; } + location @empty_504 { return 504 "504\n"; } + location @empty_505 { return 505 "505\n"; } } diff --git a/ip-echo-daemon b/ip-echo-daemon index 5b66801..1864956 100755 --- a/ip-echo-daemon +++ b/ip-echo-daemon @@ -22,14 +22,23 @@ class AddressHandler(BaseHTTPRequestHandler): server_version = "ip-echo-daemon" sys_version = "" + def send_error(self, code, message=None, explain=None): + body = (str(code) + "\n").encode("ascii") + self.send_response(code) + self.send_header("Content-Type", "text/plain; charset=us-ascii") + self.send_header("Content-Length", str(len(body))) + self.end_headers() + if self.command != "HEAD": + self.wfile.write(body) + def do_GET(self): if self.path != "/": self.send_error(404) return try: address = proxied_address(self.client_address[0], self.headers.get("X-Real-IP")) - except ValueError as exc: - self.send_error(400, str(exc)) + except ValueError: + self.send_error(400) return body = (str(address) + "\n").encode("ascii") self.send_response(200) diff --git a/tests/test_he_dyndns.py b/tests/test_he_dyndns.py index d839a26..0fbe343 100644 --- a/tests/test_he_dyndns.py +++ b/tests/test_he_dyndns.py @@ -34,6 +34,24 @@ class EchoTests(unittest.TestCase): with self.assertRaisesRegex(ValueError, "loopback"): ip_echo.proxied_address("192.0.2.10", "198.51.100.20") + def test_error_response_contains_only_status_code(self): + handler = object.__new__(ip_echo.AddressHandler) + handler.command = "GET" + handler.wfile = mock.Mock() + handler.send_response = mock.Mock() + handler.send_header = mock.Mock() + handler.end_headers = mock.Mock() + + handler.send_error(404, "ignored") + + handler.send_response.assert_called_once_with(404) + handler.send_header.assert_has_calls([ + mock.call("Content-Type", "text/plain; charset=us-ascii"), + mock.call("Content-Length", "4"), + ]) + handler.end_headers.assert_called_once_with() + handler.wfile.write.assert_called_once_with(b"404\n") + class DiscoveryTests(unittest.TestCase): @mock.patch.object(he_dyndns, "http_get") |
