基于Golang的端口扫描器

第一版 (8s)

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
package main

import (
"fmt"
"net"
"sort"
"time"
)

// 定义一个缓冲池
func worker(ports, results chan int) {
for p := range ports {
addresss := fmt.Sprintf("在这里写你的url:%d", p)
conn, err := net.Dial("tcp", addresss)
if err != nil {
results <- -p
continue
}
conn.Close()
results <- p
}
}

func main() {
start := time.Now()
var open_ports []int
var closed_ports []int
ports := make(chan int, 500)
results := make(chan int)
for i := 1; i < cap(ports); i++ {
go worker(ports, results)
}
go func() {
for i := 1; i <= 1000; i++ {
ports <- i
}
}()

for i := 1; i < 1000; i++ {
ports := <-results
if ports > 0 {
open_ports = append(open_ports, ports)
} else {
closed_ports = append(closed_ports, -ports)
}
}

close(ports)
defer close(results)
sort.Ints(open_ports)
sort.Ints(closed_ports)

for _, port := range open_ports {
fmt.Printf("%d is open \n", port)
}
elapsed := time.Since(start) / 1e9
fmt.Printf("\n\n%d seconds", elapsed)

}

// 8 seconds

   1000个端口,500个资源池,跑出来结果需要8s,而且有的开放的端口还显示的closed,看着之前写的python的多线程跑出结果4s,沉默了.分析流程可得,设置多少个资源池,每次就会先把资源池里的东西跑完,再换下一批,或者是太快了,肉眼看着就一块儿出来的.但结果就是一批一批的出,很显然这不是我想看到的.

   我本身的需求是要在资源池里输入一个,工作完后直接去输出,而不是等所有工作池完成一遍后再全返回到管道