aerotun_handlers.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/aerotun"
  6. "proxmox-router/internal/auth"
  7. )
  8. func (s *Server) handleListAeroTUNTunnels(w http.ResponseWriter, r *http.Request) {
  9. tunnels := s.vpnMgr.AeroTUN.List()
  10. for i := range tunnels {
  11. tunnels[i] = tunnels[i].PublicView()
  12. }
  13. w.Header().Set("Content-Type", "application/json")
  14. _ = json.NewEncoder(w).Encode(tunnels)
  15. }
  16. func (s *Server) handleGetAeroTUNStats(w http.ResponseWriter, r *http.Request) {
  17. stats := s.vpnMgr.AeroTUN.GetSummaryStats()
  18. w.Header().Set("Content-Type", "application/json")
  19. _ = json.NewEncoder(w).Encode(stats)
  20. }
  21. func requireAeroTUNAdmin(w http.ResponseWriter, r *http.Request) bool {
  22. sess := getSessionFromContext(r.Context())
  23. if sess == nil || sess.Role != auth.RoleFull {
  24. http.Error(w, "AeroTUN administration requires full privileges", http.StatusForbidden)
  25. return false
  26. }
  27. return true
  28. }
  29. func (s *Server) handleGenerateAeroTUNKey(w http.ResponseWriter, r *http.Request) {
  30. if !requireAeroTUNAdmin(w, r) {
  31. return
  32. }
  33. w.Header().Set("Cache-Control", "no-store")
  34. key, err := aerotun.GenerateSecureKey()
  35. if err != nil {
  36. http.Error(w, err.Error(), http.StatusInternalServerError)
  37. return
  38. }
  39. w.Header().Set("Content-Type", "application/json")
  40. _ = json.NewEncoder(w).Encode(map[string]string{"key": key})
  41. }
  42. func (s *Server) handleSaveAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  43. if !requireAeroTUNAdmin(w, r) {
  44. return
  45. }
  46. var inst aerotun.TunnelInstance
  47. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  48. http.Error(w, "invalid request body", http.StatusBadRequest)
  49. return
  50. }
  51. saved, err := s.vpnMgr.AeroTUN.Save(inst)
  52. if err != nil {
  53. http.Error(w, err.Error(), http.StatusInternalServerError)
  54. return
  55. }
  56. w.Header().Set("Content-Type", "application/json")
  57. _ = json.NewEncoder(w).Encode(saved.PublicView())
  58. }
  59. func (s *Server) handleDeleteAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  60. if !requireAeroTUNAdmin(w, r) {
  61. return
  62. }
  63. id := r.PathValue("id")
  64. if id == "" {
  65. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  66. return
  67. }
  68. if err := s.vpnMgr.AeroTUN.Delete(id); err != nil {
  69. http.Error(w, err.Error(), http.StatusInternalServerError)
  70. return
  71. }
  72. w.Header().Set("Content-Type", "application/json")
  73. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  74. }
  75. func (s *Server) handleSetAeroTUNTunnelState(w http.ResponseWriter, r *http.Request) {
  76. if !requireAeroTUNAdmin(w, r) {
  77. return
  78. }
  79. id := r.PathValue("id")
  80. if id == "" {
  81. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  82. return
  83. }
  84. var req struct {
  85. Disabled bool `json:"disabled"`
  86. }
  87. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  88. http.Error(w, "invalid JSON", http.StatusBadRequest)
  89. return
  90. }
  91. if err := s.vpnMgr.AeroTUN.ToggleState(id, req.Disabled); err != nil {
  92. http.Error(w, err.Error(), http.StatusInternalServerError)
  93. return
  94. }
  95. w.Header().Set("Content-Type", "application/json")
  96. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  97. }
  98. func (s *Server) handleStartAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  99. if !requireAeroTUNAdmin(w, r) {
  100. return
  101. }
  102. id := r.PathValue("id")
  103. if id == "" {
  104. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  105. return
  106. }
  107. if err := s.vpnMgr.AeroTUN.StartTunnel(id); err != nil {
  108. http.Error(w, err.Error(), http.StatusInternalServerError)
  109. return
  110. }
  111. w.Header().Set("Content-Type", "application/json")
  112. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  113. }
  114. func (s *Server) handleStopAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
  115. if !requireAeroTUNAdmin(w, r) {
  116. return
  117. }
  118. id := r.PathValue("id")
  119. if id == "" {
  120. http.Error(w, "missing tunnel ID", http.StatusBadRequest)
  121. return
  122. }
  123. if err := s.vpnMgr.AeroTUN.StopTunnel(id); err != nil {
  124. http.Error(w, err.Error(), http.StatusInternalServerError)
  125. return
  126. }
  127. w.Header().Set("Content-Type", "application/json")
  128. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  129. }