Просмотр исходного кода

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 неделю назад
Родитель
Сommit
1960ff236b

+ 4
- 4
example.config.toml Просмотреть файл

116
 # back to this server (e.g. mtg sits behind an SNI router whose DNS points
116
 # back to this server (e.g. mtg sits behind an SNI router whose DNS points
117
 # at itself), override the destination here.
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
 # The hostname from the secret is still used for SNI in the TLS handshake.
124
 # The hostname from the secret is still used for SNI in the TLS handshake.
125
 #
125
 #

+ 2
- 2
internal/cli/doctor.go Просмотреть файл

298
 
298
 
299
 func (d *Doctor) checkFrontingDomain(ntw mtglib.Network) bool {
299
 func (d *Doctor) checkFrontingDomain(ntw mtglib.Network) bool {
300
 	host := d.conf.Secret.Host
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
 	port := d.conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort)
305
 	port := d.conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort)

+ 1
- 1
internal/cli/run_proxy.go Просмотреть файл

343
 		Secret:                      conf.Secret,
343
 		Secret:                      conf.Secret,
344
 		Concurrency:                 conf.GetConcurrency(mtglib.DefaultConcurrency),
344
 		Concurrency:                 conf.GetConcurrency(mtglib.DefaultConcurrency),
345
 		DomainFrontingPort:          conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort),
345
 		DomainFrontingPort:          conf.GetDomainFrontingPort(mtglib.DefaultDomainFrontingPort),
346
-		DomainFrontingIP:            conf.GetDomainFrontingIP(nil),
346
+		DomainFrontingIP:            conf.GetDomainFrontingHost(),
347
 		DomainFrontingProxyProtocol: conf.GetDomainFrontingProxyProtocol(false),
347
 		DomainFrontingProxyProtocol: conf.GetDomainFrontingProxyProtocol(false),
348
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
348
 		PreferIP:                    conf.PreferIP.Get(mtglib.DefaultPreferIP),
349
 		AutoUpdate:                  conf.AutoUpdate.Get(false),
349
 		AutoUpdate:                  conf.AutoUpdate.Get(false),

+ 2
- 3
internal/config/config.go Просмотреть файл

4
 	"bytes"
4
 	"bytes"
5
 	"encoding/json"
5
 	"encoding/json"
6
 	"fmt"
6
 	"fmt"
7
-	"net"
8
 	"net/url"
7
 	"net/url"
9
 
8
 
10
 	"github.com/9seconds/mtg/v2/mtglib"
9
 	"github.com/9seconds/mtg/v2/mtglib"
118
 	return c.DomainFrontingPort.Get(defaultValue)
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
 	if host := c.DomainFronting.Host.Get(""); host != "" {
121
 	if host := c.DomainFronting.Host.Get(""); host != "" {
123
 		return host
122
 		return host
124
 	}
123
 	}
125
 	if ip := c.DomainFronting.IP.Get(nil); ip != nil {
124
 	if ip := c.DomainFronting.IP.Get(nil); ip != nil {
126
 		return ip.String()
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
 		return ip.String()
128
 		return ip.String()
130
 	}
129
 	}
131
 	return ""
130
 	return ""

+ 21
- 2
internal/config/config_test.go Просмотреть файл

74
 	suite.NotEmpty(conf.String())
74
 	suite.NotEmpty(conf.String())
75
 }
75
 }
76
 
76
 
77
-func (suite *ConfigTestSuite) TestDomainFrontingHostOrIP() {
77
+func (suite *ConfigTestSuite) TestDomainFrontingHostAndIPMutuallyExclusive() {
78
 	conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
78
 	conf, err := config.Parse(suite.ReadConfig("minimal.toml"))
79
 	suite.NoError(err)
79
 	suite.NoError(err)
80
 
80
 
81
 	suite.NoError(conf.DomainFronting.Host.Set("fronting-backend"))
81
 	suite.NoError(conf.DomainFronting.Host.Set("fronting-backend"))
82
 	suite.NoError(conf.DomainFronting.IP.Set("10.0.0.10"))
82
 	suite.NoError(conf.DomainFronting.IP.Set("10.0.0.10"))
83
 	suite.Error(conf.Validate())
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
 func TestConfig(t *testing.T) {
107
 func TestConfig(t *testing.T) {

+ 5
- 0
internal/config/testdata/domain_fronting_host.toml Просмотреть файл

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 Просмотреть файл

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

Загрузка…
Отмена
Сохранить