guiconfiguration.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. AuthMode AuthMode `xml:"authMode,omitempty" json:"authMode"`
  18. RawUseTLS bool `xml:"tls,attr" json:"useTLS"`
  19. APIKey string `xml:"apikey,omitempty" json:"apiKey"`
  20. InsecureAdminAccess bool `xml:"insecureAdminAccess,omitempty" json:"insecureAdminAccess"`
  21. Theme string `xml:"theme" json:"theme" default:"default"`
  22. Debugging bool `xml:"debugging,attr" json:"debugging"`
  23. InsecureSkipHostCheck bool `xml:"insecureSkipHostcheck,omitempty" json:"insecureSkipHostcheck"`
  24. InsecureAllowFrameLoading bool `xml:"insecureAllowFrameLoading,omitempty" json:"insecureAllowFrameLoading"`
  25. }
  26. func (c GUIConfiguration) IsAuthEnabled() bool {
  27. return c.AuthMode == AuthModeLDAP || (len(c.User) > 0 && len(c.Password) > 0)
  28. }
  29. func (c GUIConfiguration) IsOverridden() bool {
  30. return os.Getenv("STGUIADDRESS") != ""
  31. }
  32. func (c GUIConfiguration) Address() string {
  33. if override := os.Getenv("STGUIADDRESS"); override != "" {
  34. // This value may be of the form "scheme://address:port" or just
  35. // "address:port". We need to chop off the scheme. We try to parse it as
  36. // an URL if it contains a slash. If that fails, return it as is and let
  37. // some other error handling handle it.
  38. if strings.Contains(override, "/") {
  39. url, err := url.Parse(override)
  40. if err != nil {
  41. return override
  42. }
  43. if strings.HasPrefix(url.Scheme, "unix") {
  44. return url.Path
  45. }
  46. return url.Host
  47. }
  48. return override
  49. }
  50. return c.RawAddress
  51. }
  52. func (c GUIConfiguration) Network() string {
  53. if override := os.Getenv("STGUIADDRESS"); strings.Contains(override, "/") {
  54. url, err := url.Parse(override)
  55. if err != nil {
  56. return "tcp"
  57. }
  58. if strings.HasPrefix(url.Scheme, "unix") {
  59. return "unix"
  60. }
  61. }
  62. if strings.HasPrefix(c.RawAddress, "/") {
  63. return "unix"
  64. }
  65. return "tcp"
  66. }
  67. func (c GUIConfiguration) UseTLS() bool {
  68. if override := os.Getenv("STGUIADDRESS"); override != "" {
  69. if strings.HasPrefix(override, "http") {
  70. return strings.HasPrefix(override, "https:")
  71. }
  72. if strings.HasPrefix(override, "unix") {
  73. return strings.HasPrefix(override, "unixs:")
  74. }
  75. }
  76. return c.RawUseTLS
  77. }
  78. func (c GUIConfiguration) URL() string {
  79. if strings.HasPrefix(c.RawAddress, "/") {
  80. return "unix://" + c.RawAddress
  81. }
  82. u := url.URL{
  83. Scheme: "http",
  84. Host: c.Address(),
  85. Path: "/",
  86. }
  87. if c.UseTLS() {
  88. u.Scheme = "https"
  89. }
  90. if strings.HasPrefix(u.Host, ":") {
  91. // Empty host, i.e. ":port", use IPv4 localhost
  92. u.Host = "127.0.0.1" + u.Host
  93. } else if strings.HasPrefix(u.Host, "0.0.0.0:") {
  94. // IPv4 all zeroes host, convert to IPv4 localhost
  95. u.Host = "127.0.0.1" + u.Host[7:]
  96. } else if strings.HasPrefix(u.Host, "[::]:") {
  97. // IPv6 all zeroes host, convert to IPv6 localhost
  98. u.Host = "[::1]" + u.Host[4:]
  99. }
  100. return u.String()
  101. }
  102. // IsValidAPIKey returns true when the given API key is valid, including both
  103. // the value in config and any overrides
  104. func (c GUIConfiguration) IsValidAPIKey(apiKey string) bool {
  105. switch apiKey {
  106. case "":
  107. return false
  108. case c.APIKey, os.Getenv("STGUIAPIKEY"):
  109. return true
  110. default:
  111. return false
  112. }
  113. }
  114. func (c GUIConfiguration) Copy() GUIConfiguration {
  115. return c
  116. }