api.go 20 KB

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