api.go 31 KB

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