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.
 
 
 
 
 
 

183 lines
4.3 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. "time"
  20. "fmt"
  21. "tea.fen.gg/fengg/server/types"
  22. "github.com/jmoiron/sqlx"
  23. _ "github.com/lib/pq"
  24. )
  25. const (
  26. ExceptionQueryTmpl = `SELECT * FROM exceptions %s;`
  27. ExceptionNamedInsertTmpl = `
  28. INSERT INTO exceptions (
  29. created_at, updated_at, type, alert, drop, pattern_id
  30. ) VALUES (
  31. now(), now(), :type, :alert, :drop, :pattern_id
  32. ) RETURNING id;`
  33. ExceptionNamedUpdateTmpl = `
  34. UPDATE exceptions
  35. SET updated_at=now(), alert=:alert, drop=:drop
  36. WHERE %s;`
  37. )
  38. type Exception struct {
  39. ID uint `db:"id" json:"id"`
  40. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  41. UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
  42. Type types.NFQActionType `db:"type" json:"type"`
  43. Alert bool `db:"alert" json:"alert"`
  44. Drop bool `db:"drop" json:"drop"`
  45. PatternID uint `db:"pattern_id" json:"patternId"`
  46. // can be loaded with PreLoad function
  47. Pattern *Pattern `db:"-" json:"-"`
  48. }
  49. type Exceptions []*Exception
  50. func NewException() *Exception {
  51. return &Exception{
  52. CreatedAt: time.Now(),
  53. UpdatedAt: time.Now(),
  54. Alert: true,
  55. Drop: false,
  56. }
  57. }
  58. func (exceptions Exceptions) PreLoad() error {
  59. for _, exception := range exceptions {
  60. err := exception.PreLoad()
  61. if err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }
  67. func (exceptions *Exceptions) FindAll() error {
  68. db, err := sqlx.Connect(dbDriver, dbConnect)
  69. if err != nil {
  70. return err
  71. }
  72. defer db.Close()
  73. err = db.Select(exceptions, fmt.Sprintf(ExceptionQueryTmpl, ""))
  74. if err != nil {
  75. return err
  76. }
  77. return exceptions.PreLoad()
  78. }
  79. func (exception *Exception) PreLoad() error {
  80. var pattern Pattern
  81. err := pattern.FindByID(exception.PatternID)
  82. exception.Pattern = &pattern
  83. return err
  84. }
  85. func (exception *Exception) FindByPattern(pattern Pattern) error {
  86. db, err := sqlx.Connect(dbDriver, dbConnect)
  87. if err != nil {
  88. logger.Error().Err(err).Msg("cannot open database")
  89. return err
  90. }
  91. defer db.Close()
  92. err = db.Get(exception,
  93. fmt.Sprintf(ExceptionQueryTmpl, "WHERE pattern_id=$1"), pattern.ID)
  94. if err != nil {
  95. return err
  96. }
  97. return exception.PreLoad()
  98. }
  99. // Exists directly updates the struct with the findings
  100. func (exception *Exception) Exists() bool {
  101. db, err := sqlx.Connect(dbDriver, dbConnect)
  102. if err != nil {
  103. logger.Error().Err(err).Msg("cannot open database")
  104. return false
  105. }
  106. defer db.Close()
  107. err = db.Get(exception,
  108. fmt.Sprintf(ExceptionQueryTmpl, "WHERE id=$1 or pattern_id=$2"),
  109. exception.ID, exception.PatternID,
  110. ); if err != nil {
  111. return false
  112. }
  113. err = exception.PreLoad()
  114. if err != nil {
  115. logger.Warn().Err(err).Msg("cannot preload exception struct")
  116. }
  117. return true
  118. }
  119. func (exception *Exception) Update() error {
  120. db, err := sqlx.Connect(dbDriver, dbConnect)
  121. if err != nil {
  122. return err
  123. }
  124. defer db.Close()
  125. _, err = db.NamedExec(fmt.Sprintf(ExceptionNamedUpdateTmpl, "id=:id"), exception)
  126. return err
  127. }
  128. func (exception *Exception) Create() error {
  129. db, err := sqlx.Connect(dbDriver, dbConnect)
  130. if err != nil {
  131. return err
  132. }
  133. defer db.Close()
  134. rows, err := db.NamedQuery(ExceptionNamedInsertTmpl, exception)
  135. if err != nil {
  136. return err
  137. }
  138. defer rows.Close()
  139. for rows.Next() {
  140. err = rows.Scan(&exception.ID)
  141. if err != nil {
  142. return err
  143. }
  144. }
  145. return nil
  146. }
  147. func (exceptions *Exceptions) FindOutOfSync(migration PortalMigration) error {
  148. db, err := sqlx.Connect(dbDriver, dbConnect)
  149. if err != nil {
  150. return err
  151. }
  152. defer db.Close()
  153. err = db.Select(exceptions,
  154. fmt.Sprintf(ExceptionQueryTmpl, "WHERE updated_at > $1"), migration.Version)
  155. if err != nil {
  156. return err
  157. }
  158. return exceptions.PreLoad()
  159. }