api.go 21 KB

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