guiconfiguration.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. }
  24. func (c GUIConfiguration) Address() string {
  25. if override := os.Getenv("STGUIADDRESS"); override != "" {
  26. // This value may be of the form "scheme://address:port" or just
  27. // "address:port". We need to chop off the scheme. We try to parse it as
  28. // an URL if it contains a slash. If that fails, return it as is and let
  29. // some other error handling handle it.
  30. if strings.Contains(override, "/") {
  31. url, err := url.Parse(override)
  32. if err != nil {
  33. return override
  34. }
  35. return url.Host
  36. }
  37. return override
  38. }
  39. return c.RawAddress
  40. }
  41. func (c GUIConfiguration) UseTLS() bool {
  42. if override := os.Getenv("STGUIADDRESS"); override != "" && strings.HasPrefix(override, "http") {
  43. return strings.HasPrefix(override, "https:")
  44. }
  45. return c.RawUseTLS
  46. }
  47. func (c GUIConfiguration) URL() string {
  48. u := url.URL{
  49. Scheme: "http",
  50. Host: c.Address(),
  51. Path: "/",
  52. }
  53. if c.UseTLS() {
  54. u.Scheme = "https"
  55. }
  56. if strings.HasPrefix(u.Host, ":") {
  57. // Empty host, i.e. ":port", use IPv4 localhost
  58. u.Host = "127.0.0.1" + u.Host
  59. } else if strings.HasPrefix(u.Host, "0.0.0.0:") {
  60. // IPv4 all zeroes host, convert to IPv4 localhost
  61. u.Host = "127.0.0.1" + u.Host[7:]
  62. } else if strings.HasPrefix(u.Host, "[::]:") {
  63. // IPv6 all zeroes host, convert to IPv6 localhost
  64. u.Host = "[::1]" + u.Host[4:]
  65. }
  66. return u.String()
  67. }
  68. // IsValidAPIKey returns true when the given API key is valid, including both
  69. // the value in config and any overrides
  70. func (c GUIConfiguration) IsValidAPIKey(apiKey string) bool {
  71. switch apiKey {
  72. case "":
  73. return false
  74. case c.APIKey, os.Getenv("STGUIAPIKEY"):
  75. return true
  76. default:
  77. return false
  78. }
  79. }