mirror of
https://github.com/shadow1ng/fscan.git
synced 2025-11-05 10:45:27 +00:00
perf: 优化ParsePort.go的代码,添加注释,规范输出
This commit is contained in:
parent
56c4453c7f
commit
ec346409f7
@ -1,32 +1,46 @@
|
|||||||
package Common
|
package Common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ParsePort(ports string) (scanPorts []int) {
|
// ParsePort 解析端口配置字符串为端口号列表
|
||||||
|
func ParsePort(ports string) []int {
|
||||||
if ports == "" {
|
if ports == "" {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var scanPorts []int
|
||||||
slices := strings.Split(ports, ",")
|
slices := strings.Split(ports, ",")
|
||||||
|
|
||||||
|
// 处理每个端口配置
|
||||||
for _, port := range slices {
|
for _, port := range slices {
|
||||||
port = strings.TrimSpace(port)
|
port = strings.TrimSpace(port)
|
||||||
if port == "" {
|
if port == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理预定义端口组
|
||||||
if PortGroup[port] != "" {
|
if PortGroup[port] != "" {
|
||||||
port = PortGroup[port]
|
groupPorts := ParsePort(PortGroup[port])
|
||||||
scanPorts = append(scanPorts, ParsePort(port)...)
|
scanPorts = append(scanPorts, groupPorts...)
|
||||||
|
fmt.Printf("[*] 解析端口组 %s -> %v\n", port, groupPorts)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 处理端口范围
|
||||||
upper := port
|
upper := port
|
||||||
if strings.Contains(port, "-") {
|
if strings.Contains(port, "-") {
|
||||||
ranges := strings.Split(port, "-")
|
ranges := strings.Split(port, "-")
|
||||||
if len(ranges) < 2 {
|
if len(ranges) < 2 {
|
||||||
|
fmt.Printf("[!] 无效的端口范围格式: %s\n", port)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 确保起始端口小于结束端口
|
||||||
startPort, _ := strconv.Atoi(ranges[0])
|
startPort, _ := strconv.Atoi(ranges[0])
|
||||||
endPort, _ := strconv.Atoi(ranges[1])
|
endPort, _ := strconv.Atoi(ranges[1])
|
||||||
if startPort < endPort {
|
if startPort < endPort {
|
||||||
@ -37,27 +51,40 @@ func ParsePort(ports string) (scanPorts []int) {
|
|||||||
upper = ranges[0]
|
upper = ranges[0]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 生成端口列表
|
||||||
start, _ := strconv.Atoi(port)
|
start, _ := strconv.Atoi(port)
|
||||||
end, _ := strconv.Atoi(upper)
|
end, _ := strconv.Atoi(upper)
|
||||||
for i := start; i <= end; i++ {
|
for i := start; i <= end; i++ {
|
||||||
if i > 65535 || i < 1 {
|
if i > 65535 || i < 1 {
|
||||||
|
fmt.Printf("[!] 忽略无效端口: %d\n", i)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
scanPorts = append(scanPorts, i)
|
scanPorts = append(scanPorts, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 去重并排序
|
||||||
scanPorts = removeDuplicate(scanPorts)
|
scanPorts = removeDuplicate(scanPorts)
|
||||||
|
sort.Ints(scanPorts)
|
||||||
|
|
||||||
|
fmt.Printf("[*] 共解析 %d 个有效端口\n", len(scanPorts))
|
||||||
return scanPorts
|
return scanPorts
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeDuplicate 对整数切片进行去重
|
||||||
func removeDuplicate(old []int) []int {
|
func removeDuplicate(old []int) []int {
|
||||||
result := []int{}
|
// 使用map存储不重复的元素
|
||||||
temp := map[int]struct{}{}
|
temp := make(map[int]struct{})
|
||||||
|
var result []int
|
||||||
|
|
||||||
|
// 遍历并去重
|
||||||
for _, item := range old {
|
for _, item := range old {
|
||||||
if _, ok := temp[item]; !ok {
|
if _, exists := temp[item]; !exists {
|
||||||
temp[item] = struct{}{}
|
temp[item] = struct{}{}
|
||||||
result = append(result, item)
|
result = append(result, item)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user