Skip to content

Commit d72ea03

Browse files
committed
improvement: change http(s) listener to web listener addr and a public web addr
1 parent 94d9168 commit d72ea03

File tree

8 files changed

+133
-134
lines changed

8 files changed

+133
-134
lines changed

internal/cmd/configure.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ func configureCommand() *cobra.Command {
5252
command.RunE = func(command *cobra.Command, args []string) error {
5353
c := &config.Config{}
5454

55-
c.HttpListenAddr = "0.0.0.0:80"
56-
c.HttpsListenAddr = "0.0.0.0:443"
55+
c.WebListenAddr = "0.0.0.0:443"
5756
c.MetricsListenAddr = "127.0.0.1:9090"
58-
c.ServerUrl = fmt.Sprintf("https://%s", domain)
57+
c.WebPublicAddr = fmt.Sprintf("%s:443", domain)
5958

6059
c.Keys = config.Keys{
6160
ControlKey: key.NewServerKey().String(),

internal/config/config.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
"github.com/jsiebens/ionscale/internal/util"
1212
"github.com/mitchellh/go-homedir"
1313
"gopkg.in/yaml.v3"
14+
"net/url"
1415
"os"
1516
"path/filepath"
16-
"strings"
1717
tkey "tailscale.com/types/key"
1818
"time"
1919
)
@@ -95,15 +95,13 @@ func LoadConfig(path string) (*Config, error) {
9595
dnsProviderConfigured = true
9696
}
9797

98-
return cfg, nil
98+
return cfg.Validate()
9999
}
100100

101101
func defaultConfig() *Config {
102102
return &Config{
103-
HttpListenAddr: ":8080",
104-
HttpsListenAddr: ":8443",
103+
WebListenAddr: ":8080",
105104
MetricsListenAddr: ":9091",
106-
ServerUrl: "https://localhost:8843",
107105
Database: Database{
108106
Type: "sqlite",
109107
Url: "./ionscale.db?_pragma=busy_timeout(5000)&_pragma=journal_mode(WAL)&_pragma=foreign_keys(ON)",
@@ -135,17 +133,18 @@ type ServerKeys struct {
135133
}
136134

137135
type Config struct {
138-
HttpListenAddr string `yaml:"http_listen_addr,omitempty" env:"HTTP_LISTEN_ADDR"`
139-
HttpsListenAddr string `yaml:"https_listen_addr,omitempty" env:"HTTPS_LISTEN_ADDR"`
136+
WebListenAddr string `yaml:"web_listen_addr,omitempty" env:"WEB_LISTEN_ADDR"`
140137
MetricsListenAddr string `yaml:"metrics_listen_addr,omitempty" env:"METRICS_LISTEN_ADDR"`
141-
ServerUrl string `yaml:"server_url,omitempty" env:"SERVER_URL"`
138+
WebPublicAddr string `yaml:"web_public_addr,omitempty" env:"WEB_PUBLIC_ADDR"`
142139
Tls Tls `yaml:"tls,omitempty" envPrefix:"TLS_"`
143140
PollNet PollNet `yaml:"poll_net,omitempty" envPrefix:"POLL_NET_"`
144141
Keys Keys `yaml:"keys,omitempty" envPrefix:"KEYS_"`
145142
Database Database `yaml:"database,omitempty" envPrefix:"DB_"`
146143
Auth Auth `yaml:"auth,omitempty" envPrefix:"AUTH_"`
147144
DNS DNS `yaml:"dns,omitempty"`
148145
Logging Logging `yaml:"logging,omitempty" envPrefix:"LOGGING_"`
146+
147+
WebPublicUrl *url.URL `yaml:"-"`
149148
}
150149

151150
type Tls struct {
@@ -212,9 +211,24 @@ type SystemAdminPolicy struct {
212211
Filters []string `yaml:"filters,omitempty"`
213212
}
214213

214+
func (c *Config) Validate() (*Config, error) {
215+
publicWebUrl, err := publicAddrToUrl(c.WebPublicAddr)
216+
if err != nil {
217+
return nil, err
218+
}
219+
220+
c.WebPublicUrl = publicWebUrl
221+
return c, nil
222+
}
223+
215224
func (c *Config) CreateUrl(format string, a ...interface{}) string {
216225
path := fmt.Sprintf(format, a...)
217-
return strings.TrimSuffix(c.ServerUrl, "/") + "/" + strings.TrimPrefix(path, "/")
226+
u := url.URL{
227+
Scheme: c.WebPublicUrl.Scheme,
228+
Host: c.WebPublicUrl.Host,
229+
Path: path,
230+
}
231+
return u.String()
218232
}
219233

220234
func (c *Config) ReadServerKeys(defaultKeys *domain.ControlKeys) (*ServerKeys, error) {

internal/config/funcs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package config
22

33
import (
4+
"fmt"
5+
"net"
6+
"net/url"
47
"os"
58
"strings"
69
)
@@ -20,3 +23,28 @@ func GetString(key, defaultValue string) string {
2023
}
2124
return defaultValue
2225
}
26+
27+
func publicAddrToUrl(addr string) (*url.URL, error) {
28+
scheme := "https"
29+
30+
if strings.HasPrefix(addr, "http://") {
31+
scheme = "http"
32+
addr = strings.TrimPrefix(addr, "http://")
33+
}
34+
35+
if strings.HasPrefix(addr, "https://") {
36+
scheme = "https"
37+
addr = strings.TrimPrefix(addr, "https://")
38+
}
39+
40+
host, port, err := net.SplitHostPort(addr)
41+
if err != nil {
42+
return nil, fmt.Errorf("invalid public addr")
43+
}
44+
45+
if (port == "443" && scheme == "https") || (port == "80" && scheme == "http") || port == "" {
46+
return &url.URL{Scheme: scheme, Host: host}, nil
47+
}
48+
49+
return &url.URL{Scheme: scheme, Host: fmt.Sprintf("%s:%s", host, port)}, nil
50+
}

internal/config/funcs_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package config
2+
3+
import (
4+
"fmt"
5+
"github.com/stretchr/testify/require"
6+
"net/url"
7+
"testing"
8+
)
9+
10+
func TestPublicAddrToUrl(t *testing.T) {
11+
mustParseUrl := func(s string) *url.URL {
12+
parse, err := url.Parse(s)
13+
require.NoError(t, err)
14+
return parse
15+
}
16+
17+
parameters := []struct {
18+
input string
19+
expected *url.URL
20+
err error
21+
}{
22+
{"localtest.me", nil, fmt.Errorf("invalid public addr")},
23+
{"localtest.me:443", mustParseUrl("https://localtest.me"), nil},
24+
{"localtest.me:80", mustParseUrl("https://localtest.me:80"), nil},
25+
{"localtest.me:8080", mustParseUrl("https://localtest.me:8080"), nil},
26+
{"http://localtest.me:8080", mustParseUrl("http://localtest.me:8080"), nil},
27+
}
28+
29+
for _, p := range parameters {
30+
t.Run(fmt.Sprintf("Testing [%v]", p.input), func(t *testing.T) {
31+
url, err := publicAddrToUrl(p.input)
32+
require.Equal(t, p.expected, url)
33+
require.Equal(t, p.err, err)
34+
})
35+
}
36+
}

internal/handlers/http_redirect.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package handlers
22

33
import (
4-
"github.com/caddyserver/certmagic"
54
"github.com/jsiebens/ionscale/internal/config"
65
"github.com/labstack/echo/v4"
76
"github.com/labstack/echo/v4/middleware"
8-
"net"
9-
"net/http"
107
)
118

129
func httpsRedirectSkipper(c config.Tls) func(ctx echo.Context) bool {
@@ -23,38 +20,3 @@ func HttpsRedirect(c config.Tls) echo.MiddlewareFunc {
2320
Skipper: httpsRedirectSkipper(c),
2421
})
2522
}
26-
27-
func HttpRedirectHandler(tls config.Tls) echo.HandlerFunc {
28-
if tls.Disable {
29-
return IndexHandler(http.StatusNotFound)
30-
}
31-
32-
if tls.AcmeEnabled {
33-
cfg := certmagic.NewDefault()
34-
if len(cfg.Issuers) > 0 {
35-
if am, ok := cfg.Issuers[0].(*certmagic.ACMEIssuer); ok {
36-
handler := am.HTTPChallengeHandler(http.HandlerFunc(httpRedirectHandler))
37-
return echo.WrapHandler(handler)
38-
}
39-
}
40-
}
41-
42-
return echo.WrapHandler(http.HandlerFunc(httpRedirectHandler))
43-
}
44-
45-
func httpRedirectHandler(w http.ResponseWriter, r *http.Request) {
46-
toURL := "https://"
47-
requestHost := hostOnly(r.Host)
48-
toURL += requestHost
49-
toURL += r.URL.RequestURI()
50-
w.Header().Set("Connection", "close")
51-
http.Redirect(w, r, toURL, http.StatusMovedPermanently)
52-
}
53-
54-
func hostOnly(hostport string) string {
55-
host, _, err := net.SplitHostPort(hostport)
56-
if err != nil {
57-
return hostport
58-
}
59-
return host
60-
}

internal/handlers/id_token.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import (
1717
func NewIDTokenHandlers(machineKey key.MachinePublic, config *config.Config, repository domain.Repository) *IDTokenHandlers {
1818
return &IDTokenHandlers{
1919
machineKey: machineKey,
20-
issuer: config.ServerUrl,
20+
issuer: config.WebPublicUrl.String(),
2121
repository: repository,
2222
}
2323
}
2424

2525
func NewOIDCConfigHandlers(config *config.Config, repository domain.Repository) *OIDCConfigHandlers {
2626
return &OIDCConfigHandlers{
27-
issuer: config.ServerUrl,
27+
issuer: config.WebPublicUrl.String(),
2828
jwksUri: config.CreateUrl("/.well-known/jwks"),
2929
repository: repository,
3030
}

0 commit comments

Comments
 (0)