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

Address round-two review: rename mtglib privates, reorder, more tests

- 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.
pull/480/head
Alexey Dolotov 1 неделю назад
Родитель
Сommit
908b32afff

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

37
 	PublicIPv4                  TypeIP          `json:"publicIpv4"`
37
 	PublicIPv4                  TypeIP          `json:"publicIpv4"`
38
 	PublicIPv6                  TypeIP          `json:"publicIpv6"`
38
 	PublicIPv6                  TypeIP          `json:"publicIpv6"`
39
 	DomainFronting              struct {
39
 	DomainFronting              struct {
40
-		IP            TypeIP   `json:"ip"`
41
 		Host          TypeHost `json:"host"`
40
 		Host          TypeHost `json:"host"`
41
+		IP            TypeIP   `json:"ip"`
42
 		Port          TypePort `json:"port"`
42
 		Port          TypePort `json:"port"`
43
 		ProxyProtocol TypeBool `json:"proxyProtocol"`
43
 		ProxyProtocol TypeBool `json:"proxyProtocol"`
44
 	} `json:"domainFronting"`
44
 	} `json:"domainFronting"`

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

90
 	suite.Equal("fronting-backend", conf.GetDomainFrontingHost())
90
 	suite.Equal("fronting-backend", conf.GetDomainFrontingHost())
91
 }
91
 }
92
 
92
 
93
+func (suite *ConfigTestSuite) TestDomainFrontingHostAcceptsLiteralIP() {
94
+	conf, err := config.Parse(suite.ReadConfig("domain_fronting_host_ip.toml"))
95
+	suite.NoError(err)
96
+	suite.NoError(conf.Validate())
97
+	suite.Equal("10.0.0.1", conf.GetDomainFrontingHost())
98
+}
99
+
93
 func (suite *ConfigTestSuite) TestDomainFrontingIPFromTOML() {
100
 func (suite *ConfigTestSuite) TestDomainFrontingIPFromTOML() {
94
 	conf, err := config.Parse(suite.ReadConfig("domain_fronting_ip.toml"))
101
 	conf, err := config.Parse(suite.ReadConfig("domain_fronting_ip.toml"))
95
 	suite.NoError(err)
102
 	suite.NoError(err)

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

24
 	PublicIPv4                  string `toml:"public-ipv4" json:"publicIpv4,omitempty"`
24
 	PublicIPv4                  string `toml:"public-ipv4" json:"publicIpv4,omitempty"`
25
 	PublicIPv6                  string `toml:"public-ipv6" json:"publicIpv6,omitempty"`
25
 	PublicIPv6                  string `toml:"public-ipv6" json:"publicIpv6,omitempty"`
26
 	DomainFronting              struct {
26
 	DomainFronting              struct {
27
-		IP            string `toml:"ip" json:"ip,omitempty"`
28
 		Host          string `toml:"host" json:"host,omitempty"`
27
 		Host          string `toml:"host" json:"host,omitempty"`
28
+		IP            string `toml:"ip" json:"ip,omitempty"`
29
 		Port          uint   `toml:"port" json:"port,omitempty"`
29
 		Port          uint   `toml:"port" json:"port,omitempty"`
30
 		ProxyProtocol bool   `toml:"proxy-protocol" json:"proxyProtocol,omitempty"`
30
 		ProxyProtocol bool   `toml:"proxy-protocol" json:"proxyProtocol,omitempty"`
31
 	} `toml:"domain-fronting" json:"domainFronting,omitempty"`
31
 	} `toml:"domain-fronting" json:"domainFronting,omitempty"`

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

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

+ 6
- 5
mtglib/proxy.go Просмотреть файл

30
 	idleTimeout                 time.Duration
30
 	idleTimeout                 time.Duration
31
 	handshakeTimeout            time.Duration
31
 	handshakeTimeout            time.Duration
32
 	domainFrontingPort          int
32
 	domainFrontingPort          int
33
-	domainFrontingIP            string
33
+	domainFrontingHost          string
34
 	domainFrontingProxyProtocol bool
34
 	domainFrontingProxyProtocol bool
35
 	workerPool                  *ants.PoolWithFunc
35
 	workerPool                  *ants.PoolWithFunc
36
 	telegram                    *dc.Telegram
36
 	telegram                    *dc.Telegram
48
 }
48
 }
49
 
49
 
50
 // DomainFrontingAddress returns a host:port pair for a fronting domain.
50
 // DomainFrontingAddress returns a host:port pair for a fronting domain.
51
-// If DomainFrontingIP is set, it is used instead of resolving the hostname.
51
+// If a fronting host (literal IP or hostname) is configured, it is used
52
+// instead of resolving the secret's hostname.
52
 func (p *Proxy) DomainFrontingAddress() string {
53
 func (p *Proxy) DomainFrontingAddress() string {
53
 	host := p.secret.Host
54
 	host := p.secret.Host
54
-	if p.domainFrontingIP != "" {
55
-		host = p.domainFrontingIP
55
+	if p.domainFrontingHost != "" {
56
+		host = p.domainFrontingHost
56
 	}
57
 	}
57
 
58
 
58
 	return net.JoinHostPort(host, strconv.Itoa(p.domainFrontingPort))
59
 	return net.JoinHostPort(host, strconv.Itoa(p.domainFrontingPort))
354
 		eventStream:              opts.EventStream,
355
 		eventStream:              opts.EventStream,
355
 		logger:                   logger,
356
 		logger:                   logger,
356
 		domainFrontingPort:       opts.getDomainFrontingPort(),
357
 		domainFrontingPort:       opts.getDomainFrontingPort(),
357
-		domainFrontingIP:         opts.DomainFrontingIP,
358
+		domainFrontingHost:       opts.DomainFrontingIP,
358
 		tolerateTimeSkewness:     opts.getTolerateTimeSkewness(),
359
 		tolerateTimeSkewness:     opts.getTolerateTimeSkewness(),
359
 		idleTimeout:              opts.getIdleTimeout(),
360
 		idleTimeout:              opts.getIdleTimeout(),
360
 		handshakeTimeout:         opts.getHandshakeTimeout(),
361
 		handshakeTimeout:         opts.getHandshakeTimeout(),

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