-
Notifications
You must be signed in to change notification settings - Fork 356
config: accept hostname for [domain-fronting] target #480
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
46ffe4e
1960ff2
908b32a
dfc805b
a7ede7c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t" | ||
| bind-to = "0.0.0.0:3128" | ||
|
|
||
| [domain-fronting] | ||
| host = "fronting-backend" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t" | ||
| bind-to = "0.0.0.0:3128" | ||
|
|
||
| [domain-fronting] | ||
| host = "10.0.0.1" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t" | ||
| bind-to = "0.0.0.0:3128" | ||
|
|
||
| [domain-fronting] | ||
| ip = "10.0.0.10" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net" | ||
| "strings" | ||
| ) | ||
|
|
||
| // TypeHost is a non-empty string that is either a literal IP address | ||
| // (IPv4 or IPv6) or a hostname suitable for DNS resolution. It does not | ||
| // include a port — the port belongs in a separate field. | ||
| type TypeHost struct { | ||
| Value string | ||
| } | ||
|
|
||
| func (t *TypeHost) Set(value string) error { | ||
| if value == "" { | ||
| return fmt.Errorf("host cannot be empty") | ||
| } | ||
|
|
||
| if net.ParseIP(value) != nil { | ||
| t.Value = value | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| if strings.ContainsAny(value, " \t\n/?#") { | ||
| return fmt.Errorf("incorrect host %q", value) | ||
| } | ||
|
|
||
| // At this point value is not a parsed IP (IPv6 literals returned | ||
| // above), so any remaining colon indicates a host:port form, which | ||
| // belongs in a separate field. | ||
| if strings.Contains(value, ":") { | ||
| return fmt.Errorf("host must not contain a port: %q", value) | ||
| } | ||
|
|
||
| t.Value = value | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (t TypeHost) Get(defaultValue string) string { | ||
| if t.Value == "" { | ||
| return defaultValue | ||
| } | ||
|
|
||
| return t.Value | ||
| } | ||
|
|
||
| func (t *TypeHost) UnmarshalText(data []byte) error { | ||
| return t.Set(string(data)) | ||
| } | ||
|
|
||
| func (t TypeHost) MarshalText() ([]byte, error) { | ||
| return []byte(t.Value), nil | ||
| } | ||
|
|
||
| func (t TypeHost) String() string { | ||
| return t.Value | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package config_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/9seconds/mtg/v2/internal/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/suite" | ||
| ) | ||
|
|
||
| type typeHostTestStruct struct { | ||
| Value config.TypeHost `json:"value"` | ||
| } | ||
|
|
||
| type TypeHostTestSuite struct { | ||
| suite.Suite | ||
| } | ||
|
|
||
| func (suite *TypeHostTestSuite) TestUnmarshalFail() { | ||
| testData := []string{ | ||
| "", | ||
| "web:8443", | ||
| "http://example.com", | ||
| "example.com/path", | ||
| "two words", | ||
| } | ||
|
|
||
| for _, v := range testData { | ||
| data, err := json.Marshal(map[string]string{ | ||
| "value": v, | ||
| }) | ||
| suite.NoError(err) | ||
|
|
||
| suite.T().Run(v, func(t *testing.T) { | ||
| assert.Error(t, json.Unmarshal(data, &typeHostTestStruct{})) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (suite *TypeHostTestSuite) TestUnmarshalOk() { | ||
| testData := []string{ | ||
| "example.com", | ||
| "web", | ||
| "sub.example.com", | ||
| "127.0.0.1", | ||
| "2001:db8::1", | ||
| } | ||
|
|
||
| for _, v := range testData { | ||
| value := v | ||
|
|
||
| data, err := json.Marshal(map[string]string{ | ||
| "value": value, | ||
| }) | ||
| suite.NoError(err) | ||
|
|
||
| suite.T().Run(value, func(t *testing.T) { | ||
| testStruct := &typeHostTestStruct{} | ||
| assert.NoError(t, json.Unmarshal(data, testStruct)) | ||
| assert.Equal(t, value, testStruct.Value.Get("")) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (suite *TypeHostTestSuite) TestGet() { | ||
| value := config.TypeHost{} | ||
| suite.Equal("default", value.Get("default")) | ||
|
|
||
| suite.NoError(value.Set("example.com")) | ||
| suite.Equal("example.com", value.Get("default")) | ||
| } | ||
|
|
||
| func TestTypeHost(t *testing.T) { | ||
| t.Parallel() | ||
| suite.Run(t, &TypeHostTestSuite{}) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -105,11 +105,14 @@ type ProxyOpts struct { | |
| // This is an optional setting. | ||
| DomainFrontingPort uint | ||
|
|
||
| // DomainFrontingIP is an IP address to use when connecting to the fronting | ||
| // domain instead of resolving the hostname from the secret via DNS. | ||
| // | ||
| // This is useful when DNS resolution of the fronting host is blocked. | ||
| // The hostname from the secret is still used for SNI in the TLS handshake. | ||
| // DomainFrontingIP is the address to use when connecting to the fronting | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it makes sense to deprecate this option then. Otherwise we will have 2 ways how to set the same effective values
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed on the principle — two ways to set the same effective value is confusing. The codebase already has a clean pattern for this:
One thing I'd like to pin down before the commit: scope. Are you asking to deprecate the config option The Go field is the dial target consumed by anyone using mtg as a library. With this PR it already holds either a hostname or an IP, so deprecating the symbol would mean introducing a renamed
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, glad we agree here. Yes, I'm thinking about config option. We can add a comment for DomainFrontingIP that it is present but unnecessary, and just remove all its usage. I understand that in theory it is going to break backward compatibility but this is not a new pattern. I think we are fine here. For example, this is how Go itself communicates such deprecations: https://pkg.go.dev/net just take a look at DualStack. It has been deprecated and flipped its original meaning. Technically, this was not backward compatible change.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Two scope questions before the commit:
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that good compromise would be to issue a warning in logs if any value is set. And ignore afterwards
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirming: warn + ignore for the config key, the One side effect worth flagging: a config with only |
||
| // domain instead of resolving the hostname from the secret via DNS. It | ||
| // can be a literal IP or a hostname; hostnames are resolved at dial time | ||
| // via the native dialer (which honours dual-stack and Happy Eyeballs). | ||
| // | ||
| // This is useful when DNS resolution of the secret's hostname is blocked | ||
| // or loops back to this server. The hostname from the secret is still | ||
| // used for SNI in the TLS handshake. | ||
| // | ||
| // This is an optional setting. | ||
| DomainFrontingIP string | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if there is a possibility to validate that this domain is resolvable. We can set any double dutch here. IP is fine, but I do believe that we can do something about resolving hostname.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd push back on doing DNS at parse time. Three reasons:
Codebase precedent. The closest existing field is
mtglib.Secret.Host(the secret's SNI hostname), andSecret.Set()does no DNS validation — only non-empty. Same "any double dutch" risk, deliberate choice.The reachability check already lives in
doctor.checkFrontingDomain()ininternal/cli/doctor.goresolves and dials the fronting target end-to-end; with this PR it picks uphostviaGetDomainFrontingHost(). A bogus hostname surfaces the dialer's DNS error there. That's the right layer for semantic checks — explicit, opt-in, with proper diagnostics.Resolving at parse time defeats the point of accepting a hostname. The motivating case (mtg behind an SNI router on a docker network) specifically needs dial-time resolution: the alias may resolve in-container but not on the host, and the address family can flip between v4/v6 per client (Happy Eyeballs). If we resolve at parse, either we cache the IP and lose all that, or we discard and resolve again at dial — in which case the parse-time resolve is just a flaky boot dependency.
Operational: a transient DNS hiccup at startup would prevent the proxy from starting, and a one-shot resolve doesn't catch the host going stale later — so it adds fragility without much real safety.
If the concern is that
doctor's message for an unresolvable host is too generic (it surfaces whateverDialContextreturns), I can add an explicitLookupIPAddrstep incheckFrontingDomainso the error reads "hostname X cannot be resolved" rather than being nested inside a dial error. Want me to wire that in?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, thanks. I see your point. But I must reply on that argument:
We should not think about different modes, host and container one. There should be only one environment we have to think about: one that runs mtg. If it happens to be in a container, let it be it. If it happens to be a generic host one, let it be a host one. If something is resolved on the host, but not in a container, then this is not a concern of mtg.
But this is not a performative concern, just my opinion in this regard. Such rigid behavior helps making a resilient software