Golang:如何检查 HTTP 请求是否在 TCP 负载字节中

Golang: how to check if an HTTP request is in the TCP payload bytes

我正在使用 gopacket package and every time I have a TCP packet I want to check if the payload contains an HTTP request. Is there an easy way to do that instead of writing my own parser? There is also a function (see: func ReadRequest(b *bufio.Reader)) which returns a Request 结构,但我不知道应该使用哪种输入。 tcp.Payload是byte[]数组,好像有我需要解析的信息(看下面的例子):

// Get the TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
    fmt.Printf("TCP ")
    // Get actual TCP data from this layer
    tcp, _ := tcpLayer.(*layers.TCP)
    srcPort = tcp.SrcPort
    dstPort = tcp.DstPort
    if tcp.SYN {
        fmt.Print("[SYN] ")
    }
    if tcp.ACK {
        fmt.Print("[ACK] ")
    }
    if tcp.FIN {
        fmt.Print("[FIN] ")
    }
    fmt.Printf("%s:%d > %s:%d ", srcIP, srcPort, dstIP, dstPort)

    fmt.Println(string(tcp.Payload))
}

发送 HTTP 请求后,我得到以下输出:

PKT [001] TCP [SYN] 192.168.2.6:59095 > 192.168.3.5:80
PKT [002] TCP [SYN] [ACK] 192.168.3.5:80 > 192.168.2.6:59095
PKT [003] TCP [ACK] 192.168.2.6:59095 > 192.168.3.5:80
PKT [004] TCP [ACK] 192.168.2.6:59095 > 192.168.3.5:80 GET /certificates/test.pdf HTTP/1.1
User-Agent: Wget/1.15 (linux-gnu)
Accept: */*
Host: 192.168.3.5
Connection: Keep-Alive

欢迎提出任何建议...

在这个问题的帮助下: 下面对 http.ReadRequest() 的尝试成功了...

if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer != nil {
   tcp, _ := tcpLayer.(*layers.TCP)
   if len(tcp.Payload) != 0 {
      reader := bufio.NewReader(bytes.NewReader(tcp.Payload))
      httpReq, err := http.ReadRequest(reader)
      ...
  }
}