api.go 33 KB

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