feat(guesttools): add custom shared folders with volume checks, agent v9, and settings UI
95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package guesttools
|
|
|
|
import (
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
)
|
|
|
|
// DriveShareAgentVersion is the minimum agent version recommended for drive sharing.
|
|
const DriveShareAgentVersion = 8
|
|
|
|
// NormalizeDriveLetter accepts C, C:, or C:\ and returns the canonical letter C.
|
|
func NormalizeDriveLetter(raw string) (string, bool) {
|
|
s := strings.ToUpper(strings.TrimSpace(raw))
|
|
s = strings.TrimSuffix(s, `\`)
|
|
s = strings.TrimSuffix(s, ":")
|
|
if len(s) != 1 || s[0] < 'A' || s[0] > 'Z' {
|
|
return "", false
|
|
}
|
|
return s, true
|
|
}
|
|
|
|
func configuredDriveRoots(cfg VMConfig) map[string]string {
|
|
roots := make(map[string]string)
|
|
for _, raw := range cfg.SharedDrives {
|
|
drive, ok := NormalizeDriveLetter(raw)
|
|
if !ok {
|
|
continue
|
|
}
|
|
root := cfg.DriveRoots[drive]
|
|
if root == "" || (cfg.VerifyDriveRoot != nil && cfg.DriveVolumes[drive] == "") {
|
|
continue
|
|
}
|
|
roots[drive] = root
|
|
}
|
|
return roots
|
|
}
|
|
|
|
func cloneStringMap(src map[string]string) map[string]string {
|
|
if src == nil {
|
|
return nil
|
|
}
|
|
dst := make(map[string]string, len(src))
|
|
for k, v := range src {
|
|
dst[k] = v
|
|
}
|
|
return dst
|
|
}
|
|
|
|
func enabledDriveLetters(vm *registeredVM) []string {
|
|
out := []string{}
|
|
if vm == nil || !vm.driveSharingEnabled {
|
|
return out
|
|
}
|
|
for drive := range vm.sharedDriveRoots {
|
|
out = append(out, drive)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
func allowPROPFINDDepth(w http.ResponseWriter, r *http.Request) bool {
|
|
if r.Method != "PROPFIND" {
|
|
return true
|
|
}
|
|
depth := strings.TrimSpace(r.Header.Get("Depth"))
|
|
if depth == "" {
|
|
depth = "1"
|
|
}
|
|
if depth != "0" && depth != "1" {
|
|
http.Error(w, "PROPFIND requires Depth: 0 or 1", http.StatusBadRequest)
|
|
return false
|
|
}
|
|
r.Header.Set("Depth", depth)
|
|
return true
|
|
}
|
|
|
|
func allowReadOnlyDAV(w http.ResponseWriter, r *http.Request) bool {
|
|
const allow = "OPTIONS, GET, HEAD, PROPFIND"
|
|
switch r.Method {
|
|
case http.MethodGet, http.MethodHead, "PROPFIND":
|
|
return true
|
|
case http.MethodOptions:
|
|
w.Header().Set("Allow", allow)
|
|
w.Header().Set("DAV", "1")
|
|
w.Header().Set("MS-Author-Via", "DAV")
|
|
w.WriteHeader(http.StatusOK)
|
|
return false
|
|
default:
|
|
w.Header().Set("Allow", allow)
|
|
http.Error(w, "read-only drive share", http.StatusMethodNotAllowed)
|
|
return false
|
|
}
|
|
}
|