aerotun_handlers.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/aerotun"
  6. )
  7. func (s *Server) handleListAeroTUNTunnels(w http.ResponseWriter, r *http.Request) {
  8. tunnels := s.vpnMgr.AeroTUN.List()
  9. w.Header().Set("Content-Type", "application/json")
  10. _ = json.NewEncoder(w).Encode(tunnels)
  11. }
  12. func (s *Server) handleGetAeroTUNStats(w http.ResponseWriter, r *http.Request) {
  13. stats := s.vpnMgr.AeroTUN.GetSummaryStats()
  14. w.Header().Set("Content-Type", "application/json")
  15. _ = json.NewEncoder(w).Encode(stats)
  16. }
  17. func (s *Server) handleGenerateAeroTUNKey(w http.ResponseWriter, r *http.Request) {
  18. key, err := aerotun.GenerateSecureKey()
  19. if err != nil {
  20. http.Error(w, err.Error(), http.StatusInternalServerError)
  21. return
  22. }
  23. w.Header().Set("Content-Type", "application/json")
  24. _ = json.NewEncoder(w).Encode(map[string]string{"key": key})
  25. }
  26. func (s *Server) handleSaveAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  27. var inst aerotun.TunnelInstance
  28. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  29. http.Error(w, "invalid request body", http.StatusBadRequest)
  30. return
  31. }
  32. saved, err := s.vpnMgr.AeroTUN.Save(inst)
  33. if err != nil {
  34. http.Error(w, err.Error(), http.StatusInternalServerError)
  35. return
  36. }
  37. w.Header().Set("Content-Type", "application/json")
  38. _ = json.NewEncoder(w).Encode(saved)
  39. }
  40. func (s *Server) handleDeleteAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  41. id := r.PathValue("id")
  42. if id == "" {
  43. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  44. return
  45. }
  46. if err := s.vpnMgr.AeroTUN.Delete(id); err != nil {
  47. http.Error(w, err.Error(), http.StatusInternalServerError)
  48. return
  49. }
  50. w.Header().Set("Content-Type", "application/json")
  51. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  52. }
  53. func (s *Server) handleSetAeroTUNTunnelState(w http.ResponseWriter, r *http.Request) {
  54. id := r.PathValue("id")
  55. if id == "" {
  56. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  57. return
  58. }
  59. var req struct {
  60. Disabled bool `json:"disabled"`
  61. }
  62. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  63. http.Error(w, "invalid JSON", http.StatusBadRequest)
  64. return
  65. }
  66. if err := s.vpnMgr.AeroTUN.ToggleState(id, req.Disabled); err != nil {
  67. http.Error(w, err.Error(), http.StatusInternalServerError)
  68. return
  69. }
  70. w.Header().Set("Content-Type", "application/json")
  71. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  72. }
  73. func (s *Server) handleStartAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  74. id := r.PathValue("id")
  75. if id == "" {
  76. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  77. return
  78. }
  79. if err := s.vpnMgr.AeroTUN.StartTunnel(id); err != nil {
  80. http.Error(w, err.Error(), http.StatusInternalServerError)
  81. return
  82. }
  83. w.Header().Set("Content-Type", "application/json")
  84. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  85. }
  86. func (s *Server) handleStopAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  87. id := r.PathValue("id")
  88. if id == "" {
  89. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  90. return
  91. }
  92. if err := s.vpnMgr.AeroTUN.StopTunnel(id); err != nil {
  93. http.Error(w, err.Error(), http.StatusInternalServerError)
  94. return
  95. }
  96. w.Header().Set("Content-Type", "application/json")
  97. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  98. }