dhcp_server_handlers.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package api
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "proxmox-router/internal/dhcp"
  6. )
  7. func (s *Server) handleGetDHCPServerSummary(w http.ResponseWriter, r *http.Request) {
  8. summary := s.dhcpServerMgr.GetSummary()
  9. w.Header().Set("Content-Type", "application/json")
  10. _ = json.NewEncoder(w).Encode(summary)
  11. }
  12. func (s *Server) handleListDHCPServers(w http.ResponseWriter, r *http.Request) {
  13. servers := s.dhcpServerMgr.ListServers()
  14. w.Header().Set("Content-Type", "application/json")
  15. _ = json.NewEncoder(w).Encode(servers)
  16. }
  17. func (s *Server) handleSaveDHCPServer(w http.ResponseWriter, r *http.Request) {
  18. var inst dhcp.DHCPServerInstance
  19. if err := json.NewDecoder(r.Body).Decode(&inst); err != nil {
  20. http.Error(w, "invalid request body", http.StatusBadRequest)
  21. return
  22. }
  23. saved, err := s.dhcpServerMgr.SaveServer(inst)
  24. if err != nil {
  25. http.Error(w, err.Error(), http.StatusInternalServerError)
  26. return
  27. }
  28. w.Header().Set("Content-Type", "application/json")
  29. _ = json.NewEncoder(w).Encode(saved)
  30. }
  31. func (s *Server) handleDeleteDHCPServer(w http.ResponseWriter, r *http.Request) {
  32. id := r.PathValue("id")
  33. if id == "" {
  34. http.Error(w, "missing server ID", http.StatusBadRequest)
  35. return
  36. }
  37. if err := s.dhcpServerMgr.DeleteServer(id); err != nil {
  38. http.Error(w, err.Error(), http.StatusInternalServerError)
  39. return
  40. }
  41. w.Header().Set("Content-Type", "application/json")
  42. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  43. }
  44. func (s *Server) handleSetDHCPServerState(w http.ResponseWriter, r *http.Request) {
  45. id := r.PathValue("id")
  46. if id == "" {
  47. http.Error(w, "missing server ID", http.StatusBadRequest)
  48. return
  49. }
  50. var req struct {
  51. Disabled bool `json:"disabled"`
  52. }
  53. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  54. http.Error(w, "invalid JSON", http.StatusBadRequest)
  55. return
  56. }
  57. if err := s.dhcpServerMgr.ToggleServerState(id, req.Disabled); err != nil {
  58. http.Error(w, err.Error(), http.StatusInternalServerError)
  59. return
  60. }
  61. w.Header().Set("Content-Type", "application/json")
  62. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  63. }
  64. // ======================== NETWORKS ========================
  65. func (s *Server) handleListDHCPNetworks(w http.ResponseWriter, r *http.Request) {
  66. networks := s.dhcpServerMgr.ListNetworks()
  67. w.Header().Set("Content-Type", "application/json")
  68. _ = json.NewEncoder(w).Encode(networks)
  69. }
  70. func (s *Server) handleSaveDHCPNetwork(w http.ResponseWriter, r *http.Request) {
  71. var netw dhcp.DHCPNetwork
  72. if err := json.NewDecoder(r.Body).Decode(&netw); err != nil {
  73. http.Error(w, "invalid request body", http.StatusBadRequest)
  74. return
  75. }
  76. saved, err := s.dhcpServerMgr.SaveNetwork(netw)
  77. if err != nil {
  78. http.Error(w, err.Error(), http.StatusInternalServerError)
  79. return
  80. }
  81. w.Header().Set("Content-Type", "application/json")
  82. _ = json.NewEncoder(w).Encode(saved)
  83. }
  84. func (s *Server) handleDeleteDHCPNetwork(w http.ResponseWriter, r *http.Request) {
  85. id := r.PathValue("id")
  86. if id == "" {
  87. http.Error(w, "missing network ID", http.StatusBadRequest)
  88. return
  89. }
  90. if err := s.dhcpServerMgr.DeleteNetwork(id); err != nil {
  91. http.Error(w, err.Error(), http.StatusInternalServerError)
  92. return
  93. }
  94. w.Header().Set("Content-Type", "application/json")
  95. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  96. }
  97. // ======================== LEASES ========================
  98. func (s *Server) handleListDHCPLeases(w http.ResponseWriter, r *http.Request) {
  99. leases := s.dhcpServerMgr.ListLeases()
  100. w.Header().Set("Content-Type", "application/json")
  101. _ = json.NewEncoder(w).Encode(leases)
  102. }
  103. func (s *Server) handleSaveDHCPStaticLease(w http.ResponseWriter, r *http.Request) {
  104. var lease dhcp.DHCPLease
  105. if err := json.NewDecoder(r.Body).Decode(&lease); err != nil {
  106. http.Error(w, "invalid request body", http.StatusBadRequest)
  107. return
  108. }
  109. saved, err := s.dhcpServerMgr.SaveStaticLease(lease)
  110. if err != nil {
  111. http.Error(w, err.Error(), http.StatusInternalServerError)
  112. return
  113. }
  114. w.Header().Set("Content-Type", "application/json")
  115. _ = json.NewEncoder(w).Encode(saved)
  116. }
  117. func (s *Server) handleDeleteDHCPStaticLease(w http.ResponseWriter, r *http.Request) {
  118. id := r.PathValue("id")
  119. if id == "" {
  120. http.Error(w, "missing lease ID", http.StatusBadRequest)
  121. return
  122. }
  123. if err := s.dhcpServerMgr.DeleteStaticLease(id); err != nil {
  124. http.Error(w, err.Error(), http.StatusInternalServerError)
  125. return
  126. }
  127. w.Header().Set("Content-Type", "application/json")
  128. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  129. }
  130. func (s *Server) handleMakeDHCPStaticLease(w http.ResponseWriter, r *http.Request) {
  131. var req struct {
  132. MACAddress string `json:"mac_address"`
  133. Address string `json:"address"`
  134. HostName string `json:"hostname"`
  135. Comment string `json:"comment"`
  136. }
  137. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  138. http.Error(w, "invalid JSON", http.StatusBadRequest)
  139. return
  140. }
  141. st, err := s.dhcpServerMgr.MakeStaticLease(req.MACAddress, req.Address, req.HostName, req.Comment)
  142. if err != nil {
  143. http.Error(w, err.Error(), http.StatusInternalServerError)
  144. return
  145. }
  146. w.Header().Set("Content-Type", "application/json")
  147. _ = json.NewEncoder(w).Encode(st)
  148. }
  149. // ======================== OPTIONS ========================
  150. func (s *Server) handleListDHCPOptions(w http.ResponseWriter, r *http.Request) {
  151. options := s.dhcpServerMgr.ListOptions()
  152. w.Header().Set("Content-Type", "application/json")
  153. _ = json.NewEncoder(w).Encode(options)
  154. }
  155. func (s *Server) handleSaveDHCPOption(w http.ResponseWriter, r *http.Request) {
  156. var opt dhcp.DHCPOption
  157. if err := json.NewDecoder(r.Body).Decode(&opt); err != nil {
  158. http.Error(w, "invalid request body", http.StatusBadRequest)
  159. return
  160. }
  161. saved, err := s.dhcpServerMgr.SaveOption(opt)
  162. if err != nil {
  163. http.Error(w, err.Error(), http.StatusInternalServerError)
  164. return
  165. }
  166. w.Header().Set("Content-Type", "application/json")
  167. _ = json.NewEncoder(w).Encode(saved)
  168. }
  169. func (s *Server) handleDeleteDHCPOption(w http.ResponseWriter, r *http.Request) {
  170. id := r.PathValue("id")
  171. if id == "" {
  172. http.Error(w, "missing option ID", http.StatusBadRequest)
  173. return
  174. }
  175. if err := s.dhcpServerMgr.DeleteOption(id); err != nil {
  176. http.Error(w, err.Error(), http.StatusInternalServerError)
  177. return
  178. }
  179. w.Header().Set("Content-Type", "application/json")
  180. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  181. }
  182. // ======================== WIZARD ========================
  183. func (s *Server) handleRunDHCPSetup(w http.ResponseWriter, r *http.Request) {
  184. var req dhcp.DHCPServerSetupRequest
  185. if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
  186. http.Error(w, "invalid request body", http.StatusBadRequest)
  187. return
  188. }
  189. if err := s.dhcpServerMgr.RunSetupWizard(req); err != nil {
  190. http.Error(w, err.Error(), http.StatusInternalServerError)
  191. return
  192. }
  193. w.Header().Set("Content-Type", "application/json")
  194. _ = json.NewEncoder(w).Encode(map[string]bool{"success": true})
  195. }