From 34b2094ad64141fb9a0f804fe59485775dfb6586 Mon Sep 17 00:00:00 2001 From: 3err0 Date: Wed, 6 Dec 2023 19:28:05 +0800 Subject: [PATCH] first --- webctl.go | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ webctl.service | 11 +++++ 2 files changed, 124 insertions(+) create mode 100644 webctl.go create mode 100644 webctl.service diff --git a/webctl.go b/webctl.go new file mode 100644 index 0000000..f9917d9 --- /dev/null +++ b/webctl.go @@ -0,0 +1,113 @@ +package main + +import ( + "io/ioutil" + "log" + "fmt" + "net" + "net/http" + "os/exec" + "runtime" + "errors" +) + +func getLocalIP() (string, error) { + addrs, err := net.InterfaceAddrs() + if err != nil { + return "", err + } + + for _, addr := range addrs { + if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + return ipnet.IP.String(), nil + } + } + } + + return "", errors.New("cannot find local IP address") +} + +func ipHandler(w http.ResponseWriter, r *http.Request) { + ip, err := getLocalIP() + if err != nil { + http.Error(w, "Error getting IP address: "+err.Error(), http.StatusInternalServerError) + return + } + + fmt.Fprintf(w, "%s", ip) +} + +func rebootSystem() error { + var cmd *exec.Cmd + + switch runtime.GOOS { + case "linux": + cmd = exec.Command("sudo", "reboot") + case "windows": + cmd = exec.Command("shutdown", "/r", "/t", "0") + default: + return errors.New("unsupported platform") + } + + return cmd.Start() +} + +func rebootHandler(w http.ResponseWriter, r *http.Request) { + err := rebootSystem() + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte("Reboot initiated")) +} + +func commandHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "POST" { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } + + body, err := ioutil.ReadAll(r.Body) + if err != nil { + log.Printf("Error reading request body: %s", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + defer r.Body.Close() + + command := string(body) + var cmd *exec.Cmd + if runtime.GOOS == "windows" { + cmd = exec.Command("cmd.exe", "/C", command) + } else { + cmd = exec.Command("/bin/bash", "-c", command) + } + output, err := cmd.CombinedOutput() + if err != nil { + log.Printf("Error executing command: %s, Error: %s", command, err) + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte(err.Error())) + return + } + + w.WriteHeader(http.StatusOK) + w.Write(output) +} + +func osHandler(w http.ResponseWriter, r *http.Request) { + osInfo := runtime.GOOS + w.WriteHeader(http.StatusOK) + w.Write([]byte("Operating System: " + osInfo)) +} + +func main() { + http.HandleFunc("/reboot", rebootHandler) + http.HandleFunc("/command", commandHandler) + http.HandleFunc("/os", osHandler) + http.HandleFunc("/ip", ipHandler) + + log.Fatal(http.ListenAndServe("0.0.0.0:99", nil)) +} \ No newline at end of file diff --git a/webctl.service b/webctl.service new file mode 100644 index 0000000..45b2945 --- /dev/null +++ b/webctl.service @@ -0,0 +1,11 @@ +[Unit] +Description=RemoteSysManager Service +After=network.target + +[Service] +Type=simple +ExecStart=/usr/local/bin/webctl +Restart=on-failure + +[Install] +WantedBy=multi-user.target \ No newline at end of file