Selaa lähdekoodia

Address self-review: rename helper, end-to-end TOML tests

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).
pull/480/head
Alexey Dolotov 1 viikko sitten
vanhempi
commit
1960ff236b

+ 4
- 4
example.config.toml Näytä tiedosto

@@ -116,10 +116,10 @@ allow-fallback-on-unknown-dc = false
116 116
 # back to this server (e.g. mtg sits behind an SNI router whose DNS points
117 117
 # at itself), override the destination here.
118 118
 #
119
-# Use `host` for a hostname (resolved at dial time, so a dual-stack DNS
120
-# record can reach the right backend address family for IPv4 or IPv6
121
-# clients) or for a literal IP. Use `ip` if you specifically need a
122
-# literal IP and want strict IP validation. Setting both is an error.
119
+# Use `host` — accepts a hostname or a literal IP. Hostnames are resolved
120
+# at dial time, so a dual-stack DNS record can reach the right backend
121
+# address family for IPv4 or IPv6 clients. `ip` is kept for backward
122
+# compatibility (literal IP only). Setting both is an error.
123 123
 #
124 124
 # The hostname from the secret is still used for SNI in the TLS handshake.
125 125
 #

+ 2
- 2
internal/cli/doctor.go Näytä tiedosto

@@ -298,8 +298,8 @@ func (d *Doctor) checkNetworkAddresses(ntw mtglib.Network, addresses []string) e
298 298
 
299 299
 func (d *Doctor) checkFrontingDomain(ntw mtglib.Network) bool {
300 300
 	host := d.conf.Secret.Host
301
-	if ip := d.conf.GetDomainFrontingIP(nil); ip != "" {
302
-		host = ip
301
+	if override := d.conf.GetDomainFrontingHost(); override != "" {
302
+		host = override
303 303
 	}
304 304
 
305 305
 	port := d.conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort)

+ 1
- 1
internal/cli/run_proxy.go Näytä tiedosto

@@ -343,7 +343,7 @@ func runProxy(conf *config.Config, version string) error { //nolint: funlen, cyc
343 343
 		Secret:                      conf.Secret,
344 344
 		Concurrency:                 conf.GetConcurrency(mtglib.DefaultConcurrency),
345 345
 		DomainFrontingPort:          conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort),
346
-		DomainFrontingIP:            conf.GetDomainFrontingIP(nil),
346
+		DomainFrontingIP:            conf.GetDomainFrontingHost(),
347 347
 		DomainFrontingProxyProtocol: conf.GetDomainFrontingProxyProtocol(false),
348 348
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
349 349
 		AutoUpdate:                  conf.AutoUpdate.Get(false),

+ 2
- 3
internal/config/config.go Näytä tiedosto

@@ -4,7 +4,6 @@ import (
4 4
 	"bytes"
5 5
 	"encoding/json"
6 6
 	"fmt"
7
-	"net"
8 7
 	"net/url"
9 8
 
10 9
 	"github.com/9seconds/mtg/v2/mtglib"
@@ -118,14 +117,14 @@ func (c *Config) GetDomainFrontingPort(defaultValue uint) uint {
118 117
 	return c.DomainFrontingPort.Get(defaultValue)
119 118
 }
120 119
 
121
-func (c *Config) GetDomainFrontingIP(defaultValue net.IP) string {
120
+func (c *Config) GetDomainFrontingHost() string {
122 121
 	if host := c.DomainFronting.Host.Get(""); host != "" {
123 122
 		return host
124 123
 	}
125 124
 	if ip := c.DomainFronting.IP.Get(nil); ip != nil {
126 125
 		return ip.String()
127 126
 	}
128
-	if ip := c.DomainFrontingIP.Get(defaultValue); ip != nil {
127
+	if ip := c.DomainFrontingIP.Get(nil); ip != nil {
129 128
 		return ip.String()
130 129
 	}
131 130
 	return ""

+ 21
- 2
internal/config/config_test.go Näytä tiedosto

@@ -74,15 +74,34 @@ func (suite *ConfigTestSuite) TestString() {
74 74
 	suite.NotEmpty(conf.String())
75 75
 }
76 76
 
77
-func (suite *ConfigTestSuite) TestDomainFrontingHostOrIP() {
77
+func (suite *ConfigTestSuite) TestDomainFrontingHostAndIPMutuallyExclusive() {
78 78
 	conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
79 79
 	suite.NoError(err)
80 80
 
81 81
 	suite.NoError(conf.DomainFronting.Host.Set("fronting-backend"))
82 82
 	suite.NoError(conf.DomainFronting.IP.Set("10.0.0.10"))
83 83
 	suite.Error(conf.Validate())
84
+}
84 85
 
85
-	suite.Equal("fronting-backend", conf.GetDomainFrontingIP(nil))
86
+func (suite *ConfigTestSuite) TestDomainFrontingHostFromTOML() {
87
+	conf, err := config.Parse(suite.ReadConfig("domain_fronting_host.toml"))
88
+	suite.NoError(err)
89
+	suite.NoError(conf.Validate())
90
+	suite.Equal("fronting-backend", conf.GetDomainFrontingHost())
91
+}
92
+
93
+func (suite *ConfigTestSuite) TestDomainFrontingIPFromTOML() {
94
+	conf, err := config.Parse(suite.ReadConfig("domain_fronting_ip.toml"))
95
+	suite.NoError(err)
96
+	suite.NoError(conf.Validate())
97
+	suite.Equal("10.0.0.10", conf.GetDomainFrontingHost())
98
+}
99
+
100
+func (suite *ConfigTestSuite) TestDomainFrontingNotSet() {
101
+	conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
102
+	suite.NoError(err)
103
+	suite.NoError(conf.Validate())
104
+	suite.Equal("", conf.GetDomainFrontingHost())
86 105
 }
87 106
 
88 107
 func TestConfig(t *testing.T) {

+ 5
- 0
internal/config/testdata/domain_fronting_host.toml Näytä tiedosto

@@ -0,0 +1,5 @@
1
+secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
2
+bind-to = "0.0.0.0:3128"
3
+
4
+[domain-fronting]
5
+host = "fronting-backend"

+ 5
- 0
internal/config/testdata/domain_fronting_ip.toml Näytä tiedosto

@@ -0,0 +1,5 @@
1
+secret = "7oe1GqLy6TBc38CV3jx7q09nb29nbGUuY29t"
2
+bind-to = "0.0.0.0:3128"
3
+
4
+[domain-fronting]
5
+ip = "10.0.0.10"

Loading…
Peruuta
Tallenna