| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- package api
- import (
- "encoding/json"
- "net/http"
- "time"
- "proxmox-router/internal/tools"
- )
- func (s *Server) handleToolsPing(w http.ResponseWriter, r *http.Request) {
- var req tools.PingRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- timeoutSec := req.Timeout
- if timeoutSec <= 0 {
- timeoutSec = 10
- }
- ctx, cancel := r.Context(), func() {}
- if timeoutSec > 0 {
- ctx, cancel = r.Context(), func() {}
- // Allow up to 35 seconds for ping
- _ = time.Duration(timeoutSec) * time.Second
- }
- defer cancel()
- resp, err := s.toolsMgr.RunPing(ctx, req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, resp)
- }
- func (s *Server) handleToolsTraceroute(w http.ResponseWriter, r *http.Request) {
- var req tools.TracerouteRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- resp, err := s.toolsMgr.RunTraceroute(r.Context(), req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, resp)
- }
- func (s *Server) handleToolsDNS(w http.ResponseWriter, r *http.Request) {
- var req tools.DNSRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- resp, err := s.toolsMgr.RunDNSLookup(r.Context(), req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, resp)
- }
- func (s *Server) handleToolsPortProbe(w http.ResponseWriter, r *http.Request) {
- var req tools.PortProbeRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- resp, err := s.toolsMgr.RunPortProbe(r.Context(), req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, resp)
- }
- func (s *Server) handleToolsMTR(w http.ResponseWriter, r *http.Request) {
- var req tools.MTRRequest
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
- return
- }
- resp, err := s.toolsMgr.RunMTR(r.Context(), req)
- if err != nil {
- writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
- return
- }
- writeJSON(w, http.StatusOK, resp)
- }
|