Browse Source

got the db thing working good

AETH-erial 10 months ago
parent
commit
2f5e328deb
3 changed files with 9 additions and 7 deletions
  1. 1 1
      pkg/html/templates/ip_table.html
  2. 3 1
      pkg/scanner.go
  3. 5 5
      pkg/storage.go

+ 1 - 1
pkg/html/templates/ip_table.html

@@ -3,6 +3,6 @@
     <div class="col border border-white text-white"><p class="font-monospace fs-3">{{ .Fqdn }}</p></div>
     <div class="col border border-white text-white"><p class="font-monospace fs-3">{{ .IpAddress }}</p></div>
     <div class="col border border-white text-white"><p class="font-monospace fs-3">{{ .PingResponse }}</p></div>
-    <div class="col border border-white text-white"><p class="font-monospace fs-3">{{ .ListeningPorts }}</p></div>
+    <div class="col border border-white text-white"><p class="font-monospace fs-3">{{ .PortString }}</p></div>
 </div>
 {{ end }}

+ 3 - 1
pkg/scanner.go

@@ -121,10 +121,12 @@ func NetSweep(ips []net.IP, ports []int, scanned chan Host) {
 		wg.Add(1)
 		go func(target string, portnum []int, wgrp *sync.WaitGroup) {
 			defer wgrp.Done()
+			portscanned := PortWalk(target, portnum)
 			scanned <- Host{
 				Fqdn:           getFqdn(target),
 				IpAddress:      target,
-				ListeningPorts: PortWalk(target, portnum),
+				ListeningPorts: portscanned,
+				PortString:     strings.Trim(strings.Join(strings.Fields(fmt.Sprint(portscanned)), ","), "[]"),
 			}
 
 		}(ips[i].String(), ports, wg)

+ 5 - 5
pkg/storage.go

@@ -74,7 +74,7 @@ func (r *SQLiteRepo) Migrate() error {
         id INTEGER PRIMARY KEY AUTOINCREMENT,
         fqdn TEXT NOT NULL,
         ipv4_address TEXT NOT NULL UNIQUE,
-        listening_port INTEGER[] NOT NULL
+        listening_port TEXT NOT NULL
     );
     `
 
@@ -88,7 +88,7 @@ Create an entry in the hosts table
 	:param host: a Host entry from a port scan
 */
 func (r *SQLiteRepo) Create(host Host) (*Host, error) {
-	res, err := r.db.Exec("INSERT INTO hosts(fqdn, ipv4_address, listening_port) values(?,?,?)", host.Fqdn, host.IpAddress, host.ListeningPorts)
+	res, err := r.db.Exec("INSERT INTO hosts(fqdn, ipv4_address, listening_port) values(?,?,?)", host.Fqdn, host.IpAddress, host.PortString)
 	if err != nil {
 		var sqliteErr sqlite3.Error
 		if errors.As(err, &sqliteErr) {
@@ -119,7 +119,7 @@ func (r *SQLiteRepo) All() ([]Host, error) {
 	var all []Host
 	for rows.Next() {
 		var host Host
-		if err := rows.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.ListeningPorts); err != nil {
+		if err := rows.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.PortString); err != nil {
 			return nil, err
 		}
 		all = append(all, host)
@@ -132,7 +132,7 @@ func (r *SQLiteRepo) GetByIP(ip string) (*Host, error) {
 	row := r.db.QueryRow("SELECT * FROM hosts WHERE ipv4_address = ?", ip)
 
 	var host Host
-	if err := row.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.ListeningPorts); err != nil {
+	if err := row.Scan(&host.Id, &host.Fqdn, &host.IpAddress, &host.PortString); err != nil {
 		if errors.Is(err, sql.ErrNoRows) {
 			return nil, ErrNotExists
 		}
@@ -146,7 +146,7 @@ func (r *SQLiteRepo) Update(id int64, updated Host) (*Host, error) {
 	if id == 0 {
 		return nil, errors.New("invalid updated ID")
 	}
-	res, err := r.db.Exec("UPDATE hosts SET fqdn = ?, ipv4_address = ?, listening_port = ? WHERE id = ?", updated.Fqdn, updated.IpAddress, updated.ListeningPorts, id)
+	res, err := r.db.Exec("UPDATE hosts SET fqdn = ?, ipv4_address = ?, listening_port = ? WHERE id = ?", updated.Fqdn, updated.IpAddress, updated.PortString, id)
 	if err != nil {
 		return nil, err
 	}