api.go 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  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("POST /api/routing/babel/interfaces", s.handleSaveBabelInterface)
  382. s.mux.HandleFunc("DELETE /api/routing/babel/interfaces/{id}", s.handleDeleteBabelInterface)
  383. s.mux.HandleFunc("POST /api/routing/babel/interfaces/{id}/state", s.handleSetBabelInterfaceState)
  384. s.mux.HandleFunc("GET /api/routing/babel/neighbors", s.handleGetBabelNeighbors)
  385. s.mux.HandleFunc("GET /api/routing/babel/routes", s.handleGetBabelRoutes)
  386. // RAdv (/routing radv)
  387. s.mux.HandleFunc("GET /api/routing/radv/instances", s.handleListRAdvInstances)
  388. s.mux.HandleFunc("POST /api/routing/radv/instances", s.handleSaveRAdvInstance)
  389. s.mux.HandleFunc("DELETE /api/routing/radv/instances/{id}", s.handleDeleteRAdvInstance)
  390. s.mux.HandleFunc("POST /api/routing/radv/instances/{id}/state", s.handleSetRAdvInstanceState)
  391. s.mux.HandleFunc("GET /api/routing/radv/interfaces", s.handleListRAdvInterfaces)
  392. s.mux.HandleFunc("POST /api/routing/radv/interfaces", s.handleSaveRAdvInterface)
  393. s.mux.HandleFunc("DELETE /api/routing/radv/interfaces/{id}", s.handleDeleteRAdvInterface)
  394. s.mux.HandleFunc("POST /api/routing/radv/interfaces/{id}/state", s.handleSetRAdvInterfaceState)
  395. // Routing Filters (/routing filter)
  396. s.mux.HandleFunc("GET /api/routing/filters", s.handleListRoutingFilters)
  397. s.mux.HandleFunc("POST /api/routing/filters", s.handleSaveRoutingFilter)
  398. s.mux.HandleFunc("DELETE /api/routing/filters/{id}", s.handleDeleteRoutingFilter)
  399. s.mux.HandleFunc("POST /api/routing/filters/{id}/state", s.handleSetRoutingFilterState)
  400. s.mux.HandleFunc("GET /api/routing/filters/rules", s.handleListRoutingFilters)
  401. s.mux.HandleFunc("POST /api/routing/filters/rules", s.handleSaveRoutingFilter)
  402. s.mux.HandleFunc("DELETE /api/routing/filters/rules/{id}", s.handleDeleteRoutingFilter)
  403. s.mux.HandleFunc("POST /api/routing/filters/rules/{id}/state", s.handleSetRoutingFilterState)
  404. s.mux.HandleFunc("GET /api/routing/filters/community-lists", s.handleListCommunityLists)
  405. s.mux.HandleFunc("POST /api/routing/filters/community-lists", s.handleSaveCommunityList)
  406. s.mux.HandleFunc("DELETE /api/routing/filters/community-lists/{id}", s.handleDeleteCommunityList)
  407. s.mux.HandleFunc("POST /api/routing/filters/community-lists/{id}/state", s.handleSetCommunityListState)
  408. s.mux.HandleFunc("GET /api/routing/filters/as-lists", s.handleListASLists)
  409. s.mux.HandleFunc("POST /api/routing/filters/as-lists", s.handleSaveASList)
  410. s.mux.HandleFunc("DELETE /api/routing/filters/as-lists/{id}", s.handleDeleteASList)
  411. s.mux.HandleFunc("POST /api/routing/filters/as-lists/{id}/state", s.handleSetASListState)
  412. // Troubleshooting Tools (/tool ping, traceroute, mtr, dns, port-probe)
  413. s.mux.HandleFunc("POST /api/tools/ping", s.handleToolsPing)
  414. s.mux.HandleFunc("POST /api/tools/ping/stream", s.handleToolsPingStream)
  415. s.mux.HandleFunc("GET /api/tools/ping/stream", s.handleToolsPingStream)
  416. s.mux.HandleFunc("POST /api/tools/traceroute", s.handleToolsTraceroute)
  417. s.mux.HandleFunc("POST /api/tools/traceroute/stream", s.handleToolsTracerouteStream)
  418. s.mux.HandleFunc("GET /api/tools/traceroute/stream", s.handleToolsTracerouteStream)
  419. s.mux.HandleFunc("POST /api/tools/dns", s.handleToolsDNS)
  420. s.mux.HandleFunc("POST /api/tools/port-probe", s.handleToolsPortProbe)
  421. s.mux.HandleFunc("POST /api/tools/mtr", s.handleToolsMTR)
  422. // Bandwidth Test Tool (/tool bandwidth-test / BwTest)
  423. s.mux.HandleFunc("GET /api/tools/bwtest/server", s.handleGetBwTestServer)
  424. s.mux.HandleFunc("POST /api/tools/bwtest/server", s.handleUpdateBwTestServer)
  425. s.mux.HandleFunc("POST /api/tools/bwtest/start", s.handleStartBwTest)
  426. s.mux.HandleFunc("POST /api/tools/bwtest/stop", s.handleStopBwTest)
  427. s.mux.HandleFunc("GET /api/tools/bwtest/status", s.handleGetBwTestStatus)
  428. s.mux.HandleFunc("GET /api/tools/bwtest/stream", s.handleStreamBwTest)
  429. s.mux.HandleFunc("GET /api/tools/bwtest/history", s.handleGetBwTestHistory)
  430. // DHCP Client (/ip dhcp-client)
  431. s.mux.HandleFunc("GET /api/dhcp/clients", s.handleListDHCPClients)
  432. s.mux.HandleFunc("GET /api/dhcp/clients/summary", s.handleGetDHCPSummary)
  433. s.mux.HandleFunc("POST /api/dhcp/clients", s.handleCreateDHCPClient)
  434. s.mux.HandleFunc("PUT /api/dhcp/clients/{id}", s.handleUpdateDHCPClient)
  435. s.mux.HandleFunc("DELETE /api/dhcp/clients/{id}", s.handleDeleteDHCPClient)
  436. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/state", s.handleToggleDHCPClientState)
  437. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/renew", s.handleRenewDHCPClient)
  438. s.mux.HandleFunc("POST /api/dhcp/clients/{id}/release", s.handleReleaseDHCPClient)
  439. // DHCP Server (/ip dhcp-server)
  440. s.mux.HandleFunc("GET /api/dhcp-server/summary", s.handleGetDHCPServerSummary)
  441. s.mux.HandleFunc("GET /api/dhcp-server/servers", s.handleListDHCPServers)
  442. s.mux.HandleFunc("POST /api/dhcp-server/servers", s.handleSaveDHCPServer)
  443. s.mux.HandleFunc("DELETE /api/dhcp-server/servers/{id}", s.handleDeleteDHCPServer)
  444. s.mux.HandleFunc("POST /api/dhcp-server/servers/{id}/state", s.handleSetDHCPServerState)
  445. s.mux.HandleFunc("GET /api/dhcp-server/networks", s.handleListDHCPNetworks)
  446. s.mux.HandleFunc("POST /api/dhcp-server/networks", s.handleSaveDHCPNetwork)
  447. s.mux.HandleFunc("DELETE /api/dhcp-server/networks/{id}", s.handleDeleteDHCPNetwork)
  448. s.mux.HandleFunc("GET /api/dhcp-server/leases", s.handleListDHCPLeases)
  449. s.mux.HandleFunc("POST /api/dhcp-server/leases/static", s.handleSaveDHCPStaticLease)
  450. s.mux.HandleFunc("DELETE /api/dhcp-server/leases/{id}", s.handleDeleteDHCPStaticLease)
  451. s.mux.HandleFunc("POST /api/dhcp-server/leases/make-static", s.handleMakeDHCPStaticLease)
  452. s.mux.HandleFunc("GET /api/dhcp-server/options", s.handleListDHCPOptions)
  453. s.mux.HandleFunc("POST /api/dhcp-server/options", s.handleSaveDHCPOption)
  454. s.mux.HandleFunc("DELETE /api/dhcp-server/options/{id}", s.handleDeleteDHCPOption)
  455. s.mux.HandleFunc("POST /api/dhcp-server/setup", s.handleRunDHCPSetup)
  456. // VRRP Redundancy & High Availability
  457. s.mux.HandleFunc("GET /api/vrrp/summary", s.handleGetVRRPSummary)
  458. s.mux.HandleFunc("GET /api/vrrp/instances", s.handleListVRRPInstances)
  459. s.mux.HandleFunc("POST /api/vrrp/instances", s.handleSaveVRRPInstance)
  460. s.mux.HandleFunc("DELETE /api/vrrp/instances/{id}", s.handleDeleteVRRPInstance)
  461. s.mux.HandleFunc("POST /api/vrrp/instances/{id}/state", s.handleToggleVRRPState)
  462. // Router DNS & Settings (/ip dns)
  463. s.mux.HandleFunc("GET /api/dns/settings", s.handleGetDNSSettings)
  464. s.mux.HandleFunc("POST /api/dns/settings", s.handleSaveDNSSettings)
  465. s.mux.HandleFunc("GET /api/dns/static", s.handleListDNSStatic)
  466. s.mux.HandleFunc("POST /api/dns/static", s.handleSaveDNSStatic)
  467. s.mux.HandleFunc("DELETE /api/dns/static/{id}", s.handleDeleteDNSStatic)
  468. s.mux.HandleFunc("POST /api/dns/static/{id}/state", s.handleSetDNSStaticState)
  469. s.mux.HandleFunc("POST /api/dns/flush", s.handleFlushDNSCache)
  470. s.mux.HandleFunc("GET /api/dns/status", s.handleGetDNSStatus)
  471. // System Settings (/system identity & clock)
  472. s.mux.HandleFunc("GET /api/system/settings", s.handleGetSystemSettings)
  473. s.mux.HandleFunc("POST /api/system/settings", s.handleSaveSystemSettings)
  474. // Management Services (/ip service)
  475. s.mux.HandleFunc("GET /api/system/services", s.handleListServices)
  476. s.mux.HandleFunc("POST /api/system/services/{name}", s.handleUpdateService)
  477. s.mux.HandleFunc("PUT /api/system/services/{name}", s.handleUpdateService)
  478. s.mux.HandleFunc("POST /api/system/services/{name}/state", s.handleSetServiceState)
  479. // Certificate Management (/certificate)
  480. s.mux.HandleFunc("GET /api/certificates", s.handleListCertificates)
  481. s.mux.HandleFunc("POST /api/certificates/ca", s.handleCreateCA)
  482. s.mux.HandleFunc("POST /api/certificates/generate", s.handleCreateCertificate)
  483. s.mux.HandleFunc("POST /api/certificates/import", s.handleImportCertificate)
  484. s.mux.HandleFunc("POST /api/certificates/letsencrypt", s.handleIssueLetsEncrypt)
  485. s.mux.HandleFunc("GET /api/certificates/{name}", s.handleGetCertificate)
  486. s.mux.HandleFunc("GET /api/certificates/{name}/export", s.handleExportCertificate)
  487. s.mux.HandleFunc("DELETE /api/certificates/{name}", s.handleDeleteCertificate)
  488. // Reverse Proxy & Application Gateway (/reverse-proxy)
  489. s.mux.HandleFunc("GET /api/proxy/config", s.handleGetProxyConfig)
  490. s.mux.HandleFunc("POST /api/proxy/config", s.handleUpdateProxyConfig)
  491. s.mux.HandleFunc("GET /api/proxy/rules", s.handleListProxyRules)
  492. s.mux.HandleFunc("POST /api/proxy/rules", s.handleSaveProxyRule)
  493. s.mux.HandleFunc("GET /api/proxy/rules/{id}", s.handleGetProxyRule)
  494. s.mux.HandleFunc("PUT /api/proxy/rules/{id}", s.handleSaveProxyRule)
  495. s.mux.HandleFunc("DELETE /api/proxy/rules/{id}", s.handleDeleteProxyRule)
  496. s.mux.HandleFunc("POST /api/proxy/rules/{id}/state", s.handleSetProxyRuleState)
  497. s.mux.HandleFunc("GET /api/proxy/status", s.handleGetProxyStatus)
  498. // ACME HTTP-01 Challenge responder for Let's Encrypt
  499. s.mux.HandleFunc("GET /.well-known/acme-challenge/{token}", s.handleACMEChallenge)
  500. // Logging (/log)
  501. s.mux.HandleFunc("GET /api/system/logs", s.handleListLogs)
  502. // Authentication & User Management
  503. s.mux.HandleFunc("POST /api/auth/login", s.handleLogin)
  504. s.mux.HandleFunc("POST /api/auth/logout", s.handleLogout)
  505. s.mux.HandleFunc("GET /api/auth/me", s.handleGetMe)
  506. s.mux.HandleFunc("POST /api/auth/change-password", s.handleChangePassword)
  507. s.mux.HandleFunc("GET /api/users", s.handleListUsers)
  508. s.mux.HandleFunc("POST /api/users", s.handleCreateUser)
  509. s.mux.HandleFunc("PUT /api/users/{username}", s.handleUpdateUser)
  510. s.mux.HandleFunc("DELETE /api/users/{username}", s.handleDeleteUser)
  511. s.mux.HandleFunc("POST /api/users/{username}/password", s.handleAdminResetPassword)
  512. // Web UI Static Files
  513. if s.fsHandler != nil {
  514. s.mux.Handle("/", s.fsHandler)
  515. }
  516. }
  517. func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  518. // Enable CORS for local development/testing
  519. w.Header().Set("Access-Control-Allow-Origin", "*")
  520. w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS, PUT")
  521. w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
  522. if r.Method == http.MethodOptions {
  523. w.WriteHeader(http.StatusOK)
  524. return
  525. }
  526. path := r.URL.Path
  527. // 1. ACME challenge responder MUST always be accessible on port 80
  528. if strings.HasPrefix(path, "/.well-known/acme-challenge/") {
  529. s.mux.ServeHTTP(w, r)
  530. return
  531. }
  532. // 2. Reverse Proxy interceptor (prior to management checks, auth and static handlers)
  533. if s.proxyMgr != nil && s.proxyMgr.IsEnabled() {
  534. isMgmtAPI := strings.HasPrefix(path, "/api/")
  535. if !isMgmtAPI {
  536. if s.proxyMgr.HandleHTTP(w, r) {
  537. return
  538. }
  539. }
  540. }
  541. // 3. HTTP and HTTPS independent management service settings for DebTik Web UI / API
  542. if s.servicesMgr != nil {
  543. service := "www"
  544. if r.TLS != nil {
  545. service = "www-ssl"
  546. }
  547. if www, ok := s.servicesMgr.GetService(service); ok && www.Disabled {
  548. w.Header().Set("Content-Type", "text/plain; charset=utf-8")
  549. w.WriteHeader(http.StatusServiceUnavailable)
  550. _, _ = fmt.Fprintf(w, "503 Service Unavailable: %s management service is disabled\n", service)
  551. return
  552. }
  553. }
  554. // Authentication protection for API routes
  555. if strings.HasPrefix(path, "/api/") {
  556. // Public endpoint: Login
  557. if path != "/api/auth/login" {
  558. token := extractToken(r)
  559. sess, err := s.authMgr.ValidateSession(token)
  560. if err != nil {
  561. writeJSON(w, http.StatusUnauthorized, map[string]any{
  562. "error": true,
  563. "message": "Authentication required",
  564. "login_required": true,
  565. })
  566. return
  567. }
  568. // Admin privileges required for /api/users management
  569. if strings.HasPrefix(path, "/api/users") && sess.Role != auth.RoleFull {
  570. writeJSON(w, http.StatusForbidden, map[string]any{
  571. "error": true,
  572. "message": "Administrative privileges required",
  573. })
  574. return
  575. }
  576. // Read-only role protection: forbid mutating operations (except logout & self password change)
  577. if sess.Role == auth.RoleRead && r.Method != http.MethodGet && r.Method != http.MethodHead {
  578. if path != "/api/auth/logout" && path != "/api/auth/change-password" {
  579. writeJSON(w, http.StatusForbidden, map[string]any{
  580. "error": true,
  581. "message": "Read-only account cannot perform configuration modifications",
  582. })
  583. return
  584. }
  585. }
  586. // Inject session into request context
  587. ctx := context.WithValue(r.Context(), sessionContextKey, sess)
  588. r = r.WithContext(ctx)
  589. }
  590. }
  591. s.mux.ServeHTTP(w, r)
  592. }
  593. func (s *Server) handleSystemStatus(w http.ResponseWriter, r *http.Request) {
  594. status, err := system.GetStatus()
  595. if err != nil {
  596. writeError(w, http.StatusInternalServerError, err.Error())
  597. return
  598. }
  599. ifaces, err := s.netMgr.ListInterfaces()
  600. if err == nil {
  601. status.TotalIfaces = len(ifaces)
  602. for _, iface := range ifaces {
  603. if iface.State == "UP" {
  604. status.UpIfaces++
  605. } else {
  606. status.DownIfaces++
  607. }
  608. if iface.IsVLAN {
  609. status.VlanIfaces++
  610. }
  611. }
  612. }
  613. writeJSON(w, http.StatusOK, status)
  614. }
  615. func (s *Server) handleRoutes(w http.ResponseWriter, r *http.Request) {
  616. routes, err := system.GetRoutingTable()
  617. if err != nil {
  618. writeError(w, http.StatusInternalServerError, err.Error())
  619. return
  620. }
  621. writeJSON(w, http.StatusOK, routes)
  622. }
  623. func (s *Server) handleListInterfaces(w http.ResponseWriter, r *http.Request) {
  624. ifaces, err := s.netMgr.ListInterfaces()
  625. if err != nil {
  626. writeError(w, http.StatusInternalServerError, err.Error())
  627. return
  628. }
  629. writeJSON(w, http.StatusOK, ifaces)
  630. }
  631. func (s *Server) handleInterfacesTrafficStream(w http.ResponseWriter, r *http.Request) {
  632. flusher, ok := w.(http.Flusher)
  633. if !ok {
  634. http.Error(w, "Streaming unsupported", http.StatusInternalServerError)
  635. return
  636. }
  637. w.Header().Set("Content-Type", "text/event-stream")
  638. w.Header().Set("Cache-Control", "no-cache")
  639. w.Header().Set("Connection", "keep-alive")
  640. w.Header().Set("Access-Control-Allow-Origin", "*")
  641. ch := s.netMgr.SubscribeTraffic()
  642. defer s.netMgr.UnsubscribeTraffic(ch)
  643. // Send immediate initial snapshot
  644. initSnap := s.netMgr.GetLatestTrafficSnapshot()
  645. if initSnap != nil {
  646. data, _ := json.Marshal(initSnap)
  647. fmt.Fprintf(w, "data: %s\n\n", data)
  648. flusher.Flush()
  649. }
  650. notify := r.Context().Done()
  651. for {
  652. select {
  653. case <-notify:
  654. return
  655. case snap, ok := <-ch:
  656. if !ok {
  657. return
  658. }
  659. data, _ := json.Marshal(snap)
  660. fmt.Fprintf(w, "data: %s\n\n", data)
  661. flusher.Flush()
  662. }
  663. }
  664. }
  665. func (s *Server) handleInterfacesTrafficRates(w http.ResponseWriter, r *http.Request) {
  666. snap := s.netMgr.GetLatestTrafficSnapshot()
  667. if snap == nil {
  668. snap = &network.TrafficSnapshot{
  669. Timestamp: time.Now().Unix(),
  670. Interfaces: make(map[string]network.InterfaceLiveRate),
  671. }
  672. }
  673. writeJSON(w, http.StatusOK, snap)
  674. }
  675. func (s *Server) handleGetInterface(w http.ResponseWriter, r *http.Request) {
  676. name := r.PathValue("name")
  677. iface, err := s.netMgr.GetInterface(name)
  678. if err != nil {
  679. writeError(w, http.StatusNotFound, err.Error())
  680. return
  681. }
  682. writeJSON(w, http.StatusOK, iface)
  683. }
  684. func (s *Server) handleSetInterfaceState(w http.ResponseWriter, r *http.Request) {
  685. name := r.PathValue("name")
  686. var req struct {
  687. State string `json:"state"` // "up" or "down"
  688. }
  689. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  690. writeError(w, http.StatusBadRequest, "invalid request body")
  691. return
  692. }
  693. up := strings.ToLower(req.State) == "up"
  694. if err := s.netMgr.SetInterfaceState(name, up); err != nil {
  695. writeError(w, http.StatusInternalServerError, err.Error())
  696. return
  697. }
  698. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "state": req.State})
  699. }
  700. func (s *Server) handleSetInterfaceMTU(w http.ResponseWriter, r *http.Request) {
  701. name := r.PathValue("name")
  702. var req struct {
  703. MTU int `json:"mtu"`
  704. }
  705. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  706. writeError(w, http.StatusBadRequest, "invalid request body")
  707. return
  708. }
  709. if err := s.netMgr.SetInterfaceMTU(name, req.MTU); err != nil {
  710. writeError(w, http.StatusInternalServerError, err.Error())
  711. return
  712. }
  713. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "mtu": req.MTU})
  714. }
  715. func (s *Server) handleAddInterfaceIP(w http.ResponseWriter, r *http.Request) {
  716. name := r.PathValue("name")
  717. var req network.AddIPRequest
  718. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  719. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  720. return
  721. }
  722. if err := s.netMgr.AddInterfaceIP(name, req.IP); err != nil {
  723. writeError(w, http.StatusInternalServerError, err.Error())
  724. return
  725. }
  726. // If this interface is a bridge, also sync to BridgeManager persistent config
  727. if s.bridgeMgr != nil {
  728. _ = s.bridgeMgr.AddBridgeIP(name, req.IP)
  729. }
  730. // Persist to Debian network configuration
  731. if s.persistMgr != nil {
  732. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  733. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  734. return
  735. }
  736. }
  737. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  738. }
  739. func (s *Server) handleDeleteInterfaceIP(w http.ResponseWriter, r *http.Request) {
  740. name := r.PathValue("name")
  741. var req network.AddIPRequest
  742. if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.IP == "" {
  743. writeError(w, http.StatusBadRequest, "missing or invalid 'ip' parameter")
  744. return
  745. }
  746. if err := s.netMgr.DeleteInterfaceIP(name, req.IP); err != nil {
  747. writeError(w, http.StatusInternalServerError, err.Error())
  748. return
  749. }
  750. // If this interface is a bridge, also delete from BridgeManager persistent config
  751. if s.bridgeMgr != nil {
  752. _ = s.bridgeMgr.DeleteBridgeIP(name, req.IP)
  753. }
  754. // Persist to Debian network configuration
  755. if s.persistMgr != nil {
  756. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  757. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  758. return
  759. }
  760. }
  761. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "ip": req.IP})
  762. }
  763. func (s *Server) handleSetInterfaceMetadata(w http.ResponseWriter, r *http.Request) {
  764. name := r.PathValue("name")
  765. var req struct {
  766. Role string `json:"role"`
  767. Description string `json:"description"`
  768. }
  769. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  770. writeError(w, http.StatusBadRequest, "invalid request body")
  771. return
  772. }
  773. previous := s.metaMgr.Get(name)
  774. if err := s.metaMgr.Set(name, req.Role, req.Description); err != nil {
  775. writeError(w, http.StatusInternalServerError, err.Error())
  776. return
  777. }
  778. if s.fwMgr != nil {
  779. if err := s.fwMgr.Reapply(); err != nil {
  780. _ = s.metaMgr.Set(name, previous.Role, previous.Description)
  781. _ = s.fwMgr.Reapply()
  782. writeError(w, 500, "Interface role update rolled back: "+err.Error())
  783. return
  784. }
  785. }
  786. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name, "role": req.Role, "description": req.Description})
  787. }
  788. func (s *Server) handleListRoleTags(w http.ResponseWriter, r *http.Request) {
  789. roles := s.metaMgr.GetRolesWithUsage()
  790. writeJSON(w, http.StatusOK, roles)
  791. }
  792. func (s *Server) handleSaveRoleTag(w http.ResponseWriter, r *http.Request) {
  793. var req network.RoleTag
  794. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  795. writeError(w, http.StatusBadRequest, "invalid request body")
  796. return
  797. }
  798. id := r.PathValue("id")
  799. if id != "" {
  800. req.ID = id
  801. }
  802. saved, err := s.metaMgr.SaveRole(req)
  803. if err != nil {
  804. writeError(w, http.StatusBadRequest, err.Error())
  805. return
  806. }
  807. writeJSON(w, http.StatusOK, saved)
  808. }
  809. func (s *Server) handleDeleteRoleTag(w http.ResponseWriter, r *http.Request) {
  810. id := r.PathValue("id")
  811. if id == "" {
  812. writeError(w, http.StatusBadRequest, "missing role ID")
  813. return
  814. }
  815. if err := s.metaMgr.DeleteRole(id); err != nil {
  816. writeError(w, http.StatusBadRequest, err.Error())
  817. return
  818. }
  819. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted", "id": id})
  820. }
  821. func (s *Server) handleRenameInterface(w http.ResponseWriter, r *http.Request) {
  822. name := r.PathValue("name")
  823. var req struct {
  824. Name string `json:"name"`
  825. NewName string `json:"new_name"`
  826. }
  827. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  828. writeError(w, http.StatusBadRequest, "invalid request body")
  829. return
  830. }
  831. targetName := req.NewName
  832. if targetName == "" {
  833. targetName = req.Name
  834. }
  835. targetName = strings.TrimSpace(targetName)
  836. if targetName == "" {
  837. writeError(w, http.StatusBadRequest, "new interface name is required")
  838. return
  839. }
  840. info, err := s.netMgr.RenameInterface(name, targetName)
  841. if err != nil {
  842. writeError(w, http.StatusInternalServerError, err.Error())
  843. return
  844. }
  845. // Propagate rename to all referencing subsystems
  846. if s.bridgeMgr != nil {
  847. s.bridgeMgr.RenameInterface(name, targetName)
  848. }
  849. if s.dhcpMgr != nil {
  850. s.dhcpMgr.RenameInterface(name, targetName)
  851. }
  852. if s.dhcpServerMgr != nil {
  853. s.dhcpServerMgr.RenameInterface(name, targetName)
  854. }
  855. if s.vrrpMgr != nil {
  856. s.vrrpMgr.RenameInterface(name, targetName)
  857. }
  858. if s.routingMgr != nil {
  859. s.routingMgr.RenameInterface(name, targetName)
  860. }
  861. if s.fwMgr != nil {
  862. s.fwMgr.RenameInterface(name, targetName)
  863. }
  864. if s.vpnMgr != nil {
  865. s.vpnMgr.RenameInterface(name, targetName)
  866. }
  867. if s.sdwanMgr != nil {
  868. s.sdwanMgr.RenameInterface(name, targetName)
  869. }
  870. if s.persistMgr != nil {
  871. if _, err := s.persistMgr.SaveDebianConfig(); err != nil {
  872. writeError(w, 500, "Live change applied but persistence failed: "+err.Error())
  873. return
  874. }
  875. }
  876. writeJSON(w, http.StatusOK, map[string]any{
  877. "success": true,
  878. "old_name": name,
  879. "new_name": targetName,
  880. "interface": info,
  881. })
  882. }
  883. func (s *Server) handleListVLANs(w http.ResponseWriter, r *http.Request) {
  884. vlans, err := s.netMgr.ListVLANs()
  885. if err != nil {
  886. writeError(w, http.StatusInternalServerError, err.Error())
  887. return
  888. }
  889. writeJSON(w, http.StatusOK, vlans)
  890. }
  891. func (s *Server) handleCreateVLAN(w http.ResponseWriter, r *http.Request) {
  892. var req network.CreateVLANRequest
  893. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  894. writeError(w, http.StatusBadRequest, "invalid request body")
  895. return
  896. }
  897. if req.ParentName == "" || req.VlanID <= 0 {
  898. writeError(w, http.StatusBadRequest, "parent_name and valid vlan_id (1-4094) are required")
  899. return
  900. }
  901. iface, err := s.netMgr.CreateVLAN(req)
  902. if err != nil {
  903. writeError(w, http.StatusInternalServerError, err.Error())
  904. return
  905. }
  906. writeJSON(w, http.StatusCreated, iface)
  907. }
  908. func (s *Server) handleDeleteVLAN(w http.ResponseWriter, r *http.Request) {
  909. name := r.PathValue("name")
  910. if err := s.netMgr.DeleteVLAN(name); err != nil {
  911. writeError(w, http.StatusInternalServerError, err.Error())
  912. return
  913. }
  914. writeJSON(w, http.StatusOK, map[string]any{"success": true, "name": name})
  915. }
  916. func (s *Server) handleGetDebianConfig(w http.ResponseWriter, r *http.Request) {
  917. cfg, err := s.persistMgr.GenerateDebianConfig()
  918. if err != nil {
  919. writeError(w, http.StatusInternalServerError, err.Error())
  920. return
  921. }
  922. writeJSON(w, http.StatusOK, map[string]string{
  923. "path": network.DebianVlansCfgPath,
  924. "content": cfg,
  925. })
  926. }
  927. func (s *Server) handleSaveDebianConfig(w http.ResponseWriter, r *http.Request) {
  928. cfg, err := s.persistMgr.SaveDebianConfig()
  929. if err != nil {
  930. writeError(w, http.StatusInternalServerError, err.Error())
  931. return
  932. }
  933. writeJSON(w, http.StatusOK, map[string]any{
  934. "success": true,
  935. "path": network.DebianVlansCfgPath,
  936. "content": cfg,
  937. "message": "Configuration saved to " + network.DebianVlansCfgPath,
  938. })
  939. }
  940. func writeJSON(w http.ResponseWriter, status int, data any) {
  941. w.Header().Set("Content-Type", "application/json")
  942. w.WriteHeader(status)
  943. _ = json.NewEncoder(w).Encode(data)
  944. }
  945. func writeError(w http.ResponseWriter, status int, message string) {
  946. writeJSON(w, status, map[string]any{
  947. "error": true,
  948. "message": message,
  949. })
  950. }