-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
46ffe4e
Accept hostname for [domain-fronting] target
dolonet 1960ff2
Address self-review: rename helper, end-to-end TOML tests
dolonet 908b32a
Address round-two review: rename mtglib privates, reorder, more tests
dolonet dfc805b
Clarify why TypeHost rejects ':' (IPv6 already returned above)
dolonet a7ede7c
Deprecate "ip" in favour of "host" for domain fronting
dolonet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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{}) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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