api.go 22 KB

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