fix chroot

This commit is contained in:
Zoe
2025-05-27 14:13:37 +00:00
parent e1f58e8aaf
commit fa319076d1
4 changed files with 207 additions and 56 deletions

View File

@@ -1,6 +1,12 @@
package main
import "github.com/gofiber/fiber/v3"
import (
"fmt"
"os"
"path/filepath"
"github.com/gofiber/fiber/v3"
)
type MyPlugin struct{}
@@ -16,6 +22,60 @@ func (p *MyPlugin) RegisterRoutes(router fiber.Router) {
router.Get("/hello", func(c fiber.Ctx) error {
return c.SendString("Hello from MyPlugin!")
})
router.Get("/dir/:path?", func(c fiber.Ctx) error {
// Get the directory path from the URL parameter
// If the parameter is empty, default to the current working directory.
dirPath := c.Params("path")
if dirPath == "" {
var err error
dirPath, err = os.Getwd() // Get current working directory
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Failed to get current working directory",
})
}
}
// Ensure the path is absolute for security and clarity
absPath, err := filepath.Abs(dirPath)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": fmt.Sprintf("Failed to get absolute path for %s: %v", dirPath, err),
})
}
// Read the directory contents
files, err := os.ReadDir(absPath)
if err != nil {
// Handle different error types for better feedback
if os.IsNotExist(err) {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{
"error": fmt.Sprintf("Directory not found: %s", absPath),
})
}
if os.IsPermission(err) {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
"error": fmt.Sprintf("Permission denied to access directory: %s", absPath),
})
}
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": fmt.Sprintf("Failed to read directory %s: %v", absPath, err),
})
}
// Extract file names and prepare the response
var fileNames []string
for _, file := range files {
fileNames = append(fileNames, file.Name())
}
// Return the list of file names as JSON
return c.JSON(fiber.Map{
"directory": absPath,
"files": fileNames,
})
})
}
// Exported symbol