api.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "sync"
  9. "time"
  10. "debtik/internal/auth"
  11. "debtik/internal/backup"
  12. "debtik/internal/bridge"
  13. "debtik/internal/bwtest"
  14. "debtik/internal/certs"
  15. "debtik/internal/dhcp"
  16. "debtik/internal/dns"
  17. "debtik/internal/dynamicrouting"
  18. "debtik/internal/firewall"
  19. "debtik/internal/network"
  20. "debtik/internal/reverseproxy"
  21. "debtik/internal/routing"
  22. "debtik/internal/sdwan"
  23. "debtik/internal/services"
  24. "debtik/internal/system"
  25. "debtik/internal/tools"
  26. "debtik/internal/vpn"
  27. "debtik/internal/vrrp"
  28. )
  29. type Server struct {
  30. aeroDraftMu sync.Mutex
  31. aeroDrafts map[string]aeroIdentityDraft
  32. netMgr *network.Manager
  33. persistMgr *network.PersistenceManager
  34. metaMgr *network.MetadataManager
  35. vpnMgr *vpn.Manager
  36. fwMgr *firewall.FirewallManager
  37. routingMgr *routing.RoutingManager
  38. dynRoutingMgr *dynamicrouting.Manager
  39. bridgeMgr *bridge.BridgeManager
  40. toolsMgr *tools.Manager
  41. dhcpMgr *dhcp.Manager
  42. dhcpServerMgr *dhcp.ServerManager
  43. vrrpMgr *vrrp.Manager
  44. bwtestMgr *bwtest.Manager
  45. dnsMgr *dns.Manager
  46. authMgr *auth.Manager
  47. servicesMgr *services.Manager
  48. certMgr *certs.CertManager
  49. proxyMgr *reverseproxy.Manager
  50. backupMgr *backup.Manager
  51. sdwanMgr *sdwan.Manager
  52. mux *http.ServeMux
  53. fsHandler http.Handler
  54. }
  55. 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, authMgr *auth.Manager, servicesMgr *services.Manager, certMgr *certs.CertManager, proxyMgr *reverseproxy.Manager, backupMgr *backup.Manager, sdwanMgr *sdwan.Manager, fsHandler http.Handler) *Server {
  56. if backupMgr == nil {
  57. backupMgr = backup.NewManager("", "", "", nil)
  58. }
  59. if sdwanMgr == nil {
  60. sdwanMgr = sdwan.NewManager()
  61. }
  62. firewall.SDWANResolver = sdwanMgr.ResolveGroupInterfaces
  63. s := &Server{
  64. netMgr: netMgr,
  65. persistMgr: persistMgr,
  66. metaMgr: metaMgr,
  67. vpnMgr: vpnMgr,
  68. fwMgr: fwMgr,
  69. routingMgr: routingMgr,
  70. dynRoutingMgr: dynRoutingMgr,
  71. bridgeMgr: bridgeMgr,
  72. toolsMgr: toolsMgr,
  73. dhcpMgr: dhcpMgr,
  74. dhcpServerMgr: dhcpServerMgr,
  75. vrrpMgr: vrrpMgr,
  76. bwtestMgr: bwtestMgr,
  77. dnsMgr: dnsMgr,
  78. authMgr: authMgr,
  79. servicesMgr: servicesMgr,
  80. certMgr: certMgr,
  81. proxyMgr: proxyMgr,
  82. backupMgr: backupMgr,
  83. sdwanMgr: sdwanMgr,
  84. mux: http.NewServeMux(),
  85. fsHandler: fsHandler,
  86. }
  87. s.registerRoutes()
  88. return s
  89. }
  90. func (s *Server) registerRoutes() {
  91. s.mux.HandleFunc("GET /api/update/status", s.handleUpdateStatus)
  92. s.mux.HandleFunc("POST /api/update/config", s.handleUpdateConfig)
  93. s.mux.HandleFunc("POST /api/update/check", s.handleUpdateCheck)
  94. s.mux.HandleFunc("POST /api/update/{action}", s.handleUpdateAction)
  95. // Backup & Restore Endpoints
  96. s.mux.HandleFunc("GET /api/system/backup/export", s.handleBackupExport)
  97. s.mux.HandleFunc("GET /api/system/backup/download", s.handleBackupExport)
  98. s.mux.HandleFunc("POST /api/system/backup/inspect", s.handleBackupInspect)
  99. s.mux.HandleFunc("POST /api/system/backup/restore", s.handleBackupRestore)
  100. s.mux.HandleFunc("GET /api/system/backups", s.handleBackupList)
  101. s.mux.HandleFunc("GET /api/system/backups/{filename}", s.handleBackupDownloadLocal)
  102. s.mux.HandleFunc("POST /api/system/backups/{filename}/restore", s.handleBackupRestoreLocal)
  103. // API Endpoints
  104. s.mux.HandleFunc("GET /api/system/status", s.handleSystemStatus)
  105. // Routing & Policy Rules (/ip route & /ip route rule)
  106. s.mux.HandleFunc("GET /api/routes", s.handleListRoutes)
  107. s.mux.HandleFunc("POST /api/routes", s.handleSaveRoute)
  108. s.mux.HandleFunc("DELETE /api/routes/{id}", s.handleDeleteRoute)
  109. s.mux.HandleFunc("POST /api/routes/{id}/state", s.handleSetRouteState)
  110. s.mux.HandleFunc("GET /api/routing/rules", s.handleListPolicyRules)
  111. s.mux.HandleFunc("POST /api/routing/rules", s.handleSavePolicyRule)
  112. s.mux.HandleFunc("DELETE /api/routing/rules/{id}", s.handleDeletePolicyRule)
  113. s.mux.HandleFunc("GET /api/routing/tables", s.handleListRoutingTables)
  114. s.mux.HandleFunc("POST /api/routing/tables", s.handleSaveRoutingTable)
  115. // Firewall Endpoints (/ip firewall)
  116. s.mux.HandleFunc("GET /api/firewall/capabilities", s.handleFirewallCapabilities)
  117. s.mux.HandleFunc("POST /api/firewall/{kind}/{id}/reset-counters", s.handleResetRuleCounters)
  118. s.mux.HandleFunc("GET /api/firewall/summary", s.handleFirewallSummary)
  119. s.mux.HandleFunc("POST /api/firewall/reset-counters", s.handleResetFirewallCounters)
  120. // Filter Rules
  121. s.mux.HandleFunc("GET /api/firewall/filter", s.handleListFilterRules)
  122. s.mux.HandleFunc("POST /api/firewall/filter", s.handleSaveFilterRule)
  123. s.mux.HandleFunc("DELETE /api/firewall/filter/{id}", s.handleDeleteFilterRule)
  124. s.mux.HandleFunc("POST /api/firewall/filter/{id}/move", s.handleMoveFilterRule)
  125. s.mux.HandleFunc("POST /api/firewall/filter/{id}/state", s.handleSetFilterRuleState)
  126. // NAT Rules
  127. s.mux.HandleFunc("GET /api/firewall/nat", s.handleListNATRules)
  128. s.mux.HandleFunc("POST /api/firewall/nat", s.handleSaveNATRule)
  129. s.mux.HandleFunc("DELETE /api/firewall/nat/{id}", s.handleDeleteNATRule)
  130. s.mux.HandleFunc("POST /api/firewall/nat/{id}/move", s.handleMoveNATRule)
  131. s.mux.HandleFunc("POST /api/firewall/nat/{id}/state", s.handleSetNATRuleState)
  132. // Mangle Rules
  133. s.mux.HandleFunc("GET /api/firewall/mangle", s.handleListMangleRules)
  134. s.mux.HandleFunc("POST /api/firewall/mangle", s.handleSaveMangleRule)
  135. s.mux.HandleFunc("DELETE /api/firewall/mangle/{id}", s.handleDeleteMangleRule)
  136. s.mux.HandleFunc("POST /api/firewall/mangle/{id}/move", s.handleMoveMangleRule)
  137. s.mux.HandleFunc("POST /api/firewall/mangle/{id}/state", s.handleSetMangleRuleState)
  138. // Address Lists
  139. s.mux.HandleFunc("GET /api/firewall/address-lists", s.handleListAddressLists)
  140. s.mux.HandleFunc("POST /api/firewall/address-lists", s.handleSaveAddressListEntry)
  141. s.mux.HandleFunc("DELETE /api/firewall/address-lists/{id}", s.handleDeleteAddressListEntry)
  142. s.mux.HandleFunc("POST /api/firewall/address-lists/{id}/state", s.handleSetAddressListEntryState)
  143. // Geo-Location
  144. s.mux.HandleFunc("GET /api/firewall/geoip/status", s.handleGeoIPStatus)
  145. s.mux.HandleFunc("POST /api/firewall/geoip/config", s.handleGeoIPConfig)
  146. s.mux.HandleFunc("GET /api/firewall/geoip/countries", s.handleListGeoCountries)
  147. s.mux.HandleFunc("POST /api/firewall/geoip/countries/{code}/toggle", s.handleToggleGeoCountry)
  148. s.mux.HandleFunc("POST /api/firewall/geoip/countries/{code}/update", s.handleUpdateGeoCountry)
  149. s.mux.HandleFunc("POST /api/firewall/geoip/update-now", s.handleGeoIPUpdateNow)
  150. s.mux.HandleFunc("POST /api/firewall/geoip/batch-toggle", s.handleGeoIPBatchToggle)
  151. s.mux.HandleFunc("GET /api/firewall/geoip/countries/{code}/subnets", s.handleGeoCountrySubnets)
  152. // Active Connections
  153. s.mux.HandleFunc("GET /api/firewall/connections", s.handleListConnections)
  154. s.mux.HandleFunc("POST /api/firewall/connections/kill", s.handleKillConnection)
  155. s.mux.HandleFunc("GET /api/interfaces", s.handleListInterfaces)
  156. s.mux.HandleFunc("GET /api/interfaces/traffic/stream", s.handleInterfacesTrafficStream)
  157. s.mux.HandleFunc("GET /api/interfaces/traffic/rates", s.handleInterfacesTrafficRates)
  158. s.mux.HandleFunc("GET /api/interfaces/rates", s.handleInterfacesTrafficRates)
  159. s.mux.HandleFunc("GET /api/roles", s.handleListRoleTags)
  160. s.mux.HandleFunc("POST /api/roles", s.handleSaveRoleTag)
  161. s.mux.HandleFunc("PUT /api/roles/{id}", s.handleSaveRoleTag)
  162. s.mux.HandleFunc("DELETE /api/roles/{id}", s.handleDeleteRoleTag)
  163. s.mux.HandleFunc("GET /api/interfaces/roles", s.handleListRoleTags)
  164. s.mux.HandleFunc("GET /api/interfaces/{name}", s.handleGetInterface)
  165. s.mux.HandleFunc("POST /api/interfaces/{name}/state", s.handleSetInterfaceState)
  166. s.mux.HandleFunc("POST /api/interfaces/{name}/mtu", s.handleSetInterfaceMTU)
  167. s.mux.HandleFunc("POST /api/interfaces/{name}/ip", s.handleAddInterfaceIP)
  168. s.mux.HandleFunc("DELETE /api/interfaces/{name}/ip", s.handleDeleteInterfaceIP)
  169. s.mux.HandleFunc("POST /api/interfaces/{name}/metadata", s.handleSetInterfaceMetadata)
  170. s.mux.HandleFunc("POST /api/interfaces/{name}/rename", s.handleRenameInterface)
  171. s.mux.HandleFunc("GET /api/vlans", s.handleListVLANs)
  172. s.mux.HandleFunc("POST /api/vlans", s.handleCreateVLAN)
  173. s.mux.HandleFunc("DELETE /api/vlans/{name}", s.handleDeleteVLAN)
  174. // Bridge Management (/interface bridge)
  175. s.mux.HandleFunc("GET /api/bridge/summary", s.handleBridgeSummary)
  176. s.mux.HandleFunc("GET /api/bridge/bridges", s.handleListBridges)
  177. s.mux.HandleFunc("POST /api/bridge/bridges", s.handleSaveBridge)
  178. s.mux.HandleFunc("DELETE /api/bridge/bridges/{id}", s.handleDeleteBridge)
  179. s.mux.HandleFunc("POST /api/bridge/bridges/{id}/state", s.handleSetBridgeState)
  180. s.mux.HandleFunc("POST /api/bridge/bridges/{id}/ip", s.handleAddBridgeIP)
  181. s.mux.HandleFunc("DELETE /api/bridge/bridges/{id}/ip", s.handleDeleteBridgeIP)
  182. s.mux.HandleFunc("GET /api/bridge/ports", s.handleListBridgePorts)
  183. s.mux.HandleFunc("POST /api/bridge/ports", s.handleSaveBridgePort)
  184. s.mux.HandleFunc("DELETE /api/bridge/ports/{id}", s.handleDeleteBridgePort)
  185. s.mux.HandleFunc("POST /api/bridge/ports/{id}/state", s.handleSetBridgePortState)
  186. s.mux.HandleFunc("GET /api/bridge/vlans", s.handleListBridgeVLANs)
  187. s.mux.HandleFunc("POST /api/bridge/vlans", s.handleSaveBridgeVLAN)
  188. s.mux.HandleFunc("DELETE /api/bridge/vlans/{id}", s.handleDeleteBridgeVLAN)
  189. s.mux.HandleFunc("POST /api/bridge/vlans/{id}/state", s.handleSetBridgeVLANState)
  190. s.mux.HandleFunc("GET /api/bridge/hosts", s.handleListBridgeHosts)
  191. s.mux.HandleFunc("POST /api/bridge/hosts/flush", s.handleFlushBridgeHosts)
  192. s.mux.HandleFunc("GET /api/debian/config", s.handleGetDebianConfig)
  193. s.mux.HandleFunc("POST /api/debian/apply", s.handleSaveDebianConfig)
  194. // VPN & WAN Endpoints
  195. s.mux.HandleFunc("GET /api/vpn/status", s.handleOverallVPNStatus)
  196. // PPPoE Multi-Instance
  197. s.mux.HandleFunc("GET /api/vpn/pppoe/clients", s.handleListPPPoE)
  198. s.mux.HandleFunc("POST /api/vpn/pppoe/clients", s.handleSavePPPoE)
  199. s.mux.HandleFunc("DELETE /api/vpn/pppoe/clients/{name}", s.handleDeletePPPoE)
  200. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/state", s.handleSetPPPoEState)
  201. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/connect", s.handleConnectPPPoE)
  202. s.mux.HandleFunc("POST /api/vpn/pppoe/clients/{name}/disconnect", s.handleDisconnectPPPoE)
  203. // WireGuard Multi-Interface & Multi-Peer
  204. s.mux.HandleFunc("GET /api/vpn/wireguard/interfaces", s.handleListWireGuardInterfaces)
  205. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces", s.handleSaveWireGuardInterface)
  206. s.mux.HandleFunc("DELETE /api/vpn/wireguard/interfaces/{name}", s.handleDeleteWireGuardInterface)
  207. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/state", s.handleSetWireGuardInterfaceState)
  208. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/start", s.handleStartWireGuardInterface)
  209. s.mux.HandleFunc("POST /api/vpn/wireguard/interfaces/{name}/stop", s.handleStopWireGuardInterface)
  210. s.mux.HandleFunc("GET /api/vpn/wireguard/peers", s.handleListWireGuardPeers)
  211. s.mux.HandleFunc("POST /api/vpn/wireguard/peers", s.handleSaveWireGuardPeer)
  212. s.mux.HandleFunc("POST /api/vpn/wireguard/peers/state", s.handleSetWireGuardPeerState)
  213. s.mux.HandleFunc("POST /api/vpn/wireguard/peers/delete", s.handleDeleteWireGuardPeer)
  214. s.mux.HandleFunc("DELETE /api/vpn/wireguard/peers", s.handleDeleteWireGuardPeer)
  215. s.mux.HandleFunc("DELETE /api/vpn/wireguard/peers/{iface}/{pubkey...}", s.handleDeleteWireGuardPeer)
  216. s.mux.HandleFunc("POST /api/vpn/wireguard/genkeys", s.handleWireGuardGenKeys)
  217. s.mux.HandleFunc("POST /api/vpn/wireguard/calc-pubkey", s.handleWireGuardCalcPubKey)
  218. // PPTP Multi-Instance
  219. s.mux.HandleFunc("GET /api/vpn/pptp/clients", s.handleListPPTP)
  220. s.mux.HandleFunc("POST /api/vpn/pptp/clients", s.handleSavePPTP)
  221. s.mux.HandleFunc("DELETE /api/vpn/pptp/clients/{name}", s.handleDeletePPTP)
  222. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/state", s.handleSetPPTPState)
  223. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/connect", s.handleConnectPPTP)
  224. s.mux.HandleFunc("POST /api/vpn/pptp/clients/{name}/disconnect", s.handleDisconnectPPTP)
  225. // L2TP Multi-Instance
  226. s.mux.HandleFunc("GET /api/vpn/l2tp/clients", s.handleListL2TP)
  227. s.mux.HandleFunc("POST /api/vpn/l2tp/clients", s.handleSaveL2TP)
  228. s.mux.HandleFunc("DELETE /api/vpn/l2tp/clients/{name}", s.handleDeleteL2TP)
  229. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/state", s.handleSetL2TPState)
  230. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/connect", s.handleConnectL2TP)
  231. s.mux.HandleFunc("POST /api/vpn/l2tp/clients/{name}/disconnect", s.handleDisconnectL2TP)
  232. // IPSec Multi-Tunnel
  233. s.mux.HandleFunc("GET /api/vpn/ipsec/tunnels", s.handleListIPSec)
  234. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels", s.handleSaveIPSecTunnel)
  235. s.mux.HandleFunc("DELETE /api/vpn/ipsec/tunnels/{name}", s.handleDeleteIPSecTunnel)
  236. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/state", s.handleSetIPSecState)
  237. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/start", s.handleInitiateIPSecTunnel)
  238. s.mux.HandleFunc("POST /api/vpn/ipsec/tunnels/{name}/stop", s.handleTerminateIPSecTunnel)
  239. // EoIP (Ethernet over IP / GRETAP)
  240. s.mux.HandleFunc("GET /api/vpn/eoip", s.handleListEoIP)
  241. s.mux.HandleFunc("POST /api/vpn/eoip", s.handleSaveEoIP)
  242. s.mux.HandleFunc("DELETE /api/vpn/eoip/{id}", s.handleDeleteEoIP)
  243. s.mux.HandleFunc("POST /api/vpn/eoip/{id}/state", s.handleSetEoIPState)
  244. // VXLAN (Virtual Extensible LAN overlay network)
  245. s.mux.HandleFunc("GET /api/vpn/vxlan", s.handleListVXLAN)
  246. s.mux.HandleFunc("POST /api/vpn/vxlan", s.handleSaveVXLAN)
  247. s.mux.HandleFunc("DELETE /api/vpn/vxlan/{id}", s.handleDeleteVXLAN)
  248. s.mux.HandleFunc("POST /api/vpn/vxlan/{id}/state", s.handleSetVXLANState)
  249. s.mux.HandleFunc("GET /api/vpn/vxlan/vteps", s.handleListVXLANVteps)
  250. s.mux.HandleFunc("POST /api/vpn/vxlan/vteps", s.handleSaveVXLANVtep)
  251. s.mux.HandleFunc("DELETE /api/vpn/vxlan/{id}/vteps/{vtepid}", s.handleDeleteVXLANVtep)
  252. // VXLAN aliases under /api/interface/vxlan
  253. s.mux.HandleFunc("GET /api/interface/vxlan", s.handleListVXLAN)
  254. s.mux.HandleFunc("POST /api/interface/vxlan", s.handleSaveVXLAN)
  255. s.mux.HandleFunc("DELETE /api/interface/vxlan/{id}", s.handleDeleteVXLAN)
  256. s.mux.HandleFunc("POST /api/interface/vxlan/{id}/state", s.handleSetVXLANState)
  257. s.mux.HandleFunc("GET /api/interface/vxlan/vteps", s.handleListVXLANVteps)
  258. s.mux.HandleFunc("POST /api/interface/vxlan/vteps", s.handleSaveVXLANVtep)
  259. s.mux.HandleFunc("DELETE /api/interface/vxlan/{id}/vteps/{vtepid}", s.handleDeleteVXLANVtep)
  260. // SD-WAN (Software-Defined WAN Multi-Link Load Balancing & SLA)
  261. s.mux.HandleFunc("GET /api/sdwan/groups", s.handleListSDWANGroups)
  262. s.mux.HandleFunc("POST /api/sdwan/groups", s.handleSaveSDWANGroup)
  263. s.mux.HandleFunc("GET /api/sdwan/groups/{id}", s.handleGetSDWANGroup)
  264. s.mux.HandleFunc("DELETE /api/sdwan/groups/{id}", s.handleDeleteSDWANGroup)
  265. s.mux.HandleFunc("POST /api/sdwan/groups/{id}/state", s.handleSetSDWANGroupState)
  266. s.mux.HandleFunc("POST /api/sdwan/groups/{id}/members", s.handleSaveSDWANMember)
  267. s.mux.HandleFunc("PUT /api/sdwan/groups/{id}/members", s.handleSaveSDWANMember)
  268. s.mux.HandleFunc("PUT /api/sdwan/groups/{id}/members/{iface}", s.handleSaveSDWANMember)
  269. s.mux.HandleFunc("DELETE /api/sdwan/groups/{id}/members/{iface}", s.handleDeleteSDWANMember)
  270. s.mux.HandleFunc("GET /api/sdwan/telemetry", s.handleGetSDWANTelemetry)
  271. // AeroTUN (High-speed ChaCha20-Poly1305 + LZ4 Encrypted UDP Tunnel)
  272. s.mux.HandleFunc("GET /api/vpn/aerotun/tunnels", s.handleListAeroTUNTunnels)
  273. s.mux.HandleFunc("POST /api/vpn/aerotun/tunnels", s.handleSaveAeroTUNTunnel)
  274. s.mux.HandleFunc("DELETE /api/vpn/aerotun/tunnels/{id}", s.handleDeleteAeroTUNTunnel)
  275. s.mux.HandleFunc("POST /api/vpn/aerotun/tunnels/{id}/state", s.handleSetAeroTUNTunnelState)
  276. s.mux.HandleFunc("POST /api/vpn/aerotun/tunnels/{id}/start", s.handleStartAeroTUNTunnel)
  277. s.mux.HandleFunc("POST /api/vpn/aerotun/tunnels/{id}/stop", s.handleStopAeroTUNTunnel)
  278. s.mux.HandleFunc("GET /api/vpn/aerotun/genkey", s.handleGenerateAeroTUNKey)
  279. s.mux.HandleFunc("POST /api/vpn/aerotun/identity-drafts", s.handleCreateAeroIdentityDraft)
  280. s.mux.HandleFunc("DELETE /api/vpn/aerotun/identity-drafts/{id}", s.handleDeleteAeroIdentityDraft)
  281. s.mux.HandleFunc("GET /api/vpn/aerotun/stats", s.handleGetAeroTUNStats)
  282. s.mux.HandleFunc("GET /api/vpn/aerotun/health", s.handleAeroTUNHealth)
  283. s.mux.HandleFunc("POST /api/vpn/aerotun/tunnels/{id}/health-check", s.handleAeroTUNHealthCheck)
  284. // PPP Profiles (/ppp profile)
  285. s.mux.HandleFunc("GET /api/vpn/ppp/profiles", s.handleListPPPProfiles)
  286. s.mux.HandleFunc("POST /api/vpn/ppp/profiles", s.handleSavePPPProfile)
  287. s.mux.HandleFunc("DELETE /api/vpn/ppp/profiles/{name}", s.handleDeletePPPProfile)
  288. // PPP Secrets (/ppp secret)
  289. s.mux.HandleFunc("GET /api/vpn/ppp/secrets", s.handleListPPPSecrets)
  290. s.mux.HandleFunc("POST /api/vpn/ppp/secrets", s.handleSavePPPSecret)
  291. s.mux.HandleFunc("DELETE /api/vpn/ppp/secrets/{id}", s.handleDeletePPPSecret)
  292. s.mux.HandleFunc("POST /api/vpn/ppp/secrets/{id}/state", s.handleSetPPPSecretState)
  293. // Active PPP Sessions (/ppp active)
  294. s.mux.HandleFunc("GET /api/vpn/ppp/active", s.handleListPPPSessions)
  295. s.mux.HandleFunc("DELETE /api/vpn/ppp/active/{iface}", s.handleDisconnectPPPSession)
  296. // VPN Server Settings
  297. s.mux.HandleFunc("GET /api/vpn/servers/pppoe", s.handleGetPPPoEServer)
  298. s.mux.HandleFunc("POST /api/vpn/servers/pppoe", s.handleSetPPPoEServer)
  299. s.mux.HandleFunc("GET /api/vpn/servers/pptp", s.handleGetPPTPServer)
  300. s.mux.HandleFunc("POST /api/vpn/servers/pptp", s.handleSetPPTPServer)
  301. s.mux.HandleFunc("GET /api/vpn/servers/l2tp", s.handleGetL2TPServer)
  302. s.mux.HandleFunc("POST /api/vpn/servers/l2tp", s.handleSetL2TPServer)
  303. s.mux.HandleFunc("GET /api/vpn/servers/ipsec", s.handleGetIPSecServer)
  304. s.mux.HandleFunc("POST /api/vpn/servers/ipsec", s.handleSetIPSecServer)
  305. // WireGuard Client Config Generator
  306. s.mux.HandleFunc("POST /api/vpn/wireguard/client-config", s.handleGenerateWGClientConfig)
  307. // Dynamic Routing (BGP & OSPF)
  308. s.mux.HandleFunc("GET /api/routing/dynamic/summary", s.handleDynamicRoutingSummary)
  309. // BGP
  310. s.mux.HandleFunc("GET /api/routing/bgp/instance", s.handleGetBGPInstance)
  311. s.mux.HandleFunc("POST /api/routing/bgp/instance", s.handleUpdateBGPInstance)
  312. s.mux.HandleFunc("GET /api/routing/bgp/peers", s.handleListBGPPeers)
  313. s.mux.HandleFunc("POST /api/routing/bgp/peers", s.handleSaveBGPPeer)
  314. s.mux.HandleFunc("DELETE /api/routing/bgp/peers/{id}", s.handleDeleteBGPPeer)
  315. s.mux.HandleFunc("POST /api/routing/bgp/peers/{id}/state", s.handleSetBGPPeerState)
  316. s.mux.HandleFunc("GET /api/routing/bgp/networks", s.handleListBGPNetworks)
  317. s.mux.HandleFunc("POST /api/routing/bgp/networks", s.handleSaveBGPNetwork)
  318. s.mux.HandleFunc("DELETE /api/routing/bgp/networks/{id}", s.handleDeleteBGPNetwork)
  319. s.mux.HandleFunc("GET /api/routing/bgp/routes", s.handleGetBGPRoutes)
  320. // OSPF (RouterOS v7 Multi-Instance OSPF)
  321. s.mux.HandleFunc("GET /api/routing/ospf/instances", s.handleListOSPFInstances)
  322. s.mux.HandleFunc("POST /api/routing/ospf/instances", s.handleSaveOSPFInstance)
  323. s.mux.HandleFunc("DELETE /api/routing/ospf/instances/{id}", s.handleDeleteOSPFInstance)
  324. s.mux.HandleFunc("POST /api/routing/ospf/instances/{id}/state", s.handleSetOSPFInstanceState)
  325. s.mux.HandleFunc("GET /api/routing/ospf/instance", s.handleGetOSPFInstance)
  326. s.mux.HandleFunc("POST /api/routing/ospf/instance", s.handleUpdateOSPFInstance)
  327. s.mux.HandleFunc("GET /api/routing/ospf/areas", s.handleListOSPFAreas)
  328. s.mux.HandleFunc("POST /api/routing/ospf/areas", s.handleSaveOSPFArea)
  329. s.mux.HandleFunc("DELETE /api/routing/ospf/areas/{id}", s.handleDeleteOSPFArea)
  330. s.mux.HandleFunc("POST /api/routing/ospf/areas/{id}/state", s.handleSetOSPFAreaState)
  331. s.mux.HandleFunc("GET /api/routing/ospf/area-ranges", s.handleListOSPFAreaRanges)
  332. s.mux.HandleFunc("POST /api/routing/ospf/area-ranges", s.handleSaveOSPFAreaRange)
  333. s.mux.HandleFunc("DELETE /api/routing/ospf/area-ranges/{id}", s.handleDeleteOSPFAreaRange)
  334. s.mux.HandleFunc("GET /api/routing/ospf/templates", s.handleListOSPFInterfaces)
  335. s.mux.HandleFunc("POST /api/routing/ospf/templates", s.handleSaveOSPFInterface)
  336. s.mux.HandleFunc("DELETE /api/routing/ospf/templates/{id}", s.handleDeleteOSPFInterface)
  337. s.mux.HandleFunc("POST /api/routing/ospf/templates/{id}/state", s.handleSetOSPFInterfaceState)
  338. s.mux.HandleFunc("GET /api/routing/ospf/interfaces", s.handleListOSPFInterfaces)
  339. s.mux.HandleFunc("POST /api/routing/ospf/interfaces", s.handleSaveOSPFInterface)
  340. s.mux.HandleFunc("DELETE /api/routing/ospf/interfaces/{id}", s.handleDeleteOSPFInterface)
  341. s.mux.HandleFunc("POST /api/routing/ospf/interfaces/{id}/state", s.handleSetOSPFInterfaceState)
  342. s.mux.HandleFunc("GET /api/routing/ospf/interfaces-live", s.handleGetOSPFInterfacesLive)
  343. s.mux.HandleFunc("GET /api/routing/ospf/static-neighbors", s.handleListOSPFStaticNeighbors)
  344. s.mux.HandleFunc("POST /api/routing/ospf/static-neighbors", s.handleSaveOSPFStaticNeighbor)
  345. s.mux.HandleFunc("DELETE /api/routing/ospf/static-neighbors/{id}", s.handleDeleteOSPFStaticNeighbor)
  346. s.mux.HandleFunc("POST /api/routing/ospf/static-neighbors/{id}/state", s.handleSetOSPFStaticNeighborState)
  347. s.mux.HandleFunc("GET /api/routing/ospf/neighbors", s.handleGetOSPFNeighbors)
  348. s.mux.HandleFunc("GET /api/routing/ospf/lsa", s.handleGetOSPFLSA)
  349. s.mux.HandleFunc("GET /api/routing/ospf/routes", s.handleGetOSPFRoutes)
  350. // RIP (/routing rip)
  351. s.mux.HandleFunc("GET /api/routing/rip/instances", s.handleListRIPInstances)
  352. s.mux.HandleFunc("POST /api/routing/rip/instances", s.handleSaveRIPInstance)
  353. s.mux.HandleFunc("DELETE /api/routing/rip/instances/{id}", s.handleDeleteRIPInstance)
  354. s.mux.HandleFunc("POST /api/routing/rip/instances/{id}/state", s.handleSetRIPInstanceState)
  355. s.mux.HandleFunc("GET /api/routing/rip/interfaces", s.handleListRIPInterfaces)
  356. s.mux.HandleFunc("POST /api/routing/rip/interfaces", s.handleSaveRIPInterface)
  357. s.mux.HandleFunc("DELETE /api/routing/rip/interfaces/{id}", s.handleDeleteRIPInterface)
  358. s.mux.HandleFunc("POST /api/routing/rip/interfaces/{id}/state", s.handleSetRIPInterfaceState)
  359. s.mux.HandleFunc("GET /api/routing/rip/neighbors", s.handleListRIPNeighbors)
  360. s.mux.HandleFunc("POST /api/routing/rip/neighbors", s.handleSaveRIPNeighbor)
  361. s.mux.HandleFunc("DELETE /api/routing/rip/neighbors/{id}", s.handleDeleteRIPNeighbor)
  362. s.mux.HandleFunc("GET /api/routing/rip/neighbors-live", s.handleGetRIPNeighborsLive)
  363. s.mux.HandleFunc("GET /api/routing/rip/routes", s.handleGetRIPRoutes)
  364. // BFD (/routing bfd)
  365. s.mux.HandleFunc("GET /api/routing/bfd/instance", s.handleGetBFDInstance)
  366. s.mux.HandleFunc("POST /api/routing/bfd/instance", s.handleSaveBFDInstance)
  367. s.mux.HandleFunc("GET /api/routing/bfd/interfaces", s.handleListBFDInterfaces)
  368. s.mux.HandleFunc("POST /api/routing/bfd/interfaces", s.handleSaveBFDInterface)
  369. s.mux.HandleFunc("DELETE /api/routing/bfd/interfaces/{id}", s.handleDeleteBFDInterface)
  370. s.mux.HandleFunc("POST /api/routing/bfd/interfaces/{id}/state", s.handleSetBFDInterfaceState)
  371. s.mux.HandleFunc("GET /api/routing/bfd/peers", s.handleListBFDNeighbors)
  372. s.mux.HandleFunc("POST /api/routing/bfd/peers", s.handleSaveBFDNeighbor)
  373. s.mux.HandleFunc("DELETE /api/routing/bfd/peers/{id}", s.handleDeleteBFDNeighbor)
  374. s.mux.HandleFunc("GET /api/routing/bfd/sessions", s.handleGetBFDSessions)
  375. // Babel (/routing babel)
  376. s.mux.HandleFunc("GET /api/routing/babel/instances", s.handleListBabelInstances)
  377. s.mux.HandleFunc("POST /api/routing/babel/instances", s.handleSaveBabelInstance)
  378. s.mux.HandleFunc("DELETE /api/routing/babel/instances/{id}", s.handleDeleteBabelInstance)
  379. s.mux.HandleFunc("POST /api/routing/babel/instances/{id}/state", s.handleSetBabelInstanceState)
  380. s.mux.HandleFunc("GET /api/routing/babel/interfaces", s.handleListBabelInterfaces)
  381. s.mux.HandleFunc("GET /api/routing/babel/interfaces-live", s.handleGetBabelInterfacesLive)
  382. s.mux.HandleFunc("POST /api/routing/babel/interfaces", s.handleSaveBabelInterface)
  383. s.mux.HandleFunc("DELETE /api/routing/babel/interfaces/{id}", s.handleDeleteBabelInterface)
  384. s.mux.HandleFunc("POST /api/routing/babel/interfaces/{id}/state", s.handleSetBabelInterfaceState)
  385. s.mux.HandleFunc("GET /api/routing/babel/neighbors", s.handleGetBabelNeighbors)
  386. s.mux.HandleFunc("GET /api/routing/babel/routes", s.handleGetBabelRoutes)
  387. // RAdv (/routing radv)
  388. s.mux.HandleFunc("GET /api/routing/radv/instances", s.handleListRAdvInstances)
  389. s.mux.HandleFunc("POST /api/routing/radv/instances", s.handleSaveRAdvInstance)
  390. s.mux.HandleFunc("DELETE /api/routing/radv/instances/{id}", s.handleDeleteRAdvInstance)
  391. s.mux.HandleFunc("POST /api/routing/radv/instances/{id}/state", s.handleSetRAdvInstanceState)
  392. s.mux.HandleFunc("GET /api/routing/radv/interfaces", s.handleListRAdvInterfaces)
  393. s.mux.HandleFunc("POST /api/routing/radv/interfaces", s.handleSaveRAdvInterface)
  394. s.mux.HandleFunc("DELETE /api/routing/radv/interfaces/{id}", s.handleDeleteRAdvInterface)
  395. s.mux.HandleFunc("POST /api/routing/radv/interfaces/{id}/state", s.handleSetRAdvInterfaceState)
  396. // EIGRP (/routing eigrp)
  397. s.mux.HandleFunc("GET /api/routing/eigrp/instances", s.handleListEIGRPInstances)
  398. s.mux.HandleFunc("POST /api/routing/eigrp/instances", s.handleSaveEIGRPInstance)
  399. s.mux.HandleFunc("DELETE /api/routing/eigrp/instances/{id}", s.handleDeleteEIGRPInstance)
  400. s.mux.HandleFunc("POST /api/routing/eigrp/instances/{id}/state", s.handleSetEIGRPInstanceState)
  401. s.mux.HandleFunc("GET /api/routing/eigrp/interfaces", s.handleListEIGRPInterfaces)
  402. s.mux.HandleFunc("POST /api/routing/eigrp/interfaces", s.handleSaveEIGRPInterface)
  403. s.mux.HandleFunc("DELETE /api/routing/eigrp/interfaces/{id}", s.handleDeleteEIGRPInterface)
  404. s.mux.HandleFunc("POST /api/routing/eigrp/interfaces/{id}/state", s.handleSetEIGRPInterfaceState)
  405. s.mux.HandleFunc("GET /api/routing/eigrp/networks", s.handleListEIGRPNetworks)
  406. s.mux.HandleFunc("POST /api/routing/eigrp/networks", s.handleSaveEIGRPNetwork)
  407. s.mux.HandleFunc("DELETE /api/routing/eigrp/networks/{id}", s.handleDeleteEIGRPNetwork)
  408. s.mux.HandleFunc("GET /api/routing/eigrp/neighbors", s.handleListEIGRPNeighbors)
  409. s.mux.HandleFunc("GET /api/routing/eigrp/topology", s.handleListEIGRPTopology)
  410. // Routing Filters (/routing filter)
  411. s.mux.HandleFunc("GET /api/routing/filters", s.handleListRoutingFilters)
  412. s.mux.HandleFunc("POST /api/routing/filters", s.handleSaveRoutingFilter)
  413. s.mux.HandleFunc("DELETE /api/routing/filters/{id}", s.handleDeleteRoutingFilter)
  414. s.mux.HandleFunc("POST /api/routing/filters/{id}/state", s.handleSetRoutingFilterState)
  415. s.mux.HandleFunc("POST /api/routing/filters/{id}/move", s.handleMoveRoutingFilter)
  416. s.mux.HandleFunc("GET /api/routing/filters/rules", s.handleListRoutingFilters)
  417. s.mux.HandleFunc("POST /api/routing/filters/rules", s.handleSaveRoutingFilter)
  418. s.mux.HandleFunc("DELETE /api/routing/filters/rules/{id}", s.handleDeleteRoutingFilter)
  419. s.mux.HandleFunc("POST /api/routing/filters/rules/{id}/state", s.handleSetRoutingFilterState)
  420. s.mux.HandleFunc("POST /api/routing/filters/rules/{id}/move", s.handleMoveRoutingFilter)
  421. s.mux.HandleFunc("GET /api/routing/filters/community-lists", s.handleListCommunityLists)
  422. s.mux.HandleFunc("POST /api/routing/filters/community-lists", s.handleSaveCommunityList)
  423. s.mux.HandleFunc("DELETE /api/routing/filters/community-lists/{id}", s.handleDeleteCommunityList)
  424. s.mux.HandleFunc("POST /api/routing/filters/community-lists/{id}/state", s.handleSetCommunityListState)
  425. s.mux.HandleFunc("GET /api/routing/filters/as-lists", s.handleListASLists)
  426. s.mux.HandleFunc("POST /api/routing/filters/as-lists", s.handleSaveASList)
  427. s.mux.HandleFunc("DELETE /api/routing/filters/as-lists/{id}", s.handleDeleteASList)
  428. s.mux.HandleFunc("POST /api/routing/filters/as-lists/{id}/state", s.handleSetASListState)
  429. // Troubleshooting Tools (/tool ping, traceroute, mtr, dns, port-probe)
  430. s.mux.HandleFunc("POST /api/tools/ping", s.handleToolsPing)
  431. s.mux.HandleFunc("POST /api/tools/ping/stream", s.handleToolsPingStream)
  432. s.mux.HandleFunc("GET /api/tools/ping/stream", s.handleToolsPingStream)
  433. s.mux.HandleFunc("POST /api/tools/traceroute", s.handleToolsTraceroute)
  434. s.mux.HandleFunc("POST /api/tools/traceroute/stream", s.handleToolsTracerouteStream)
  435. s.mux.HandleFunc("GET /api/tools/traceroute/stream", s.handleToolsTracerouteStream)
  436. s.mux.HandleFunc("POST /api/tools/dns", s.handleToolsDNS)
  437. s.mux.HandleFunc("POST /api/tools/port-probe", s.handleToolsPortProbe)
  438. s.mux.HandleFunc("POST /api/tools/mtr", s.handleToolsMTR)
  439. // Bandwidth Test Tool (/tool bandwidth-test / BwTest)
  440. s.mux.HandleFunc("GET /api/tools/bwtest/server", s.handleGetBwTestServer)
  441. s.mux.HandleFunc("POST /api/tools/bwtest/server", s.handleUpdateBwTestServer)
  442. s.mux.HandleFunc("POST /api/tools/bwtest/start", s.handleStartBwTest)
  443. s.mux.HandleFunc("POST /api/tools/bwtest/stop", s.handleStopBwTest)
  444. s.mux.HandleFunc("GET /api/tools/bwtest/status", s.handleGetBwTestStatus)
  445. s.mux.HandleFunc("GET /api/tools/bwtest/stream", s.handleStreamBwTest)
  446. s.mux.HandleFunc("GET /api/tools/bwtest/history", s.handleGetBwTestHistory)
  447. // DHCP Client (/ip dhcp-client)
  448. s.mux.HandleFunc("GET /api/dhcp/clients", s.handleListDHCPClients)
  449. s.mux.HandleFunc("GET /api/dhcp/clients/summary", s.handleGetDHCPSummary)
  450. s.mux.HandleFunc("POST /api/dhcp/clients", s.handleCreateDHCPClient)
  451. s.mux.HandleFunc("PUT /api/dhcp/clients/{id}", s.handleUpdateDHCPClient)
  452. s.mux.HandleFunc("DELETE /api/dhcp/clients/{id}", s.handleDeleteDHCPClient)
  453. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/state", s.handleToggleDHCPClientState)
  454. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/renew", s.handleRenewDHCPClient)
  455. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/release", s.handleReleaseDHCPClient)
  456. // DHCP Server (/ip dhcp-server)
  457. s.mux.HandleFunc("GET /api/dhcp-server/summary", s.handleGetDHCPServerSummary)
  458. s.mux.HandleFunc("GET /api/dhcp-server/servers", s.handleListDHCPServers)
  459. s.mux.HandleFunc("POST /api/dhcp-server/servers", s.handleSaveDHCPServer)
  460. s.mux.HandleFunc("DELETE /api/dhcp-server/servers/{id}", s.handleDeleteDHCPServer)
  461. s.mux.HandleFunc("POST /api/dhcp-server/servers/{id}/state", s.handleSetDHCPServerState)
  462. s.mux.HandleFunc("GET /api/dhcp-server/networks", s.handleListDHCPNetworks)
  463. s.mux.HandleFunc("POST /api/dhcp-server/networks", s.handleSaveDHCPNetwork)
  464. s.mux.HandleFunc("DELETE /api/dhcp-server/networks/{id}", s.handleDeleteDHCPNetwork)
  465. s.mux.HandleFunc("GET /api/dhcp-server/leases", s.handleListDHCPLeases)
  466. s.mux.HandleFunc("POST /api/dhcp-server/leases/static", s.handleSaveDHCPStaticLease)
  467. s.mux.HandleFunc("DELETE /api/dhcp-server/leases/{id}", s.handleDeleteDHCPStaticLease)
  468. s.mux.HandleFunc("POST /api/dhcp-server/leases/make-static", s.handleMakeDHCPStaticLease)
  469. s.mux.HandleFunc("GET /api/dhcp-server/options", s.handleListDHCPOptions)
  470. s.mux.HandleFunc("POST /api/dhcp-server/options", s.handleSaveDHCPOption)
  471. s.mux.HandleFunc("DELETE /api/dhcp-server/options/{id}", s.handleDeleteDHCPOption)
  472. s.mux.HandleFunc("POST /api/dhcp-server/setup", s.handleRunDHCPSetup)
  473. // VRRP Redundancy & High Availability
  474. s.mux.HandleFunc("GET /api/vrrp/summary", s.handleGetVRRPSummary)
  475. s.mux.HandleFunc("GET /api/vrrp/instances", s.handleListVRRPInstances)
  476. s.mux.HandleFunc("POST /api/vrrp/instances", s.handleSaveVRRPInstance)
  477. s.mux.HandleFunc("DELETE /api/vrrp/instances/{id}", s.handleDeleteVRRPInstance)
  478. s.mux.HandleFunc("POST /api/vrrp/instances/{id}/state", s.handleToggleVRRPState)
  479. // Router DNS & Settings (/ip dns)
  480. s.mux.HandleFunc("GET /api/dns/settings", s.handleGetDNSSettings)
  481. s.mux.HandleFunc("POST /api/dns/settings", s.handleSaveDNSSettings)
  482. s.mux.HandleFunc("GET /api/dns/static", s.handleListDNSStatic)
  483. s.mux.HandleFunc("POST /api/dns/static", s.handleSaveDNSStatic)
  484. s.mux.HandleFunc("DELETE /api/dns/static/{id}", s.handleDeleteDNSStatic)
  485. s.mux.HandleFunc("POST /api/dns/static/{id}/state", s.handleSetDNSStaticState)
  486. s.mux.HandleFunc("POST /api/dns/flush", s.handleFlushDNSCache)
  487. s.mux.HandleFunc("GET /api/dns/status", s.handleGetDNSStatus)
  488. // System Settings (/system identity & clock)
  489. s.mux.HandleFunc("GET /api/system/settings", s.handleGetSystemSettings)
  490. s.mux.HandleFunc("POST /api/system/settings", s.handleSaveSystemSettings)
  491. // Management Services (/ip service)
  492. s.mux.HandleFunc("GET /api/system/services", s.handleListServices)
  493. s.mux.HandleFunc("POST /api/system/services/{name}", s.handleUpdateService)
  494. s.mux.HandleFunc("PUT /api/system/services/{name}", s.handleUpdateService)
  495. s.mux.HandleFunc("POST /api/system/services/{name}/state", s.handleSetServiceState)
  496. // Certificate Management (/certificate)
  497. s.mux.HandleFunc("GET /api/certificates", s.handleListCertificates)
  498. s.mux.HandleFunc("POST /api/certificates/ca", s.handleCreateCA)
  499. s.mux.HandleFunc("POST /api/certificates/generate", s.handleCreateCertificate)
  500. s.mux.HandleFunc("POST /api/certificates/import", s.handleImportCertificate)
  501. s.mux.HandleFunc("POST /api/certificates/letsencrypt", s.handleIssueLetsEncrypt)
  502. s.mux.HandleFunc("GET /api/certificates/{name}", s.handleGetCertificate)
  503. s.mux.HandleFunc("GET /api/certificates/{name}/export", s.handleExportCertificate)
  504. s.mux.HandleFunc("DELETE /api/certificates/{name}", s.handleDeleteCertificate)
  505. // Reverse Proxy & Application Gateway (/reverse-proxy)
  506. s.mux.HandleFunc("GET /api/proxy/config", s.handleGetProxyConfig)
  507. s.mux.HandleFunc("POST /api/proxy/config", s.handleUpdateProxyConfig)
  508. s.mux.HandleFunc("GET /api/proxy/rules", s.handleListProxyRules)
  509. s.mux.HandleFunc("POST /api/proxy/rules", s.handleSaveProxyRule)
  510. s.mux.HandleFunc("GET /api/proxy/rules/{id}", s.handleGetProxyRule)
  511. s.mux.HandleFunc("PUT /api/proxy/rules/{id}", s.handleSaveProxyRule)
  512. s.mux.HandleFunc("DELETE /api/proxy/rules/{id}", s.handleDeleteProxyRule)
  513. s.mux.HandleFunc("POST /api/proxy/rules/{id}/state", s.handleSetProxyRuleState)
  514. s.mux.HandleFunc("GET /api/proxy/status", s.handleGetProxyStatus)
  515. // ACME HTTP-01 Challenge responder for Let's Encrypt
  516. s.mux.HandleFunc("GET /.well-known/acme-challenge/{token}", s.handleACMEChallenge)
  517. // Logging (/log)
  518. s.mux.HandleFunc("GET /api/system/logs", s.handleListLogs)
  519. // Authentication & User Management
  520. s.mux.HandleFunc("POST /api/auth/login", s.handleLogin)
  521. s.mux.HandleFunc("POST /api/auth/logout", s.handleLogout)
  522. s.mux.HandleFunc("GET /api/auth/me", s.handleGetMe)
  523. s.mux.HandleFunc("POST /api/auth/change-password", s.handleChangePassword)
  524. s.mux.HandleFunc("GET /api/users", s.handleListUsers)
  525. s.mux.HandleFunc("POST /api/users", s.handleCreateUser)
  526. s.mux.HandleFunc("PUT /api/users/{username}", s.handleUpdateUser)
  527. s.mux.HandleFunc("DELETE /api/users/{username}", s.handleDeleteUser)
  528. s.mux.HandleFunc("POST /api/users/{username}/password", s.handleAdminResetPassword)
  529. // Web UI Static Files
  530. if s.fsHandler != nil {
  531. s.mux.Handle("/", s.fsHandler)
  532. }
  533. }
  534. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  535. // Enable CORS for local development/testing
  536. w.Header().Set("Access-Control-Allow-Origin", "*")
  537. w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, PUT")
  538. w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
  539. if r.Method == http.MethodOptions {
  540. w.WriteHeader(http.StatusOK)
  541. return
  542. }
  543. path := r.URL.Path
  544. // 1. ACME challenge responder MUST always be accessible on port 80
  545. if strings.HasPrefix(path, "/.well-known/acme-challenge/") {
  546. s.mux.ServeHTTP(w, r)
  547. return
  548. }
  549. // 2. Reverse Proxy interceptor (prior to management checks, auth and static handlers)
  550. if s.proxyMgr != nil && s.proxyMgr.IsEnabled() {
  551. isMgmtAPI := strings.HasPrefix(path, "/api/")
  552. if !isMgmtAPI {
  553. if s.proxyMgr.HandleHTTP(w, r) {
  554. return
  555. }
  556. }
  557. }
  558. // 3. HTTP and HTTPS independent management service settings for DebTik Web UI / API
  559. if s.servicesMgr != nil {
  560. service := "www"
  561. if r.TLS != nil {
  562. service = "www-ssl"
  563. }
  564. if www, ok := s.servicesMgr.GetService(service); ok && www.Disabled {
  565. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  566. w.WriteHeader(http.StatusServiceUnavailable)
  567. _, _ = fmt.Fprintf(w, "503 Service Unavailable: %s management service is disabled\n", service)
  568. return
  569. }
  570. }
  571. // Authentication protection for API routes
  572. if strings.HasPrefix(path, "/api/") {
  573. // Public endpoint: Login
  574. if path != "/api/auth/login" {
  575. token := extractToken(r)
  576. sess, err := s.authMgr.ValidateSession(token)
  577. if err != nil {
  578. writeJSON(w, http.StatusUnauthorized, map[string]any{
  579. "error": true,
  580. "message": "Authentication required",
  581. "login_required": true,
  582. })
  583. return
  584. }
  585. // Admin privileges required for /api/users management
  586. if strings.HasPrefix(path, "/api/users") && sess.Role != auth.RoleFull {
  587. writeJSON(w, http.StatusForbidden, map[string]any{
  588. "error": true,
  589. "message": "Administrative privileges required",
  590. })
  591. return
  592. }
  593. // Read-only role protection: forbid mutating operations (except logout & self password change)
  594. if sess.Role == auth.RoleRead && r.Method != http.MethodGet && r.Method != http.MethodHead {
  595. if path != "/api/auth/logout" && path != "/api/auth/change-password" {
  596. writeJSON(w, http.StatusForbidden, map[string]any{
  597. "error": true,
  598. "message": "Read-only account cannot perform configuration modifications",
  599. })
  600. return
  601. }
  602. }
  603. // Inject session into request context
  604. ctx := context.WithValue(r.Context(), sessionContextKey, sess)
  605. r = r.WithContext(ctx)
  606. }
  607. }
  608. s.mux.ServeHTTP(w, r)
  609. }
  610. func (s *Server) handleSystemStatus(w http.ResponseWriter, r *http.Request) {
  611. status, err := system.GetStatus()
  612. if err != nil {
  613. writeError(w, http.StatusInternalServerError, err.Error())
  614. return
  615. }
  616. ifaces, err := s.netMgr.ListInterfaces()
  617. if err == nil {
  618. status.TotalIfaces = len(ifaces)
  619. for _, iface := range ifaces {
  620. if iface.State == "UP" {
  621. status.UpIfaces++
  622. } else {
  623. status.DownIfaces++
  624. }
  625. if iface.IsVLAN {
  626. status.VlanIfaces++
  627. }
  628. }
  629. }
  630. writeJSON(w, http.StatusOK, status)
  631. }
  632. func (s *Server) handleRoutes(w http.ResponseWriter, r *http.Request) {
  633. routes, err := system.GetRoutingTable()
  634. if err != nil {
  635. writeError(w, http.StatusInternalServerError, err.Error())
  636. return
  637. }
  638. writeJSON(w, http.StatusOK, routes)
  639. }
  640. func (s *Server) handleListInterfaces(w http.ResponseWriter, r *http.Request) {
  641. ifaces, err := s.netMgr.ListInterfaces()
  642. if err != nil {
  643. writeError(w, http.StatusInternalServerError, err.Error())
  644. return
  645. }
  646. writeJSON(w, http.StatusOK, ifaces)
  647. }
  648. func (s *Server) handleInterfacesTrafficStream(w http.ResponseWriter, r *http.Request) {
  649. flusher, ok := w.(http.Flusher)
  650. if !ok {
  651. http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
  652. return
  653. }
  654. w.Header().Set("Content-Type", "text/event-stream")
  655. w.Header().Set("Cache-Control", "no-cache")
  656. w.Header().Set("Connection", "keep-alive")
  657. w.Header().Set("Access-Control-Allow-Origin", "*")
  658. ch := s.netMgr.SubscribeTraffic()
  659. defer s.netMgr.UnsubscribeTraffic(ch)
  660. // Send immediate initial snapshot
  661. initSnap := s.netMgr.GetLatestTrafficSnapshot()
  662. if initSnap != nil {
  663. data, _ := json.Marshal(initSnap)
  664. fmt.Fprintf(w, "data: %s\n\n", data)
  665. flusher.Flush()
  666. }
  667. notify := r.Context().Done()
  668. for {
  669. select {
  670. case <-notify:
  671. return
  672. case snap, ok := <-ch:
  673. if !ok {
  674. return
  675. }
  676. data, _ := json.Marshal(snap)
  677. fmt.Fprintf(w, "data: %s\n\n", data)
  678. flusher.Flush()
  679. }
  680. }
  681. }
  682. func (s *Server) handleInterfacesTrafficRates(w http.ResponseWriter, r *http.Request) {
  683. snap := s.netMgr.GetLatestTrafficSnapshot()
  684. if snap == nil {
  685. snap = &network.TrafficSnapshot{
  686. Timestamp: time.Now().Unix(),
  687. Interfaces: make(map[string]network.InterfaceLiveRate),
  688. }
  689. }
  690. writeJSON(w, http.StatusOK, snap)
  691. }
  692. func (s *Server) handleGetInterface(w http.ResponseWriter, r *http.Request) {
  693. name := r.PathValue("name")
  694. iface, err := s.netMgr.GetInterface(name)
  695. if err != nil {
  696. writeError(w, http.StatusNotFound, err.Error())
  697. return
  698. }
  699. writeJSON(w, http.StatusOK, iface)
  700. }
  701. func (s *Server) handleSetInterfaceState(w http.ResponseWriter, r *http.Request) {
  702. name := r.PathValue("name")
  703. var req struct {
  704. State string `json:"state"` // "up" or "down"
  705. }
  706. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  707. writeError(w, http.StatusBadRequest, "invalid request body")
  708. return
  709. }
  710. up := strings.ToLower(req.State) == "up"
  711. if err := s.netMgr.SetInterfaceState(name, up); err != nil {
  712. writeError(w, http.StatusInternalServerError, err.Error())
  713. return
  714. }
  715. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "state": req.State})
  716. }
  717. func (s *Server) handleSetInterfaceMTU(w http.ResponseWriter, r *http.Request) {
  718. name := r.PathValue("name")
  719. var req struct {
  720. MTU int `json:"mtu"`
  721. }
  722. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  723. writeError(w, http.StatusBadRequest, "invalid request body")
  724. return
  725. }
  726. if err := s.netMgr.SetInterfaceMTU(name, req.MTU); err != nil {
  727. writeError(w, http.StatusInternalServerError, err.Error())
  728. return
  729. }
  730. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "mtu": req.MTU})
  731. }
  732. func (s *Server) handleAddInterfaceIP(w http.ResponseWriter, r *http.Request) {
  733. name := r.PathValue("name")
  734. var req network.AddIPRequest
  735. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  736. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  737. return
  738. }
  739. if err := s.netMgr.AddInterfaceIP(name, req.IP); err != nil {
  740. writeError(w, http.StatusInternalServerError, err.Error())
  741. return
  742. }
  743. // If this interface is a bridge, also sync to BridgeManager persistent config
  744. if s.bridgeMgr != nil {
  745. _ = s.bridgeMgr.AddBridgeIP(name, req.IP)
  746. }
  747. // Persist to Debian network configuration
  748. if s.persistMgr != nil {
  749. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  750. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  751. return
  752. }
  753. }
  754. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  755. }
  756. func (s *Server) handleDeleteInterfaceIP(w http.ResponseWriter, r *http.Request) {
  757. name := r.PathValue("name")
  758. var req network.AddIPRequest
  759. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  760. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  761. return
  762. }
  763. if err := s.netMgr.DeleteInterfaceIP(name, req.IP); err != nil {
  764. writeError(w, http.StatusInternalServerError, err.Error())
  765. return
  766. }
  767. // If this interface is a bridge, also delete from BridgeManager persistent config
  768. if s.bridgeMgr != nil {
  769. _ = s.bridgeMgr.DeleteBridgeIP(name, req.IP)
  770. }
  771. // Persist to Debian network configuration
  772. if s.persistMgr != nil {
  773. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  774. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  775. return
  776. }
  777. }
  778. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  779. }
  780. func (s *Server) handleSetInterfaceMetadata(w http.ResponseWriter, r *http.Request) {
  781. name := r.PathValue("name")
  782. var req struct {
  783. Role string `json:"role"`
  784. Description string `json:"description"`
  785. }
  786. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  787. writeError(w, http.StatusBadRequest, "invalid request body")
  788. return
  789. }
  790. previous := s.metaMgr.Get(name)
  791. if err := s.metaMgr.Set(name, req.Role, req.Description); err != nil {
  792. writeError(w, http.StatusInternalServerError, err.Error())
  793. return
  794. }
  795. if s.fwMgr != nil {
  796. if err := s.fwMgr.Reapply(); err != nil {
  797. _ = s.metaMgr.Set(name, previous.Role, previous.Description)
  798. _ = s.fwMgr.Reapply()
  799. writeError(w, 500, "Interface role update rolled back: "+err.Error())
  800. return
  801. }
  802. }
  803. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "role": req.Role, "description": req.Description})
  804. }
  805. func (s *Server) handleListRoleTags(w http.ResponseWriter, r *http.Request) {
  806. roles := s.metaMgr.GetRolesWithUsage()
  807. writeJSON(w, http.StatusOK, roles)
  808. }
  809. func (s *Server) handleSaveRoleTag(w http.ResponseWriter, r *http.Request) {
  810. var req network.RoleTag
  811. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  812. writeError(w, http.StatusBadRequest, "invalid request body")
  813. return
  814. }
  815. id := r.PathValue("id")
  816. if id != "" {
  817. req.ID = id
  818. }
  819. saved, err := s.metaMgr.SaveRole(req)
  820. if err != nil {
  821. writeError(w, http.StatusBadRequest, err.Error())
  822. return
  823. }
  824. writeJSON(w, http.StatusOK, saved)
  825. }
  826. func (s *Server) handleDeleteRoleTag(w http.ResponseWriter, r *http.Request) {
  827. id := r.PathValue("id")
  828. if id == "" {
  829. writeError(w, http.StatusBadRequest, "missing role ID")
  830. return
  831. }
  832. if err := s.metaMgr.DeleteRole(id); err != nil {
  833. writeError(w, http.StatusBadRequest, err.Error())
  834. return
  835. }
  836. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted", "id": id})
  837. }
  838. func (s *Server) handleRenameInterface(w http.ResponseWriter, r *http.Request) {
  839. name := r.PathValue("name")
  840. var req struct {
  841. Name string `json:"name"`
  842. NewName string `json:"new_name"`
  843. }
  844. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  845. writeError(w, http.StatusBadRequest, "invalid request body")
  846. return
  847. }
  848. targetName := req.NewName
  849. if targetName == "" {
  850. targetName = req.Name
  851. }
  852. targetName = strings.TrimSpace(targetName)
  853. if targetName == "" {
  854. writeError(w, http.StatusBadRequest, "new interface name is required")
  855. return
  856. }
  857. info, err := s.netMgr.RenameInterface(name, targetName)
  858. if err != nil {
  859. writeError(w, http.StatusInternalServerError, err.Error())
  860. return
  861. }
  862. // Propagate rename to all referencing subsystems
  863. if s.bridgeMgr != nil {
  864. s.bridgeMgr.RenameInterface(name, targetName)
  865. }
  866. if s.dhcpMgr != nil {
  867. s.dhcpMgr.RenameInterface(name, targetName)
  868. }
  869. if s.dhcpServerMgr != nil {
  870. s.dhcpServerMgr.RenameInterface(name, targetName)
  871. }
  872. if s.vrrpMgr != nil {
  873. s.vrrpMgr.RenameInterface(name, targetName)
  874. }
  875. if s.routingMgr != nil {
  876. s.routingMgr.RenameInterface(name, targetName)
  877. }
  878. if s.fwMgr != nil {
  879. s.fwMgr.RenameInterface(name, targetName)
  880. }
  881. if s.vpnMgr != nil {
  882. s.vpnMgr.RenameInterface(name, targetName)
  883. }
  884. if s.sdwanMgr != nil {
  885. s.sdwanMgr.RenameInterface(name, targetName)
  886. }
  887. if s.persistMgr != nil {
  888. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  889. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  890. return
  891. }
  892. }
  893. writeJSON(w, http.StatusOK, map[string]any{
  894. "success": true,
  895. "old_name": name,
  896. "new_name": targetName,
  897. "interface": info,
  898. })
  899. }
  900. func (s *Server) handleListVLANs(w http.ResponseWriter, r *http.Request) {
  901. vlans, err := s.netMgr.ListVLANs()
  902. if err != nil {
  903. writeError(w, http.StatusInternalServerError, err.Error())
  904. return
  905. }
  906. writeJSON(w, http.StatusOK, vlans)
  907. }
  908. func (s *Server) handleCreateVLAN(w http.ResponseWriter, r *http.Request) {
  909. var req network.CreateVLANRequest
  910. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  911. writeError(w, http.StatusBadRequest, "invalid request body")
  912. return
  913. }
  914. if req.ParentName == "" || req.VlanID <= 0 {
  915. writeError(w, http.StatusBadRequest, "parent_name and valid vlan_id (1-4094) are required")
  916. return
  917. }
  918. iface, err := s.netMgr.CreateVLAN(req)
  919. if err != nil {
  920. writeError(w, http.StatusInternalServerError, err.Error())
  921. return
  922. }
  923. writeJSON(w, http.StatusCreated, iface)
  924. }
  925. func (s *Server) handleDeleteVLAN(w http.ResponseWriter, r *http.Request) {
  926. name := r.PathValue("name")
  927. if err := s.netMgr.DeleteVLAN(name); err != nil {
  928. writeError(w, http.StatusInternalServerError, err.Error())
  929. return
  930. }
  931. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  932. }
  933. func (s *Server) handleGetDebianConfig(w http.ResponseWriter, r *http.Request) {
  934. cfg, err := s.persistMgr.GenerateDebianConfig()
  935. if err != nil {
  936. writeError(w, http.StatusInternalServerError, err.Error())
  937. return
  938. }
  939. writeJSON(w, http.StatusOK, map[string]string{
  940. "path": network.DebianVlansCfgPath,
  941. "content": cfg,
  942. })
  943. }
  944. func (s *Server) handleSaveDebianConfig(w http.ResponseWriter, r *http.Request) {
  945. cfg, err := s.persistMgr.SaveDebianConfig()
  946. if err != nil {
  947. writeError(w, http.StatusInternalServerError, err.Error())
  948. return
  949. }
  950. writeJSON(w, http.StatusOK, map[string]any{
  951. "success": true,
  952. "path": network.DebianVlansCfgPath,
  953. "content": cfg,
  954. "message": "Configuration saved to " + network.DebianVlansCfgPath,
  955. })
  956. }
  957. func writeJSON(w http.ResponseWriter, status int, data any) {
  958. w.Header().Set("Content-Type", "application/json")
  959. w.WriteHeader(status)
  960. _ = json.NewEncoder(w).Encode(data)
  961. }
  962. func writeError(w http.ResponseWriter, status int, message string) {
  963. writeJSON(w, status, map[string]any{
  964. "error": true,
  965. "message": message,
  966. })
  967. }