Skip to content

config: accept hostname for [domain-fronting] target#480

Open
dolonet wants to merge 4 commits into9seconds:masterfrom
dolonet:feat/domain-fronting-host
Open

config: accept hostname for [domain-fronting] target#480
dolonet wants to merge 4 commits into9seconds:masterfrom
dolonet:feat/domain-fronting-host

Conversation

@dolonet
Copy link
Copy Markdown
Contributor

@dolonet dolonet commented Apr 28, 2026

Summary

Today [domain-fronting].ip only accepts a literal IP (TypeIP
net.ParseIP). When mtg sits behind an SNI router (HAProxy, etc.)
whose DNS for the secret's hostname points back at the same host,
mtg's default fronting behaviour resolves that hostname and dials
itself, looping. Working around this in deployment requires pinning a
static docker container IP and subnet so the literal-IP field can
target the fronting backend directly.

This adds a sibling [domain-fronting].host field that accepts either
a hostname or an IP. Hostnames are resolved at dial time by the native
dialer (which already handles dual-stack / Happy Eyeballs), so an A+AAAA
docker-DNS or normal DNS record reaches the right backend address
family for IPv4 and IPv6 clients without a static pin.

ip semantics are unchanged. Setting both host and ip is rejected
in Validate().

API stability

mtglib.ProxyOpts.DomainFrontingIP is still a plain string. The
dial path already passes it to net.JoinHostPort + DialContext,
both of which accept hostnames. Only the field's doc comment was
clarified — no signature change, no new public symbols in mtglib.

internal/cli/run_proxy.go, simple_run.go and doctor.go all
continue to work via the existing Config.GetDomainFrontingIP()
helper, which now prefers Host over IP over the legacy flat
domain-fronting-ip.

What's added

  • internal/config/type_host.goTypeHost (accepts hostname or IP,
    rejects empty, host:port, URLs, whitespace).
  • internal/config/type_host_test.go — Get / UnmarshalOk / UnmarshalFail.
  • internal/config/config_test.goTestDomainFrontingHostOrIP
    covering precedence and mutual-exclusion.
  • [domain-fronting] section of example.config.toml updated to
    document host alongside ip.

Motivation

Direct follow-up to a review thread on #478, which currently works
around the literal-IP restriction with a pinned docker subnet. Once
this lands, that PR collapses to ~3 lines (host = "web") and its
IPv6 PROXY-protocol-family caveat goes away.

Test plan

  • go build ./...
  • go vet ./...
  • go test ./... — full suite green
  • New TypeHost tests pass (5 accept, 5 reject)
  • New TestDomainFrontingHostOrIP covers Validate() rejection of both-set and Host-wins precedence

dolonet added 2 commits April 28, 2026 07:34
The existing `[domain-fronting].ip` only accepts a literal IP. That
forces SNI-router setups to pin a static container address (and a
static docker subnet) so mtg can dial the fronting backend directly
instead of resolving the secret's hostname via DNS, which would loop
back into mtg through the SNI router.

Add a sibling `[domain-fronting].host` that accepts either a hostname
or an IP. Hostnames are resolved at dial time by the native dialer
(Happy Eyeballs / dual-stack), so a docker-DNS or any A+AAAA record
naturally picks the right backend address family per client. Setting
both `host` and `ip` is rejected at validation.

The mtglib API stays backward compatible: ProxyOpts.DomainFrontingIP
is still a plain string and the dial path already calls JoinHostPort +
DialContext, both of which accept hostnames. Only the doc comment was
clarified.
Follow-up to the previous commit on this branch:

- Rename Config.GetDomainFrontingIP -> GetDomainFrontingHost. The
  helper now returns a hostname or an IP, so the old name was a lie.
  Drop the unused defaultValue net.IP parameter (every caller passed
  nil). Update internal/cli/run_proxy.go and internal/cli/doctor.go;
  rename the misleading `ip` local var in doctor.go to `override`.

- Add TOML fixtures (domain_fronting_host.toml, domain_fronting_ip.toml)
  so the new field is exercised through the actual Parse()->JSON->Config
  path users hit, not just via direct .Set() calls. Plus a positive
  backward-compat test confirming an `ip`-only legacy config still
  validates and resolves correctly, and a no-fronting test confirming
  the unset case returns empty.

- Clarify example.config.toml: `ip` is kept for backward compatibility,
  not because it has stricter validation semantics worth choosing over
  `host`.

mtglib.ProxyOpts.DomainFrontingIP keeps its name (public API).
@dolonet
Copy link
Copy Markdown
Contributor Author

dolonet commented Apr 28, 2026

A few open questions worth deciding before merge — happy to do either branch:

1. Additive vs. rename-with-deprecation. This PR adds host alongside ip so existing configs keep working untouched. Two parallel fields (with mutual-exclusion validation) is the cost. The alternative is to rename iphost and keep ip as an alias that emits a deprecation warning via the existing doctor template (the same pattern used for the flat domain-fronting-ip[domain-fronting].ip migration in 2.3.0). Cleaner long-term schema, slightly more code. Which do you prefer?

2. simple_run CLI flag. --domain-fronting-ip in internal/cli/simple_run.go still only accepts a literal IP. Inconsistent with the config-file path now. I left it untouched to keep this PR focused, but I'm happy to add --domain-fronting-host (with the same mutual-exclusion check) in the same PR or a follow-up.

3. Mutual-exclusion scope. The current check rejects [domain-fronting].host + [domain-fronting].ip together, but doesn't flag the (unlikely) combo of new [domain-fronting].host + legacy flat domain-fronting-ip. That's consistent with how the existing precedence already silently overrides flat → grouped, so I left it. Easy to broaden if you'd rather warn on every collision.

dolonet added 2 commits April 28, 2026 08:28
- mtglib/proxy.go: rename private field domainFrontingIP -> domainFrontingHost
  and update DomainFrontingAddress() doc comment to reflect that hostnames
  are now accepted. The exported mtglib.ProxyOpts.DomainFrontingIP is
  unchanged (public API), so the assignment in NewProxy now reads
  `domainFrontingHost: opts.DomainFrontingIP,` which makes the
  public-vs-internal naming explicitly visible at the boundary.
- internal/config/{parse,config}.go: reorder so Host comes before IP in
  the [domain-fronting] struct. Cosmetic, but signals Host is the
  preferred forward path.
- Add TestDomainFrontingHostAcceptsLiteralIP + domain_fronting_host_ip.toml
  fixture exercising the documented "host accepts hostname or literal IP"
  contract end-to-end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant