Skip to content

Instantly share code, notes, and snippets.

@drizzt
Last active January 12, 2024 21:30
Show Gist options
  • Save drizzt/0a9e409e7cc6cb7cb6f0b113800ecf9c to your computer and use it in GitHub Desktop.
Save drizzt/0a9e409e7cc6cb7cb6f0b113800ecf9c to your computer and use it in GitHub Desktop.

Revisions

  1. drizzt revised this gist Jan 12, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.py
    Original file line number Diff line number Diff line change
    @@ -40,6 +40,6 @@ async def topic(request):
    app = Starlette(
    debug=True,
    routes=[
    Route("/{path:path}", topic, methods=["GET", "POST", "PUT"]),
    Route("/{path:path}", topic, methods=["POST"]),
    ],
    )
  2. drizzt revised this gist Jan 12, 2024. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ async def topic(request):
    pubKey = request.headers.get("crypto-key").encode()
    salt = request.headers.get("encryption").encode()

    body = await request.body() + b"\n" + pubKey + b"\n" + salt + b"\naesgcm"
    body = b"aesgcm\nEncryption: " + salt + b"\nCrypto-Key: " + pubKey + b"\n" + await request.body()

    headers = dict(request.headers)
    del headers["content-encoding"]
  3. drizzt created this gist Dec 11, 2023.
    45 changes: 45 additions & 0 deletions main.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,45 @@
    #!/usr/bin/python3


    from starlette.applications import Starlette
    from starlette.responses import Response
    from starlette.routing import Route

    import httpx


    async def topic(request):
    host = request.headers.get("host")
    path = request.path_params["path"]
    target_url = f"https://{host}/{path}"

    pubKey = request.headers.get("crypto-key").encode()
    salt = request.headers.get("encryption").encode()

    body = await request.body() + b"\n" + pubKey + b"\n" + salt + b"\naesgcm"

    headers = dict(request.headers)
    del headers["content-encoding"]
    del headers["content-length"]

    # Forward the request to the target URL
    async with httpx.AsyncClient() as client:
    upstream_response = await client.request(
    method=request.method,
    url=target_url,
    headers=headers,
    data=body,
    params=request.query_params,
    )

    return Response(
    content=upstream_response.content, status_code=upstream_response.status_code
    )


    app = Starlette(
    debug=True,
    routes=[
    Route("/{path:path}", topic, methods=["GET", "POST", "PUT"]),
    ],
    )
    32 changes: 32 additions & 0 deletions nginx_nfty.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    map $http_content_encoding $ntfy_backend {
    "aesgcm" "127.0.0.1:8000";
    default "127.0.0.1:2586";
    }

    server {
    listen 80;
    listen [::]:80;
    server_name YOURDOMAIN;

    location /generic {
    proxy_pass http://127.0.0.1:5000;
    }

    location / {
    add_header alt-svc 'h3-27=":443"; ma=86400, h3-28=":443"; ma=86400, h3-29=":443"; ma=86400, h3=":443"; ma=86400';

    proxy_pass http://$ntfy_backend;
    proxy_http_version 1.1;

    proxy_set_header Host $host;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    proxy_connect_timeout 3m;
    proxy_send_timeout 3m;
    proxy_read_timeout 3m;

    client_max_body_size 0; # Stream request body to backend
    }
    }