Option -gui-address should accept scheme prefixes (fixes #2371)
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math/rand"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
@@ -288,12 +289,72 @@ func (orig OptionsConfiguration) Copy() OptionsConfiguration {
|
||||
}
|
||||
|
||||
type GUIConfiguration struct {
|
||||
Enabled bool `xml:"enabled,attr" json:"enabled" default:"true"`
|
||||
Address string `xml:"address" json:"address" default:"127.0.0.1:8384"`
|
||||
User string `xml:"user,omitempty" json:"user"`
|
||||
Password string `xml:"password,omitempty" json:"password"`
|
||||
UseTLS bool `xml:"tls,attr" json:"useTLS"`
|
||||
APIKey string `xml:"apikey,omitempty" json:"apiKey"`
|
||||
Enabled bool `xml:"enabled,attr" json:"enabled" default:"true"`
|
||||
RawAddress string `xml:"address" json:"address" default:"127.0.0.1:8384"`
|
||||
User string `xml:"user,omitempty" json:"user"`
|
||||
Password string `xml:"password,omitempty" json:"password"`
|
||||
RawUseTLS bool `xml:"tls,attr" json:"useTLS"`
|
||||
RawAPIKey string `xml:"apikey,omitempty" json:"apiKey"`
|
||||
}
|
||||
|
||||
func (c GUIConfiguration) Address() string {
|
||||
if override := os.Getenv("STGUIADDRESS"); override != "" {
|
||||
// This value may be of the form "scheme://address:port" or just
|
||||
// "address:port". We need to chop off the scheme. We try to parse it as
|
||||
// an URL if it contains a slash. If that fails, return it as is and let
|
||||
// some other error handling handle it.
|
||||
|
||||
if strings.Contains(override, "/") {
|
||||
url, err := url.Parse(override)
|
||||
if err != nil {
|
||||
return override
|
||||
}
|
||||
return url.Host
|
||||
}
|
||||
|
||||
return override
|
||||
}
|
||||
|
||||
return c.RawAddress
|
||||
}
|
||||
|
||||
func (c GUIConfiguration) UseTLS() bool {
|
||||
if override := os.Getenv("STGUIADDRESS"); override != "" {
|
||||
return strings.HasPrefix(override, "https:")
|
||||
}
|
||||
return c.RawUseTLS
|
||||
}
|
||||
|
||||
func (c GUIConfiguration) URL() string {
|
||||
u := url.URL{
|
||||
Scheme: "http",
|
||||
Host: c.Address(),
|
||||
Path: "/",
|
||||
}
|
||||
|
||||
if c.UseTLS() {
|
||||
u.Scheme = "https"
|
||||
}
|
||||
|
||||
if strings.HasPrefix(u.Host, ":") {
|
||||
// Empty host, i.e. ":port", use IPv4 localhost
|
||||
u.Host = "127.0.0.1" + u.Host
|
||||
} else if strings.HasPrefix(u.Host, "0.0.0.0:") {
|
||||
// IPv4 all zeroes host, convert to IPv4 localhost
|
||||
u.Host = "127.0.0.1" + u.Host[7:]
|
||||
} else if strings.HasPrefix(u.Host, "[::]:") {
|
||||
// IPv6 all zeroes host, convert to IPv6 localhost
|
||||
u.Host = "[::1]" + u.Host[4:]
|
||||
}
|
||||
|
||||
return u.String()
|
||||
}
|
||||
|
||||
func (c GUIConfiguration) APIKey() string {
|
||||
if override := os.Getenv("STGUIAPIKEY"); override != "" {
|
||||
return override
|
||||
}
|
||||
return c.RawAPIKey
|
||||
}
|
||||
|
||||
func New(myID protocol.DeviceID) Configuration {
|
||||
@@ -463,8 +524,8 @@ func (cfg *Configuration) prepare(myID protocol.DeviceID) {
|
||||
cfg.Options.ReconnectIntervalS = 5
|
||||
}
|
||||
|
||||
if cfg.GUI.APIKey == "" {
|
||||
cfg.GUI.APIKey = randomString(32)
|
||||
if cfg.GUI.RawAPIKey == "" {
|
||||
cfg.GUI.RawAPIKey = randomString(32)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -528,7 +528,7 @@ func TestRequiresRestart(t *testing.T) {
|
||||
}
|
||||
|
||||
newCfg = cfg
|
||||
newCfg.GUI.UseTLS = !cfg.GUI.UseTLS
|
||||
newCfg.GUI.RawUseTLS = !cfg.GUI.RawUseTLS
|
||||
if !ChangeRequiresRestart(cfg, newCfg) {
|
||||
t.Error("Changing GUI options requires restart")
|
||||
}
|
||||
@@ -551,7 +551,7 @@ func TestCopy(t *testing.T) {
|
||||
cfg.Devices[0].Addresses[0] = "wrong"
|
||||
cfg.Folders[0].Devices[0].DeviceID = protocol.DeviceID{0, 1, 2, 3}
|
||||
cfg.Options.ListenAddress[0] = "wrong"
|
||||
cfg.GUI.APIKey = "wrong"
|
||||
cfg.GUI.RawAPIKey = "wrong"
|
||||
|
||||
bsChanged, err := json.MarshalIndent(cfg, "", " ")
|
||||
if err != nil {
|
||||
@@ -634,3 +634,25 @@ func TestLargeRescanInterval(t *testing.T) {
|
||||
t.Error("negative rescan interval should become zero")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGUIConfigURL(t *testing.T) {
|
||||
testcases := [][2]string{
|
||||
{"192.0.2.42:8080", "http://192.0.2.42:8080/"},
|
||||
{":8080", "http://127.0.0.1:8080/"},
|
||||
{"0.0.0.0:8080", "http://127.0.0.1:8080/"},
|
||||
{"127.0.0.1:8080", "http://127.0.0.1:8080/"},
|
||||
{"127.0.0.2:8080", "http://127.0.0.2:8080/"},
|
||||
{"[::]:8080", "http://[::1]:8080/"},
|
||||
{"[2001::42]:8080", "http://[2001::42]:8080/"},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
c := GUIConfiguration{
|
||||
RawAddress: tc[0],
|
||||
}
|
||||
u := c.URL()
|
||||
if u != tc[1] {
|
||||
t.Errorf("Incorrect URL %s != %s for addr %s", u, tc[1], tc[0])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user