-
Notifications
You must be signed in to change notification settings - Fork 81
/
scan.go
128 lines (109 loc) · 2.82 KB
/
scan.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"log"
"os"
"os/signal"
"sync"
"sync/atomic"
"time"
)
type ScanRecord struct {
IP string
PingRTT time.Duration
RTT time.Duration
httpVerifyTimeout time.Duration
}
type ScanOptions struct {
Config *GScanConfig
recordMutex sync.Mutex
records []*ScanRecord
scanCounter int32
}
func (options *ScanOptions) AddRecord(rec *ScanRecord) {
options.recordMutex.Lock()
if nil == options.records {
options.records = make([]*ScanRecord, 0)
}
options.records = append(options.records, rec)
options.recordMutex.Unlock()
log.Printf("Found a record: IP=%s, RTT=%s\n", rec.IP, rec.RTT.String())
}
func (options *ScanOptions) IncScanCounter() {
scanCounter := atomic.AddInt32(&(options.scanCounter), 1)
if scanCounter%1000 == 0 {
log.Printf("Scanned %d IPs, Found %d records\n", scanCounter, options.RecordSize())
}
}
func (options *ScanOptions) RecordSize() int {
options.recordMutex.Lock()
defer options.recordMutex.Unlock()
return len(options.records)
}
var testIPFunc func(ip string, config *GScanConfig, record *ScanRecord) bool
func testip(ip string, config *GScanConfig) *ScanRecord {
record := new(ScanRecord)
record.IP = ip
for i := 0; i < config.ScanCountPerIP; i++ {
if !testIPFunc(ip, config, record) {
return nil
}
}
record.PingRTT = record.PingRTT / time.Duration(config.ScanCountPerIP)
record.RTT = record.RTT / time.Duration(config.ScanCountPerIP)
return record
}
func testip_worker(ctx context.Context, ch chan string, options *ScanOptions, wg *sync.WaitGroup) {
defer wg.Done()
for ip := range ch {
var pingRTT time.Duration
if options.Config.VerifyPing {
start := time.Now()
// pingRTT = (options.Config.Ping.ScanMinPingRTT + options.Config.Ping.ScanMaxPingRTT) / 2
if err := Ping(ip, options.Config.Ping.ScanMaxPingRTT); err != nil {
continue
}
pingRTT = time.Since(start)
if options.Config.Ping.ScanMinPingRTT > 0 && pingRTT < options.Config.Ping.ScanMinPingRTT {
continue
}
}
record := testip(ip, options.Config)
if record != nil {
record.PingRTT = record.PingRTT + pingRTT
select {
case <-ctx.Done():
return
default:
options.AddRecord(record)
}
}
options.IncScanCounter()
}
}
func StartScan(options *ScanOptions, cfg *ScanConfig, ipqueue chan string) (evalCount int) {
var wg sync.WaitGroup
wg.Add(options.Config.ScanWorker)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := make(chan string, 100)
for i := 0; i < options.Config.ScanWorker; i++ {
go testip_worker(ctx, ch, options, &wg)
}
for ip := range ipqueue {
select {
case ch <- ip:
case <-interrupt:
return
}
evalCount++
if options.RecordSize() >= cfg.RecordLimit {
break
}
}
close(ch)
wg.Wait()
return evalCount
}