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.
 
 
 
 
 
 

157 lines
4.6 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. "github.com/jmoiron/sqlx"
  22. _ "github.com/lib/pq"
  23. )
  24. const (
  25. R2ProfileCleanUpTmpl = `
  26. DELETE FROM r2_profiles
  27. WHERE created_at < now() - interval '%d days';`
  28. R2ProfileNamedInsertTmpl = `
  29. INSERT INTO r2_profiles (
  30. created_at, ip_information_id, r2_daily, r2_weekly, r2_monthly,
  31. r2_daily_network, r2_weekly_network, r2_monthly_network
  32. ) VALUES (
  33. now(), :ip_information_id, :r2_daily, :r2_weekly, :r2_monthly,
  34. :r2_daily_network, :r2_weekly_network, :r2_monthly_network
  35. ) RETURNING id;`
  36. R2ProfileQueryTmpl = `
  37. SELECT p.*,
  38. i.id "ip_information_id.id", i.created_at "ip_information_id.created_at",
  39. i.updated_at "ip_information_id.updated_at", i.ip "ip_information_id.ip",
  40. i.mac "ip_information_id.mac", i.reverse_dns "ip_information_id.reverse_dns",
  41. i.net_bios "ip_information_id.net_bios",
  42. i.country "ip_information_id.country",
  43. i.european_union "ip_information_id.european_union",
  44. i.anonymous_proxy "ip_information_id.anonymous_proxy",
  45. i.satellite_provider "ip_information_id.satellite_provider"
  46. FROM r2_profiles as p,
  47. ip_information as i
  48. WHERE
  49. p.ip_information_id = i.id
  50. %s;`
  51. )
  52. type R2Profile struct {
  53. ID uint `db:"id" json:"id"`
  54. CreatedAt time.Time `db:"created_at" json:"createdAt"`
  55. IPInformationID uint `db:"ip_information_id" json:"ipInformationId"`
  56. IPInformation IPInformation `db:"-" json:"ipInformation"`
  57. R2Daily float64 `db:"r2_daily" json:"r2Daily"`
  58. R2Weekly float64 `db:"r2_weekly" json:"r2Weekly"`
  59. R2Monthly float64 `db:"r2_monthly" json:"r2Monthly"`
  60. R2DailyNetwork float64 `db:"r2_daily_network" json:"r2DailyNetwork"`
  61. R2WeeklyNetwork float64 `db:"r2_weekly_network" json:"r2WeeklyNetwork"`
  62. R2MonthlyNetwork float64 `db:"r2_monthly_network" json:"r2MonthlyNetwork"`
  63. }
  64. type R2Profiles []R2Profile
  65. func NewR2Profile() *R2Profile {
  66. return &R2Profile{ CreatedAt: time.Now() }
  67. }
  68. // make R2Profiles sortable
  69. func (profiles R2Profiles) Len() int { return len(profiles) }
  70. func (profiles R2Profiles) Swap(i, j int) {
  71. profiles[i], profiles[j] = profiles[j], profiles[i]
  72. }
  73. func (profiles R2Profiles) Less(i, j int) bool {
  74. return profiles[i].CreatedAt.Unix() < profiles[j].CreatedAt.Unix()
  75. }
  76. func (profiles *R2Profiles) FindAll() error {
  77. db, err := sqlx.Connect(dbDriver, dbConnect)
  78. if err != nil {
  79. return err
  80. }
  81. defer db.Close()
  82. return db.Select(&profiles, fmt.Sprintf(R2ProfileQueryTmpl, ""))
  83. }
  84. // FindTodaysProfiles selects all profiles with a creation time of now()::date
  85. func (profiles *R2Profiles) FindTodaysProfiles() error {
  86. db, err := sqlx.Connect(dbDriver, dbConnect)
  87. if err != nil {
  88. return err
  89. }
  90. defer db.Close()
  91. return db.Select(profiles, fmt.Sprintf(
  92. R2ProfileQueryTmpl,
  93. "AND p.created_at::date = now()::date",
  94. ))
  95. }
  96. // CleanUp will purge all profiles older then X days
  97. func (profiles *R2Profiles) CleanUp(days int) error {
  98. db, err := sqlx.Connect(dbDriver, dbConnect)
  99. if err != nil {
  100. return err
  101. }
  102. defer db.Close()
  103. db.MustExec(fmt.Sprintf(R2ProfileCleanUpTmpl, days))
  104. db.MustExec(fmt.Sprintf(IPProfileCleanUpTmpl, days))
  105. return nil
  106. }
  107. func (profile *R2Profile) Create() error {
  108. db, err := sqlx.Connect(dbDriver, dbConnect)
  109. if err != nil {
  110. return err
  111. }
  112. defer db.Close()
  113. rows, err := db.NamedQuery(R2ProfileNamedInsertTmpl, profile)
  114. if err != nil {
  115. return err
  116. }
  117. defer rows.Close()
  118. for rows.Next() {
  119. err = rows.Scan(&profile.ID)
  120. if err != nil {
  121. return err
  122. }
  123. }
  124. return nil
  125. }
  126. func (profile *R2Profile) Exists() bool {
  127. db, err := sqlx.Connect(dbDriver, dbConnect)
  128. if err != nil {
  129. logger.Error().Err(err).Msg("cannot connect to database")
  130. return false
  131. }
  132. defer db.Close()
  133. err = db.Get(profile, fmt.Sprintf(R2ProfileQueryTmpl, ` AND p.id=$1`), profile.ID)
  134. return err == nil
  135. }