database.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package db
  2. import (
  3. "time"
  4. "gorm.io/driver/sqlite"
  5. "gorm.io/gorm"
  6. _ "github.com/mattn/go-sqlite3"
  7. )
  8. type DB struct {
  9. *gorm.DB
  10. }
  11. type DHCPLease struct {
  12. ID uint `gorm:"primaryKey"`
  13. MAC string `gorm:"index"`
  14. IP string
  15. Hostname string
  16. ExpiresAt int64
  17. }
  18. type DHCPStaticBinding struct {
  19. ID uint `gorm:"primaryKey"`
  20. MAC string `gorm:"uniqueIndex"`
  21. IP string `gorm:"uniqueIndex"`
  22. Hostname string
  23. Description string
  24. Enabled bool
  25. }
  26. type DNSZone struct {
  27. ID uint `gorm:"primaryKey"`
  28. Name string `gorm:"uniqueIndex"`
  29. Type string // master, slave, forward
  30. }
  31. type DNSRecord struct {
  32. ID uint `gorm:"primaryKey"`
  33. ZoneID uint
  34. Zone DNSZone
  35. Name string `gorm:"index"`
  36. Type string // A, CNAME, MX, TXT
  37. Value string
  38. TTL int
  39. Enabled bool
  40. }
  41. type DNSQueryLog struct {
  42. ID uint `gorm:"primaryKey"`
  43. ClientIP string
  44. QueryName string
  45. QueryType string
  46. Response string
  47. Timestamp int64
  48. }
  49. func InitDB(path string) (*DB, error) {
  50. db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
  51. if err != nil {
  52. return nil, err
  53. }
  54. // Auto migrate
  55. err = db.AutoMigrate(
  56. &DHCPLease{},
  57. &DHCPStaticBinding{},
  58. &DNSZone{},
  59. &DNSRecord{},
  60. &DNSQueryLog{},
  61. )
  62. if err != nil {
  63. return nil, err
  64. }
  65. return &DB{db}, nil
  66. }
  67. func (d *DB) GetActiveLeases() ([]DHCPLease, error) {
  68. var leases []DHCPLease
  69. err := d.Where("expires_at > ?", time.Now().Unix()).Find(&leases).Error
  70. return leases, err
  71. }
  72. func (d *DB) GetStaticBindings() ([]DHCPStaticBinding, error) {
  73. var bindings []DHCPStaticBinding
  74. err := d.Where("enabled = ?", true).Find(&bindings).Error
  75. return bindings, err
  76. }
  77. func (d *DB) GetDNSRecords() ([]DNSRecord, error) {
  78. var records []DNSRecord
  79. err := d.Where("enabled = ?", true).Preload("Zone").Find(&records).Error
  80. return records, err
  81. }
  82. func (d *DB) GetDNSZones() ([]DNSZone, error) {
  83. var zones []DNSZone
  84. err := d.Find(&zones).Error
  85. return zones, err
  86. }
  87. func (d *DB) CreateDNSZone(name, zoneType string) error {
  88. zone := DNSZone{
  89. Name: name,
  90. Type: zoneType,
  91. }
  92. return d.Create(&zone).Error
  93. }
  94. func (d *DB) DeleteDNSZone(id uint) error {
  95. return d.Delete(&DNSZone{}, id).Error
  96. }
  97. func (d *DB) AddQueryLog(clientIP, queryName, queryType, response string) error {
  98. log := DNSQueryLog{
  99. ClientIP: clientIP,
  100. QueryName: queryName,
  101. QueryType: queryType,
  102. Response: response,
  103. Timestamp: time.Now().Unix(),
  104. }
  105. return d.Create(&log).Error
  106. }