| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- package api
- import (
- "encoding/json"
- "net/http"
- "proxmox-router/internal/aerotun"
- )
- func (s *Server) handleListAeroTUNTunnels(w http.ResponseWriter, r *http.Request) {
- tunnels := s.vpnMgr.AeroTUN.List()
- 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 (s *Server) handleGenerateAeroTUNKey(w http.ResponseWriter, r *http.Request) {
- 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) {
- 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)
- }
- func (s *Server) handleDeleteAeroTUNTunnel(w http.ResponseWriter, r *http.Request) {
- 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) {
- 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) {
- 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) {
- 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})
- }
|