-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.go
104 lines (87 loc) · 1.76 KB
/
scanner.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
package main
import (
"fmt"
"log"
"net"
"strings"
"sync"
"time"
)
type scanTask struct {
ip string
port int
timeout int
}
var results sync.Map
func beginScan(sp scanParams) {
tasks := generateTask(sp)
runTask(tasks, sp.concurrency)
printResult()
}
func generateTask(sp scanParams) []scanTask {
tasks := make([]scanTask, 0)
for _, ip := range sp.ipList {
for _, port := range sp.portList {
ipPort := scanTask{ip: ip.String(), port: port, timeout: sp.timeout}
tasks = append(tasks, ipPort)
}
}
return tasks
}
func runTask(tasks []scanTask, concurrency int) {
wg := &sync.WaitGroup{}
taskChan := make(chan scanTask, concurrency)
for i := 0; i < concurrency; i++ {
go scan(taskChan, wg)
}
for _, task := range tasks {
wg.Add(1)
taskChan <- task
}
close(taskChan)
wg.Wait()
}
func scan(taskChan chan scanTask, wg *sync.WaitGroup) {
for task := range taskChan {
err := connect(task)
saveResult(task, err)
wg.Done()
}
}
func saveResult(t scanTask, err error) {
if err != nil {
return
}
// log.Printf("find ip:%v, port: %v\n", t.ip, t.port)
if t.port > 0 {
v, ok := results.Load(t.ip)
if ok {
ports, ok1 := v.([]int)
if ok1 {
ports = append(ports, t.port)
results.Store(t.ip, ports)
}
} else {
ports := make([]int, 0)
ports = append(ports, t.port)
results.Store(t.ip, ports)
}
}
}
func printResult() {
results.Range(func(key, value interface{}) bool {
log.Printf("ip:%v\n", key)
log.Printf("ports: %v\n", value)
log.Println(strings.Repeat("-", 100))
return true
})
}
func connect(t scanTask) error {
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%v:%v", t.ip, t.port), time.Duration(t.timeout)*time.Second)
defer func() {
if conn != nil {
_ = conn.Close()
}
}()
return err
}