Skip to content
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

[v13] WebPublicAddr includes user specified port. #27376

Merged
merged 4 commits into from Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions lib/service/servicecfg/config_test.go
Expand Up @@ -644,3 +644,46 @@ func TestVerifyEnabledService(t *testing.T) {
})
}
}

func TestWebPublicAddr(t *testing.T) {
tests := []struct {
name string
config ProxyConfig
expected string
}{
{
name: "no public address specified",
expected: "https://<proxyhost>:3080",
},
{
name: "default port",
config: ProxyConfig{
PublicAddrs: []utils.NetAddr{
{Addr: "0.0.0.0", AddrNetwork: "tcp"},
},
},
expected: "https://0.0.0.0:3080",
},
{
name: "non-default port",
config: ProxyConfig{
PublicAddrs: []utils.NetAddr{
{Addr: "0.0.0.0:443", AddrNetwork: "tcp"},
},
},
expected: "https://0.0.0.0:443",
},
}

for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()

out, err := test.config.WebPublicAddr()
require.NoError(t, err)

require.Equal(t, test.expected, out)
})
}
}
13 changes: 12 additions & 1 deletion lib/service/servicecfg/proxy.go
Expand Up @@ -141,7 +141,18 @@ type ProxyConfig struct {
// WebPublicAddr returns the address for the web endpoint on this proxy that
// can be reached by clients.
func (c ProxyConfig) WebPublicAddr() (string, error) {
return c.getDefaultAddr(c.WebAddr.Port(defaults.HTTPListenPort)), nil
// Use the port from the first public address if possible.
if len(c.PublicAddrs) > 0 {
publicAddr := c.PublicAddrs[0]
u := url.URL{
Scheme: "https",
Host: net.JoinHostPort(publicAddr.Host(), strconv.Itoa(publicAddr.Port(defaults.HTTPListenPort))),
}
return u.String(), nil
}

port := c.WebAddr.Port(defaults.HTTPListenPort)
return c.getDefaultAddr(port), nil
}

func (c ProxyConfig) getDefaultAddr(port int) string {
Expand Down