gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
write a go code to store LUKS headers in a basic http server for backup
- User
write a go code to store LUKS headers in a basic http server for backup
- Assistant
Sure! Here's a Go code snippet that sets up a basic HTTP server and allows you to backup LUKS (Linux Unified Key Setup) headers:
package main import ( "log" "net/http" "os" "path/filepath" ) func handleLUKSHeaderBackup(w http.ResponseWriter, r *http.Request) { // Path to the LUKS header file luksHeaderPath := "/path/to/luks_header_file.img" // Open the LUKS header file luksHeaderFile, err := os.Open(luksHeaderPath) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer luksHeaderFile.Close() // Set the appropriate headers for the response w.Header().Set("Content-Disposition", "attachment; filename=luks_header.img") w.Header().Set("Content-Type", "application/octet-stream") w.Header().Set("Content-Length", string(StatFileSize(luksHeaderPath))) // Copy the LUKS header file to the response writer _, err = io.Copy(w, luksHeaderFile) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } func main() { // Set up a basic HTTP server http.HandleFunc("/backup_luks_header", handleLUKSHeaderBackup) err := http.ListenAndServe(":8080", nil) if err != nil { log.Fatal("Error starting HTTP server: ", err) } } // StatFileSize returns the file size of a given path func StatFileSize(path string) int64 { fileInfo, err := os.Stat(path) if err != nil { log.Fatal("Error getting file info: ", err) } return fileInfo.Size() }