go -简单工作池

xjreopfe  于 2023-09-28  发布在  Go
关注(0)|答案(1)|浏览(87)

作为一个初学go和编程的人,我一直在用gopacket库编写一个端口扫描器,大部分代码都已经完成了,但是我遇到了一个问题,产生了太多的goroutines,并得到了'read ip 4 0.0.0.0:i/o timeout'我做了一些研究,似乎我需要实现一个工人池我一直在尝试实现这个例子'https://gobyexample.com/worker-pools',因为我还在学习goroutines和通道我已经学习了几天了,似乎无法弄清楚如何在我的程序中正确地实现上面的例子,你们能给予我一些指针吗?或者最好是一个代码修复示例。

package main

import (
    "fmt"
    "log"
    "net"
    "time"

    "github.com/google/gopacket"
    "github.com/google/gopacket/layers"
)

// Get preferred outbound ip and port of this machine
func GetOutboundIPPort() (net.IP, int) {
    conn, err := net.Dial("udp", "1.1.1.1:80")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    localAddr := conn.LocalAddr().(*net.UDPAddr)
    return localAddr.IP, localAddr.Port
}

func ipv4_gen(out chan net.IP) {
    ip, ipnet, err := net.ParseCIDR("192.168.0.0/24")
    if err != nil {
        log.Fatal(err)
    }
    for ip := ip.Mask(ipnet.Mask); ipnet.Contains(ip); inc(ip) {
        time.Sleep(300 * time.Millisecond)
        dstaddrs, err := net.LookupIP(ip.String())
        if err != nil {
            log.Fatal(err)
        }
        dstip := dstaddrs[0].To4()
        out <- dstip
    }
    close(out)
}

func inc(ip net.IP) {
    for j := len(ip) - 1; j >= 0; j-- {
        ip[j]++
        if ip[j] > 0 {
            break
        }
    }
}

func port_scanner(dstip net.IP) {
    dstport := layers.TCPPort(80)

    srcip, port := GetOutboundIPPort()
    srcport := layers.TCPPort(port)

    ip := &layers.IPv4{
        SrcIP:    srcip,
        DstIP:    dstip,
        Protocol: layers.IPProtocolTCP,
    }
    tcp := &layers.TCP{
        SrcPort: srcport,
        DstPort: dstport,
        Seq:     1105024978,
        SYN:     true,
        Window:  14600,
    }
    tcp.SetNetworkLayerForChecksum(ip)
    buf := gopacket.NewSerializeBuffer()
    opts := gopacket.SerializeOptions{
        ComputeChecksums: true,
        FixLengths:       true,
    }
    if err := gopacket.SerializeLayers(buf, opts, tcp); err != nil {
        log.Fatal(err)
    }

    conn, err := net.ListenPacket("ip4:tcp", "0.0.0.0")
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    if _, err := conn.WriteTo(buf.Bytes(), &net.IPAddr{IP: dstip}); err != nil {
        log.Fatal(err)
    }

    // Set deadline so we don't wait forever.
    if err := conn.SetDeadline(time.Now().Add(10 * time.Second)); err != nil {
        log.Fatal(err)
    }

    for {
        b := make([]byte, 4096)
        n, addr, err := conn.ReadFrom(b)
        if err != nil {
            log.Println("error reading packet: ", err)
            return
        } else if addr.String() == dstip.String() {
            // Decode a packet
            packet := gopacket.NewPacket(b[:n], layers.LayerTypeTCP, gopacket.Default)
            // Get the TCP layer from this packet
            if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
                tcp, _ := tcpLayer.(*layers.TCP)

                if tcp.DstPort == srcport {
                    if tcp.SYN && tcp.ACK {
                        fmt.Printf("Discovered open port %d/tcp on %s\n", dstport, dstip)
                    }
                    return
                }
            }
        }
    }
}

func worker(id int, ips <-chan net.IP) {
    for ip := range ips {
        go port_scanner(ip)
    }
}

func main() {
    ips := make(chan net.IP)
    go ipv4_gen(ips)

    for w := 1; w <= 10; w++ {
        go worker(w, ips)
    }
}
qfe3c7zg

qfe3c7zg1#

你为worker内部的每一项工作都开始了一个新的goroutine,所以它违背了它的目的。
你需要运行工作,而不是在worker内部启动一个goroutine。

相关问题