api.go 27 KB

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