4
0

webuiServer.go 3.6 KB

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