api.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "proxmox-router/internal/dynamicrouting"
  7. "proxmox-router/internal/firewall"
  8. "proxmox-router/internal/network"
  9. "proxmox-router/internal/routing"
  10. "proxmox-router/internal/system"
  11. "proxmox-router/internal/vpn"
  12. )
  13. type Server struct {
  14. netMgr *network.Manager
  15. persistMgr *network.PersistenceManager
  16. metaMgr *network.MetadataManager
  17. vpnMgr *vpn.Manager
  18. fwMgr *firewall.FirewallManager
  19. routingMgr *routing.RoutingManager
  20. dynRoutingMgr *dynamicrouting.Manager
  21. mux *http.ServeMux
  22. fsHandler http.Handler
  23. }
  24. func NewServer(netMgr *network.Manager, persistMgr *network.PersistenceManager, metaMgr *network.MetadataManager, vpnMgr *vpn.Manager, fwMgr *firewall.FirewallManager, routingMgr *routing.RoutingManager, dynRoutingMgr *dynamicrouting.Manager, fsHandler http.Handler) *Server {
  25. s := &Server{
  26. netMgr: netMgr,
  27. persistMgr: persistMgr,
  28. metaMgr: metaMgr,
  29. vpnMgr: vpnMgr,
  30. fwMgr: fwMgr,
  31. routingMgr: routingMgr,
  32. dynRoutingMgr: dynRoutingMgr,
  33. mux: http.NewServeMux(),
  34. fsHandler: fsHandler,
  35. }
  36. s.registerRoutes()
  37. return s
  38. }
  39. func (s *Server) registerRoutes() {
  40. // API Endpoints
  41. s.mux.HandleFunc("GET /api/system/status", s.handleSystemStatus)
  42. // Routing & Policy Rules (/ip route & /ip route rule)
  43. s.mux.HandleFunc("GET /api/routes", s.handleListRoutes)
  44. s.mux.HandleFunc("POST /api/routes", s.handleSaveRoute)
  45. s.mux.HandleFunc("DELETE /api/routes/{id}", s.handleDeleteRoute)
  46. s.mux.HandleFunc("POST /api/routes/{id}/state", s.handleSetRouteState)
  47. s.mux.HandleFunc("GET /api/routing/rules", s.handleListPolicyRules)
  48. s.mux.HandleFunc("POST /api/routing/rules", s.handleSavePolicyRule)
  49. s.mux.HandleFunc("DELETE /api/routing/rules/{id}", s.handleDeletePolicyRule)
  50. s.mux.HandleFunc("GET /api/routing/tables", s.handleListRoutingTables)
  51. s.mux.HandleFunc("POST /api/routing/tables", s.handleSaveRoutingTable)
  52. // Firewall Endpoints (/ip firewall)
  53. s.mux.HandleFunc("GET /api/firewall/summary", s.handleFirewallSummary)
  54. s.mux.HandleFunc("POST /api/firewall/reset-counters", s.handleResetFirewallCounters)
  55. // Filter Rules
  56. s.mux.HandleFunc("GET /api/firewall/filter", s.handleListFilterRules)
  57. s.mux.HandleFunc("POST /api/firewall/filter", s.handleSaveFilterRule)
  58. s.mux.HandleFunc("DELETE /api/firewall/filter/{id}", s.handleDeleteFilterRule)
  59. s.mux.HandleFunc("POST /api/firewall/filter/{id}/move", s.handleMoveFilterRule)
  60. s.mux.HandleFunc("POST /api/firewall/filter/{id}/state", s.handleSetFilterRuleState)
  61. // NAT Rules
  62. s.mux.HandleFunc("GET /api/firewall/nat", s.handleListNATRules)
  63. s.mux.HandleFunc("POST /api/firewall/nat", s.handleSaveNATRule)
  64. s.mux.HandleFunc("DELETE /api/firewall/nat/{id}", s.handleDeleteNATRule)
  65. s.mux.HandleFunc("POST /api/firewall/nat/{id}/move", s.handleMoveNATRule)
  66. s.mux.HandleFunc("POST /api/firewall/nat/{id}/state", s.handleSetNATRuleState)
  67. // Mangle Rules
  68. s.mux.HandleFunc("GET /api/firewall/mangle", s.handleListMangleRules)
  69. s.mux.HandleFunc("POST /api/firewall/mangle", s.handleSaveMangleRule)
  70. s.mux.HandleFunc("DELETE /api/firewall/mangle/{id}", s.handleDeleteMangleRule)
  71. s.mux.HandleFunc("POST /api/firewall/mangle/{id}/move", s.handleMoveMangleRule)
  72. s.mux.HandleFunc("POST /api/firewall/mangle/{id}/state", s.handleSetMangleRuleState)
  73. // Address Lists
  74. s.mux.HandleFunc("GET /api/firewall/address-lists", s.handleListAddressLists)
  75. s.mux.HandleFunc("POST /api/firewall/address-lists", s.handleSaveAddressListEntry)
  76. s.mux.HandleFunc("DELETE /api/firewall/address-lists/{id}", s.handleDeleteAddressListEntry)
  77. // Active Connections
  78. s.mux.HandleFunc("GET /api/firewall/connections", s.handleListConnections)
  79. s.mux.HandleFunc("POST /api/firewall/connections/kill", s.handleKillConnection)
  80. s.mux.HandleFunc("GET /api/interfaces", s.handleListInterfaces)
  81. s.mux.HandleFunc("GET /api/interfaces/{name}", s.handleGetInterface)
  82. s.mux.HandleFunc("POST /api/interfaces/{name}/state", s.handleSetInterfaceState)
  83. s.mux.HandleFunc("POST /api/interfaces/{name}/mtu", s.handleSetInterfaceMTU)
  84. s.mux.HandleFunc("POST /api/interfaces/{name}/ip", s.handleAddInterfaceIP)
  85. s.mux.HandleFunc("DELETE /api/interfaces/{name}/ip", s.handleDeleteInterfaceIP)
  86. s.mux.HandleFunc("POST /api/interfaces/{name}/metadata", s.handleSetInterfaceMetadata)
  87. s.mux.HandleFunc("GET /api/vlans", s.handleListVLANs)
  88. s.mux.HandleFunc("POST /api/vlans", s.handleCreateVLAN)
  89. s.mux.HandleFunc("DELETE /api/vlans/{name}", s.handleDeleteVLAN)
  90. s.mux.HandleFunc("GET /api/debian/config", s.handleGetDebianConfig)
  91. s.mux.HandleFunc("POST /api/debian/apply", s.handleSaveDebianConfig)
  92. // VPN & WAN Endpoints
  93. s.mux.HandleFunc("GET /api/vpn/status", s.handleOverallVPNStatus)
  94. // PPPoE Multi-Instance
  95. s.mux.HandleFunc("GET /api/vpn/pppoe/clients", s.handleListPPPoE)
  96. s.mux.HandleFunc("POST /api/vpn/pppoe/clients", s.handleSavePPPoE)
  97. s.mux.HandleFunc("DELETE /api/vpn/pppoe/clients/{name}", s.handleDeletePPPoE)
  98. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/state", s.handleSetPPPoEState)
  99. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/connect", s.handleConnectPPPoE)
  100. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/disconnect", s.handleDisconnectPPPoE)
  101. // WireGuard Multi-Interface & Multi-Peer
  102. s.mux.HandleFunc("GET /api/vpn/wireguard/interfaces", s.handleListWireGuardInterfaces)
  103. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces", s.handleSaveWireGuardInterface)
  104. s.mux.HandleFunc("DELETE /api/vpn/wireguard/interfaces/{name}", s.handleDeleteWireGuardInterface)
  105. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/state", s.handleSetWireGuardInterfaceState)
  106. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/start", s.handleStartWireGuardInterface)
  107. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/stop", s.handleStopWireGuardInterface)
  108. s.mux.HandleFunc("GET /api/vpn/wireguard/peers", s.handleListWireGuardPeers)
  109. s.mux.HandleFunc("POST /api/vpn/wireguard/peers", s.handleSaveWireGuardPeer)
  110. s.mux.HandleFunc("DELETE /api/vpn/wireguard/peers/{iface}/{pubkey}", s.handleDeleteWireGuardPeer)
  111. s.mux.HandleFunc("POST /api/vpn/wireguard/genkeys", s.handleWireGuardGenKeys)
  112. // PPTP Multi-Instance
  113. s.mux.HandleFunc("GET /api/vpn/pptp/clients", s.handleListPPTP)
  114. s.mux.HandleFunc("POST /api/vpn/pptp/clients", s.handleSavePPTP)
  115. s.mux.HandleFunc("DELETE /api/vpn/pptp/clients/{name}", s.handleDeletePPTP)
  116. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/state", s.handleSetPPTPState)
  117. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/connect", s.handleConnectPPTP)
  118. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/disconnect", s.handleDisconnectPPTP)
  119. // L2TP Multi-Instance
  120. s.mux.HandleFunc("GET /api/vpn/l2tp/clients", s.handleListL2TP)
  121. s.mux.HandleFunc("POST /api/vpn/l2tp/clients", s.handleSaveL2TP)
  122. s.mux.HandleFunc("DELETE /api/vpn/l2tp/clients/{name}", s.handleDeleteL2TP)
  123. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/state", s.handleSetL2TPState)
  124. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/connect", s.handleConnectL2TP)
  125. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/disconnect", s.handleDisconnectL2TP)
  126. // IPSec Multi-Tunnel
  127. s.mux.HandleFunc("GET /api/vpn/ipsec/tunnels", s.handleListIPSec)
  128. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels", s.handleSaveIPSecTunnel)
  129. s.mux.HandleFunc("DELETE /api/vpn/ipsec/tunnels/{name}", s.handleDeleteIPSecTunnel)
  130. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/state", s.handleSetIPSecState)
  131. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/start", s.handleInitiateIPSecTunnel)
  132. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/stop", s.handleTerminateIPSecTunnel)
  133. // EoIP (Ethernet over IP / GRETAP)
  134. s.mux.HandleFunc("GET /api/vpn/eoip", s.handleListEoIP)
  135. s.mux.HandleFunc("POST /api/vpn/eoip", s.handleSaveEoIP)
  136. s.mux.HandleFunc("DELETE /api/vpn/eoip/{id}", s.handleDeleteEoIP)
  137. s.mux.HandleFunc("POST /api/vpn/eoip/{id}/state", s.handleSetEoIPState)
  138. // PPP Profiles (/ppp profile)
  139. s.mux.HandleFunc("GET /api/vpn/ppp/profiles", s.handleListPPPProfiles)
  140. s.mux.HandleFunc("POST /api/vpn/ppp/profiles", s.handleSavePPPProfile)
  141. s.mux.HandleFunc("DELETE /api/vpn/ppp/profiles/{name}", s.handleDeletePPPProfile)
  142. // PPP Secrets (/ppp secret)
  143. s.mux.HandleFunc("GET /api/vpn/ppp/secrets", s.handleListPPPSecrets)
  144. s.mux.HandleFunc("POST /api/vpn/ppp/secrets", s.handleSavePPPSecret)
  145. s.mux.HandleFunc("DELETE /api/vpn/ppp/secrets/{id}", s.handleDeletePPPSecret)
  146. s.mux.HandleFunc("POST /api/vpn/ppp/secrets/{id}/state", s.handleSetPPPSecretState)
  147. // Active PPP Sessions (/ppp active)
  148. s.mux.HandleFunc("GET /api/vpn/ppp/active", s.handleListPPPSessions)
  149. s.mux.HandleFunc("DELETE /api/vpn/ppp/active/{iface}", s.handleDisconnectPPPSession)
  150. // VPN Server Settings
  151. s.mux.HandleFunc("GET /api/vpn/servers/pppoe", s.handleGetPPPoEServer)
  152. s.mux.HandleFunc("POST /api/vpn/servers/pppoe", s.handleSetPPPoEServer)
  153. s.mux.HandleFunc("GET /api/vpn/servers/pptp", s.handleGetPPTPServer)
  154. s.mux.HandleFunc("POST /api/vpn/servers/pptp", s.handleSetPPTPServer)
  155. s.mux.HandleFunc("GET /api/vpn/servers/l2tp", s.handleGetL2TPServer)
  156. s.mux.HandleFunc("POST /api/vpn/servers/l2tp", s.handleSetL2TPServer)
  157. s.mux.HandleFunc("GET /api/vpn/servers/ipsec", s.handleGetIPSecServer)
  158. s.mux.HandleFunc("POST /api/vpn/servers/ipsec", s.handleSetIPSecServer)
  159. // WireGuard Client Config Generator
  160. s.mux.HandleFunc("POST /api/vpn/wireguard/client-config", s.handleGenerateWGClientConfig)
  161. // Dynamic Routing (BGP & OSPF)
  162. s.mux.HandleFunc("GET /api/routing/dynamic/summary", s.handleDynamicRoutingSummary)
  163. // BGP
  164. s.mux.HandleFunc("GET /api/routing/bgp/instance", s.handleGetBGPInstance)
  165. s.mux.HandleFunc("POST /api/routing/bgp/instance", s.handleUpdateBGPInstance)
  166. s.mux.HandleFunc("GET /api/routing/bgp/peers", s.handleListBGPPeers)
  167. s.mux.HandleFunc("POST /api/routing/bgp/peers", s.handleSaveBGPPeer)
  168. s.mux.HandleFunc("DELETE /api/routing/bgp/peers/{id}", s.handleDeleteBGPPeer)
  169. s.mux.HandleFunc("POST /api/routing/bgp/peers/{id}/state", s.handleSetBGPPeerState)
  170. s.mux.HandleFunc("GET /api/routing/bgp/networks", s.handleListBGPNetworks)
  171. s.mux.HandleFunc("POST /api/routing/bgp/networks", s.handleSaveBGPNetwork)
  172. s.mux.HandleFunc("DELETE /api/routing/bgp/networks/{id}", s.handleDeleteBGPNetwork)
  173. s.mux.HandleFunc("GET /api/routing/bgp/routes", s.handleGetBGPRoutes)
  174. // OSPF
  175. s.mux.HandleFunc("GET /api/routing/ospf/instance", s.handleGetOSPFInstance)
  176. s.mux.HandleFunc("POST /api/routing/ospf/instance", s.handleUpdateOSPFInstance)
  177. s.mux.HandleFunc("GET /api/routing/ospf/areas", s.handleListOSPFAreas)
  178. s.mux.HandleFunc("POST /api/routing/ospf/areas", s.handleSaveOSPFArea)
  179. s.mux.HandleFunc("DELETE /api/routing/ospf/areas/{id}", s.handleDeleteOSPFArea)
  180. s.mux.HandleFunc("GET /api/routing/ospf/interfaces", s.handleListOSPFInterfaces)
  181. s.mux.HandleFunc("POST /api/routing/ospf/interfaces", s.handleSaveOSPFInterface)
  182. s.mux.HandleFunc("DELETE /api/routing/ospf/interfaces/{id}", s.handleDeleteOSPFInterface)
  183. s.mux.HandleFunc("POST /api/routing/ospf/interfaces/{id}/state", s.handleSetOSPFInterfaceState)
  184. s.mux.HandleFunc("GET /api/routing/ospf/neighbors", s.handleGetOSPFNeighbors)
  185. s.mux.HandleFunc("GET /api/routing/ospf/routes", s.handleGetOSPFRoutes)
  186. // Web UI Static Files
  187. if s.fsHandler != nil {
  188. s.mux.Handle("/", s.fsHandler)
  189. }
  190. }
  191. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  192. // Enable CORS for local development/testing
  193. w.Header().Set("Access-Control-Allow-Origin", "*")
  194. w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, PUT")
  195. w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
  196. if r.Method == http.MethodOptions {
  197. w.WriteHeader(http.StatusOK)
  198. return
  199. }
  200. s.mux.ServeHTTP(w, r)
  201. }
  202. func (s *Server) handleSystemStatus(w http.ResponseWriter, r *http.Request) {
  203. status, err := system.GetStatus()
  204. if err != nil {
  205. writeError(w, http.StatusInternalServerError, err.Error())
  206. return
  207. }
  208. ifaces, err := s.netMgr.ListInterfaces()
  209. if err == nil {
  210. status.TotalIfaces = len(ifaces)
  211. for _, iface := range ifaces {
  212. if iface.State == "UP" {
  213. status.UpIfaces++
  214. } else {
  215. status.DownIfaces++
  216. }
  217. if iface.IsVLAN {
  218. status.VlanIfaces++
  219. }
  220. }
  221. }
  222. writeJSON(w, http.StatusOK, status)
  223. }
  224. func (s *Server) handleRoutes(w http.ResponseWriter, r *http.Request) {
  225. routes, err := system.GetRoutingTable()
  226. if err != nil {
  227. writeError(w, http.StatusInternalServerError, err.Error())
  228. return
  229. }
  230. writeJSON(w, http.StatusOK, routes)
  231. }
  232. func (s *Server) handleListInterfaces(w http.ResponseWriter, r *http.Request) {
  233. ifaces, err := s.netMgr.ListInterfaces()
  234. if err != nil {
  235. writeError(w, http.StatusInternalServerError, err.Error())
  236. return
  237. }
  238. writeJSON(w, http.StatusOK, ifaces)
  239. }
  240. func (s *Server) handleGetInterface(w http.ResponseWriter, r *http.Request) {
  241. name := r.PathValue("name")
  242. iface, err := s.netMgr.GetInterface(name)
  243. if err != nil {
  244. writeError(w, http.StatusNotFound, err.Error())
  245. return
  246. }
  247. writeJSON(w, http.StatusOK, iface)
  248. }
  249. func (s *Server) handleSetInterfaceState(w http.ResponseWriter, r *http.Request) {
  250. name := r.PathValue("name")
  251. var req struct {
  252. State string `json:"state"` // "up" or "down"
  253. }
  254. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  255. writeError(w, http.StatusBadRequest, "invalid request body")
  256. return
  257. }
  258. up := strings.ToLower(req.State) == "up"
  259. if err := s.netMgr.SetInterfaceState(name, up); err != nil {
  260. writeError(w, http.StatusInternalServerError, err.Error())
  261. return
  262. }
  263. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "state": req.State})
  264. }
  265. func (s *Server) handleSetInterfaceMTU(w http.ResponseWriter, r *http.Request) {
  266. name := r.PathValue("name")
  267. var req struct {
  268. MTU int `json:"mtu"`
  269. }
  270. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  271. writeError(w, http.StatusBadRequest, "invalid request body")
  272. return
  273. }
  274. if err := s.netMgr.SetInterfaceMTU(name, req.MTU); err != nil {
  275. writeError(w, http.StatusInternalServerError, err.Error())
  276. return
  277. }
  278. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "mtu": req.MTU})
  279. }
  280. func (s *Server) handleAddInterfaceIP(w http.ResponseWriter, r *http.Request) {
  281. name := r.PathValue("name")
  282. var req network.AddIPRequest
  283. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  284. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  285. return
  286. }
  287. if err := s.netMgr.AddInterfaceIP(name, req.IP); err != nil {
  288. writeError(w, http.StatusInternalServerError, err.Error())
  289. return
  290. }
  291. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  292. }
  293. func (s *Server) handleDeleteInterfaceIP(w http.ResponseWriter, r *http.Request) {
  294. name := r.PathValue("name")
  295. var req network.AddIPRequest
  296. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  297. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  298. return
  299. }
  300. if err := s.netMgr.DeleteInterfaceIP(name, req.IP); err != nil {
  301. writeError(w, http.StatusInternalServerError, err.Error())
  302. return
  303. }
  304. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  305. }
  306. func (s *Server) handleSetInterfaceMetadata(w http.ResponseWriter, r *http.Request) {
  307. name := r.PathValue("name")
  308. var req struct {
  309. Role string `json:"role"`
  310. Description string `json:"description"`
  311. }
  312. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  313. writeError(w, http.StatusBadRequest, "invalid request body")
  314. return
  315. }
  316. if err := s.metaMgr.Set(name, req.Role, req.Description); err != nil {
  317. writeError(w, http.StatusInternalServerError, err.Error())
  318. return
  319. }
  320. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "role": req.Role, "description": req.Description})
  321. }
  322. func (s *Server) handleListVLANs(w http.ResponseWriter, r *http.Request) {
  323. vlans, err := s.netMgr.ListVLANs()
  324. if err != nil {
  325. writeError(w, http.StatusInternalServerError, err.Error())
  326. return
  327. }
  328. writeJSON(w, http.StatusOK, vlans)
  329. }
  330. func (s *Server) handleCreateVLAN(w http.ResponseWriter, r *http.Request) {
  331. var req network.CreateVLANRequest
  332. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  333. writeError(w, http.StatusBadRequest, "invalid request body")
  334. return
  335. }
  336. if req.ParentName == "" || req.VlanID <= 0 {
  337. writeError(w, http.StatusBadRequest, "parent_name and valid vlan_id (1-4094) are required")
  338. return
  339. }
  340. iface, err := s.netMgr.CreateVLAN(req)
  341. if err != nil {
  342. writeError(w, http.StatusInternalServerError, err.Error())
  343. return
  344. }
  345. writeJSON(w, http.StatusCreated, iface)
  346. }
  347. func (s *Server) handleDeleteVLAN(w http.ResponseWriter, r *http.Request) {
  348. name := r.PathValue("name")
  349. if err := s.netMgr.DeleteVLAN(name); err != nil {
  350. writeError(w, http.StatusInternalServerError, err.Error())
  351. return
  352. }
  353. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  354. }
  355. func (s *Server) handleGetDebianConfig(w http.ResponseWriter, r *http.Request) {
  356. cfg, err := s.persistMgr.GenerateDebianConfig()
  357. if err != nil {
  358. writeError(w, http.StatusInternalServerError, err.Error())
  359. return
  360. }
  361. writeJSON(w, http.StatusOK, map[string]string{
  362. "path": network.DebianVlansCfgPath,
  363. "content": cfg,
  364. })
  365. }
  366. func (s *Server) handleSaveDebianConfig(w http.ResponseWriter, r *http.Request) {
  367. cfg, err := s.persistMgr.SaveDebianConfig()
  368. if err != nil {
  369. writeError(w, http.StatusInternalServerError, err.Error())
  370. return
  371. }
  372. writeJSON(w, http.StatusOK, map[string]any{
  373. "success": true,
  374. "path": network.DebianVlansCfgPath,
  375. "content": cfg,
  376. "message": "Configuration saved to " + network.DebianVlansCfgPath,
  377. })
  378. }
  379. func writeJSON(w http.ResponseWriter, status int, data any) {
  380. w.Header().Set("Content-Type", "application/json")
  381. w.WriteHeader(status)
  382. _ = json.NewEncoder(w).Encode(data)
  383. }
  384. func writeError(w http.ResponseWriter, status int, message string) {
  385. writeJSON(w, status, map[string]any{
  386. "error": true,
  387. "message": message,
  388. })
  389. }