server.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. package web
  2. import (
  3. "fmt"
  4. "dhcp-dns-manager/internal/config"
  5. "dhcp-dns-manager/internal/db"
  6. "dhcp-dns-manager/internal/dhcp"
  7. "dhcp-dns-manager/internal/dns"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. "time"
  11. )
  12. type Server struct {
  13. config *config.WebConfig
  14. db *db.DB
  15. dhcpServer *dhcp.Server
  16. dnsServer *dns.Server
  17. router *gin.Engine
  18. configManager *ConfigManager
  19. }
  20. type User struct {
  21. ID uint `gorm:"primaryKey"`
  22. Username string `gorm:"uniqueIndex"`
  23. Password string
  24. IsAdmin bool
  25. }
  26. // Session represents a user session
  27. type Session struct {
  28. ID string `gorm:"primaryKey"`
  29. UserID string // username
  30. ExpiresAt time.Time
  31. CreatedAt time.Time
  32. }
  33. func NewServer(cfg *config.WebConfig, database *db.DB, d *dhcp.Server, n *dns.Server, cm *ConfigManager) *Server {
  34. gin.SetMode(gin.ReleaseMode)
  35. s := &Server{
  36. config: cfg,
  37. db: database,
  38. dhcpServer: d,
  39. dnsServer: n,
  40. router: gin.New(),
  41. configManager: cm,
  42. }
  43. // Auto-migrate Session table
  44. s.db.AutoMigrate(&Session{})
  45. // Wire up config reloader so DHCP server picks up web UI config changes
  46. d.SetConfigReloader(func() *config.DHCPConfig {
  47. cfg := cm.GetConfig()
  48. dhcpCfg := new(config.DHCPConfig)
  49. *dhcpCfg = cfg.DHCP // copy the value
  50. return dhcpCfg
  51. })
  52. s.setupRoutes()
  53. // Start session cleanup goroutine
  54. go s.cleanupSessions()
  55. return s
  56. }
  57. func (s *Server) cleanupSessions() {
  58. ticker := time.NewTicker(1 * time.Hour)
  59. defer ticker.Stop()
  60. for range ticker.C {
  61. s.db.Where("expires_at < ?", time.Now()).Delete(&Session{})
  62. }
  63. }
  64. func (s *Server) setupRoutes() {
  65. // Custom recovery middleware that returns JSON
  66. s.router.Use(gin.CustomRecovery(func(c *gin.Context, err any) {
  67. c.JSON(http.StatusInternalServerError, gin.H{
  68. "error": fmt.Sprintf("Internal server error: %v", err),
  69. })
  70. c.Abort()
  71. }))
  72. s.router.Use(gin.Logger())
  73. // Inject ConfigManager into context
  74. s.router.Use(func(c *gin.Context) {
  75. c.Set("configManager", s.configManager)
  76. c.Next()
  77. })
  78. // Static files
  79. s.router.Static("/static", "./web/static")
  80. // Public routes
  81. s.router.GET("/", s.handleIndex)
  82. s.router.POST("/api/login", s.handleLogin)
  83. s.router.GET("/api/health", func(c *gin.Context) {
  84. c.JSON(http.StatusOK, gin.H{"status": "ok", "message": "Server is running"})
  85. })
  86. s.router.POST("/api/session/verify", s.handleVerifySession)
  87. // Protected routes
  88. protected := s.router.Group("/api")
  89. protected.Use(s.authMiddleware())
  90. {
  91. // Dashboard
  92. protected.GET("/dashboard", s.handleDashboard)
  93. // DHCP
  94. protected.GET("/dhcp/config", s.handleGetDHCPConfig)
  95. protected.PUT("/dhcp/config", s.handleUpdateDHCPConfig)
  96. protected.GET("/dhcp/leases", s.handleGetLeases)
  97. protected.GET("/dhcp/bindings", s.handleGetBindings)
  98. protected.POST("/dhcp/bindings", s.handleCreateBinding)
  99. protected.DELETE("/dhcp/bindings/:id", s.handleDeleteBinding)
  100. protected.POST("/dhcp/leases/evict", s.handleEvictClient)
  101. // DNS
  102. protected.GET("/dns/config", s.handleGetDNSConfig)
  103. protected.PUT("/dns/config", s.handleUpdateDNSConfig)
  104. protected.GET("/dns/records", s.handleGetRecords)
  105. protected.POST("/dns/records", s.handleCreateRecord)
  106. protected.DELETE("/dns/records/:id", s.handleDeleteRecord)
  107. protected.GET("/dns/logs", s.handleGetLogs)
  108. protected.GET("/dns/zones", s.handleGetZones)
  109. protected.POST("/dns/zones", s.handleCreateZone)
  110. protected.DELETE("/dns/zones/:id", s.handleDeleteZone)
  111. // Config
  112. protected.GET("/config", s.handleGetFullConfig)
  113. protected.PUT("/config", s.handleUpdateConfig)
  114. protected.GET("/config/export", s.handleExportConfig)
  115. protected.POST("/config/import", s.handleImportConfig)
  116. // Service
  117. protected.POST("/service/restart", s.handleRestartService)
  118. }
  119. }
  120. func (s *Server) authMiddleware() gin.HandlerFunc {
  121. return func(c *gin.Context) {
  122. sessionID := c.GetHeader("X-Session-ID")
  123. if sessionID == "" {
  124. c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
  125. c.Abort()
  126. return
  127. }
  128. // Validate session from database
  129. var session Session
  130. if err := s.db.Where("id = ? AND expires_at > ?", sessionID, time.Now()).First(&session).Error; err != nil {
  131. c.JSON(http.StatusUnauthorized, gin.H{"error": "Session expired"})
  132. c.Abort()
  133. return
  134. }
  135. c.Next()
  136. }
  137. }
  138. func (s *Server) Start() error {
  139. addr := fmt.Sprintf("%s:%d", s.config.Host, s.config.Port)
  140. return s.router.Run(addr)
  141. }
  142. // Handlers
  143. func (s *Server) handleIndex(c *gin.Context) {
  144. c.File("./web/templates/index.html")
  145. }
  146. func (s *Server) handleLogin(c *gin.Context) {
  147. var req struct {
  148. Username string `json:"username"`
  149. Password string `json:"password"`
  150. }
  151. if err := c.ShouldBindJSON(&req); err != nil {
  152. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  153. return
  154. }
  155. // TODO: Validate against database
  156. // For now, simple demo auth
  157. if req.Username == "admin" && req.Password == "admin" {
  158. sessionID := fmt.Sprintf("session-%d-%s", time.Now().UnixNano(), req.Username)
  159. session := Session{
  160. ID: sessionID,
  161. UserID: req.Username,
  162. ExpiresAt: time.Now().Add(30 * 24 * time.Hour), // 30 days
  163. }
  164. s.db.Create(&session)
  165. c.JSON(http.StatusOK, gin.H{
  166. "session_id": sessionID,
  167. "is_admin": true,
  168. })
  169. return
  170. }
  171. c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid credentials"})
  172. }
  173. func (s *Server) handleVerifySession(c *gin.Context) {
  174. var req struct {
  175. SessionID string `json:"session_id"`
  176. }
  177. if err := c.ShouldBindJSON(&req); err != nil {
  178. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  179. return
  180. }
  181. var session Session
  182. if err := s.db.Where("id = ? AND expires_at > ?", req.SessionID, time.Now()).First(&session).Error; err != nil {
  183. c.JSON(http.StatusUnauthorized, gin.H{"error": "Session expired"})
  184. return
  185. }
  186. c.JSON(http.StatusOK, gin.H{
  187. "valid": true,
  188. "user_id": session.UserID,
  189. "username": session.UserID,
  190. })
  191. }
  192. func (s *Server) handleDashboard(c *gin.Context) {
  193. leases := s.dhcpServer.GetLeases()
  194. bindings, _ := s.dhcpServer.GetStaticBindings()
  195. records, _ := s.dnsServer.GetDNSRecords()
  196. c.JSON(http.StatusOK, gin.H{
  197. "active_leases": len(leases),
  198. "static_bindings": len(bindings),
  199. "dns_records": len(records),
  200. "leases": leases,
  201. "bindings": bindings,
  202. "records": records,
  203. })
  204. }
  205. func (s *Server) handleGetLeases(c *gin.Context) {
  206. leases := s.dhcpServer.GetLeases()
  207. c.JSON(http.StatusOK, gin.H{"leases": leases})
  208. }
  209. func (s *Server) handleGetBindings(c *gin.Context) {
  210. bindings, err := s.dhcpServer.GetStaticBindings()
  211. if err != nil {
  212. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  213. return
  214. }
  215. c.JSON(http.StatusOK, gin.H{"bindings": bindings})
  216. }
  217. func (s *Server) handleCreateBinding(c *gin.Context) {
  218. var req struct {
  219. MAC string `json:"mac"`
  220. IP string `json:"ip"`
  221. Hostname string `json:"hostname"`
  222. Description string `json:"description"`
  223. }
  224. if err := c.ShouldBindJSON(&req); err != nil {
  225. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  226. return
  227. }
  228. if err := s.dhcpServer.CreateStaticBinding(req.MAC, req.IP, req.Hostname, req.Description); err != nil {
  229. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  230. return
  231. }
  232. c.JSON(http.StatusOK, gin.H{"message": "Binding created"})
  233. }
  234. func (s *Server) handleDeleteBinding(c *gin.Context) {
  235. _ = c.Param("id")
  236. // TODO: Convert to uint and delete
  237. c.JSON(http.StatusOK, gin.H{"message": "Binding deleted"})
  238. }
  239. func (s *Server) handleEvictClient(c *gin.Context) {
  240. var req struct {
  241. MAC string `json:"mac"`
  242. IP string `json:"ip"`
  243. }
  244. if err := c.ShouldBindJSON(&req); err != nil {
  245. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  246. return
  247. }
  248. if req.MAC == "" {
  249. c.JSON(http.StatusBadRequest, gin.H{"error": "MAC is required"})
  250. return
  251. }
  252. if err := s.dhcpServer.EvictClient(req.MAC); err != nil {
  253. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  254. return
  255. }
  256. c.JSON(http.StatusOK, gin.H{"message": "Client evicted successfully"})
  257. }
  258. func (s *Server) handleGetRecords(c *gin.Context) {
  259. records, err := s.dnsServer.GetDNSRecords()
  260. if err != nil {
  261. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  262. return
  263. }
  264. c.JSON(http.StatusOK, gin.H{"records": records})
  265. }
  266. func (s *Server) handleCreateRecord(c *gin.Context) {
  267. var req struct {
  268. Name string `json:"name"`
  269. Type string `json:"type"`
  270. Value string `json:"value"`
  271. TTL int `json:"ttl"`
  272. }
  273. if err := c.ShouldBindJSON(&req); err != nil {
  274. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  275. return
  276. }
  277. if err := s.dnsServer.CreateDNSRecord(req.Name, req.Type, req.Value, req.TTL); err != nil {
  278. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  279. return
  280. }
  281. c.JSON(http.StatusOK, gin.H{"message": "Record created"})
  282. }
  283. func (s *Server) handleDeleteRecord(c *gin.Context) {
  284. _ = c.Param("id")
  285. // TODO: Convert to uint and delete
  286. c.JSON(http.StatusOK, gin.H{"message": "Record deleted"})
  287. }
  288. func (s *Server) handleGetLogs(c *gin.Context) {
  289. logs, err := s.dnsServer.GetQueryLogs(100)
  290. if err != nil {
  291. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  292. return
  293. }
  294. c.JSON(http.StatusOK, gin.H{"logs": logs})
  295. }
  296. func (s *Server) handleGetZones(c *gin.Context) {
  297. zones, err := s.dnsServer.GetDNSZones()
  298. if err != nil {
  299. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  300. return
  301. }
  302. c.JSON(http.StatusOK, gin.H{"zones": zones})
  303. }
  304. func (s *Server) handleCreateZone(c *gin.Context) {
  305. var req struct {
  306. Name string `json:"name"`
  307. Type string `json:"type"`
  308. }
  309. if err := c.ShouldBindJSON(&req); err != nil {
  310. c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
  311. return
  312. }
  313. if err := s.dnsServer.CreateDNSZone(req.Name, req.Type); err != nil {
  314. c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
  315. return
  316. }
  317. c.JSON(http.StatusOK, gin.H{"message": "Zone created"})
  318. }
  319. func (s *Server) handleDeleteZone(c *gin.Context) {
  320. _ = c.Param("id")
  321. // TODO: Convert to uint and delete
  322. c.JSON(http.StatusOK, gin.H{"message": "Zone deleted"})
  323. }
  324. func (s *Server) handleGetConfig(c *gin.Context) {
  325. // Return current config (without sensitive data)
  326. c.JSON(http.StatusOK, gin.H{"config": "placeholder"})
  327. }
  328. func (s *Server) handleUpdateConfig(c *gin.Context) {
  329. // Update config
  330. c.JSON(http.StatusOK, gin.H{"message": "Config updated"})
  331. }