Skip to content

Commit 7700c56

Browse files
committed
fix: resolve data races, remove dead code, refactor resolve_site
- cache.go: Fix Save() data race (RLock->Lock), improve GetProgress total - interrupt_handler.go: Mutex->RWMutex for better read concurrency - resolve_site.go: Extract runScan() to eliminate ~200 lines duplication, remove unused Websites param, case-insensitive title matching, remove duplicate MarkCompleted+Save - main.go: Thread-safe GetWebsites() in interrupt handler - tools: Remove dead variables, update function signatures
1 parent 2afb71e commit 7700c56

File tree

6 files changed

+127
-262
lines changed

6 files changed

+127
-262
lines changed

main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,17 @@ func setupInterruptHandler() {
250250
// Give goroutines a moment to stop
251251
time.Sleep(100 * time.Millisecond)
252252

253-
if interruptData != nil && len(interruptData.Websites) > 0 {
254-
fmt.Printf("\n[*] Found %d websites before interruption\n", len(interruptData.Websites))
253+
// Use thread-safe getter to avoid race with worker goroutines
254+
websites := interruptData.GetWebsites()
255+
if interruptData != nil && len(websites) > 0 {
256+
fmt.Printf("\n[*] Found %d websites before interruption\n", len(websites))
255257
fmt.Print("\nDo you want to export the results? (Y/n): ")
256258

257259
var response string
258260
_, _ = fmt.Scanln(&response)
259261

260262
if response == "y" || response == "Y" || response == "" {
261-
modules.ExportInterruptedResults(interruptData.Websites, interruptData.Domain,
263+
modules.ExportInterruptedResults(websites, interruptData.Domain,
262264
interruptData.Timeout, interruptData.IPBlocks)
263265
fmt.Println("\n[+] Results exported successfully")
264266
} else {

modules/cache.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func LoadCache(filePath string) (*Cache, error) {
7070

7171
// Save persists cache to file
7272
func (c *Cache) Save() error {
73-
c.mu.RLock()
74-
defer c.mu.RUnlock()
73+
c.mu.Lock()
74+
defer c.mu.Unlock()
7575

7676
c.Data.LastUpdate = time.Now().Format(time.RFC3339)
7777

@@ -152,17 +152,25 @@ func (c *Cache) GetResults() [][]string {
152152
func (c *Cache) GetProgress() (scanned int, total int, results int) {
153153
c.mu.RLock()
154154
defer c.mu.RUnlock()
155-
return len(c.Data.ScannedIPs), 0, len(c.Data.Results)
155+
totalIPs := len(c.Data.ScannedIPs)
156+
// Use IP blocks to estimate total if available
157+
if len(c.Data.IPBlocks) > 0 {
158+
totalIPs = 0
159+
for range c.Data.IPBlocks {
160+
// Approximate: each block contributes IPs (exact count requires CIDR parsing)
161+
totalIPs += 256 // rough estimate per block
162+
}
163+
}
164+
return len(c.Data.ScannedIPs), totalIPs, len(c.Data.Results)
156165
}
157166

158167
// GenerateCacheFileName generates a cache file name based on scan parameters
159168
func GenerateCacheFileName(asn, domain string) string {
160-
timestamp := time.Now().Unix()
161169
if asn != "" {
162170
return "ipmap_" + asn + "_cache.json"
163171
}
164172
if domain != "" {
165173
return "ipmap_" + SanitizeFilename(domain) + "_cache.json"
166174
}
167-
return "ipmap_" + strconv.FormatInt(timestamp, 10) + "_cache.json"
175+
return "ipmap_" + strconv.FormatInt(time.Now().Unix(), 10) + "_cache.json"
168176
}

modules/interrupt_handler.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type InterruptData struct {
1010
Timeout int
1111
Cancelled bool // Flag to indicate cancellation
1212
CancelCh chan struct{} // Channel to signal cancellation
13-
mu sync.Mutex
13+
mu sync.RWMutex
1414
}
1515

1616
// NewInterruptData creates a new InterruptData with initialized cancel channel
@@ -38,8 +38,8 @@ func (id *InterruptData) IsCancelled() bool {
3838
if id == nil {
3939
return false
4040
}
41-
id.mu.Lock()
42-
defer id.mu.Unlock()
41+
id.mu.RLock()
42+
defer id.mu.RUnlock()
4343
return id.Cancelled
4444
}
4545

@@ -58,7 +58,7 @@ func (id *InterruptData) GetWebsites() [][]string {
5858
if id == nil {
5959
return nil
6060
}
61-
id.mu.Lock()
62-
defer id.mu.Unlock()
61+
id.mu.RLock()
62+
defer id.mu.RUnlock()
6363
return id.Websites
6464
}

0 commit comments

Comments
 (0)