guiconfiguration.go 3.0 KB

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