mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-16 16:25:48 +00:00
Compare commits
1 Commits
extra-disk
...
ssr-system
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
605e3f7e9d |
@@ -19,6 +19,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/pocketbase/dbx"
|
||||||
"github.com/pocketbase/pocketbase"
|
"github.com/pocketbase/pocketbase"
|
||||||
"github.com/pocketbase/pocketbase/apis"
|
"github.com/pocketbase/pocketbase/apis"
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
@@ -112,6 +113,8 @@ func (h *Hub) initialize(e *core.ServeEvent) error {
|
|||||||
// set URL if BASE_URL env is set
|
// set URL if BASE_URL env is set
|
||||||
if h.appURL != "" {
|
if h.appURL != "" {
|
||||||
settings.Meta.AppURL = h.appURL
|
settings.Meta.AppURL = h.appURL
|
||||||
|
} else {
|
||||||
|
h.appURL = settings.Meta.AppURL
|
||||||
}
|
}
|
||||||
if err := e.App.Save(settings); err != nil {
|
if err := e.App.Save(settings); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -297,3 +300,30 @@ func (h *Hub) MakeLink(parts ...string) string {
|
|||||||
}
|
}
|
||||||
return base
|
return base
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type SystemInfo struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Id string `json:"id"`
|
||||||
|
Status string `json:"status"`
|
||||||
|
Port uint16 `json:"port"`
|
||||||
|
Host string `json:"host"`
|
||||||
|
Info string `json:"info"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Hub) getUserSystemsFromRequest(req *http.Request) ([]SystemInfo, error) {
|
||||||
|
systems := []SystemInfo{}
|
||||||
|
token, err := req.Cookie("beszauth")
|
||||||
|
if err != nil {
|
||||||
|
return systems, err
|
||||||
|
}
|
||||||
|
if token.Value != "" {
|
||||||
|
user, err := h.FindAuthRecordByToken(token.Value)
|
||||||
|
if err != nil {
|
||||||
|
return systems, err
|
||||||
|
}
|
||||||
|
h.DB().NewQuery("SELECT s.id, s.info, s.status, s.name, s.port, s.host FROM systems s JOIN json_each(s.users) AS je WHERE je.value = {:user_id}").Bind(dbx.Params{
|
||||||
|
"user_id": user.Id,
|
||||||
|
}).All(&systems)
|
||||||
|
}
|
||||||
|
return systems, err
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,18 +3,96 @@
|
|||||||
package hub
|
package hub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"beszel"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"net/http/httputil"
|
"net/http/httputil"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/pocketbase/pocketbase/core"
|
"github.com/pocketbase/pocketbase/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// responseModifier wraps an http.RoundTripper to modify HTML responses
|
||||||
|
type responseModifier struct {
|
||||||
|
transport http.RoundTripper
|
||||||
|
hub *Hub
|
||||||
|
}
|
||||||
|
|
||||||
|
// RoundTrip implements http.RoundTripper interface with response modification
|
||||||
|
func (rm *responseModifier) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||||
|
resp, err := rm.transport.RoundTrip(req)
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only modify HTML responses
|
||||||
|
contentType := resp.Header.Get("Content-Type")
|
||||||
|
if !strings.Contains(contentType, "text/html") {
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read the response body
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return resp, err
|
||||||
|
}
|
||||||
|
resp.Body.Close()
|
||||||
|
|
||||||
|
// Modify the HTML content here
|
||||||
|
modifiedBody := rm.modifyHTML(string(body), req)
|
||||||
|
|
||||||
|
// Create a new response with the modified body
|
||||||
|
resp.Body = io.NopCloser(strings.NewReader(modifiedBody))
|
||||||
|
resp.ContentLength = int64(len(modifiedBody))
|
||||||
|
resp.Header.Set("Content-Length", fmt.Sprintf("%d", len(modifiedBody)))
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// modifyHTML applies modifications to HTML content
|
||||||
|
func (rm *responseModifier) modifyHTML(html string, req *http.Request) string {
|
||||||
|
parsedURL, err := url.Parse(rm.hub.appURL)
|
||||||
|
if err != nil {
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
// fix base paths in html if using subpath
|
||||||
|
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
||||||
|
html = strings.ReplaceAll(html, "./", basePath)
|
||||||
|
html = strings.Replace(html, "{{V}}", beszel.Version, 1)
|
||||||
|
slog.Info("modifying HTML", "appURL", rm.hub.appURL)
|
||||||
|
html = strings.Replace(html, "{{HUB_URL}}", rm.hub.appURL, 1)
|
||||||
|
|
||||||
|
systems, err := rm.hub.getUserSystemsFromRequest(req)
|
||||||
|
if err != nil {
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
systemsJson, err := json.Marshal(systems)
|
||||||
|
if err != nil {
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
html = strings.Replace(html, "'{SYSTEMS}'", string(systemsJson), 1)
|
||||||
|
|
||||||
|
return html
|
||||||
|
}
|
||||||
|
|
||||||
// startServer sets up the development server for Beszel
|
// startServer sets up the development server for Beszel
|
||||||
func (h *Hub) startServer(se *core.ServeEvent) error {
|
func (h *Hub) startServer(se *core.ServeEvent) error {
|
||||||
|
slog.Info("starting server", "appURL", h.appURL)
|
||||||
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
proxy := httputil.NewSingleHostReverseProxy(&url.URL{
|
||||||
Scheme: "http",
|
Scheme: "http",
|
||||||
Host: "localhost:5173",
|
Host: "localhost:5173",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Set up custom transport with response modification
|
||||||
|
proxy.Transport = &responseModifier{
|
||||||
|
transport: http.DefaultTransport,
|
||||||
|
hub: h,
|
||||||
|
}
|
||||||
|
|
||||||
se.Router.GET("/{path...}", func(e *core.RequestEvent) error {
|
se.Router.GET("/{path...}", func(e *core.RequestEvent) error {
|
||||||
proxy.ServeHTTP(e.Response, e.Request)
|
proxy.ServeHTTP(e.Response, e.Request)
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -5,7 +5,9 @@ package hub
|
|||||||
import (
|
import (
|
||||||
"beszel"
|
"beszel"
|
||||||
"beszel/site"
|
"beszel/site"
|
||||||
|
"encoding/json"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -24,9 +26,9 @@ func (h *Hub) startServer(se *core.ServeEvent) error {
|
|||||||
// fix base paths in html if using subpath
|
// fix base paths in html if using subpath
|
||||||
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
basePath := strings.TrimSuffix(parsedURL.Path, "/") + "/"
|
||||||
indexFile, _ := fs.ReadFile(site.DistDirFS, "index.html")
|
indexFile, _ := fs.ReadFile(site.DistDirFS, "index.html")
|
||||||
indexContent := strings.ReplaceAll(string(indexFile), "./", basePath)
|
html := strings.ReplaceAll(string(indexFile), "./", basePath)
|
||||||
indexContent = strings.Replace(indexContent, "{{V}}", beszel.Version, 1)
|
html = strings.Replace(html, "{{V}}", beszel.Version, 1)
|
||||||
indexContent = strings.Replace(indexContent, "{{HUB_URL}}", h.appURL, 1)
|
html = strings.Replace(html, "{{HUB_URL}}", h.appURL, 1)
|
||||||
// set up static asset serving
|
// set up static asset serving
|
||||||
staticPaths := [2]string{"/static/", "/assets/"}
|
staticPaths := [2]string{"/static/", "/assets/"}
|
||||||
serveStatic := apis.Static(site.DistDirFS, false)
|
serveStatic := apis.Static(site.DistDirFS, false)
|
||||||
@@ -45,7 +47,16 @@ func (h *Hub) startServer(se *core.ServeEvent) error {
|
|||||||
e.Response.Header().Del("X-Frame-Options")
|
e.Response.Header().Del("X-Frame-Options")
|
||||||
e.Response.Header().Set("Content-Security-Policy", csp)
|
e.Response.Header().Set("Content-Security-Policy", csp)
|
||||||
}
|
}
|
||||||
return e.HTML(http.StatusOK, indexContent)
|
systems, err := h.getUserSystemsFromRequest(e.Request)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error getting user systems", "error", err)
|
||||||
|
}
|
||||||
|
systemsJson, err := json.Marshal(systems)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error marshalling user systems", "error", err)
|
||||||
|
}
|
||||||
|
html = strings.Replace(html, "'{SYSTEMS}'", string(systemsJson), 1)
|
||||||
|
return e.HTML(http.StatusOK, html)
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,8 @@
|
|||||||
globalThis.BESZEL = {
|
globalThis.BESZEL = {
|
||||||
BASE_PATH: "%BASE_URL%",
|
BASE_PATH: "%BASE_URL%",
|
||||||
HUB_VERSION: "{{V}}",
|
HUB_VERSION: "{{V}}",
|
||||||
HUB_URL: "{{HUB_URL}}"
|
HUB_URL: "{{HUB_URL}}",
|
||||||
|
SYSTEMS: '{SYSTEMS}'
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ export const pb = new PocketBase(basePath)
|
|||||||
export const isAdmin = () => pb.authStore.record?.role === "admin"
|
export const isAdmin = () => pb.authStore.record?.role === "admin"
|
||||||
export const isReadOnlyUser = () => pb.authStore.record?.role === "readonly"
|
export const isReadOnlyUser = () => pb.authStore.record?.role === "readonly"
|
||||||
|
|
||||||
|
export const updateCookieToken = () => {
|
||||||
|
console.log("setting token", pb.authStore.token)
|
||||||
|
document.cookie = `beszauth=${pb.authStore.token}; path=/; expires=${new Date(
|
||||||
|
Date.now() + 7 * 24 * 60 * 60 * 1000
|
||||||
|
).toString()}`
|
||||||
|
}
|
||||||
|
|
||||||
export const verifyAuth = () => {
|
export const verifyAuth = () => {
|
||||||
pb.collection("users")
|
pb.collection("users")
|
||||||
.authRefresh()
|
.authRefresh()
|
||||||
|
|||||||
@@ -141,7 +141,13 @@ export async function subscribe() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** Refresh all systems with latest data from the hub */
|
/** Refresh all systems with latest data from the hub */
|
||||||
export async function refresh() {
|
export async function refresh(records: SystemRecord[] = []) {
|
||||||
|
if (records.length) {
|
||||||
|
for (const record of records) {
|
||||||
|
add(record)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const records = await fetchSystems()
|
const records = await fetchSystems()
|
||||||
if (!records.length) {
|
if (!records.length) {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import ReactDOM from "react-dom/client"
|
|||||||
import { ThemeProvider } from "./components/theme-provider.tsx"
|
import { ThemeProvider } from "./components/theme-provider.tsx"
|
||||||
import { DirectionProvider } from "@radix-ui/react-direction"
|
import { DirectionProvider } from "@radix-ui/react-direction"
|
||||||
import { $authenticated, $publicKey, $copyContent, $direction } from "./lib/stores.ts"
|
import { $authenticated, $publicKey, $copyContent, $direction } from "./lib/stores.ts"
|
||||||
import { pb, updateUserSettings } from "./lib/api.ts"
|
import { pb, updateUserSettings, updateCookieToken } from "./lib/api.ts"
|
||||||
import * as systemsManager from "./lib/systemsManager.ts"
|
import * as systemsManager from "./lib/systemsManager.ts"
|
||||||
import { useStore } from "@nanostores/react"
|
import { useStore } from "@nanostores/react"
|
||||||
import { Toaster } from "./components/ui/toaster.tsx"
|
import { Toaster } from "./components/ui/toaster.tsx"
|
||||||
@@ -27,8 +27,10 @@ const App = memo(() => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// change auth store on auth change
|
// change auth store on auth change
|
||||||
|
updateCookieToken()
|
||||||
pb.authStore.onChange(() => {
|
pb.authStore.onChange(() => {
|
||||||
$authenticated.set(pb.authStore.isValid)
|
$authenticated.set(pb.authStore.isValid)
|
||||||
|
updateCookieToken()
|
||||||
})
|
})
|
||||||
// get version / public key
|
// get version / public key
|
||||||
pb.send("/api/beszel/getkey", {}).then((data) => {
|
pb.send("/api/beszel/getkey", {}).then((data) => {
|
||||||
@@ -36,11 +38,17 @@ const App = memo(() => {
|
|||||||
})
|
})
|
||||||
// get user settings
|
// get user settings
|
||||||
updateUserSettings()
|
updateUserSettings()
|
||||||
|
const startingSystems = globalThis.BESZEL.SYSTEMS
|
||||||
|
for (const system of startingSystems) {
|
||||||
|
// if (typeof system.info === "string") {
|
||||||
|
system.info = JSON.parse(system.info as unknown as string)
|
||||||
|
// }
|
||||||
|
}
|
||||||
// need to get system list before alerts
|
// need to get system list before alerts
|
||||||
systemsManager.init()
|
systemsManager.init()
|
||||||
systemsManager
|
systemsManager
|
||||||
// get current systems list
|
// get current systems list
|
||||||
.refresh()
|
.refresh(startingSystems)
|
||||||
// subscribe to new system updates
|
// subscribe to new system updates
|
||||||
.then(systemsManager.subscribe)
|
.then(systemsManager.subscribe)
|
||||||
// get current alerts
|
// get current alerts
|
||||||
@@ -51,6 +59,7 @@ const App = memo(() => {
|
|||||||
// updateFavicon("favicon.svg")
|
// updateFavicon("favicon.svg")
|
||||||
alertManager.unsubscribe()
|
alertManager.unsubscribe()
|
||||||
systemsManager.unsubscribe()
|
systemsManager.unsubscribe()
|
||||||
|
globalThis.BESZEL.SYSTEMS = []
|
||||||
}
|
}
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|||||||
2
beszel/site/src/types.d.ts
vendored
2
beszel/site/src/types.d.ts
vendored
@@ -7,6 +7,8 @@ declare global {
|
|||||||
BASE_PATH: string
|
BASE_PATH: string
|
||||||
HUB_VERSION: string
|
HUB_VERSION: string
|
||||||
HUB_URL: string
|
HUB_URL: string
|
||||||
|
/** initial list of systems */
|
||||||
|
SYSTEMS: SystemRecord[]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import path from "path"
|
|||||||
import tailwindcss from "@tailwindcss/vite"
|
import tailwindcss from "@tailwindcss/vite"
|
||||||
import react from "@vitejs/plugin-react-swc"
|
import react from "@vitejs/plugin-react-swc"
|
||||||
import { lingui } from "@lingui/vite-plugin"
|
import { lingui } from "@lingui/vite-plugin"
|
||||||
import { version } from "./package.json"
|
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
base: "./",
|
base: "./",
|
||||||
@@ -13,13 +12,6 @@ export default defineConfig({
|
|||||||
}),
|
}),
|
||||||
lingui(),
|
lingui(),
|
||||||
tailwindcss(),
|
tailwindcss(),
|
||||||
{
|
|
||||||
name: "replace version in index.html during dev",
|
|
||||||
apply: "serve",
|
|
||||||
transformIndexHtml(html) {
|
|
||||||
return html.replace("{{V}}", version).replace("{{HUB_URL}}", "")
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
],
|
||||||
esbuild: {
|
esbuild: {
|
||||||
legalComments: "external",
|
legalComments: "external",
|
||||||
|
|||||||
Reference in New Issue
Block a user