Browse Source

contrib/sni-router: read $DOMAIN from env in haproxy.cfg

Closes #501.

Before: the SNI hostname had to be edited in haproxy.cfg, plus
DOMAIN had to be set in .env for Caddy.  Two places to keep in sync.

After: DOMAIN is the single source — set it once in .env (or export),
docker-compose forwards it into both haproxy and caddy containers,
and HAProxy interpolates ${DOMAIN} into the SNI ACL at startup.

mtg-config.toml's secret still embeds the domain in its bytes
(generated once with `mtg generate-secret <domain>`), so that one
remains a one-time edit at install — the README is updated to reflect
this.

HAProxy has supported environment-variable substitution in config
strings since 1.6.
pull/502/head
Alexey Dolotov 1 week ago
parent
commit
1606afe3b1

+ 4
- 5
contrib/sni-router/README.md View File

27
 # 2. Generate an mtg secret:
27
 # 2. Generate an mtg secret:
28
 docker run --rm nineseconds/mtg:2 generate-secret --hex YOUR_DOMAIN
28
 docker run --rm nineseconds/mtg:2 generate-secret --hex YOUR_DOMAIN
29
 
29
 
30
-# 3. Edit the config files:
31
-#    - mtg-config.toml  →  paste the secret
32
-#    - haproxy.cfg       →  replace "example.com" in the SNI ACL
33
-#    - .env or export    →  DOMAIN=your.domain
30
+# 3. Configure:
31
+#    - .env (or export)  →  DOMAIN=your.domain   # used by HAProxy + Caddy
32
+#    - mtg-config.toml   →  paste the secret
34
 
33
 
35
 # 4. (Optional) put your site content into www/
34
 # 4. (Optional) put your site content into www/
36
 
35
 
83
 | File | Purpose |
82
 | File | Purpose |
84
 |---|---|
83
 |---|---|
85
 | `docker-compose.yml` | Service definitions |
84
 | `docker-compose.yml` | Service definitions |
86
-| `haproxy.cfg` | SNI routing rules — **edit the domain** |
85
+| `haproxy.cfg` | SNI routing rules (reads `$DOMAIN` from the environment) |
87
 | `mtg-config.toml` | mtg proxy config — **paste your secret** |
86
 | `mtg-config.toml` | mtg proxy config — **paste your secret** |
88
 | `Caddyfile` | Web server config (auto-HTTPS) |
87
 | `Caddyfile` | Web server config (auto-HTTPS) |
89
 | `www/` | Static site content served by Caddy |
88
 | `www/` | Static site content served by Caddy |

+ 8
- 4
contrib/sni-router/docker-compose.yml View File

9
 # SNI/IP because the domain resolves to this server's IP.
9
 # SNI/IP because the domain resolves to this server's IP.
10
 #
10
 #
11
 # Quick start:
11
 # Quick start:
12
-#   1. Set YOUR_DOMAIN below (and in mtg-config.toml)
13
-#   2. docker compose up -d
14
-#   3. mtg generate-secret YOUR_DOMAIN   -> put it in mtg-config.toml
15
-#   4. docker compose restart mtg
12
+#   1. Set DOMAIN in a .env file next to this one (or export it)
13
+#   2. mtg generate-secret YOUR_DOMAIN   -> paste into mtg-config.toml
14
+#   3. docker compose up -d
15
+#
16
+# DOMAIN is forwarded to both Caddy (TLS cert) and HAProxy (SNI ACL),
17
+# so the SNI/cert/secret all line up from a single source.
16
 #
18
 #
17
 # See BEST_PRACTICES.md and the project wiki for background.
19
 # See BEST_PRACTICES.md and the project wiki for background.
18
 
20
 
24
       - "80:80"
26
       - "80:80"
25
     volumes:
27
     volumes:
26
       - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro,Z
28
       - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro,Z
29
+    environment:
30
+      DOMAIN: ${DOMAIN:-example.com}
27
     depends_on:
31
     depends_on:
28
       - mtg
32
       - mtg
29
       - web
33
       - web

+ 4
- 3
contrib/sni-router/haproxy.cfg View File

39
     tcp-request inspect-delay 5s
39
     tcp-request inspect-delay 5s
40
     tcp-request content accept if { req_ssl_hello_type 1 }
40
     tcp-request content accept if { req_ssl_hello_type 1 }
41
 
41
 
42
-    # Route Telegram clients to mtg.
43
-    # Replace "example.com" with the domain from your mtg secret.
44
-    use_backend mtg if { req_ssl_sni -i example.com }
42
+    # Route Telegram clients to mtg.  The domain is read from the $DOMAIN
43
+    # environment variable (forwarded by docker-compose), so it stays in
44
+    # sync with Caddy and there is no per-deploy edit to this file.
45
+    use_backend mtg if { req_ssl_sni -i "${DOMAIN}" }
45
 
46
 
46
     default_backend web
47
     default_backend web
47
 
48
 

Loading…
Cancel
Save