| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package api
- import (
- "encoding/json"
- "net/http"
- "proxmox-router/internal/aerotun"
- "proxmox-router/internal/auth"
- )
- func (s *Server) handleListAeroTUNTunnels(w http.ResponseWriter, r *http.Request) {
- tunnels := s.vpnMgr.AeroTUN.List()
- for i := range tunnels {
- tunnels[i] = tunnels[i].PublicView()
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(tunnels)
- }
- func (s *Server) handleGetAeroTUNStats(w http.ResponseWriter, r *http.Request) {
- stats := s.vpnMgr.AeroTUN.GetSummaryStats()
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(stats)
- }
- func requireAeroTUNAdmin(w http.ResponseWriter, r *http.Request) bool {
- sess := getSessionFromContext(r.Context())
- if sess == nil || sess.Role != auth.RoleFull {
- http.Error(w, "AeroTUN administration requires full privileges", http.StatusForbidden)
- return false
- }
- return true
- }
- func (s *Server) handleGenerateAeroTUNKey(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- w.Header().Set("Cache-Control", "no-store")
- key, err := aerotun.GenerateSecureKey()
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]string{"key": key})
- }
- func (s *Server) handleSaveAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- var inst aerotun.TunnelInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- http.Error(w, "invalid request body", http.StatusBadRequest)
- return
- }
- saved, err := s.vpnMgr.AeroTUN.Save(inst)
- if err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(saved.PublicView())
- }
- func (s *Server) handleDeleteAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing tunnel ID", http.StatusBadRequest)
- return
- }
- if err := s.vpnMgr.AeroTUN.Delete(id); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
- func (s *Server) handleSetAeroTUNTunnelState(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing tunnel ID", http.StatusBadRequest)
- return
- }
- var req struct {
- Disabled bool `json:"disabled"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
- http.Error(w, "invalid JSON", http.StatusBadRequest)
- return
- }
- if err := s.vpnMgr.AeroTUN.ToggleState(id, req.Disabled); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
- func (s *Server) handleStartAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing tunnel ID", http.StatusBadRequest)
- return
- }
- if err := s.vpnMgr.AeroTUN.StartTunnel(id); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
- func (s *Server) handleStopAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
- if !requireAeroTUNAdmin(w, r) {
- return
- }
- id := r.PathValue("id")
- if id == "" {
- http.Error(w, "missing tunnel ID", http.StatusBadRequest)
- return
- }
- if err := s.vpnMgr.AeroTUN.StopTunnel(id); err != nil {
- http.Error(w, err.Error(), http.StatusInternalServerError)
- return
- }
- w.Header().Set("Content-Type", "application/json")
- _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
- }
|