dynamic_routing_handlers.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "debtik/internal/dynamicrouting"
  7. )
  8. // Summary
  9. func (s *Server) handleDynamicRoutingSummary(w http.ResponseWriter, r *http.Request) {
  10. summary := s.dynRoutingMgr.GetSummary()
  11. writeJSON(w, http.StatusOK, summary)
  12. }
  13. // -------------------------------------------------------------
  14. // BGP Handlers
  15. // -------------------------------------------------------------
  16. func (s *Server) handleGetBGPInstance(w http.ResponseWriter, r *http.Request) {
  17. inst := s.dynRoutingMgr.GetBGPInstance()
  18. writeJSON(w, http.StatusOK, inst)
  19. }
  20. func (s *Server) handleUpdateBGPInstance(w http.ResponseWriter, r *http.Request) {
  21. var inst dynamicrouting.BGPInstance
  22. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  23. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  24. return
  25. }
  26. if err := s.dynRoutingMgr.UpdateBGPInstance(inst); err != nil {
  27. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  28. return
  29. }
  30. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBGPInstance())
  31. }
  32. func (s *Server) handleListBGPPeers(w http.ResponseWriter, r *http.Request) {
  33. peers := s.dynRoutingMgr.ListBGPPeers()
  34. if peers == nil {
  35. peers = []dynamicrouting.BGPPeer{}
  36. }
  37. writeJSON(w, http.StatusOK, peers)
  38. }
  39. func (s *Server) handleSaveBGPPeer(w http.ResponseWriter, r *http.Request) {
  40. var peer dynamicrouting.BGPPeer
  41. if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
  42. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  43. return
  44. }
  45. if peer.RemoteAddress == "" || peer.RemoteAS == 0 {
  46. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "remote_address and remote_as are required"})
  47. return
  48. }
  49. if err := s.dynRoutingMgr.SaveBGPPeer(&peer); err != nil {
  50. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  51. return
  52. }
  53. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "peer": peer})
  54. }
  55. func (s *Server) handleDeleteBGPPeer(w http.ResponseWriter, r *http.Request) {
  56. id := r.PathValue("id")
  57. if id == "" {
  58. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing peer id"})
  59. return
  60. }
  61. if err := s.dynRoutingMgr.DeleteBGPPeer(id); err != nil {
  62. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  63. return
  64. }
  65. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  66. }
  67. func (s *Server) handleSetBGPPeerState(w http.ResponseWriter, r *http.Request) {
  68. id := r.PathValue("id")
  69. var req struct {
  70. Disabled bool `json:"disabled"`
  71. }
  72. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  73. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  74. return
  75. }
  76. if err := s.dynRoutingMgr.SetBGPPeerState(id, req.Disabled); err != nil {
  77. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  78. return
  79. }
  80. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  81. }
  82. func (s *Server) handleListBGPNetworks(w http.ResponseWriter, r *http.Request) {
  83. nets := s.dynRoutingMgr.ListBGPNetworks()
  84. if nets == nil {
  85. nets = []dynamicrouting.BGPNetwork{}
  86. }
  87. writeJSON(w, http.StatusOK, nets)
  88. }
  89. func (s *Server) handleSaveBGPNetwork(w http.ResponseWriter, r *http.Request) {
  90. var net dynamicrouting.BGPNetwork
  91. if err := json.NewDecoder(r.Body).Decode(&net); err != nil {
  92. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  93. return
  94. }
  95. if net.Network == "" {
  96. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "network (CIDR) is required"})
  97. return
  98. }
  99. if err := s.dynRoutingMgr.SaveBGPNetwork(net); err != nil {
  100. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  101. return
  102. }
  103. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "network": net})
  104. }
  105. func (s *Server) handleDeleteBGPNetwork(w http.ResponseWriter, r *http.Request) {
  106. id := r.PathValue("id")
  107. if id == "" {
  108. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing network id"})
  109. return
  110. }
  111. if err := s.dynRoutingMgr.DeleteBGPNetwork(id); err != nil {
  112. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  113. return
  114. }
  115. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  116. }
  117. func (s *Server) handleGetBGPRoutes(w http.ResponseWriter, r *http.Request) {
  118. routes := s.dynRoutingMgr.GetBGPRoutes()
  119. if routes == nil {
  120. routes = []dynamicrouting.BGPRoute{}
  121. }
  122. writeJSON(w, http.StatusOK, routes)
  123. }
  124. // -------------------------------------------------------------
  125. // OSPF Handlers (RouterOS v7 Style OSPF)
  126. // -------------------------------------------------------------
  127. func (s *Server) handleListOSPFInstances(w http.ResponseWriter, r *http.Request) {
  128. insts := s.dynRoutingMgr.ListOSPFInstances()
  129. if insts == nil {
  130. insts = []dynamicrouting.OSPFInstance{}
  131. }
  132. writeJSON(w, http.StatusOK, insts)
  133. }
  134. func (s *Server) handleSaveOSPFInstance(w http.ResponseWriter, r *http.Request) {
  135. var inst dynamicrouting.OSPFInstance
  136. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  137. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  138. return
  139. }
  140. if err := s.dynRoutingMgr.SaveOSPFInstance(&inst); err != nil {
  141. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  142. return
  143. }
  144. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "instance": inst})
  145. }
  146. func (s *Server) handleDeleteOSPFInstance(w http.ResponseWriter, r *http.Request) {
  147. id := r.PathValue("id")
  148. if id == "" {
  149. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing instance id"})
  150. return
  151. }
  152. if err := s.dynRoutingMgr.DeleteOSPFInstance(id); err != nil {
  153. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  154. return
  155. }
  156. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  157. }
  158. func (s *Server) handleSetOSPFInstanceState(w http.ResponseWriter, r *http.Request) {
  159. id := r.PathValue("id")
  160. var req struct {
  161. Disabled bool `json:"disabled"`
  162. }
  163. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  164. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  165. return
  166. }
  167. if err := s.dynRoutingMgr.SetOSPFInstanceState(id, req.Disabled); err != nil {
  168. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  169. return
  170. }
  171. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  172. }
  173. func (s *Server) handleGetOSPFInstance(w http.ResponseWriter, r *http.Request) {
  174. inst := s.dynRoutingMgr.GetOSPFInstance()
  175. writeJSON(w, http.StatusOK, inst)
  176. }
  177. func (s *Server) handleUpdateOSPFInstance(w http.ResponseWriter, r *http.Request) {
  178. var inst dynamicrouting.OSPFInstance
  179. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  180. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  181. return
  182. }
  183. if err := s.dynRoutingMgr.UpdateOSPFInstance(inst); err != nil {
  184. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  185. return
  186. }
  187. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetOSPFInstance())
  188. }
  189. func (s *Server) handleListOSPFAreas(w http.ResponseWriter, r *http.Request) {
  190. areas := s.dynRoutingMgr.ListOSPFAreas()
  191. if areas == nil {
  192. areas = []dynamicrouting.OSPFArea{}
  193. }
  194. writeJSON(w, http.StatusOK, areas)
  195. }
  196. func (s *Server) handleSaveOSPFArea(w http.ResponseWriter, r *http.Request) {
  197. var area dynamicrouting.OSPFArea
  198. if err := json.NewDecoder(r.Body).Decode(&area); err != nil {
  199. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  200. return
  201. }
  202. if area.AreaID == "" {
  203. area.AreaID = "0.0.0.0"
  204. }
  205. if err := s.dynRoutingMgr.SaveOSPFArea(area); err != nil {
  206. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  207. return
  208. }
  209. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area": area})
  210. }
  211. func (s *Server) handleDeleteOSPFArea(w http.ResponseWriter, r *http.Request) {
  212. id := r.PathValue("id")
  213. if id == "" {
  214. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing area id"})
  215. return
  216. }
  217. if err := s.dynRoutingMgr.DeleteOSPFArea(id); err != nil {
  218. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  219. return
  220. }
  221. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  222. }
  223. func (s *Server) handleSetOSPFAreaState(w http.ResponseWriter, r *http.Request) {
  224. id := r.PathValue("id")
  225. var req struct {
  226. Disabled bool `json:"disabled"`
  227. }
  228. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  229. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  230. return
  231. }
  232. if err := s.dynRoutingMgr.SetOSPFAreaState(id, req.Disabled); err != nil {
  233. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  234. return
  235. }
  236. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  237. }
  238. // Area Ranges
  239. func (s *Server) handleListOSPFAreaRanges(w http.ResponseWriter, r *http.Request) {
  240. ranges := s.dynRoutingMgr.ListOSPFAreaRanges()
  241. if ranges == nil {
  242. ranges = []dynamicrouting.OSPFAreaRange{}
  243. }
  244. writeJSON(w, http.StatusOK, ranges)
  245. }
  246. func (s *Server) handleSaveOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
  247. var ar dynamicrouting.OSPFAreaRange
  248. if err := json.NewDecoder(r.Body).Decode(&ar); err != nil {
  249. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  250. return
  251. }
  252. if ar.Prefix == "" {
  253. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Prefix is required"})
  254. return
  255. }
  256. if err := s.dynRoutingMgr.SaveOSPFAreaRange(ar); err != nil {
  257. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  258. return
  259. }
  260. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area_range": ar})
  261. }
  262. func (s *Server) handleDeleteOSPFAreaRange(w http.ResponseWriter, r *http.Request) {
  263. id := r.PathValue("id")
  264. if id == "" {
  265. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing range id"})
  266. return
  267. }
  268. if err := s.dynRoutingMgr.DeleteOSPFAreaRange(id); err != nil {
  269. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  270. return
  271. }
  272. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  273. }
  274. // Interface Templates
  275. func (s *Server) handleListOSPFInterfaces(w http.ResponseWriter, r *http.Request) {
  276. ifaces := s.dynRoutingMgr.ListOSPFInterfaces()
  277. if ifaces == nil {
  278. ifaces = []dynamicrouting.OSPFInterface{}
  279. }
  280. writeJSON(w, http.StatusOK, ifaces)
  281. }
  282. func (s *Server) handleSaveOSPFInterface(w http.ResponseWriter, r *http.Request) {
  283. var iface dynamicrouting.OSPFInterface
  284. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  285. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  286. return
  287. }
  288. if err := s.dynRoutingMgr.SaveOSPFInterface(iface); err != nil {
  289. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  290. return
  291. }
  292. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "interface": iface})
  293. }
  294. func (s *Server) handleDeleteOSPFInterface(w http.ResponseWriter, r *http.Request) {
  295. id := r.PathValue("id")
  296. if id == "" {
  297. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface id"})
  298. return
  299. }
  300. if err := s.dynRoutingMgr.DeleteOSPFInterface(id); err != nil {
  301. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  302. return
  303. }
  304. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  305. }
  306. func (s *Server) handleSetOSPFInterfaceState(w http.ResponseWriter, r *http.Request) {
  307. id := r.PathValue("id")
  308. var req struct {
  309. Disabled bool `json:"disabled"`
  310. }
  311. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  312. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  313. return
  314. }
  315. if err := s.dynRoutingMgr.SetOSPFInterfaceState(id, req.Disabled); err != nil {
  316. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  317. return
  318. }
  319. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  320. }
  321. // Live Dynamic Interfaces
  322. func (s *Server) handleGetOSPFInterfacesLive(w http.ResponseWriter, r *http.Request) {
  323. live := s.dynRoutingMgr.GetOSPFInterfacesLive()
  324. if live == nil {
  325. live = []dynamicrouting.OSPFInterfaceLive{}
  326. }
  327. writeJSON(w, http.StatusOK, live)
  328. }
  329. // Static Neighbors
  330. func (s *Server) handleListOSPFStaticNeighbors(w http.ResponseWriter, r *http.Request) {
  331. nbrs := s.dynRoutingMgr.ListOSPFStaticNeighbors()
  332. if nbrs == nil {
  333. nbrs = []dynamicrouting.OSPFStaticNeighbor{}
  334. }
  335. writeJSON(w, http.StatusOK, nbrs)
  336. }
  337. func (s *Server) handleSaveOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
  338. var nbr dynamicrouting.OSPFStaticNeighbor
  339. if err := json.NewDecoder(r.Body).Decode(&nbr); err != nil {
  340. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  341. return
  342. }
  343. if nbr.Address == "" {
  344. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Address is required"})
  345. return
  346. }
  347. if err := s.dynRoutingMgr.SaveOSPFStaticNeighbor(nbr); err != nil {
  348. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  349. return
  350. }
  351. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "neighbor": nbr})
  352. }
  353. func (s *Server) handleDeleteOSPFStaticNeighbor(w http.ResponseWriter, r *http.Request) {
  354. id := r.PathValue("id")
  355. if id == "" {
  356. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
  357. return
  358. }
  359. if err := s.dynRoutingMgr.DeleteOSPFStaticNeighbor(id); err != nil {
  360. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  361. return
  362. }
  363. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  364. }
  365. func (s *Server) handleSetOSPFStaticNeighborState(w http.ResponseWriter, r *http.Request) {
  366. id := r.PathValue("id")
  367. if id == "" {
  368. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing static neighbor id"})
  369. return
  370. }
  371. var req struct {
  372. Disabled bool `json:"disabled"`
  373. }
  374. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  375. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  376. return
  377. }
  378. if err := s.dynRoutingMgr.SetOSPFStaticNeighborState(id, req.Disabled); err != nil {
  379. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  380. return
  381. }
  382. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  383. }
  384. // Live Neighbors
  385. func (s *Server) handleGetOSPFNeighbors(w http.ResponseWriter, r *http.Request) {
  386. nbrs := s.dynRoutingMgr.GetOSPFNeighbors()
  387. if nbrs == nil {
  388. nbrs = []dynamicrouting.OSPFNeighbor{}
  389. }
  390. writeJSON(w, http.StatusOK, nbrs)
  391. }
  392. // Live LSA Database
  393. func (s *Server) handleGetOSPFLSA(w http.ResponseWriter, r *http.Request) {
  394. lsa := s.dynRoutingMgr.GetOSPFLSA()
  395. if lsa == nil {
  396. lsa = []dynamicrouting.OSPFLSA{}
  397. }
  398. writeJSON(w, http.StatusOK, lsa)
  399. }
  400. func (s *Server) handleGetOSPFRoutes(w http.ResponseWriter, r *http.Request) {
  401. routes := s.dynRoutingMgr.GetOSPFRoutes()
  402. if routes == nil {
  403. routes = []dynamicrouting.OSPFRoute{}
  404. }
  405. writeJSON(w, http.StatusOK, routes)
  406. }
  407. // -------------------------------------------------------------
  408. // Routing Filters Handlers (/routing filter)
  409. // -------------------------------------------------------------
  410. func (s *Server) handleListRoutingFilters(w http.ResponseWriter, r *http.Request) {
  411. filters := s.dynRoutingMgr.ListRoutingFilters()
  412. if filters == nil {
  413. filters = []dynamicrouting.RoutingFilterRule{}
  414. }
  415. writeJSON(w, http.StatusOK, filters)
  416. }
  417. func (s *Server) handleSaveRoutingFilter(w http.ResponseWriter, r *http.Request) {
  418. var rule dynamicrouting.RoutingFilterRule
  419. if err := json.NewDecoder(r.Body).Decode(&rule); err != nil {
  420. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  421. return
  422. }
  423. if rule.Chain == "" {
  424. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Chain name is required"})
  425. return
  426. }
  427. if rule.Action == "" && rule.Rule == "" {
  428. rule.Action = "accept"
  429. }
  430. if err := s.dynRoutingMgr.SaveRoutingFilter(&rule); err != nil {
  431. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  432. return
  433. }
  434. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": rule.ID})
  435. }
  436. func (s *Server) handleDeleteRoutingFilter(w http.ResponseWriter, r *http.Request) {
  437. id := r.PathValue("id")
  438. if id == "" {
  439. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
  440. return
  441. }
  442. if err := s.dynRoutingMgr.DeleteRoutingFilter(id); err != nil {
  443. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  444. return
  445. }
  446. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  447. }
  448. func (s *Server) handleSetRoutingFilterState(w http.ResponseWriter, r *http.Request) {
  449. id := r.PathValue("id")
  450. if id == "" {
  451. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing filter rule ID"})
  452. return
  453. }
  454. var req struct {
  455. Disabled bool `json:"disabled"`
  456. }
  457. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  458. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  459. return
  460. }
  461. if err := s.dynRoutingMgr.SetRoutingFilterState(id, req.Disabled); err != nil {
  462. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  463. return
  464. }
  465. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  466. }
  467. // -------------------------------------------------------------
  468. // Community Lists Handlers (/routing filter community-list)
  469. // -------------------------------------------------------------
  470. func (s *Server) handleListCommunityLists(w http.ResponseWriter, r *http.Request) {
  471. lists := s.dynRoutingMgr.ListCommunityLists()
  472. if lists == nil {
  473. lists = []dynamicrouting.RoutingFilterCommunityList{}
  474. }
  475. writeJSON(w, http.StatusOK, lists)
  476. }
  477. func (s *Server) handleSaveCommunityList(w http.ResponseWriter, r *http.Request) {
  478. var cl dynamicrouting.RoutingFilterCommunityList
  479. if err := json.NewDecoder(r.Body).Decode(&cl); err != nil {
  480. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  481. return
  482. }
  483. if strings.TrimSpace(cl.Name) == "" {
  484. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
  485. return
  486. }
  487. if err := s.dynRoutingMgr.SaveCommunityList(&cl); err != nil {
  488. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  489. return
  490. }
  491. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": cl.ID})
  492. }
  493. func (s *Server) handleDeleteCommunityList(w http.ResponseWriter, r *http.Request) {
  494. id := r.PathValue("id")
  495. if id == "" {
  496. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
  497. return
  498. }
  499. if err := s.dynRoutingMgr.DeleteCommunityList(id); err != nil {
  500. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  501. return
  502. }
  503. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  504. }
  505. func (s *Server) handleSetCommunityListState(w http.ResponseWriter, r *http.Request) {
  506. id := r.PathValue("id")
  507. if id == "" {
  508. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing community list ID"})
  509. return
  510. }
  511. var req struct {
  512. Disabled bool `json:"disabled"`
  513. }
  514. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  515. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  516. return
  517. }
  518. if err := s.dynRoutingMgr.SetCommunityListState(id, req.Disabled); err != nil {
  519. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  520. return
  521. }
  522. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  523. }
  524. // -------------------------------------------------------------
  525. // AS Lists Handlers (/routing filter num-list)
  526. // -------------------------------------------------------------
  527. func (s *Server) handleListASLists(w http.ResponseWriter, r *http.Request) {
  528. lists := s.dynRoutingMgr.ListASLists()
  529. if lists == nil {
  530. lists = []dynamicrouting.RoutingFilterASList{}
  531. }
  532. writeJSON(w, http.StatusOK, lists)
  533. }
  534. func (s *Server) handleSaveASList(w http.ResponseWriter, r *http.Request) {
  535. var al dynamicrouting.RoutingFilterASList
  536. if err := json.NewDecoder(r.Body).Decode(&al); err != nil {
  537. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  538. return
  539. }
  540. if strings.TrimSpace(al.Name) == "" {
  541. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "List name is required"})
  542. return
  543. }
  544. if err := s.dynRoutingMgr.SaveASList(&al); err != nil {
  545. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  546. return
  547. }
  548. writeJSON(w, http.StatusOK, map[string]string{"status": "success", "id": al.ID})
  549. }
  550. func (s *Server) handleDeleteASList(w http.ResponseWriter, r *http.Request) {
  551. id := r.PathValue("id")
  552. if id == "" {
  553. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
  554. return
  555. }
  556. if err := s.dynRoutingMgr.DeleteASList(id); err != nil {
  557. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  558. return
  559. }
  560. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  561. }
  562. func (s *Server) handleSetASListState(w http.ResponseWriter, r *http.Request) {
  563. id := r.PathValue("id")
  564. if id == "" {
  565. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing AS list ID"})
  566. return
  567. }
  568. var req struct {
  569. Disabled bool `json:"disabled"`
  570. }
  571. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  572. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  573. return
  574. }
  575. if err := s.dynRoutingMgr.SetASListState(id, req.Disabled); err != nil {
  576. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  577. return
  578. }
  579. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  580. }