guiconfiguration.go 3.2 KB

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