move some util definitions and code to common lib

This commit is contained in:
2026-06-08 20:28:39 +00:00
parent 00fa5f3152
commit 10ef24e76b
9 changed files with 74 additions and 89 deletions
-18
View File
@@ -22,13 +22,6 @@ type StaticFile struct {
MimeType MimeType
}
// parsed vmpath data (ie node/type/vmid)
type VMPath struct {
Node string
Type string
VMID string
}
// type used for templated <select>
type Select struct {
ID string
@@ -43,17 +36,6 @@ type Option struct {
Display string
}
type RequestContext struct {
Cookies map[string]string
}
type Auth struct {
Username string
Token string
CSRF string
AccessManagerTicket string
}
type Icon struct {
ID string
Src string
+25 -15
View File
@@ -14,10 +14,13 @@ import (
"net/http"
"os"
"reflect"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/tdewolff/minify/v2"
paas "proxmoxaas-common-lib"
)
// get config file from configPath
@@ -173,14 +176,17 @@ func HandleNonFatalError(c *gin.Context, err error) {
c.Status(http.StatusInternalServerError)
}
func RequestGetAPI(path string, context RequestContext, body any) (*http.Response, int, error) {
func RequestGetAPI(path string, auth *paas.Auth, body any) (*http.Response, int, error) {
req, err := http.NewRequest("GET", Global.API+path, nil)
if err != nil {
return nil, 0, err
}
for k, v := range context.Cookies {
if auth != nil {
for k, v := range GetRequestContextFromCookies(*auth) {
req.AddCookie(&http.Cookie{Name: k, Value: v, Secure: true})
}
}
client := &http.Client{}
response, err := client.Do(req)
@@ -216,30 +222,36 @@ func RequestGetAPI(path string, context RequestContext, body any) (*http.Respons
return response, response.StatusCode, nil
}
func GetAuth(c *gin.Context) (Auth, error) {
func GetAuthFromRequest(c *gin.Context) (paas.Auth, error) {
_, errAuth := c.Cookie("auth")
username, errUsername := c.Cookie("username")
token, errToken := c.Cookie("PVEAuthCookie")
csrf, errCSRF := c.Cookie("CSRFPreventionToken")
access, errAccess := c.Cookie("PAASAccessManagerTicket")
if errUsername != nil || errAuth != nil || errToken != nil || errCSRF != nil || errAccess != nil {
return Auth{}, fmt.Errorf("error occured getting user cookies: (auth: %s, token: %s, csrf: %s)", errAuth, errToken, errCSRF)
return paas.Auth{}, fmt.Errorf("error occured getting user cookies: (auth: %s, token: %s, csrf: %s)", errAuth, errToken, errCSRF)
} else {
return Auth{username, token, csrf, access}, nil
return paas.Auth{Username: username, Token: token, CSRF: csrf, AccessManagerTicket: access}, nil
}
}
func ExtractVMPath(c *gin.Context) (VMPath, error) {
func GetInstancePathFromRequest(c *gin.Context) (paas.InstancePath, error) {
req_node := c.Query("node")
req_type := c.Query("type")
req_vmid := c.Query("vmid")
if req_node == "" || req_type == "" || req_vmid == "" {
return VMPath{}, fmt.Errorf("request missing required values: (node: %s, type: %s, vmid: %s)", req_node, req_type, req_vmid)
return paas.InstancePath{}, fmt.Errorf("request missing required values: (node: %s, type: %s, vmid: %s)", req_node, req_type, req_vmid)
}
vm_path := VMPath{
Node: req_node,
Type: req_type,
VMID: req_vmid,
vmid_int, err := strconv.ParseUint(req_vmid, 10, 64)
if err != nil {
return paas.InstancePath{}, err
}
vm_path := paas.InstancePath{
NodeName: req_node,
InstanceType: paas.InstanceType(req_type),
InstanceID: paas.InstanceID(vmid_int),
}
return vm_path, nil
}
@@ -271,13 +283,11 @@ func FormatNumber(val int64, base int64) (string, string) {
}
}
func GetRequestContextFromCookies(auth Auth) RequestContext {
return RequestContext{
Cookies: map[string]string{
func GetRequestContextFromCookies(auth paas.Auth) map[string]string {
return map[string]string{
"username": auth.Username,
"PVEAuthCookie": auth.Token,
"CSRFPreventionToken": auth.CSRF,
"PAASAccessManagerTicket": auth.AccessManagerTicket,
},
}
}
+6 -8
View File
@@ -96,7 +96,7 @@ var Green = color.RGB{
}
func HandleGETAccount(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
account, err := GetUser(auth)
@@ -186,11 +186,10 @@ func HandleGETAccount(c *gin.Context) {
}
}
func GetUser(auth common.Auth) (Account, error) {
func GetUser(auth paas.Auth) (Account, error) {
account := Account{}
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), ctx, &body)
res, code, err := common.RequestGetAPI(fmt.Sprintf("/access/users/%s", auth.Username), &auth, &body)
if err != nil {
return account, err
}
@@ -201,13 +200,12 @@ func GetUser(auth common.Auth) (Account, error) {
return account, err
}
func GetUserPools(auth common.Auth) (map[string]paas.Pool, error) {
func GetUserPools(auth paas.Auth) (map[string]paas.Pool, error) {
pools := map[string]paas.Pool{}
// get all pools
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
res, code, err := common.RequestGetAPI("/access/pools", ctx, &body)
res, code, err := common.RequestGetAPI("/access/pools", &auth, &body)
if err != nil {
return pools, err
}
@@ -222,7 +220,7 @@ func GetUserPools(auth common.Auth) (map[string]paas.Pool, error) {
// get global config for resource type metadata
body = map[string]any{}
// get resource meta data
res, code, err = common.RequestGetAPI("/global/config/resources", ctx, &body)
res, code, err = common.RequestGetAPI("/global/config/resources", &auth, &body)
if err != nil {
return pools, err
}
+8 -8
View File
@@ -3,6 +3,7 @@ package routes
import (
"fmt"
"net/http"
paas "proxmoxaas-common-lib"
"proxmoxaas-dashboard/app/common"
"time"
@@ -20,9 +21,9 @@ type InstanceBackup struct {
}
func HandleGETBackups(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -50,9 +51,9 @@ func HandleGETBackups(c *gin.Context) {
}
func HandleGETBackupsFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -77,12 +78,11 @@ func HandleGETBackupsFragment(c *gin.Context) {
}
}
func GetInstanceBackups(vm common.VMPath, auth common.Auth) ([]InstanceBackup, error) {
func GetInstanceBackups(vm paas.InstancePath, auth paas.Auth) ([]InstanceBackup, error) {
backups := []InstanceBackup{}
path := fmt.Sprintf("/cluster/%s/%s/%s/backup", vm.Node, vm.Type, vm.VMID)
ctx := common.GetRequestContextFromCookies(auth)
path := fmt.Sprintf("/cluster/%s/%s/%d/backup", vm.NodeName, string(vm.InstanceType), uint64(vm.InstanceID))
body := []any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return backups, err
}
+20 -22
View File
@@ -34,9 +34,9 @@ type PoolConfig struct {
}
func HandleGETConfig(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -70,9 +70,9 @@ func HandleGETConfig(c *gin.Context) {
}
func HandleGETConfigVolumesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -98,9 +98,9 @@ func HandleGETConfigVolumesFragment(c *gin.Context) {
}
func HandleGETConfigNetsFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -126,9 +126,9 @@ func HandleGETConfigNetsFragment(c *gin.Context) {
}
func HandleGETConfigDevicesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -154,9 +154,9 @@ func HandleGETConfigDevicesFragment(c *gin.Context) {
}
func HandleGETConfigBootFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil {
vm_path, err := common.ExtractVMPath(c)
vm_path, err := common.GetInstancePathFromRequest(c)
if err != nil {
common.HandleNonFatalError(c, err)
return
@@ -181,12 +181,11 @@ func HandleGETConfigBootFragment(c *gin.Context) {
}
}
func GetInstanceConfig(vm common.VMPath, auth common.Auth) (InstanceConfig, error) {
func GetInstanceConfig(vm paas.InstancePath, auth paas.Auth) (InstanceConfig, error) {
config := InstanceConfig{}
path := fmt.Sprintf("/cluster/%s/%s/%s", vm.Node, vm.Type, vm.VMID)
ctx := common.GetRequestContextFromCookies(auth)
path := fmt.Sprintf("/cluster/%s/%s/%d/", vm.NodeName, string(vm.InstanceType), uint64(vm.InstanceID))
body := map[string]any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return config, err
}
@@ -205,17 +204,16 @@ func GetInstanceConfig(vm common.VMPath, auth common.Auth) (InstanceConfig, erro
return config, nil
}
func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select, error) {
func GetCPUTypes(vm paas.InstancePath, pool string, auth paas.Auth) (common.Select, error) {
cputypes := common.Select{
ID: "proctype",
Required: true,
}
// get global resource config
ctx := common.GetRequestContextFromCookies(auth)
body := map[string]any{}
path := "/global/config/resources"
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
@@ -231,7 +229,7 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
// get pool resource config
body = map[string]any{}
path = fmt.Sprintf("/access/pools/%s", pool)
res, code, err = common.RequestGetAPI(path, ctx, &body)
res, code, err = common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
@@ -246,8 +244,8 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
// use node specific rules if present, otherwise use global rules
var userCPU []paas.MatchLimit
if _, ok := poolCPUConfig.CPU.Nodes[vm.Node]; ok {
userCPU = poolCPUConfig.CPU.Nodes[vm.Node]
if _, ok := poolCPUConfig.CPU.Nodes[vm.NodeName]; ok {
userCPU = poolCPUConfig.CPU.Nodes[vm.NodeName]
} else {
userCPU = poolCPUConfig.CPU.Global
}
@@ -262,8 +260,8 @@ func GetCPUTypes(vm common.VMPath, pool string, auth common.Auth) (common.Select
} else { // cpu is a blacklist
// get the supported cpu types from the node
body = map[string]any{}
path = fmt.Sprintf("/proxmox/nodes/%s/capabilities/qemu/cpu", vm.Node)
res, code, err = common.RequestGetAPI(path, ctx, &body)
path = fmt.Sprintf("/proxmox/nodes/%s/capabilities/qemu/cpu", vm.NodeName)
res, code, err = common.RequestGetAPI(path, &auth, &body)
if err != nil {
return cputypes, err
}
+7 -7
View File
@@ -3,6 +3,7 @@ package routes
import (
"fmt"
"net/http"
paas "proxmoxaas-common-lib"
"proxmoxaas-dashboard/app/common"
"strconv"
@@ -45,7 +46,7 @@ type InstanceStatus struct {
}
func HandleGETIndex(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(auth)
if err != nil {
@@ -64,7 +65,7 @@ func HandleGETIndex(c *gin.Context) {
}
func HandleGETInstancesFragment(c *gin.Context) {
auth, err := common.GetAuth(c)
auth, err := common.GetAuthFromRequest(c)
if err == nil { // user should be authed, try to return index with population
instances, _, err := GetClusterResources(auth)
if err != nil {
@@ -86,10 +87,9 @@ func HandleGETInstancesFragment(c *gin.Context) {
}
func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]Node, error) {
ctx := common.GetRequestContextFromCookies(auth)
func GetClusterResources(auth paas.Auth) (map[uint]InstanceCard, map[string]Node, error) {
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/cluster/resources", ctx, &body)
res, code, err := common.RequestGetAPI("/proxmox/cluster/resources", &auth, &body)
if err != nil {
return nil, nil, err
}
@@ -140,7 +140,7 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No
}
body = []any{}
res, code, err = common.RequestGetAPI("/proxmox/cluster/tasks", ctx, &body)
res, code, err = common.RequestGetAPI("/proxmox/cluster/tasks", &auth, &body)
if err != nil {
return nil, nil, err
}
@@ -198,7 +198,7 @@ func GetClusterResources(auth common.Auth) (map[uint]InstanceCard, map[string]No
instance := instances[vmid]
path := fmt.Sprintf("/proxmox/nodes/%s/%s/%d/status/current", instance.Node, instance.Type, instance.VMID)
body := map[string]any{}
res, code, err := common.RequestGetAPI(path, ctx, &body)
res, code, err := common.RequestGetAPI(path, &auth, &body)
if err != nil {
return nil, nil, err
}
+1 -4
View File
@@ -24,11 +24,8 @@ type Realm struct {
func GetLoginRealms() ([]Realm, error) {
realms := []Realm{}
ctx := common.RequestContext{
Cookies: nil,
}
body := []any{}
res, code, err := common.RequestGetAPI("/proxmox/access/domains", ctx, &body)
res, code, err := common.RequestGetAPI("/proxmox/access/domains", nil, &body)
if err != nil {
return realms, err
}
+1 -1
View File
@@ -8,7 +8,7 @@ import (
)
func HandleGETSettings(c *gin.Context) {
_, err := common.GetAuth(c)
_, err := common.GetAuthFromRequest(c)
if err == nil {
c.HTML(http.StatusOK, "html/settings.html", gin.H{
"global": common.Global,