|
1 | 1 | # 2. Go练习-fscan |
2 | 2 |
|
3 | | -## 2.1. 存活主机顺序输出 |
| 3 | +## 2.1. 优化存活主机探测结果输出 |
4 | 4 |
|
5 | 5 | Plugins目录下的icmp.go文件中CheckLive函数对应着探测存活主机功能,堆栈: |
6 | 6 |
|
@@ -73,3 +73,56 @@ chanHosts := make(chan string, len(hostslist)) |
73 | 73 |
|
74 | 74 | 这回应该没啥问题了,就是不知道效率被影响的程度,毕竟多解析了一遍IP,等用的时候再说吧 |
75 | 75 |
|
| 76 | +## 2.2. 优化端口探测结果输出 |
| 77 | + |
| 78 | +fscan 端口扫描函数调用堆栈 |
| 79 | + |
| 80 | +``` |
| 81 | +Plugins.PortScan (portscan.go:48) github.com/shadow1ng/fscan/Plugins |
| 82 | +Plugins.Scan (scanner.go:40) github.com/shadow1ng/fscan/Plugins |
| 83 | +main.main (main.go:15) main |
| 84 | +runtime.main (proc.go:250) runtime |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | +原本的探测结果在`PortConnect()`函数。一边扫描一边就输出,这样太乱了。但是也有对应的接收结果的 `results` channel, 那直接删除掉原本的输出,处理 `AliveAddress` 结果即可 修改代码如下 |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | +在`PortConnect()`函数中`AliveAddress` 返回之前,处理一遍结果。 |
| 94 | + |
| 95 | +```go |
| 96 | +func PortScan(hostslist []string, ports string, timeout int64) []string { |
| 97 | + // |
| 98 | + // 中间省略 |
| 99 | + // |
| 100 | + wg.Wait() |
| 101 | + close(Addrs) |
| 102 | + close(results) |
| 103 | + MapIPToPorts(AliveAddress) |
| 104 | + return AliveAddress |
| 105 | +} |
| 106 | + |
| 107 | +func MapIPToPorts(ipPortList []string) { |
| 108 | + ipToPorts := make(map[string][]int) |
| 109 | + // 遍历列表,解析IP和端口,并将端口添加到对应IP的列表中 |
| 110 | + for _, ipPort := range ipPortList { |
| 111 | + parts := strings.Split(ipPort, ":") |
| 112 | + ip := parts[0] |
| 113 | + port, _ := strconv.Atoi(parts[1]) |
| 114 | + ipToPorts[ip] = append(ipToPorts[ip], port) |
| 115 | + } |
| 116 | + |
| 117 | + for ip, ports := range ipToPorts { |
| 118 | + sort.Ints(ports) |
| 119 | + result := fmt.Sprintf(" %s: %v", ip, ports) |
| 120 | + common.LogSuccess(result) |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +结果大概这样 |
| 126 | + |
| 127 | + |
| 128 | + |
0 commit comments