Skip to content

Instantly share code, notes, and snippets.

@oiooj
Last active December 26, 2017 10:21
Show Gist options
  • Select an option

  • Save oiooj/cce5d640b461bfd2958d042ca50b2552 to your computer and use it in GitHub Desktop.

Select an option

Save oiooj/cce5d640b461bfd2958d042ca50b2552 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"encoding/binary"
//"encoding/hex"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"log"
"time"
)
var (
device string = "wlan0"
snapshotLen int32 = 1024
promiscuous bool = false
err error
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
)
var (
serverIP string
serverPort string
clientIP string
clientPort string
)
func main() {
// Open file instead of device
handle, err = pcap.OpenOffline("2.pcapng")
if err != nil {
log.Fatal(err)
}
defer handle.Close()
/*
// Open device
handle, err = pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
defer handle.Close()
*/
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
printPacketInfo(packet)
}
}
func printPacketInfo(packet gopacket.Packet) {
ipLayer := packet.Layer(layers.LayerTypeIPv4)
var dstIP, srcIP string
if ipLayer != nil {
ip, _ := ipLayer.(*layers.IPv4)
dstIP = ip.DstIP.String()
srcIP = ip.SrcIP.String()
}
// Let's see if the packet is UDP
udpLayer := packet.Layer(layers.LayerTypeUDP)
if udpLayer != nil {
udp, _ := udpLayer.(*layers.UDP)
if serverIP == "" && len(udp.Payload) == 25 && udp.Payload[24] == 0x04 && udp.Payload[0] == 0x01 && udp.DstPort > 7000 && udp.DstPort < 8000 {
fmt.Println("pubg game statrt!")
fmt.Println("=================")
serverIP = dstIP
serverPort = udp.DstPort.String()
clientIP = srcIP
clientPort = udp.SrcPort.String()
fmt.Printf("Server IP: %s\n", serverIP)
fmt.Printf("Server UDP Port: %s\n", serverPort)
fmt.Printf("Client IP: %s\n", clientIP)
fmt.Printf("Client UDP Port: %s\n", clientPort)
fmt.Println("=================")
}
if len(udp.Payload) == 44 {
parseLocalPosition(udp)
}
}
}
func parseLocalPosition(udp *layers.UDP) {
size := len(udp.Payload)
x := udp.Payload[size-12 : size-8]
y := udp.Payload[size-8 : size-4]
z := udp.Payload[size-4:]
x_buf := bytes.NewBuffer(x)
y_buf := bytes.NewBuffer(y)
z_buf := bytes.NewBuffer(z)
var xInt, yInt, zInt int16
binary.Read(x_buf, binary.LittleEndian, &xInt)
binary.Read(y_buf, binary.LittleEndian, &yInt)
binary.Read(z_buf, binary.LittleEndian, &zInt)
fmt.Printf("location: X:%d Y:%d Z %d\n", xInt, yInt, zInt)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment