tombo2-progress’s diary

できるだけ毎日1時間を切り取ってここに晒す。誤字脱字気にしない。日本語が崩壊するのも気にしない。最終的にまとめて本ブログに書く

続き

進捗だめでsう

package main

import (
    "fmt"
    "github.com/google/gopacket"
    "github.com/google/gopacket/pcap"
    "log"
    // "strings"
    "time"
)

var (
    device       string = "lo"
    snapshot_len int32  = 1024
    promiscuous  bool   = false
    err          error
    // timeout      time.Duration = 30 * time.Second
    timeout time.Duration = 0 * time.Second
    handle  *pcap.Handle
)

func main() {
    cmdMap := map[byte]string{
        0x00: "COM_SLEEP",
        0x01: "COM_QUIT",
        0x02: "COM_INIT_DB",
        0x03: "COM_QUERY",
        0x04: "COM_FIELD_LIST",
        0x05: "COM_CREATE_DB",
        0x06: "COM_DROP_DB",
        0x07: "COM_REFRESH",
        0x08: "COM_SHUTDOWN",
        0x09: "COM_STATISTICS",
        0x0a: "COM_PROCESS_INFO",
        0x0b: "COM_CONNECT",
        0x0c: "COM_PROCESS_KILL",
        0x0d: "COM_DEBUG",
        0x0e: "COM_PING",
        0x0f: "COM_TIME",
        0x10: "COM_DELAYED_INSERT",
        0x11: "COM_CHANGE_USER",
        0x12: "COM_BINLOG_DUMP",
        0x13: "COM_TABLE_DUMP",
        0x14: "COM_CONNECT_OUT",
        0x15: "COM_REGISTER_SLAVE",
        0x16: "COM_STMT_PREPARE",
        0x17: "COM_STMT_EXECUTE",
        0x18: "COM_STMT_SEND_LONG_DATA",
        0x19: "COM_STMT_CLOSE",
        0x1a: "COM_STMT_RESET",
        0x1b: "COM_SET_OPTION",
        0x1c: "COM_STMT_FETCH",
        0x1d: "COM_DAEMON",
        0x1e: "COM_BINLOG_DUMP_GTID",
        0x1f: "COM_RESET_CONNECTION"}

    // Open device
    handle, err = pcap.OpenLive(device, snapshot_len, promiscuous, timeout)
    if err != nil {
        log.Fatal(err)
    }
    defer handle.Close()

    var filter string = "tcp and port 13306"
    err = handle.SetBPFFilter(filter)
    if err != nil {
        log.Fatal(err)
    }

    // Use the handle as a packet source to process all packets
    packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
    for packet := range packetSource.Packets() {
        applicationLayer := packet.ApplicationLayer()
        if applicationLayer != nil {
            // fmt.Println("Application layer/Payload found.")
            // fmt.Printf("%s\n", applicationLayer.Payload())

            // fmt.Printf("%x\n", applicationLayer.Payload()[0])
            // fmt.Printf("%x\n", applicationLayer.Payload()[4])
            // fmt.Printf("\n")

            byte5 := applicationLayer.Payload()[4]

            switch byte5 {
            case 0x00:
                // com_sleep or OK packet
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x01, 0x09, 0x0a, 0x0b, 0x0d, 0x0e, 0x0f, 0x10, 0x14, 0x1d, 0x1f:
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x02, 0x03, 0x05, 0x06, 0x16:
                // stirng<EOF>
                fmt.Printf("%s: ", cmdMap[byte5])
                if len(applicationLayer.Payload()) > 37 {
                    fmt.Printf("%s\n", applicationLayer.Payload()[5:37])
                } else {
                    fmt.Printf("%s\n", applicationLayer.Payload()[5:])
                }
            case 0x04:
                // str<NULL>,str<EOF>
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x07, 0x08:
                // subcommand[1]
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x0c, 0x19, 0x1a:
                // some id
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x1b:
                // option
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0x11, 0x12, 0x13, 0x15, 0x17, 0x18, 0x1c, 0x1e:
                // complex struct
                fmt.Printf("%s\n", cmdMap[byte5])
            case 0xfe:
                // OK or EOF
                fmt.Printf("OK or EOF\n")
            case 0xff:
                // ERR
                fmt.Printf("ERR\n")
            default:
                fmt.Printf("unknown type\n")
                // fmt.Printf("%s\n", cmdMap[byte5])
            }

            // Process packet here
            // fmt.Println(packet)
        }
    }
}