| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- package api
- import (
- "encoding/json"
- "net/http"
- "proxmox-router/internal/vpn"
- )
- func (s *Server) handleOverallVPNStatus(w http.ResponseWriter, r *http.Request) {
- status := s.vpnMgr.GetOverallStatus()
- writeJSON(w, http.StatusOK, status)
- }
- // ======================== PPPoE Multi-Instance Handlers ========================
- func (s *Server) handleListPPPoE(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.PPPoE.List()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSavePPPoE(w http.ResponseWriter, r *http.Request) {
- var inst vpn.PPPoEInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPoE.Save(inst); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
- }
- func (s *Server) handleDeletePPPoE(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPPoE.Delete(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleSetPPPoEState(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.PPPoE.SetEnabled(name, req.Enabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
- }
- func (s *Server) handleConnectPPPoE(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPPoE.Connect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleDisconnectPPPoE(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPPoE.Disconnect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- // ======================== WireGuard Multi-Interface & Peer Handlers ========================
- func (s *Server) handleListWireGuardInterfaces(w http.ResponseWriter, r *http.Request) {
- ifaces := s.vpnMgr.WireGuard.ListInterfaces()
- writeJSON(w, http.StatusOK, ifaces)
- }
- func (s *Server) handleSaveWireGuardInterface(w http.ResponseWriter, r *http.Request) {
- var iface vpn.WireGuardInterface
- if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.WireGuard.SaveInterface(iface); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": iface.Name})
- }
- func (s *Server) handleDeleteWireGuardInterface(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.WireGuard.DeleteInterface(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleSetWireGuardInterfaceState(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.WireGuard.SetInterfaceEnabled(name, req.Enabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
- }
- func (s *Server) handleStartWireGuardInterface(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.WireGuard.StartInterface(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleStopWireGuardInterface(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.WireGuard.StopInterface(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleListWireGuardPeers(w http.ResponseWriter, r *http.Request) {
- ifaceFilter := r.URL.Query().Get("interface")
- peers := s.vpnMgr.WireGuard.ListPeers(ifaceFilter)
- writeJSON(w, http.StatusOK, peers)
- }
- func (s *Server) handleSaveWireGuardPeer(w http.ResponseWriter, r *http.Request) {
- var peer vpn.WireGuardPeer
- if err := json.NewDecoder(r.Body).Decode(&peer); err != nil || peer.PublicKey == "" || peer.Interface == "" {
- writeError(w, http.StatusBadRequest, "interface and public_key are required")
- return
- }
- if err := s.vpnMgr.WireGuard.SavePeer(peer); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": peer.Interface, "public_key": peer.PublicKey})
- }
- func (s *Server) handleDeleteWireGuardPeer(w http.ResponseWriter, r *http.Request) {
- iface := r.PathValue("iface")
- pubkey := r.PathValue("pubkey")
- if err := s.vpnMgr.WireGuard.DeletePeer(iface, pubkey); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": iface, "public_key": pubkey})
- }
- func (s *Server) handleWireGuardGenKeys(w http.ResponseWriter, r *http.Request) {
- priv, pub, err := s.vpnMgr.WireGuard.GenerateKeyPair()
- if err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]string{
- "private_key": priv,
- "public_key": pub,
- })
- }
- // ======================== PPTP Multi-Instance Handlers ========================
- func (s *Server) handleListPPTP(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.PPTP.List()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSavePPTP(w http.ResponseWriter, r *http.Request) {
- var inst vpn.PPTPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPTP.Save(inst); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
- }
- func (s *Server) handleDeletePPTP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPTP.Delete(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleSetPPTPState(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.PPTP.SetEnabled(name, req.Enabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
- }
- func (s *Server) handleConnectPPTP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPTP.Connect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleDisconnectPPTP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPTP.Disconnect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- // ======================== L2TP Multi-Instance Handlers ========================
- func (s *Server) handleListL2TP(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.L2TP.List()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSaveL2TP(w http.ResponseWriter, r *http.Request) {
- var inst vpn.L2TPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.L2TP.Save(inst); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": inst.Name})
- }
- func (s *Server) handleDeleteL2TP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.L2TP.Delete(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleSetL2TPState(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.L2TP.SetEnabled(name, req.Enabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
- }
- func (s *Server) handleConnectL2TP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.L2TP.Connect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleDisconnectL2TP(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.L2TP.Disconnect(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- // ======================== IPSec Multi-Tunnel Handlers ========================
- func (s *Server) handleListIPSec(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.IPSec.List()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSaveIPSecTunnel(w http.ResponseWriter, r *http.Request) {
- var conn vpn.IPSecConnection
- if err := json.NewDecoder(r.Body).Decode(&conn); err != nil || conn.Name == "" {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.IPSec.SaveConnection(conn); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": conn.Name})
- }
- func (s *Server) handleDeleteIPSecTunnel(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.IPSec.DeleteConnection(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleSetIPSecState(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- var req struct {
- Enabled bool `json:"enabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.IPSec.SetEnabled(name, req.Enabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "enabled": req.Enabled})
- }
- func (s *Server) handleInitiateIPSecTunnel(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.IPSec.StartTunnel(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- func (s *Server) handleTerminateIPSecTunnel(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.IPSec.StopTunnel(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- // ======================== EoIP (Ethernet-over-IP) Handlers ========================
- func (s *Server) handleListEoIP(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.EoIP.List()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSaveEoIP(w http.ResponseWriter, r *http.Request) {
- var inst vpn.EoIPInstance
- if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.EoIP.Save(&inst); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": inst.ID, "name": inst.Name})
- }
- func (s *Server) handleDeleteEoIP(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if err := s.vpnMgr.EoIP.Delete(id); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
- }
- func (s *Server) handleSetEoIPState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.EoIP.SetState(id, req.Disabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id, "disabled": req.Disabled})
- }
- // ======================== PPP Profiles Handlers ========================
- func (s *Server) handleListPPPProfiles(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.PPPServer.ListProfiles()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSavePPPProfile(w http.ResponseWriter, r *http.Request) {
- var prof vpn.PPPProfile
- if err := json.NewDecoder(r.Body).Decode(&prof); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SaveProfile(&prof); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": prof.Name})
- }
- func (s *Server) handleDeletePPPProfile(w http.ResponseWriter, r *http.Request) {
- name := r.PathValue("name")
- if err := s.vpnMgr.PPPServer.DeleteProfile(name); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
- }
- // ======================== PPP Secrets Handlers ========================
- func (s *Server) handleListPPPSecrets(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.PPPServer.ListSecrets()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleSavePPPSecret(w http.ResponseWriter, r *http.Request) {
- var sec vpn.PPPSecret
- if err := json.NewDecoder(r.Body).Decode(&sec); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SaveSecret(&sec); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": sec.ID, "name": sec.Name})
- }
- func (s *Server) handleSetPPPSecretState(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- var req struct {
- Disabled bool `json:"disabled"`
- }
- _ = json.NewDecoder(r.Body).Decode(&req)
- if err := s.vpnMgr.PPPServer.SetSecretState(id, req.Disabled); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id, "disabled": req.Disabled})
- }
- func (s *Server) handleDeletePPPSecret(w http.ResponseWriter, r *http.Request) {
- id := r.PathValue("id")
- if err := s.vpnMgr.PPPServer.DeleteSecret(id); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
- }
- // ======================== Active PPP Sessions Handlers ========================
- func (s *Server) handleListPPPSessions(w http.ResponseWriter, r *http.Request) {
- list := s.vpnMgr.PPPServer.ListActiveSessions()
- writeJSON(w, http.StatusOK, list)
- }
- func (s *Server) handleDisconnectPPPSession(w http.ResponseWriter, r *http.Request) {
- iface := r.PathValue("iface")
- if err := s.vpnMgr.PPPServer.DisconnectSession(iface); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "interface": iface})
- }
- // ======================== VPN Servers Configuration Handlers ========================
- func (s *Server) handleGetPPPoEServer(w http.ResponseWriter, r *http.Request) {
- cfg := s.vpnMgr.PPPServer.GetPPPoEServer()
- writeJSON(w, http.StatusOK, cfg)
- }
- func (s *Server) handleSetPPPoEServer(w http.ResponseWriter, r *http.Request) {
- var cfg vpn.PPPoEServerConfig
- if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SetPPPoEServer(cfg); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true})
- }
- func (s *Server) handleGetPPTPServer(w http.ResponseWriter, r *http.Request) {
- cfg := s.vpnMgr.PPPServer.GetPPTPServer()
- writeJSON(w, http.StatusOK, cfg)
- }
- func (s *Server) handleSetPPTPServer(w http.ResponseWriter, r *http.Request) {
- var cfg vpn.PPTPServerConfig
- if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SetPPTPServer(cfg); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true})
- }
- func (s *Server) handleGetL2TPServer(w http.ResponseWriter, r *http.Request) {
- cfg := s.vpnMgr.PPPServer.GetL2TPServer()
- writeJSON(w, http.StatusOK, cfg)
- }
- func (s *Server) handleSetL2TPServer(w http.ResponseWriter, r *http.Request) {
- var cfg vpn.L2TPServerConfig
- if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SetL2TPServer(cfg); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true})
- }
- func (s *Server) handleGetIPSecServer(w http.ResponseWriter, r *http.Request) {
- cfg := s.vpnMgr.PPPServer.GetIPSecServer()
- writeJSON(w, http.StatusOK, cfg)
- }
- func (s *Server) handleSetIPSecServer(w http.ResponseWriter, r *http.Request) {
- var cfg vpn.IPSecServerConfig
- if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
- writeError(w, http.StatusBadRequest, "invalid request body")
- return
- }
- if err := s.vpnMgr.PPPServer.SetIPSecServer(cfg); err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true})
- }
- // WireGuard Client Config Generator
- func (s *Server) handleGenerateWGClientConfig(w http.ResponseWriter, r *http.Request) {
- var req struct {
- Interface string `json:"interface"`
- Address string `json:"address"`
- PrivKey string `json:"private_key"`
- Endpoint string `json:"endpoint"`
- DNS string `json:"dns"`
- }
- if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.Interface == "" {
- writeError(w, http.StatusBadRequest, "interface is required")
- return
- }
- if req.PrivKey == "" {
- priv, _, err := s.vpnMgr.WireGuard.GenerateKeyPair()
- if err == nil {
- req.PrivKey = priv
- }
- }
- conf, err := s.vpnMgr.WireGuard.GenerateClientConfig(req.Interface, req.PrivKey, req.Address, req.Endpoint, req.DNS)
- if err != nil {
- writeError(w, http.StatusInternalServerError, err.Error())
- return
- }
- writeJSON(w, http.StatusOK, map[string]any{"success": true, "config": conf, "client_private_key": req.PrivKey})
- }
|