-
-
Notifications
You must be signed in to change notification settings - Fork 23
feat(api): add Raspberry reboot endpoint and logs UI action #156
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,17 @@ | ||||||||||||||||||||||||||||||||||
| package handlers | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||||||
| "io/fs" | ||||||||||||||||||||||||||||||||||
| "net/http" | ||||||||||||||||||||||||||||||||||
| "os/exec" | ||||||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||||||
| "text/template" | ||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| "github.com/wimaha/TeslaBleHttpProxy/internal/api/models" | ||||||||||||||||||||||||||||||||||
| "github.com/wimaha/TeslaBleHttpProxy/internal/logging" | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -76,3 +80,48 @@ func LogViewer(w http.ResponseWriter, html fs.FS) error { | |||||||||||||||||||||||||||||||||
| template.New("html/layout.html").ParseFS(html, "html/layout.html", "html/logs.html")) | ||||||||||||||||||||||||||||||||||
| return tmpl.ExecuteTemplate(w, "layout.html", nil) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // RebootSystem initiates a Raspberry Pi reboot. | ||||||||||||||||||||||||||||||||||
| // The reboot is executed asynchronously so the API can return first. | ||||||||||||||||||||||||||||||||||
| func RebootSystem(w http.ResponseWriter, r *http.Request) { | ||||||||||||||||||||||||||||||||||
| var response models.Response | ||||||||||||||||||||||||||||||||||
| response.Command = "system_reboot" | ||||||||||||||||||||||||||||||||||
| response.Result = false | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| defer commonDefer(w, &response) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| logging.Warn("System reboot requested", "remote_addr", r.RemoteAddr) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| go func() { | ||||||||||||||||||||||||||||||||||
| // Give the HTTP response time to flush before rebooting the host. | ||||||||||||||||||||||||||||||||||
| time.Sleep(750 * time.Millisecond) | ||||||||||||||||||||||||||||||||||
| if err := executeReboot(); err != nil { | ||||||||||||||||||||||||||||||||||
| logging.Error("Failed to execute reboot command", "error", err) | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| response.Result = true | ||||||||||||||||||||||||||||||||||
| response.Reason = "System reboot initiated. The device will restart shortly." | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+86
to
+105
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| func executeReboot() error { | ||||||||||||||||||||||||||||||||||
| commands := [][]string{ | ||||||||||||||||||||||||||||||||||
| {"sudo", "shutdown", "-r", "now"}, | ||||||||||||||||||||||||||||||||||
| {"/sbin/shutdown", "-r", "now"}, | ||||||||||||||||||||||||||||||||||
| {"shutdown", "-r", "now"}, | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| var lastErr error | ||||||||||||||||||||||||||||||||||
| for _, args := range commands { | ||||||||||||||||||||||||||||||||||
| ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) | ||||||||||||||||||||||||||||||||||
| cmd := exec.CommandContext(ctx, args[0], args[1:]...) | ||||||||||||||||||||||||||||||||||
| err := cmd.Run() | ||||||||||||||||||||||||||||||||||
| cancel() | ||||||||||||||||||||||||||||||||||
| if err == nil { | ||||||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| lastErr = err | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+117
to
+123
|
||||||||||||||||||||||||||||||||||
| cmd := exec.CommandContext(ctx, args[0], args[1:]...) | |
| err := cmd.Run() | |
| cancel() | |
| if err == nil { | |
| return nil | |
| } | |
| lastErr = err | |
| defer cancel() | |
| cmd := exec.CommandContext(ctx, args[0], args[1:]...) | |
| output, err := cmd.CombinedOutput() | |
| if err == nil { | |
| return nil | |
| } | |
| lastErr = fmt.Errorf("command %v failed: %w (output: %s)", args, err, string(output)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
POST /api/admin/reboottriggers a host reboot but currently has no access control (authn/authz) or CSRF mitigation. As-is, any party that can reach the HTTP server (or a malicious webpage via CSRF) can reboot the device. Please add a protection mechanism (e.g., require an admin token/header, restrict to localhost/private subnets, and/or validate Origin/Referer for browser-based calls) before executing the reboot.