|
|
@@ -2,6 +2,7 @@ package main
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
+ "context"
|
|
|
"encoding/json"
|
|
|
"fmt"
|
|
|
"io/fs"
|
|
|
@@ -210,7 +211,21 @@ func netDNS(w http.ResponseWriter, r *http.Request) {
|
|
|
runShellAndWrite(w, "cat /etc/resolv.conf")
|
|
|
}
|
|
|
|
|
|
-func callLocalRPC(w http.ResponseWriter, r *http.Request, port int, method string, params any) {
|
|
|
+func callRPCResult(ctx context.Context, port int, method string, params any, result any) error {
|
|
|
+ client, err := jsonrpc2.NewRPCClient(fmt.Sprintf("http://127.0.0.1:%d/rpc", port))
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ resp, err := client.Call(ctx, method, params)
|
|
|
+ if err != nil {
|
|
|
+ return err
|
|
|
+ }
|
|
|
+
|
|
|
+ return json.Unmarshal(resp.Result, result)
|
|
|
+}
|
|
|
+
|
|
|
+func callRPCResponse(w http.ResponseWriter, r *http.Request, port int, method string, params any) {
|
|
|
client, err := jsonrpc2.NewRPCClient(fmt.Sprintf("http://127.0.0.1:%d/rpc", port))
|
|
|
if err != nil {
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
@@ -230,7 +245,7 @@ func callLocalRPC(w http.ResponseWriter, r *http.Request, port int, method strin
|
|
|
func serviceLogLevel(w http.ResponseWriter, r *http.Request, port int) {
|
|
|
switch r.Method {
|
|
|
case http.MethodGet:
|
|
|
- callLocalRPC(w, r, port, "basic.getLogLevel", nil)
|
|
|
+ callRPCResponse(w, r, port, "basic.getLogLevel", nil)
|
|
|
|
|
|
case http.MethodPost:
|
|
|
var req struct {
|
|
|
@@ -242,7 +257,7 @@ func serviceLogLevel(w http.ResponseWriter, r *http.Request, port int) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- callLocalRPC(w, r, port, "basic.setLogLevel", map[string]string{"log_level": req.Level})
|
|
|
+ callRPCResponse(w, r, port, "basic.setLogLevel", map[string]string{"log_level": req.Level})
|
|
|
|
|
|
default:
|
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
@@ -256,17 +271,34 @@ func serviceLogLevelHandler(port int) http.HandlerFunc {
|
|
|
}
|
|
|
|
|
|
func DownloadNetworkLog(w http.ResponseWriter, r *http.Request) {
|
|
|
- filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405"))
|
|
|
- tmpFile := "/tmp/" + filename
|
|
|
-
|
|
|
- cmd := exec.Command("tar", "-czf", tmpFile, "-C", "/opt/yfkj/networkd.service/log", ".")
|
|
|
- if out, err := cmd.CombinedOutput(); err != nil {
|
|
|
- http.Error(w, string(out), http.StatusInternalServerError)
|
|
|
+ if r.Method != http.MethodGet {
|
|
|
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+ filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405"))
|
|
|
+ tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
|
|
|
+
|
|
|
defer os.Remove(tmpFile)
|
|
|
|
|
|
+ cmd := exec.Command(
|
|
|
+ "tar",
|
|
|
+ "--warning=no-file-changed",
|
|
|
+ "-czf",
|
|
|
+ tmpFile,
|
|
|
+ "-C",
|
|
|
+ "/opt/yfkj/networkd.service/log",
|
|
|
+ ".",
|
|
|
+ )
|
|
|
+
|
|
|
+ out, err := cmd.CombinedOutput()
|
|
|
+ if err != nil {
|
|
|
+ if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
|
|
|
+ http.Error(w, string(out), http.StatusInternalServerError)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
w.Header().Set("Content-Type", "application/gzip")
|
|
|
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
|
|
|
|
|
@@ -289,3 +321,26 @@ func RestartNetworkService(w http.ResponseWriter, r *http.Request) {
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
w.Write([]byte(`{"status":"ok"}`))
|
|
|
}
|
|
|
+
|
|
|
+func NetworkStatusHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
+ var modem struct {
|
|
|
+ ICCID string `json:"iccid"`
|
|
|
+ RSSI string `json:"rssi"`
|
|
|
+ }
|
|
|
+ var net struct {
|
|
|
+ Status string `json:"net_ok"`
|
|
|
+ Type string `json:"net_type"`
|
|
|
+ }
|
|
|
+
|
|
|
+ err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net)
|
|
|
+ err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem)
|
|
|
+
|
|
|
+ w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
+ json.NewEncoder(w).Encode(map[string]string{
|
|
|
+ "var1": map[bool]string{true: "🟢正常", false: "🔴异常"}[err1 == nil || err2 == nil],
|
|
|
+ "var2": map[bool]string{true: "🟢正常", false: "🔴异常"}[net.Status == "true"],
|
|
|
+ "var3": net.Type,
|
|
|
+ "var4": modem.RSSI,
|
|
|
+ "var5": modem.ICCID,
|
|
|
+ })
|
|
|
+}
|