dynamic_routing_handlers.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/dynamicrouting"
  6. )
  7. // Summary
  8. func (s *Server) handleDynamicRoutingSummary(w http.ResponseWriter, r *http.Request) {
  9. summary := s.dynRoutingMgr.GetSummary()
  10. writeJSON(w, http.StatusOK, summary)
  11. }
  12. // -------------------------------------------------------------
  13. // BGP Handlers
  14. // -------------------------------------------------------------
  15. func (s *Server) handleGetBGPInstance(w http.ResponseWriter, r *http.Request) {
  16. inst := s.dynRoutingMgr.GetBGPInstance()
  17. writeJSON(w, http.StatusOK, inst)
  18. }
  19. func (s *Server) handleUpdateBGPInstance(w http.ResponseWriter, r *http.Request) {
  20. var inst dynamicrouting.BGPInstance
  21. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  22. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  23. return
  24. }
  25. if err := s.dynRoutingMgr.UpdateBGPInstance(inst); err != nil {
  26. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  27. return
  28. }
  29. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetBGPInstance())
  30. }
  31. func (s *Server) handleListBGPPeers(w http.ResponseWriter, r *http.Request) {
  32. peers := s.dynRoutingMgr.ListBGPPeers()
  33. if peers == nil {
  34. peers = []dynamicrouting.BGPPeer{}
  35. }
  36. writeJSON(w, http.StatusOK, peers)
  37. }
  38. func (s *Server) handleSaveBGPPeer(w http.ResponseWriter, r *http.Request) {
  39. var peer dynamicrouting.BGPPeer
  40. if err := json.NewDecoder(r.Body).Decode(&peer); err != nil {
  41. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  42. return
  43. }
  44. if peer.RemoteAddress == "" || peer.RemoteAS == 0 {
  45. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "remote_address and remote_as are required"})
  46. return
  47. }
  48. if err := s.dynRoutingMgr.SaveBGPPeer(peer); err != nil {
  49. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  50. return
  51. }
  52. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "peer": peer})
  53. }
  54. func (s *Server) handleDeleteBGPPeer(w http.ResponseWriter, r *http.Request) {
  55. id := r.PathValue("id")
  56. if id == "" {
  57. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing peer id"})
  58. return
  59. }
  60. if err := s.dynRoutingMgr.DeleteBGPPeer(id); err != nil {
  61. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  62. return
  63. }
  64. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  65. }
  66. func (s *Server) handleSetBGPPeerState(w http.ResponseWriter, r *http.Request) {
  67. id := r.PathValue("id")
  68. var req struct {
  69. Disabled bool `json:"disabled"`
  70. }
  71. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  72. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  73. return
  74. }
  75. if err := s.dynRoutingMgr.SetBGPPeerState(id, req.Disabled); err != nil {
  76. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  77. return
  78. }
  79. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  80. }
  81. func (s *Server) handleListBGPNetworks(w http.ResponseWriter, r *http.Request) {
  82. nets := s.dynRoutingMgr.ListBGPNetworks()
  83. if nets == nil {
  84. nets = []dynamicrouting.BGPNetwork{}
  85. }
  86. writeJSON(w, http.StatusOK, nets)
  87. }
  88. func (s *Server) handleSaveBGPNetwork(w http.ResponseWriter, r *http.Request) {
  89. var net dynamicrouting.BGPNetwork
  90. if err := json.NewDecoder(r.Body).Decode(&net); err != nil {
  91. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  92. return
  93. }
  94. if net.Network == "" {
  95. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "network (CIDR) is required"})
  96. return
  97. }
  98. if err := s.dynRoutingMgr.SaveBGPNetwork(net); err != nil {
  99. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  100. return
  101. }
  102. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "network": net})
  103. }
  104. func (s *Server) handleDeleteBGPNetwork(w http.ResponseWriter, r *http.Request) {
  105. id := r.PathValue("id")
  106. if id == "" {
  107. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing network id"})
  108. return
  109. }
  110. if err := s.dynRoutingMgr.DeleteBGPNetwork(id); err != nil {
  111. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  112. return
  113. }
  114. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  115. }
  116. func (s *Server) handleGetBGPRoutes(w http.ResponseWriter, r *http.Request) {
  117. routes := s.dynRoutingMgr.GetBGPRoutes()
  118. if routes == nil {
  119. routes = []dynamicrouting.BGPRoute{}
  120. }
  121. writeJSON(w, http.StatusOK, routes)
  122. }
  123. // -------------------------------------------------------------
  124. // OSPF Handlers
  125. // -------------------------------------------------------------
  126. func (s *Server) handleGetOSPFInstance(w http.ResponseWriter, r *http.Request) {
  127. inst := s.dynRoutingMgr.GetOSPFInstance()
  128. writeJSON(w, http.StatusOK, inst)
  129. }
  130. func (s *Server) handleUpdateOSPFInstance(w http.ResponseWriter, r *http.Request) {
  131. var inst dynamicrouting.OSPFInstance
  132. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  133. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  134. return
  135. }
  136. if err := s.dynRoutingMgr.UpdateOSPFInstance(inst); err != nil {
  137. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  138. return
  139. }
  140. writeJSON(w, http.StatusOK, s.dynRoutingMgr.GetOSPFInstance())
  141. }
  142. func (s *Server) handleListOSPFAreas(w http.ResponseWriter, r *http.Request) {
  143. areas := s.dynRoutingMgr.ListOSPFAreas()
  144. if areas == nil {
  145. areas = []dynamicrouting.OSPFArea{}
  146. }
  147. writeJSON(w, http.StatusOK, areas)
  148. }
  149. func (s *Server) handleSaveOSPFArea(w http.ResponseWriter, r *http.Request) {
  150. var area dynamicrouting.OSPFArea
  151. if err := json.NewDecoder(r.Body).Decode(&area); err != nil {
  152. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  153. return
  154. }
  155. if area.AreaID == "" {
  156. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "area_id is required"})
  157. return
  158. }
  159. if err := s.dynRoutingMgr.SaveOSPFArea(area); err != nil {
  160. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  161. return
  162. }
  163. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "area": area})
  164. }
  165. func (s *Server) handleDeleteOSPFArea(w http.ResponseWriter, r *http.Request) {
  166. id := r.PathValue("id")
  167. if id == "" {
  168. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing area id"})
  169. return
  170. }
  171. if err := s.dynRoutingMgr.DeleteOSPFArea(id); err != nil {
  172. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  173. return
  174. }
  175. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  176. }
  177. func (s *Server) handleListOSPFInterfaces(w http.ResponseWriter, r *http.Request) {
  178. ifaces := s.dynRoutingMgr.ListOSPFInterfaces()
  179. if ifaces == nil {
  180. ifaces = []dynamicrouting.OSPFInterface{}
  181. }
  182. writeJSON(w, http.StatusOK, ifaces)
  183. }
  184. func (s *Server) handleSaveOSPFInterface(w http.ResponseWriter, r *http.Request) {
  185. var iface dynamicrouting.OSPFInterface
  186. if err := json.NewDecoder(r.Body).Decode(&iface); err != nil {
  187. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON: " + err.Error()})
  188. return
  189. }
  190. if iface.Network == "" {
  191. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "network is required"})
  192. return
  193. }
  194. if err := s.dynRoutingMgr.SaveOSPFInterface(iface); err != nil {
  195. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  196. return
  197. }
  198. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "interface": iface})
  199. }
  200. func (s *Server) handleDeleteOSPFInterface(w http.ResponseWriter, r *http.Request) {
  201. id := r.PathValue("id")
  202. if id == "" {
  203. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Missing interface id"})
  204. return
  205. }
  206. if err := s.dynRoutingMgr.DeleteOSPFInterface(id); err != nil {
  207. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  208. return
  209. }
  210. writeJSON(w, http.StatusOK, map[string]string{"status": "deleted"})
  211. }
  212. func (s *Server) handleSetOSPFInterfaceState(w http.ResponseWriter, r *http.Request) {
  213. id := r.PathValue("id")
  214. var req struct {
  215. Disabled bool `json:"disabled"`
  216. }
  217. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  218. writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Invalid JSON"})
  219. return
  220. }
  221. if err := s.dynRoutingMgr.SetOSPFInterfaceState(id, req.Disabled); err != nil {
  222. writeJSON(w, http.StatusInternalServerError, map[string]string{"error": err.Error()})
  223. return
  224. }
  225. writeJSON(w, http.StatusOK, map[string]interface{}{"status": "success", "disabled": req.Disabled})
  226. }
  227. func (s *Server) handleGetOSPFNeighbors(w http.ResponseWriter, r *http.Request) {
  228. nbrs := s.dynRoutingMgr.GetOSPFNeighbors()
  229. if nbrs == nil {
  230. nbrs = []dynamicrouting.OSPFNeighbor{}
  231. }
  232. writeJSON(w, http.StatusOK, nbrs)
  233. }
  234. func (s *Server) handleGetOSPFRoutes(w http.ResponseWriter, r *http.Request) {
  235. routes := s.dynRoutingMgr.GetOSPFRoutes()
  236. if routes == nil {
  237. routes = []dynamicrouting.OSPFRoute{}
  238. }
  239. writeJSON(w, http.StatusOK, routes)
  240. }