Skip to content

Commit 3d3d18a

Browse files
committed
优化端口探测结果输出
1 parent 5ce082b commit 3d3d18a

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/develop/Go/ImprovingGo/2_fscanPro.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 2. Go练习-fscan
22

3-
## 2.1. 存活主机顺序输出
3+
## 2.1. 优化存活主机探测结果输出
44

55
Plugins目录下的icmp.go文件中CheckLive函数对应着探测存活主机功能,堆栈:
66

@@ -73,3 +73,56 @@ chanHosts := make(chan string, len(hostslist))
7373

7474
这回应该没啥问题了,就是不知道效率被影响的程度,毕竟多解析了一遍IP,等用的时候再说吧
7575

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+
![image-20240819095429072](img/2_fscanPro/image-20240819095429072.png)
88+
89+
原本的探测结果在`PortConnect()`函数。一边扫描一边就输出,这样太乱了。但是也有对应的接收结果的 `results` channel, 那直接删除掉原本的输出,处理 `AliveAddress` 结果即可 修改代码如下
90+
91+
![image-20240819095521796](img/2_fscanPro/image-20240819095521796.png)
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+
![image-20240819101048478](img/2_fscanPro/image-20240819101048478.png)
128+
104 KB
Loading
37.5 KB
Loading
53.7 KB
Loading

0 commit comments

Comments
 (0)