storage.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. GNU GENERAL PUBLIC LICENSE
  3. Version 3, 29 June 2007
  4. kyoketsu, a Client-To-Client Network Enumeration System
  5. Copyright (C) 2024 Russell Hrubesky, ChiralWorks Software LLC
  6. Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
  7. Everyone is permitted to copy and distribute verbatim copies
  8. of this license document, but changing it is not allowed.
  9. This program is free software: you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation, either version 3 of the License,
  12. or (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. See the GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package kyoketsu
  21. import (
  22. "database/sql"
  23. "errors"
  24. "github.com/mattn/go-sqlite3"
  25. )
  26. type TopologyDatabaseIO interface {
  27. /*
  28. This interface defines the Input and output methods that will be necessary
  29. for an appropriate implementation of the data storage that the distributed system will use.
  30. When I get around to implementing the client-to-client format of this, it could be anything.
  31. */
  32. Migrate() error
  33. Create(host Host) (*Host, error)
  34. All() ([]Host, error)
  35. GetByNetwork(network string) ([]Host, error)
  36. GetByIP(ip string) (*Host, error)
  37. Update(id int64, updated Host) (*Host, error)
  38. Delete(id int64) error
  39. }
  40. var (
  41. ErrDuplicate = errors.New("record already exists")
  42. ErrNotExists = errors.New("row not exists")
  43. ErrUpdateFailed = errors.New("update failed")
  44. ErrDeleteFailed = errors.New("delete failed")
  45. )
  46. type SQLiteRepo struct {
  47. db *sql.DB
  48. }
  49. // Instantiate a new SQLiteRepo struct
  50. func NewSQLiteRepo(db *sql.DB) *SQLiteRepo {
  51. return &SQLiteRepo{
  52. db: db,
  53. }
  54. }
  55. // Creates a new SQL table with necessary data
  56. func (r *SQLiteRepo) Migrate() error {
  57. query := `
  58. CREATE TABLE IF NOT EXISTS hosts(
  59. id INTEGER PRIMARY KEY AUTOINCREMENT,
  60. fqdn TEXT NOT NULL,
  61. ipv4_address TEXT NOT NULL UNIQUE,
  62. listening_port TEXT NOT NULL,
  63. network TEXT NOT NULL
  64. );
  65. `
  66. _, err := r.db.Exec(query)
  67. return err
  68. }
  69. /*
  70. Create an entry in the hosts table
  71. :param host: a Host entry from a port scan
  72. */
  73. func (r *SQLiteRepo) Create(host Host) (*Host, error) {
  74. res, err := r.db.Exec("INSERT INTO hosts(fqdn, ipv4_address, listening_port, network) values(?,?,?,?)", host.Fqdn, host.IpAddress, host.PortString, host.Network)
  75. if err != nil {
  76. var sqliteErr sqlite3.Error
  77. if errors.As(err, &sqliteErr) {
  78. if errors.Is(sqliteErr.ExtendedCode, sqlite3.ErrConstraintUnique) {
  79. return nil, ErrDuplicate
  80. }
  81. }
  82. return nil, err
  83. }
  84. id, err := res.LastInsertId()
  85. if err != nil {
  86. return nil, err
  87. }
  88. host.Id = id
  89. return &host, nil
  90. }
  91. // Get all Hosts from the host table
  92. func (r *SQLiteRepo) All() ([]Host, error) {
  93. rows, err := r.db.Query("SELECT * FROM hosts")
  94. if err != nil {
  95. return nil, err
  96. }
  97. defer rows.Close()
  98. var all []Host
  99. for rows.Next() {
  100. var host Host
  101. if err := rows.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.PortString, &host.Network); err != nil {
  102. return nil, err
  103. }
  104. all = append(all, host)
  105. }
  106. return all, nil
  107. }
  108. // Get a record by its FQDN
  109. func (r *SQLiteRepo) GetByIP(ip string) (*Host, error) {
  110. row := r.db.QueryRow("SELECT * FROM hosts WHERE ipv4_address = ?", ip)
  111. var host Host
  112. if err := row.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.PortString, &host.Network); err != nil {
  113. if errors.Is(err, sql.ErrNoRows) {
  114. return nil, ErrNotExists
  115. }
  116. return nil, err
  117. }
  118. return &host, nil
  119. }
  120. // Update a record by its ID
  121. func (r *SQLiteRepo) Update(id int64, updated Host) (*Host, error) {
  122. if id == 0 {
  123. return nil, errors.New("invalid updated ID")
  124. }
  125. res, err := r.db.Exec("UPDATE hosts SET fqdn = ?, ipv4_address = ?, listening_port = ?, network = ? WHERE id = ?", updated.Fqdn, updated.IpAddress, updated.PortString, updated.Network, id)
  126. if err != nil {
  127. return nil, err
  128. }
  129. rowsAffected, err := res.RowsAffected()
  130. if err != nil {
  131. return nil, err
  132. }
  133. if rowsAffected == 0 {
  134. return nil, ErrUpdateFailed
  135. }
  136. return &updated, nil
  137. }
  138. // Delete a record by its ID
  139. func (r *SQLiteRepo) Delete(id int64) error {
  140. res, err := r.db.Exec("DELETE FROM hosts WHERE id = ?", id)
  141. if err != nil {
  142. return err
  143. }
  144. rowsAffected, err := res.RowsAffected()
  145. if err != nil {
  146. return err
  147. }
  148. if rowsAffected == 0 {
  149. return ErrDeleteFailed
  150. }
  151. return err
  152. }
  153. func (r *SQLiteRepo) GetByNetwork(network string) ([]Host, error) {
  154. rows, err := r.db.Query("SELECT * FROM hosts WHERE network = ?", network)
  155. if err != nil {
  156. return nil, err
  157. }
  158. var hosts []Host
  159. defer rows.Close()
  160. for rows.Next() {
  161. var host Host
  162. if err := rows.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.PortString, &host.Network); err != nil {
  163. return nil, err
  164. }
  165. hosts = append(hosts, host)
  166. }
  167. return hosts, nil
  168. }