api.go 48 KB

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