page2_handler.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "time"
  9. )
  10. func downloadNetworkLog(w http.ResponseWriter, r *http.Request) {
  11. if r.Method != http.MethodGet {
  12. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  13. return
  14. }
  15. filename := fmt.Sprintf("network.log.%s.tar.gz", time.Now().Format("20060102150405"))
  16. tmpFile := fmt.Sprintf("/tmp/%d.tar.gz", time.Now().UnixNano())
  17. defer os.Remove(tmpFile)
  18. cmd := exec.Command(
  19. "tar",
  20. "--warning=no-file-changed",
  21. "-czf",
  22. tmpFile,
  23. "-C",
  24. "/opt/yfkj/networkd.service/log",
  25. ".",
  26. )
  27. out, err := cmd.CombinedOutput()
  28. if err != nil {
  29. if exitErr, ok := err.(*exec.ExitError); !ok || exitErr.ExitCode() != 1 {
  30. http.Error(w, string(out), http.StatusInternalServerError)
  31. return
  32. }
  33. }
  34. w.Header().Set("Content-Type", "application/gzip")
  35. w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
  36. http.ServeFile(w, r, tmpFile)
  37. }
  38. func restartNetworkService(w http.ResponseWriter, r *http.Request) {
  39. if r.Method != http.MethodPost {
  40. http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
  41. return
  42. }
  43. cmd := exec.Command("systemctl", "restart", "yfkj-networkd.service")
  44. if out, err := cmd.CombinedOutput(); err != nil {
  45. http.Error(w, string(out)+err.Error(), http.StatusInternalServerError)
  46. return
  47. }
  48. w.Header().Set("Content-Type", "application/json")
  49. w.Write([]byte(`{"status":"ok"}`))
  50. }
  51. func networkStatusHandler(w http.ResponseWriter, r *http.Request) {
  52. var modem struct {
  53. ICCID string `json:"iccid"`
  54. RSSI string `json:"rssi"`
  55. }
  56. var net struct {
  57. Status string `json:"net_ok"`
  58. Type string `json:"net_type"`
  59. }
  60. err1 := callRPCResult(r.Context(), 7000, "core.getNetStatus", nil, &net)
  61. err2 := callRPCResult(r.Context(), 7000, "core.getModemInfo", nil, &modem)
  62. netType := map[string]string{
  63. "有线": "🌐有线",
  64. "蜂窝": "📶蜂窝",
  65. }[net.Type]
  66. if netType == "" {
  67. netType = noValue
  68. }
  69. rssi := modem.RSSI
  70. if rssi == "" {
  71. rssi = noValue
  72. } else {
  73. rssi = "📶" + rssi
  74. }
  75. iccid := modem.ICCID
  76. if iccid == "" {
  77. iccid = noValue
  78. } else {
  79. iccid = "🔖" + iccid
  80. }
  81. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  82. json.NewEncoder(w).Encode(map[string]string{
  83. "var1": map[bool]string{true: "🟢正常", false: noValue}[err1 == nil || err2 == nil],
  84. "var2": map[bool]string{true: "🟢正常", false: noValue}[net.Status == "true"],
  85. "var3": netType,
  86. "var4": rssi,
  87. "var5": iccid,
  88. })
  89. }