4
0

webuiServer.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package httpservers
  2. import (
  3. "net/http"
  4. "os"
  5. "path"
  6. "strings"
  7. log "github.com/sirupsen/logrus"
  8. "github.com/jamesread/golure/pkg/dirs"
  9. config "github.com/OliveTin/OliveTin/internal/config"
  10. )
  11. type webUIServer struct {
  12. cfg *config.Config
  13. webuiDir string
  14. }
  15. var (
  16. customThemeCss []byte
  17. customThemeCssRead = false
  18. )
  19. func NewWebUIServer(cfg *config.Config) *webUIServer {
  20. s := &webUIServer{
  21. cfg: cfg,
  22. }
  23. s.webuiDir = s.findWebuiDir()
  24. s.setupCustomWebuiDir()
  25. return s
  26. }
  27. // Web UI files keep their names between releases, and some replacement UIs
  28. // (webUIDir) do not hash their asset names at all. Without Cache-Control,
  29. // browsers cache responses that only carry Last-Modified heuristically, and
  30. // can keep an old UI after an upgrade. no-cache makes them revalidate every
  31. // time; unchanged files still come back as a cheap 304 (see webui_etag.go).
  32. const webuiCacheControl = "no-cache"
  33. func (s *webUIServer) handleWebui(w http.ResponseWriter, r *http.Request) {
  34. w.Header().Set("Cache-Control", webuiCacheControl)
  35. // http.Dir rejects path traversal; do not Join raw URL paths into ServeFile.
  36. root := http.Dir(s.webuiDir)
  37. log.Tracef("Serving webui from %s for %s", s.webuiDir, r.URL.Path)
  38. serveFileWithContentETag(w, r, root, webuiFileName(r.URL.Path), http.FileServer(root))
  39. }
  40. // Mangle requests for any path like /logs or /config to load the webui index.html
  41. func webuiFileName(urlPath string) string {
  42. if path.Ext(urlPath) == "" {
  43. return "/index.html"
  44. }
  45. return urlPath
  46. }
  47. func (s *webUIServer) findWebuiDir() string {
  48. directoriesToSearch := []string{
  49. s.cfg.WebUIDir,
  50. "../frontend/dist/",
  51. "../webui/",
  52. "../frontend/",
  53. "/usr/share/OliveTin/frontend/",
  54. "/usr/share/OliveTin/webui/",
  55. "/var/www/OliveTin/",
  56. "/var/www/olivetin/",
  57. "/etc/OliveTin/frontend/",
  58. }
  59. dir, err := dirs.GetFirstExistingDirectory("webui", directoriesToSearch)
  60. if err != nil {
  61. log.Warnf("Did not find the webui directory, you will probably get 404 errors.")
  62. return "./webui" // Should not exist
  63. }
  64. log.Infof("Using webui directory: %s", dir)
  65. return dir
  66. }
  67. func (s *webUIServer) findCustomWebuiDir() string {
  68. dir := path.Join(s.cfg.GetDir(), "custom-webui")
  69. return dir
  70. }
  71. func (s *webUIServer) setupCustomWebuiDir() {
  72. dir := s.findCustomWebuiDir()
  73. err := os.MkdirAll(path.Join(dir, "themes/"), 0o750)
  74. if err != nil {
  75. log.Warnf("Could not create themes directory: %v", err)
  76. }
  77. }
  78. func shouldReloadThemeCss() bool {
  79. return !customThemeCssRead
  80. }
  81. func loadThemeCssFromFile(filename string) []byte {
  82. _, err := os.Stat(filename)
  83. if err == nil {
  84. css, err := os.ReadFile(filename)
  85. if err != nil {
  86. log.Tracef("Theme CSS file not read: %s", filename)
  87. return nil
  88. }
  89. return css
  90. }
  91. log.Tracef("Theme CSS file not found: %s", filename)
  92. return nil
  93. }
  94. func (s *webUIServer) generateThemeCss(w http.ResponseWriter, r *http.Request) {
  95. themeCssFilename := path.Join(s.findCustomWebuiDir(), "themes", s.cfg.ThemeName, "theme.css")
  96. if shouldReloadThemeCss() || s.cfg.ThemeCacheDisabled {
  97. customThemeCssRead = true
  98. customThemeCss = loadThemeCssFromFile(themeCssFilename)
  99. }
  100. w.Header().Add("Content-Type", "text/css")
  101. w.Header().Set("Cache-Control", webuiCacheControl)
  102. _, err := w.Write(customThemeCss)
  103. if err != nil {
  104. log.WithFields(log.Fields{
  105. "error": err,
  106. }).Warnf("Failed to write theme CSS")
  107. }
  108. }
  109. func (s *webUIServer) handleCustomWebui() http.Handler {
  110. root := http.Dir(s.findCustomWebuiDir())
  111. fileServer := http.StripPrefix("/custom-webui/", http.FileServer(root))
  112. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  113. w.Header().Set("Cache-Control", webuiCacheControl)
  114. name := "/" + strings.TrimPrefix(r.URL.Path, "/custom-webui/")
  115. serveFileWithContentETag(w, r, root, name, fileServer)
  116. })
  117. }