A More Secure Internet Connection for Your Home https://fen.gg
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
3.8 KiB

  1. package model
  2. //
  3. // Fengg Security Gateway Server Application
  4. // Copyright (C) 2020 Lukas Matt <support@fen.gg>
  5. //
  6. // This program is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. //
  18. import (
  19. "fmt"
  20. "time"
  21. "tea.fen.gg/fengg/server/types"
  22. "github.com/jmoiron/sqlx"
  23. _ "github.com/lib/pq"
  24. )
  25. const (
  26. DnsRecordNamedInsertTmpl = `
  27. INSERT INTO dns_records (
  28. created_at, updated_at, network_id, type, ipv4, ipv6, name, value, priority, ttl
  29. ) VALUES (
  30. now(), now(), :network_id, :type, :ipv4, :ipv6, :name, :value, :priority, :ttl
  31. ) RETURNING id;`
  32. DnsRecordNamedUpdateTmpl = `
  33. UPDATE dns_records
  34. SET updated_at=now(), ipv4=:ipv4, ipv6=:ipv6, priority=:priority, ttl=:ttl
  35. WHERE (network_id=:network_id, type=:type, name=:name, value=:value)
  36. OR id=:id;`
  37. DnsRecordNamedDeleteTmpl = `DELETE FROM dns_records WHERE %s;`
  38. DnsRecordQueryTmpl = `SELECT * FROM dns_records %s;`
  39. )
  40. type DnsRecord struct {
  41. ID uint `db:"id" json:"id"`
  42. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  43. UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
  44. NetworkID uint `db:"network_id" json:"networkId"`
  45. Type int16 `db:"type" json:"type"`
  46. IPv4 *types.IP `db:"ipv4" json:"ipv4"`
  47. IPv6 *types.IP `db:"ipv6" json:"ipv6"`
  48. Name string `db:"name" json:"name"`
  49. Value string `db:"value" json:"value"`
  50. Priority *int `db:"priority" json:"priority"`
  51. TTL *int `db:"ttl" json:"ttl"`
  52. }
  53. type DnsRecords []DnsRecord
  54. func NewDnsRecord() *DnsRecord {
  55. return &DnsRecord{
  56. CreatedAt: time.Now(),
  57. UpdatedAt: time.Now(),
  58. }
  59. }
  60. // Exists directly updates the struct with the findings
  61. func (record *DnsRecord) Exists() bool {
  62. db, err := sqlx.Connect(dbDriver, dbConnect)
  63. if err != nil {
  64. logger.Error().Err(err).Msg("cannot open database")
  65. return false
  66. }
  67. defer db.Close()
  68. return db.Get(
  69. record,
  70. fmt.Sprintf(
  71. DnsRecordQueryTmpl,
  72. "WHERE (network_id=$1, type=$2, name=$3, value=$4) OR id=$5;",
  73. ),
  74. record.NetworkID,
  75. record.Type,
  76. record.Name,
  77. record.Value,
  78. record.ID) == nil
  79. }
  80. func (record *DnsRecord) Update() error {
  81. db, err := sqlx.Connect(dbDriver, dbConnect)
  82. if err != nil {
  83. return err
  84. }
  85. defer db.Close()
  86. _, err = db.NamedExec(DnsRecordNamedUpdateTmpl, record)
  87. return err
  88. }
  89. func (record *DnsRecord) Delete() error {
  90. db, err := sqlx.Connect(dbDriver, dbConnect)
  91. if err != nil {
  92. return err
  93. }
  94. defer db.Close()
  95. _, err = db.NamedExec(fmt.Sprintf(DnsRecordNamedDeleteTmpl, "id=:id"), record)
  96. return err
  97. }
  98. func (record *DnsRecord) Create() error {
  99. db, err := sqlx.Connect(dbDriver, dbConnect)
  100. if err != nil {
  101. return err
  102. }
  103. defer db.Close()
  104. rows, err := db.NamedQuery(DnsRecordNamedInsertTmpl, record)
  105. if err != nil {
  106. return err
  107. }
  108. defer rows.Close()
  109. for rows.Next() {
  110. err = rows.Scan(&record.ID)
  111. if err != nil {
  112. return err
  113. }
  114. }
  115. return nil
  116. }
  117. func (records *DnsRecords) FindAll() error {
  118. db, err := sqlx.Connect(dbDriver, dbConnect)
  119. if err != nil {
  120. return err
  121. }
  122. defer db.Close()
  123. return db.Select(records, fmt.Sprintf(DnsRecordQueryTmpl, ""))
  124. }
  125. func (records *DnsRecords) FindByNetworkID(id uint) error {
  126. db, err := sqlx.Connect(dbDriver, dbConnect)
  127. if err != nil {
  128. return err
  129. }
  130. defer db.Close()
  131. return db.Select(records, fmt.Sprintf(DnsRecordQueryTmpl, "WHERE network_id=$1"), id)
  132. }