一文详解Go如何实现端口扫描器
本篇文章给大家带来了关于Go的相关知识,其中主要跟大家介绍Go怎么实现端口扫描器,有代码示例,感兴趣的朋友下面一起来看一下吧,希望对大家有帮助。
利用 GO 批量扫描服务器端口
1、端口扫描器 V1 - 基本操作
package mainimport ( "fmt" "net" "time" "unsafe")func main() { tcpScan("127.0.0.1", 1, 65535)}func tcpScan(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } for i := portStart; i <= portEnd; i++ { address := fmt.Sprintf("%s:%d", ip, i) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) continue } conn.Close() fmt.Printf("[info] %s Open \n", address) } cost := time.Since(start) fmt.Printf("[tcpScan] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
2、端口扫描器 V2 - 使用 goroutine
package mainimport ( "fmt" "net" "sync" "time" "unsafe")func main() { tcpScanByGoroutine("127.0.0.1", 1, 65535)}func tcpScanByGoroutine(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } var wg sync.WaitGroup for i := portStart; i <= portEnd; i++ { wg.Add(1) go func(j int) { defer wg.Done() address := fmt.Sprintf("%s:%d", ip, j) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) return } conn.Close() fmt.Printf("[info] %s Open \n", address) }(i) } wg.Wait() cost := time.Since(start) fmt.Printf("[tcpScanByGoroutine] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
3、端口扫描器 V3 - 利用 Goroutine + Channel
package mainimport ( "fmt" "net" "sync" "time" "unsafe")func main() { tcpScanByGoroutineWithChannel("127.0.0.1", 1, 65535)}func handleWorker(ip string, ports chan int, wg *sync.WaitGroup) { for p := range ports { address := fmt.Sprintf("%s:%d", ip, p) conn, err := net.Dial("tcp", address) if err != nil { fmt.Printf("[info] %s Close \n", address) wg.Done() continue } conn.Close() fmt.Printf("[info] %s Open \n", address) wg.Done() }}func tcpScanByGoroutineWithChannel(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } ports := make(chan int, 100) var wg sync.WaitGroup for i := 0; i < cap(ports); i++ { go handleWorker(ip, ports, &wg) } for i := portStart; i <= portEnd; i++ { wg.Add(1) ports <- i } wg.Wait() close(ports) cost := time.Since(start) fmt.Printf("[tcpScanByGoroutineWithChannel] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
4 端口扫描器 V4 - 引入两个 Channel
// packagepackage mainimport ( "fmt" "net" "sort" "time" "unsafe")func main() { tcpScanByGoroutineWithChannelAndSort("127.0.0.1", 1, 65535)}// The function handles checking if ports are open or closed for a given IP address.func handleWorker(ip string, ports chan int, results chan int) { for p := range ports { address := fmt.Sprintf("%s:%d", ip, p) conn, err := net.Dial("tcp", address) if err != nil { // fmt.Printf("[debug] ip %s Close \n", address) results <- (-p) continue } // fmt.Printf("[debug] ip %s Open \n", address) conn.Close() results <- p }}func tcpScanByGoroutineWithChannelAndSort(ip string, portStart int, portEnd int) { start := time.Now() // 参数校验 isok := verifyParam(ip, portStart, portEnd) if isok == false { fmt.Printf("[Exit]\n") } ports := make(chan int, 50) results := make(chan int) var openSlice []int var closeSlice []int // 任务生产者-分发任务 (新起一个 goroutinue ,进行分发数据) go func(a int, b int) { for i := a; i <= b; i++ { ports <- i } }(portStart, portEnd) // 任务消费者-处理任务 (每一个端口号都分配一个 goroutinue ,进行扫描) // 结果生产者-每次得到结果 再写入 结果 chan 中 for i := 0; i < cap(ports); i++ { go handleWorker(ip, ports, results) } // 结果消费者-等待收集结果 (main中的 goroutinue 不断从 chan 中阻塞式读取数据) for i := portStart; i <= portEnd; i++ { resPort := <-results if resPort > 0 { openSlice = append(openSlice, resPort) } else { closeSlice = append(closeSlice, -resPort) } } // 关闭 chan close(ports) close(results) // 排序 sort.Ints(openSlice) sort.Ints(closeSlice) // 输出 for _, p := range openSlice { fmt.Printf("[info] %s:%-8d Open\n", ip, p) } // for _, p := range closeSlice { // fmt.Printf("[info] %s:%-8d Close\n", ip, p) // } cost := time.Since(start) fmt.Printf("[tcpScanByGoroutineWithChannelAndSort] cost %s second \n", cost)}func verifyParam(ip string, portStart int, portEnd int) bool { netip := net.ParseIP(ip) if netip == nil { fmt.Println("[Error] ip type is must net.ip") return false } fmt.Printf("[Info] ip=%s | ip type is: %T | ip size is: %d \n", netip, netip, unsafe.Sizeof(netip)) if portStart < 1 || portEnd > 65535 { fmt.Println("[Error] port is must in the range of 1~65535") return false } fmt.Printf("[Info] port start:%d end:%d \n", portStart, portEnd) return true}
以上是一文详解Go如何实现端口扫描器的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

在Go中,可以使用gorilla/websocket包发送WebSocket消息。具体步骤:建立WebSocket连接。发送文本消息:调用WriteMessage(websocket.TextMessage,[]byte("消息"))。发送二进制消息:调用WriteMessage(websocket.BinaryMessage,[]byte{1,2,3})。

在Go中,函数生命周期包括定义、加载、链接、初始化、调用和返回;变量作用域分为函数级和块级,函数内的变量在内部可见,而块内的变量仅在块内可见。

Go和Go语言是不同的实体,具有不同的特性。Go(又称Golang)以其并发性、编译速度快、内存管理和跨平台优点而闻名。Go语言的缺点包括生态系统不如其他语言丰富、语法更严格以及缺乏动态类型。

在Go中,可以使用正则表达式匹配时间戳:编译正则表达式字符串,例如用于匹配ISO8601时间戳的表达式:^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$。使用regexp.MatchString函数检查字符串是否与正则表达式匹配。

内存泄漏会导致Go程序内存不断增加,可通过:关闭不再使用的资源,如文件、网络连接和数据库连接。使用弱引用防止内存泄漏,当对象不再被强引用时将其作为垃圾回收目标。利用go协程,协程栈内存会在退出时自动释放,避免内存泄漏。

使用IDE查看Go函数文档:将光标悬停在函数名称上。按下热键(GoLand:Ctrl+Q;VSCode:安装GoExtensionPack后,F1并选择"Go:ShowDocumentation")。

在Go中传递map给函数时,默认会创建副本,对副本的修改不影响原map。如果需要修改原始map,可通过指针传递。空map需小心处理,因为技术上是nil指针,传递空map给期望非空map的函数会发生错误。

在Golang中,错误包装器允许你在原始错误上追加上下文信息,从而创建新错误。这可用于统一不同库或组件抛出的错误类型,简化调试和错误处理。步骤如下:使用errors.Wrap函数将原有错误包装成新错误。新错误包含原始错误的上下文信息。使用fmt.Printf输出包装后的错误,提供更多上下文和可操作性。在处理不同类型的错误时,使用errors.Wrap函数统一错误类型。
