bunch of qol (#2748)

This commit is contained in:
Kristoffer Dalby
2025-08-27 17:09:13 +02:00
committed by GitHub
parent 1a7a2f4196
commit 8e25f7f9dd
11 changed files with 307 additions and 95 deletions

View File

@@ -622,6 +622,27 @@ func extractTarToDirectory(tarData []byte, targetDir string) error {
}
tarReader := tar.NewReader(bytes.NewReader(tarData))
// Find the top-level directory to strip
var topLevelDir string
firstPass := tar.NewReader(bytes.NewReader(tarData))
for {
header, err := firstPass.Next()
if err == io.EOF {
break
}
if err != nil {
return fmt.Errorf("failed to read tar header: %w", err)
}
if header.Typeflag == tar.TypeDir && topLevelDir == "" {
topLevelDir = strings.TrimSuffix(header.Name, "/")
break
}
}
// Second pass: extract files, stripping the top-level directory
tarReader = tar.NewReader(bytes.NewReader(tarData))
for {
header, err := tarReader.Next()
if err == io.EOF {
@@ -637,7 +658,20 @@ func extractTarToDirectory(tarData []byte, targetDir string) error {
continue // Skip potentially dangerous paths
}
targetPath := filepath.Join(targetDir, filepath.Base(cleanName))
// Strip the top-level directory
if topLevelDir != "" && strings.HasPrefix(cleanName, topLevelDir+"/") {
cleanName = strings.TrimPrefix(cleanName, topLevelDir+"/")
} else if cleanName == topLevelDir {
// Skip the top-level directory itself
continue
}
// Skip empty paths after stripping
if cleanName == "" {
continue
}
targetPath := filepath.Join(targetDir, cleanName)
switch header.Typeflag {
case tar.TypeDir:
@@ -646,6 +680,11 @@ func extractTarToDirectory(tarData []byte, targetDir string) error {
return fmt.Errorf("failed to create directory %s: %w", targetPath, err)
}
case tar.TypeReg:
// Ensure parent directories exist
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
return fmt.Errorf("failed to create parent directories for %s: %w", targetPath, err)
}
// Create file
outFile, err := os.Create(targetPath)
if err != nil {
@@ -674,7 +713,7 @@ func (t *HeadscaleInContainer) SaveProfile(savePath string) error {
return err
}
targetDir := path.Join(savePath, t.hostname+"-pprof")
targetDir := path.Join(savePath, "pprof")
return extractTarToDirectory(tarFile, targetDir)
}
@@ -685,7 +724,7 @@ func (t *HeadscaleInContainer) SaveMapResponses(savePath string) error {
return err
}
targetDir := path.Join(savePath, t.hostname+"-mapresponses")
targetDir := path.Join(savePath, "mapresponses")
return extractTarToDirectory(tarFile, targetDir)
}
@@ -1243,3 +1282,22 @@ func (t *HeadscaleInContainer) SendInterrupt() error {
return nil
}
func (t *HeadscaleInContainer) GetAllMapReponses() (map[types.NodeID][]tailcfg.MapResponse, error) {
// Execute curl inside the container to access the debug endpoint locally
command := []string{
"curl", "-s", "-H", "Accept: application/json", "http://localhost:9090/debug/mapresponses",
}
result, err := t.Execute(command)
if err != nil {
return nil, fmt.Errorf("fetching mapresponses from debug endpoint: %w", err)
}
var res map[types.NodeID][]tailcfg.MapResponse
if err := json.Unmarshal([]byte(result), &res); err != nil {
return nil, fmt.Errorf("decoding routes response: %w", err)
}
return res, nil
}