web_handlers.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package main
  2. import (
  3. "encoding/json"
  4. "io/fs"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "strings"
  9. )
  10. type LoginReq struct {
  11. Username string `json:"username"`
  12. Password string `json:"password"`
  13. }
  14. type LoginResp struct {
  15. Success bool `json:"success"`
  16. }
  17. func loginHandler(w http.ResponseWriter, r *http.Request) {
  18. if r.Method != http.MethodPost {
  19. w.WriteHeader(http.StatusMethodNotAllowed)
  20. return
  21. }
  22. var req LoginReq
  23. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  24. w.WriteHeader(http.StatusBadRequest)
  25. return
  26. }
  27. ok := req.Username == "admin" && req.Password == "admin123456"
  28. if ok {
  29. sessionID := createSession()
  30. addSession(sessionID)
  31. http.SetCookie(w, &http.Cookie{
  32. Name: "session_id",
  33. Value: sessionID,
  34. Path: "/",
  35. HttpOnly: true,
  36. SameSite: http.SameSiteStrictMode,
  37. })
  38. }
  39. w.Header().Set("Content-Type", "application/json")
  40. json.NewEncoder(w).Encode(LoginResp{Success: ok})
  41. }
  42. func logoutHandler(w http.ResponseWriter, r *http.Request) {
  43. if cookie, err := r.Cookie("session_id"); err == nil {
  44. sessionMu.Lock()
  45. delete(sessions, cookie.Value)
  46. sessionMu.Unlock()
  47. }
  48. http.SetCookie(w, &http.Cookie{
  49. Name: "session_id",
  50. Value: "",
  51. Path: "/",
  52. MaxAge: -1,
  53. })
  54. http.Redirect(w, r, "/", http.StatusFound)
  55. }
  56. func getIMEIHandler(w http.ResponseWriter, r *http.Request) {
  57. data, _ := os.ReadFile("/var/device_imei.txt")
  58. w.Write(data)
  59. }
  60. func (ui *WebUI) rootHandler(w http.ResponseWriter, r *http.Request) {
  61. path := r.URL.Path
  62. w.Header().Set("Content-Type", "text/html; charset=utf-8")
  63. w.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
  64. w.Header().Set("Pragma", "no-cache")
  65. w.Header().Set("Expires", "0")
  66. // 1, 未登录时, 只能看登录页
  67. if !checkSession(r) {
  68. data, err := fs.ReadFile(ui.fs, "login.html")
  69. if err != nil {
  70. http.NotFound(w, r)
  71. } else {
  72. w.Write(data)
  73. }
  74. return
  75. }
  76. // 2, 登录成功, 展示应用首页
  77. if path == "/" {
  78. data, err := fs.ReadFile(ui.fs, "app.html")
  79. if err != nil {
  80. http.NotFound(w, r)
  81. } else {
  82. w.Write(data)
  83. }
  84. return
  85. }
  86. // 3, 在网站内跳转其它功能页
  87. if strings.HasPrefix(path, "/pages/") { // path 形如 "/pages/page1.html"
  88. data, err := fs.ReadFile(ui.fs, path[1:])
  89. if err != nil {
  90. http.NotFound(w, r)
  91. } else {
  92. w.Write(data)
  93. }
  94. return
  95. }
  96. // 4, 访问其它不认识的页面时
  97. http.NotFound(w, r)
  98. }
  99. func runShellAndWrite(w http.ResponseWriter, cmd string) {
  100. out, err := exec.Command("sh", "-c", cmd).CombinedOutput()
  101. if err != nil {
  102. http.Error(w, err.Error()+"\n"+string(out), http.StatusInternalServerError)
  103. return
  104. }
  105. w.Write(out)
  106. }
  107. func netInterfaces(w http.ResponseWriter, r *http.Request) {
  108. runShellAndWrite(w, "ifconfig")
  109. }
  110. func netRoutes(w http.ResponseWriter, r *http.Request) {
  111. runShellAndWrite(w, "route -n")
  112. }
  113. func netDNS(w http.ResponseWriter, r *http.Request) {
  114. runShellAndWrite(w, "cat /etc/resolv.conf")
  115. }