Browse Source

fixed data race condition, mutex ftw

AETH-erial 10 months ago
parent
commit
6dc1d1da56
2 changed files with 9 additions and 4 deletions
  1. 1 0
      cmd/kyoketsu-web/kyoketsu-web.go
  2. 8 4
      pkg/scanner.go

+ 1 - 0
cmd/kyoketsu-web/kyoketsu-web.go

@@ -39,6 +39,7 @@ import (
 const dbfile = "sqlite.db"
 
 func main() {
+	os.Remove(dbfile) // TODO: remove this once i add more smart interaction with the DB
 	db, err := sql.Open("sqlite3", dbfile)
 	if err != nil {
 		log.Fatal(err)

+ 8 - 4
pkg/scanner.go

@@ -60,14 +60,17 @@ Perform a concurrent TCP port dial on a host, either by domain name or IP.
 */
 func PortWalk(addr string, portmap map[int]string) *Host {
 	wg := &sync.WaitGroup{}
+	mu := &sync.Mutex{}
 	out := []*PortScanResult{}
-
 	for p, s := range portmap {
 		wg.Add(1)
-		go func(target string, p int, s string) {
+		go func(target string, p int, s string, mu *sync.Mutex) {
 			defer wg.Done()
-			out = append(out, singlePortScan(target, p, s))
-		}(addr, p, s)
+			scanout := singlePortScan(target, p, s)
+			mu.Lock()
+			out = append(out, scanout)
+			mu.Unlock()
+		}(addr, p, s, mu)
 	}
 	wg.Wait()
 	host := &Host{IpAddress: addr, ListeningPorts: map[int]string{}}
@@ -76,6 +79,7 @@ func PortWalk(addr string, portmap map[int]string) *Host {
 			host.ListeningPorts[out[i].PortNumber] = out[i].Service
 			host.PortString = fmt.Sprintf("%s,%v", host.PortString, out[i].PortNumber)
 		}
+		host.PortString = strings.TrimPrefix(host.PortString, ",")
 
 	}
 	return host