guiconfiguration.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package config
  7. import (
  8. "net/url"
  9. "os"
  10. "strings"
  11. )
  12. type GUIConfiguration struct {
  13. Enabled bool `xml:"enabled,attr" json:"enabled" default:"true"`
  14. RawAddress string `xml:"address" json:"address" default:"127.0.0.1:8384"`
  15. User string `xml:"user,omitempty" json:"user"`
  16. Password string `xml:"password,omitempty" json:"password"`
  17. RawUseTLS bool `xml:"tls,attr" json:"useTLS"`
  18. APIKey string `xml:"apikey,omitempty" json:"apiKey"`
  19. InsecureAdminAccess bool `xml:"insecureAdminAccess,omitempty" json:"insecureAdminAccess"`
  20. Theme string `xml:"theme" json:"theme" default:"default"`
  21. Debugging bool `xml:"debugging,attr" json:"debugging"`
  22. InsecureSkipHostCheck bool `xml:"insecureSkipHostcheck,omitempty" json:"insecureSkipHostcheck"`
  23. InsecureAllowFrameLoading bool `xml:"insecureAllowFrameLoading,omitempty" json:"insecureAllowFrameLoading"`
  24. }
  25. func (c GUIConfiguration) Address() string {
  26. if override := os.Getenv("STGUIADDRESS"); override != "" {
  27. // This value may be of the form "scheme://address:port" or just
  28. // "address:port". We need to chop off the scheme. We try to parse it as
  29. // an URL if it contains a slash. If that fails, return it as is and let
  30. // some other error handling handle it.
  31. if strings.Contains(override, "/") {
  32. url, err := url.Parse(override)
  33. if err != nil {
  34. return override
  35. }
  36. return url.Host
  37. }
  38. return override
  39. }
  40. return c.RawAddress
  41. }
  42. func (c GUIConfiguration) UseTLS() bool {
  43. if override := os.Getenv("STGUIADDRESS"); override != "" && strings.HasPrefix(override, "http") {
  44. return strings.HasPrefix(override, "https:")
  45. }
  46. return c.RawUseTLS
  47. }
  48. func (c GUIConfiguration) URL() string {
  49. u := url.URL{
  50. Scheme: "http",
  51. Host: c.Address(),
  52. Path: "/",
  53. }
  54. if c.UseTLS() {
  55. u.Scheme = "https"
  56. }
  57. if strings.HasPrefix(u.Host, ":") {
  58. // Empty host, i.e. ":port", use IPv4 localhost
  59. u.Host = "127.0.0.1" + u.Host
  60. } else if strings.HasPrefix(u.Host, "0.0.0.0:") {
  61. // IPv4 all zeroes host, convert to IPv4 localhost
  62. u.Host = "127.0.0.1" + u.Host[7:]
  63. } else if strings.HasPrefix(u.Host, "[::]:") {
  64. // IPv6 all zeroes host, convert to IPv6 localhost
  65. u.Host = "[::1]" + u.Host[4:]
  66. }
  67. return u.String()
  68. }
  69. // IsValidAPIKey returns true when the given API key is valid, including both
  70. // the value in config and any overrides
  71. func (c GUIConfiguration) IsValidAPIKey(apiKey string) bool {
  72. switch apiKey {
  73. case "":
  74. return false
  75. case c.APIKey, os.Getenv("STGUIAPIKEY"):
  76. return true
  77. default:
  78. return false
  79. }
  80. }