api.go 29 KB

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