tools_handlers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "proxmox-router/internal/tools"
  7. )
  8. func (s *Server) handleToolsPing(w http.ResponseWriter, r *http.Request) {
  9. var req tools.PingRequest
  10. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  11. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  12. return
  13. }
  14. timeoutSec := req.Timeout
  15. if timeoutSec <= 0 {
  16. timeoutSec = 10
  17. }
  18. ctx, cancel := r.Context(), func() {}
  19. if timeoutSec > 0 {
  20. ctx, cancel = r.Context(), func() {}
  21. // Allow up to 35 seconds for ping
  22. _ = time.Duration(timeoutSec) * time.Second
  23. }
  24. defer cancel()
  25. resp, err := s.toolsMgr.RunPing(ctx, req)
  26. if err != nil {
  27. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  28. return
  29. }
  30. writeJSON(w, http.StatusOK, resp)
  31. }
  32. func (s *Server) handleToolsTraceroute(w http.ResponseWriter, r *http.Request) {
  33. var req tools.TracerouteRequest
  34. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  35. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  36. return
  37. }
  38. resp, err := s.toolsMgr.RunTraceroute(r.Context(), req)
  39. if err != nil {
  40. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  41. return
  42. }
  43. writeJSON(w, http.StatusOK, resp)
  44. }
  45. func (s *Server) handleToolsDNS(w http.ResponseWriter, r *http.Request) {
  46. var req tools.DNSRequest
  47. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  48. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  49. return
  50. }
  51. resp, err := s.toolsMgr.RunDNSLookup(r.Context(), req)
  52. if err != nil {
  53. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  54. return
  55. }
  56. writeJSON(w, http.StatusOK, resp)
  57. }
  58. func (s *Server) handleToolsPortProbe(w http.ResponseWriter, r *http.Request) {
  59. var req tools.PortProbeRequest
  60. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  61. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  62. return
  63. }
  64. resp, err := s.toolsMgr.RunPortProbe(r.Context(), req)
  65. if err != nil {
  66. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  67. return
  68. }
  69. writeJSON(w, http.StatusOK, resp)
  70. }
  71. func (s *Server) handleToolsMTR(w http.ResponseWriter, r *http.Request) {
  72. var req tools.MTRRequest
  73. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  74. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  75. return
  76. }
  77. resp, err := s.toolsMgr.RunMTR(r.Context(), req)
  78. if err != nil {
  79. writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
  80. return
  81. }
  82. writeJSON(w, http.StatusOK, resp)
  83. }