api.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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. s.mux.HandleFunc("POST /api/firewall/address-lists/{id}/state", s.handleSetAddressListEntryState)
  78. // Geo-Location
  79. s.mux.HandleFunc("GET /api/firewall/geoip/status", s.handleGeoIPStatus)
  80. s.mux.HandleFunc("POST /api/firewall/geoip/config", s.handleGeoIPConfig)
  81. s.mux.HandleFunc("GET /api/firewall/geoip/countries", s.handleListGeoCountries)
  82. s.mux.HandleFunc("POST /api/firewall/geoip/countries/{code}/toggle", s.handleToggleGeoCountry)
  83. s.mux.HandleFunc("POST /api/firewall/geoip/countries/{code}/update", s.handleUpdateGeoCountry)
  84. s.mux.HandleFunc("POST /api/firewall/geoip/update-now", s.handleGeoIPUpdateNow)
  85. s.mux.HandleFunc("POST /api/firewall/geoip/batch-toggle", s.handleGeoIPBatchToggle)
  86. s.mux.HandleFunc("GET /api/firewall/geoip/countries/{code}/subnets", s.handleGeoCountrySubnets)
  87. // Active Connections
  88. s.mux.HandleFunc("GET /api/firewall/connections", s.handleListConnections)
  89. s.mux.HandleFunc("POST /api/firewall/connections/kill", s.handleKillConnection)
  90. s.mux.HandleFunc("GET /api/interfaces", s.handleListInterfaces)
  91. s.mux.HandleFunc("GET /api/interfaces/{name}", s.handleGetInterface)
  92. s.mux.HandleFunc("POST /api/interfaces/{name}/state", s.handleSetInterfaceState)
  93. s.mux.HandleFunc("POST /api/interfaces/{name}/mtu", s.handleSetInterfaceMTU)
  94. s.mux.HandleFunc("POST /api/interfaces/{name}/ip", s.handleAddInterfaceIP)
  95. s.mux.HandleFunc("DELETE /api/interfaces/{name}/ip", s.handleDeleteInterfaceIP)
  96. s.mux.HandleFunc("POST /api/interfaces/{name}/metadata", s.handleSetInterfaceMetadata)
  97. s.mux.HandleFunc("GET /api/vlans", s.handleListVLANs)
  98. s.mux.HandleFunc("POST /api/vlans", s.handleCreateVLAN)
  99. s.mux.HandleFunc("DELETE /api/vlans/{name}", s.handleDeleteVLAN)
  100. s.mux.HandleFunc("GET /api/debian/config", s.handleGetDebianConfig)
  101. s.mux.HandleFunc("POST /api/debian/apply", s.handleSaveDebianConfig)
  102. // VPN & WAN Endpoints
  103. s.mux.HandleFunc("GET /api/vpn/status", s.handleOverallVPNStatus)
  104. // PPPoE Multi-Instance
  105. s.mux.HandleFunc("GET /api/vpn/pppoe/clients", s.handleListPPPoE)
  106. s.mux.HandleFunc("POST /api/vpn/pppoe/clients", s.handleSavePPPoE)
  107. s.mux.HandleFunc("DELETE /api/vpn/pppoe/clients/{name}", s.handleDeletePPPoE)
  108. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/state", s.handleSetPPPoEState)
  109. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/connect", s.handleConnectPPPoE)
  110. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/disconnect", s.handleDisconnectPPPoE)
  111. // WireGuard Multi-Interface & Multi-Peer
  112. s.mux.HandleFunc("GET /api/vpn/wireguard/interfaces", s.handleListWireGuardInterfaces)
  113. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces", s.handleSaveWireGuardInterface)
  114. s.mux.HandleFunc("DELETE /api/vpn/wireguard/interfaces/{name}", s.handleDeleteWireGuardInterface)
  115. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/state", s.handleSetWireGuardInterfaceState)
  116. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/start", s.handleStartWireGuardInterface)
  117. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/stop", s.handleStopWireGuardInterface)
  118. s.mux.HandleFunc("GET /api/vpn/wireguard/peers", s.handleListWireGuardPeers)
  119. s.mux.HandleFunc("POST /api/vpn/wireguard/peers", s.handleSaveWireGuardPeer)
  120. s.mux.HandleFunc("DELETE /api/vpn/wireguard/peers/{iface}/{pubkey}", s.handleDeleteWireGuardPeer)
  121. s.mux.HandleFunc("POST /api/vpn/wireguard/genkeys", s.handleWireGuardGenKeys)
  122. // PPTP Multi-Instance
  123. s.mux.HandleFunc("GET /api/vpn/pptp/clients", s.handleListPPTP)
  124. s.mux.HandleFunc("POST /api/vpn/pptp/clients", s.handleSavePPTP)
  125. s.mux.HandleFunc("DELETE /api/vpn/pptp/clients/{name}", s.handleDeletePPTP)
  126. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/state", s.handleSetPPTPState)
  127. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/connect", s.handleConnectPPTP)
  128. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/disconnect", s.handleDisconnectPPTP)
  129. // L2TP Multi-Instance
  130. s.mux.HandleFunc("GET /api/vpn/l2tp/clients", s.handleListL2TP)
  131. s.mux.HandleFunc("POST /api/vpn/l2tp/clients", s.handleSaveL2TP)
  132. s.mux.HandleFunc("DELETE /api/vpn/l2tp/clients/{name}", s.handleDeleteL2TP)
  133. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/state", s.handleSetL2TPState)
  134. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/connect", s.handleConnectL2TP)
  135. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/disconnect", s.handleDisconnectL2TP)
  136. // IPSec Multi-Tunnel
  137. s.mux.HandleFunc("GET /api/vpn/ipsec/tunnels", s.handleListIPSec)
  138. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels", s.handleSaveIPSecTunnel)
  139. s.mux.HandleFunc("DELETE /api/vpn/ipsec/tunnels/{name}", s.handleDeleteIPSecTunnel)
  140. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/state", s.handleSetIPSecState)
  141. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/start", s.handleInitiateIPSecTunnel)
  142. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/stop", s.handleTerminateIPSecTunnel)
  143. // EoIP (Ethernet over IP / GRETAP)
  144. s.mux.HandleFunc("GET /api/vpn/eoip", s.handleListEoIP)
  145. s.mux.HandleFunc("POST /api/vpn/eoip", s.handleSaveEoIP)
  146. s.mux.HandleFunc("DELETE /api/vpn/eoip/{id}", s.handleDeleteEoIP)
  147. s.mux.HandleFunc("POST /api/vpn/eoip/{id}/state", s.handleSetEoIPState)
  148. // PPP Profiles (/ppp profile)
  149. s.mux.HandleFunc("GET /api/vpn/ppp/profiles", s.handleListPPPProfiles)
  150. s.mux.HandleFunc("POST /api/vpn/ppp/profiles", s.handleSavePPPProfile)
  151. s.mux.HandleFunc("DELETE /api/vpn/ppp/profiles/{name}", s.handleDeletePPPProfile)
  152. // PPP Secrets (/ppp secret)
  153. s.mux.HandleFunc("GET /api/vpn/ppp/secrets", s.handleListPPPSecrets)
  154. s.mux.HandleFunc("POST /api/vpn/ppp/secrets", s.handleSavePPPSecret)
  155. s.mux.HandleFunc("DELETE /api/vpn/ppp/secrets/{id}", s.handleDeletePPPSecret)
  156. s.mux.HandleFunc("POST /api/vpn/ppp/secrets/{id}/state", s.handleSetPPPSecretState)
  157. // Active PPP Sessions (/ppp active)
  158. s.mux.HandleFunc("GET /api/vpn/ppp/active", s.handleListPPPSessions)
  159. s.mux.HandleFunc("DELETE /api/vpn/ppp/active/{iface}", s.handleDisconnectPPPSession)
  160. // VPN Server Settings
  161. s.mux.HandleFunc("GET /api/vpn/servers/pppoe", s.handleGetPPPoEServer)
  162. s.mux.HandleFunc("POST /api/vpn/servers/pppoe", s.handleSetPPPoEServer)
  163. s.mux.HandleFunc("GET /api/vpn/servers/pptp", s.handleGetPPTPServer)
  164. s.mux.HandleFunc("POST /api/vpn/servers/pptp", s.handleSetPPTPServer)
  165. s.mux.HandleFunc("GET /api/vpn/servers/l2tp", s.handleGetL2TPServer)
  166. s.mux.HandleFunc("POST /api/vpn/servers/l2tp", s.handleSetL2TPServer)
  167. s.mux.HandleFunc("GET /api/vpn/servers/ipsec", s.handleGetIPSecServer)
  168. s.mux.HandleFunc("POST /api/vpn/servers/ipsec", s.handleSetIPSecServer)
  169. // WireGuard Client Config Generator
  170. s.mux.HandleFunc("POST /api/vpn/wireguard/client-config", s.handleGenerateWGClientConfig)
  171. // Dynamic Routing (BGP & OSPF)
  172. s.mux.HandleFunc("GET /api/routing/dynamic/summary", s.handleDynamicRoutingSummary)
  173. // BGP
  174. s.mux.HandleFunc("GET /api/routing/bgp/instance", s.handleGetBGPInstance)
  175. s.mux.HandleFunc("POST /api/routing/bgp/instance", s.handleUpdateBGPInstance)
  176. s.mux.HandleFunc("GET /api/routing/bgp/peers", s.handleListBGPPeers)
  177. s.mux.HandleFunc("POST /api/routing/bgp/peers", s.handleSaveBGPPeer)
  178. s.mux.HandleFunc("DELETE /api/routing/bgp/peers/{id}", s.handleDeleteBGPPeer)
  179. s.mux.HandleFunc("POST /api/routing/bgp/peers/{id}/state", s.handleSetBGPPeerState)
  180. s.mux.HandleFunc("GET /api/routing/bgp/networks", s.handleListBGPNetworks)
  181. s.mux.HandleFunc("POST /api/routing/bgp/networks", s.handleSaveBGPNetwork)
  182. s.mux.HandleFunc("DELETE /api/routing/bgp/networks/{id}", s.handleDeleteBGPNetwork)
  183. s.mux.HandleFunc("GET /api/routing/bgp/routes", s.handleGetBGPRoutes)
  184. // OSPF
  185. s.mux.HandleFunc("GET /api/routing/ospf/instance", s.handleGetOSPFInstance)
  186. s.mux.HandleFunc("POST /api/routing/ospf/instance", s.handleUpdateOSPFInstance)
  187. s.mux.HandleFunc("GET /api/routing/ospf/areas", s.handleListOSPFAreas)
  188. s.mux.HandleFunc("POST /api/routing/ospf/areas", s.handleSaveOSPFArea)
  189. s.mux.HandleFunc("DELETE /api/routing/ospf/areas/{id}", s.handleDeleteOSPFArea)
  190. s.mux.HandleFunc("GET /api/routing/ospf/interfaces", s.handleListOSPFInterfaces)
  191. s.mux.HandleFunc("POST /api/routing/ospf/interfaces", s.handleSaveOSPFInterface)
  192. s.mux.HandleFunc("DELETE /api/routing/ospf/interfaces/{id}", s.handleDeleteOSPFInterface)
  193. s.mux.HandleFunc("POST /api/routing/ospf/interfaces/{id}/state", s.handleSetOSPFInterfaceState)
  194. s.mux.HandleFunc("GET /api/routing/ospf/neighbors", s.handleGetOSPFNeighbors)
  195. s.mux.HandleFunc("GET /api/routing/ospf/routes", s.handleGetOSPFRoutes)
  196. // Web UI Static Files
  197. if s.fsHandler != nil {
  198. s.mux.Handle("/", s.fsHandler)
  199. }
  200. }
  201. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  202. // Enable CORS for local development/testing
  203. w.Header().Set("Access-Control-Allow-Origin", "*")
  204. w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, PUT")
  205. w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
  206. if r.Method == http.MethodOptions {
  207. w.WriteHeader(http.StatusOK)
  208. return
  209. }
  210. s.mux.ServeHTTP(w, r)
  211. }
  212. func (s *Server) handleSystemStatus(w http.ResponseWriter, r *http.Request) {
  213. status, err := system.GetStatus()
  214. if err != nil {
  215. writeError(w, http.StatusInternalServerError, err.Error())
  216. return
  217. }
  218. ifaces, err := s.netMgr.ListInterfaces()
  219. if err == nil {
  220. status.TotalIfaces = len(ifaces)
  221. for _, iface := range ifaces {
  222. if iface.State == "UP" {
  223. status.UpIfaces++
  224. } else {
  225. status.DownIfaces++
  226. }
  227. if iface.IsVLAN {
  228. status.VlanIfaces++
  229. }
  230. }
  231. }
  232. writeJSON(w, http.StatusOK, status)
  233. }
  234. func (s *Server) handleRoutes(w http.ResponseWriter, r *http.Request) {
  235. routes, err := system.GetRoutingTable()
  236. if err != nil {
  237. writeError(w, http.StatusInternalServerError, err.Error())
  238. return
  239. }
  240. writeJSON(w, http.StatusOK, routes)
  241. }
  242. func (s *Server) handleListInterfaces(w http.ResponseWriter, r *http.Request) {
  243. ifaces, err := s.netMgr.ListInterfaces()
  244. if err != nil {
  245. writeError(w, http.StatusInternalServerError, err.Error())
  246. return
  247. }
  248. writeJSON(w, http.StatusOK, ifaces)
  249. }
  250. func (s *Server) handleGetInterface(w http.ResponseWriter, r *http.Request) {
  251. name := r.PathValue("name")
  252. iface, err := s.netMgr.GetInterface(name)
  253. if err != nil {
  254. writeError(w, http.StatusNotFound, err.Error())
  255. return
  256. }
  257. writeJSON(w, http.StatusOK, iface)
  258. }
  259. func (s *Server) handleSetInterfaceState(w http.ResponseWriter, r *http.Request) {
  260. name := r.PathValue("name")
  261. var req struct {
  262. State string `json:"state"` // "up" or "down"
  263. }
  264. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  265. writeError(w, http.StatusBadRequest, "invalid request body")
  266. return
  267. }
  268. up := strings.ToLower(req.State) == "up"
  269. if err := s.netMgr.SetInterfaceState(name, up); err != nil {
  270. writeError(w, http.StatusInternalServerError, err.Error())
  271. return
  272. }
  273. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "state": req.State})
  274. }
  275. func (s *Server) handleSetInterfaceMTU(w http.ResponseWriter, r *http.Request) {
  276. name := r.PathValue("name")
  277. var req struct {
  278. MTU int `json:"mtu"`
  279. }
  280. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  281. writeError(w, http.StatusBadRequest, "invalid request body")
  282. return
  283. }
  284. if err := s.netMgr.SetInterfaceMTU(name, req.MTU); err != nil {
  285. writeError(w, http.StatusInternalServerError, err.Error())
  286. return
  287. }
  288. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "mtu": req.MTU})
  289. }
  290. func (s *Server) handleAddInterfaceIP(w http.ResponseWriter, r *http.Request) {
  291. name := r.PathValue("name")
  292. var req network.AddIPRequest
  293. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  294. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  295. return
  296. }
  297. if err := s.netMgr.AddInterfaceIP(name, req.IP); err != nil {
  298. writeError(w, http.StatusInternalServerError, err.Error())
  299. return
  300. }
  301. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  302. }
  303. func (s *Server) handleDeleteInterfaceIP(w http.ResponseWriter, r *http.Request) {
  304. name := r.PathValue("name")
  305. var req network.AddIPRequest
  306. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  307. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  308. return
  309. }
  310. if err := s.netMgr.DeleteInterfaceIP(name, req.IP); err != nil {
  311. writeError(w, http.StatusInternalServerError, err.Error())
  312. return
  313. }
  314. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  315. }
  316. func (s *Server) handleSetInterfaceMetadata(w http.ResponseWriter, r *http.Request) {
  317. name := r.PathValue("name")
  318. var req struct {
  319. Role string `json:"role"`
  320. Description string `json:"description"`
  321. }
  322. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  323. writeError(w, http.StatusBadRequest, "invalid request body")
  324. return
  325. }
  326. if err := s.metaMgr.Set(name, req.Role, req.Description); err != nil {
  327. writeError(w, http.StatusInternalServerError, err.Error())
  328. return
  329. }
  330. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "role": req.Role, "description": req.Description})
  331. }
  332. func (s *Server) handleListVLANs(w http.ResponseWriter, r *http.Request) {
  333. vlans, err := s.netMgr.ListVLANs()
  334. if err != nil {
  335. writeError(w, http.StatusInternalServerError, err.Error())
  336. return
  337. }
  338. writeJSON(w, http.StatusOK, vlans)
  339. }
  340. func (s *Server) handleCreateVLAN(w http.ResponseWriter, r *http.Request) {
  341. var req network.CreateVLANRequest
  342. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  343. writeError(w, http.StatusBadRequest, "invalid request body")
  344. return
  345. }
  346. if req.ParentName == "" || req.VlanID <= 0 {
  347. writeError(w, http.StatusBadRequest, "parent_name and valid vlan_id (1-4094) are required")
  348. return
  349. }
  350. iface, err := s.netMgr.CreateVLAN(req)
  351. if err != nil {
  352. writeError(w, http.StatusInternalServerError, err.Error())
  353. return
  354. }
  355. writeJSON(w, http.StatusCreated, iface)
  356. }
  357. func (s *Server) handleDeleteVLAN(w http.ResponseWriter, r *http.Request) {
  358. name := r.PathValue("name")
  359. if err := s.netMgr.DeleteVLAN(name); err != nil {
  360. writeError(w, http.StatusInternalServerError, err.Error())
  361. return
  362. }
  363. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  364. }
  365. func (s *Server) handleGetDebianConfig(w http.ResponseWriter, r *http.Request) {
  366. cfg, err := s.persistMgr.GenerateDebianConfig()
  367. if err != nil {
  368. writeError(w, http.StatusInternalServerError, err.Error())
  369. return
  370. }
  371. writeJSON(w, http.StatusOK, map[string]string{
  372. "path": network.DebianVlansCfgPath,
  373. "content": cfg,
  374. })
  375. }
  376. func (s *Server) handleSaveDebianConfig(w http.ResponseWriter, r *http.Request) {
  377. cfg, err := s.persistMgr.SaveDebianConfig()
  378. if err != nil {
  379. writeError(w, http.StatusInternalServerError, err.Error())
  380. return
  381. }
  382. writeJSON(w, http.StatusOK, map[string]any{
  383. "success": true,
  384. "path": network.DebianVlansCfgPath,
  385. "content": cfg,
  386. "message": "Configuration saved to " + network.DebianVlansCfgPath,
  387. })
  388. }
  389. func writeJSON(w http.ResponseWriter, status int, data any) {
  390. w.Header().Set("Content-Type", "application/json")
  391. w.WriteHeader(status)
  392. _ = json.NewEncoder(w).Encode(data)
  393. }
  394. func writeError(w http.ResponseWriter, status int, message string) {
  395. writeJSON(w, status, map[string]any{
  396. "error": true,
  397. "message": message,
  398. })
  399. }