run go fmt

This commit is contained in:
wanyaoqi 2018-12-03 21:58:54 +08:00
parent 10cf59920e
commit 07ef6cbf3f
17 changed files with 307 additions and 315 deletions

View File

@ -191,28 +191,28 @@ func (p *StreamPool) connections() []*connection {
return conns return conns
} }
//FlushOlderThan finds any streams waiting for packets older than the given //FlushOlderThan finds any streams waiting for packets older than the given
//time, and pushes through the data they have (IE: tells them to stop waiting //time, and pushes through the data they have (IE: tells them to stop waiting
//and skip the data they're waiting for). //and skip the data they're waiting for).
// //
//Each Stream maintains a list of zero or more sets of bytes it has received //Each Stream maintains a list of zero or more sets of bytes it has received
//out-of-order. For example, if it has processed up through sequence number //out-of-order. For example, if it has processed up through sequence number
//10, it might have bytes [15-20), [20-25), [30,50) in its list. Each set of //10, it might have bytes [15-20), [20-25), [30,50) in its list. Each set of
//bytes also has the timestamp it was originally viewed. A flush call will //bytes also has the timestamp it was originally viewed. A flush call will
//look at the smallest subsequent set of bytes, in this case [15-20), and if //look at the smallest subsequent set of bytes, in this case [15-20), and if
//its timestamp is older than the passed-in time, it will push it and all //its timestamp is older than the passed-in time, it will push it and all
//contiguous byte-sets out to the Stream's Reassembled function. In this case, //contiguous byte-sets out to the Stream's Reassembled function. In this case,
//it will push [15-20), but also [20-25), since that's contiguous. It will //it will push [15-20), but also [20-25), since that's contiguous. It will
//only push [30-50) if its timestamp is also older than the passed-in time, //only push [30-50) if its timestamp is also older than the passed-in time,
//otherwise it will wait until the next FlushOlderThan to see if bytes [25-30) //otherwise it will wait until the next FlushOlderThan to see if bytes [25-30)
//come in. //come in.
// //
//If it pushes all bytes (or there were no sets of bytes to begin with) AND the //If it pushes all bytes (or there were no sets of bytes to begin with) AND the
//connection has not received any bytes since the passed-in time, the //connection has not received any bytes since the passed-in time, the
//connection will be closed. //connection will be closed.
// //
//Returns the number of connections flushed, and of those, the number closed //Returns the number of connections flushed, and of those, the number closed
//because of the flush. //because of the flush.
func (a *Assembler) FlushOlderThan(t time.Time) (flushed, closed int) { func (a *Assembler) FlushOlderThan(t time.Time) (flushed, closed int) {
conns := a.connPool.connections() conns := a.connPool.connections()
closes := 0 closes := 0

View File

@ -1,11 +1,11 @@
package core package core
import ( import (
"os"
"strings"
"fmt" "fmt"
"net" "net"
"os"
"strconv" "strconv"
"strings"
) )
const InternalCmdPrefix = "--" const InternalCmdPrefix = "--"
@ -25,7 +25,7 @@ type Cmd struct {
func NewCmd(p *Plug) *Cmd { func NewCmd(p *Plug) *Cmd {
return &Cmd{ return &Cmd{
plugHandle:p, plugHandle: p,
} }
} }
@ -59,7 +59,7 @@ func (cm *Cmd) parseInternalCmd() {
cm.printHelpMessage() cm.printHelpMessage()
break break
case InternalCmdEnv: case InternalCmdEnv:
fmt.Println("External plug-in path : "+cm.plugHandle.dir) fmt.Println("External plug-in path : " + cm.plugHandle.dir)
break break
case InternalCmdList: case InternalCmdList:
cm.plugHandle.PrintList() cm.plugHandle.PrintList()
@ -104,21 +104,21 @@ func (cm *Cmd) printHelpMessage() {
func (cm *Cmd) printPlugList() { func (cm *Cmd) printPlugList() {
l := len(cm.plugHandle.InternalPlugList) l := len(cm.plugHandle.InternalPlugList)
l += len(cm.plugHandle.ExternalPlugList) l += len(cm.plugHandle.ExternalPlugList)
fmt.Println("# Number of plug-ins : "+strconv.Itoa(l)) fmt.Println("# Number of plug-ins : " + strconv.Itoa(l))
} }
//print device //print device
func (cm *Cmd) printDevice() { func (cm *Cmd) printDevice() {
ifaces, err:= net.Interfaces() ifaces, err := net.Interfaces()
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, iface := range ifaces { for _, iface := range ifaces {
addrs, _ := iface.Addrs() addrs, _ := iface.Addrs()
for _,a:=range addrs { for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok { if ipnet, ok := a.(*net.IPNet); ok {
if ip4 := ipnet.IP.To4(); ip4 != nil { if ip4 := ipnet.IP.To4(); ip4 != nil {
fmt.Println("[device] : "+iface.Name+" : "+iface.HardwareAddr.String()+" "+ip4.String()) fmt.Println("[device] : " + iface.Name + " : " + iface.HardwareAddr.String() + " " + ip4.String())
} }
} }
} }
@ -136,10 +136,6 @@ func (cm *Cmd) parsePlugCmd() {
cm.Device = os.Args[1] cm.Device = os.Args[1]
plugName := os.Args[2] plugName := os.Args[2]
plugParams:= os.Args[3:] plugParams := os.Args[3:]
cm.plugHandle.SetOption(plugName, plugParams) cm.plugHandle.SetOption(plugName, plugParams)
} }

View File

@ -1,6 +1,6 @@
package core package core
type Core struct{ type Core struct {
Version string Version string
} }

View File

@ -18,9 +18,9 @@ type Dispatch struct {
} }
func NewDispatch(plug *Plug, cmd *Cmd) *Dispatch { func NewDispatch(plug *Plug, cmd *Cmd) *Dispatch {
return &Dispatch { return &Dispatch{
Plug: plug, Plug: plug,
device:cmd.Device, device: cmd.Device,
} }
} }
@ -46,7 +46,7 @@ func (d *Dispatch) Capture() {
//set up assembly //set up assembly
streamFactory := &ProtocolStreamFactory{ streamFactory := &ProtocolStreamFactory{
dispatch:d, dispatch: d,
} }
streamPool := NewStreamPool(streamFactory) streamPool := NewStreamPool(streamFactory)
assembler := NewAssembler(streamPool) assembler := NewAssembler(streamPool)
@ -85,7 +85,7 @@ type ProtocolStream struct {
func (m *ProtocolStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream { func (m *ProtocolStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream {
//init stream struct //init stream struct
stm := &ProtocolStream { stm := &ProtocolStream{
net: net, net: net,
transport: transport, transport: transport,
r: tcpreader.NewReaderStream(), r: tcpreader.NewReaderStream(),

View File

@ -1,21 +1,20 @@
package core package core
import ( import (
"io/ioutil" "fmt"
"plugin"
"github.com/google/gopacket"
"io"
mysql "github.com/40t/go-sniffer/plugSrc/mysql/build"
redis "github.com/40t/go-sniffer/plugSrc/redis/build"
hp "github.com/40t/go-sniffer/plugSrc/http/build" hp "github.com/40t/go-sniffer/plugSrc/http/build"
mongodb "github.com/40t/go-sniffer/plugSrc/mongodb/build" mongodb "github.com/40t/go-sniffer/plugSrc/mongodb/build"
"path/filepath" mysql "github.com/40t/go-sniffer/plugSrc/mysql/build"
"fmt" redis "github.com/40t/go-sniffer/plugSrc/redis/build"
"github.com/google/gopacket"
"io"
"io/ioutil"
"path" "path"
"path/filepath"
"plugin"
) )
type Plug struct { type Plug struct {
dir string dir string
ResolveStream func(net gopacket.Flow, transport gopacket.Flow, r io.Reader) ResolveStream func(net gopacket.Flow, transport gopacket.Flow, r io.Reader)
BPF string BPF string
@ -48,7 +47,7 @@ func NewPlug() *Plug {
var p Plug var p Plug
p.dir, _ = filepath.Abs( "./plug/") p.dir, _ = filepath.Abs("./plug/")
p.LoadInternalPlugList() p.LoadInternalPlugList()
p.LoadExternalPlugList() p.LoadExternalPlugList()
@ -87,7 +86,7 @@ func (p *Plug) LoadExternalPlugList() {
continue continue
} }
plug, err := plugin.Open(p.dir+"/"+fi.Name()) plug, err := plugin.Open(p.dir + "/" + fi.Name())
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -113,12 +112,12 @@ func (p *Plug) LoadExternalPlugList() {
} }
version := versionFunc.(func() string)() version := versionFunc.(func() string)()
p.ExternalPlugList[fi.Name()] = ExternalPlug { p.ExternalPlugList[fi.Name()] = ExternalPlug{
ResolvePacket:ResolvePacketFunc.(func(net gopacket.Flow, transport gopacket.Flow, r io.Reader)), ResolvePacket: ResolvePacketFunc.(func(net gopacket.Flow, transport gopacket.Flow, r io.Reader)),
SetFlag:setFlagFunc.(func([]string)), SetFlag: setFlagFunc.(func([]string)),
BPFFilter:BPFFilterFunc.(func() string), BPFFilter: BPFFilterFunc.(func() string),
Version:version, Version: version,
Name:fi.Name(), Name: fi.Name(),
} }
} }
} }
@ -131,7 +130,7 @@ func (p *Plug) PrintList() {
//Print Internal Plug //Print Internal Plug
for inPlugName, _ := range p.InternalPlugList { for inPlugName, _ := range p.InternalPlugList {
fmt.Println("internal plug : "+inPlugName) fmt.Println("internal plug : " + inPlugName)
} }
//split //split
@ -139,7 +138,7 @@ func (p *Plug) PrintList() {
//print External Plug //print External Plug
for exPlugName, _ := range p.ExternalPlugList { for exPlugName, _ := range p.ExternalPlugList {
fmt.Println("external plug : "+exPlugName) fmt.Println("external plug : " + exPlugName)
} }
} }
@ -156,7 +155,7 @@ func (p *Plug) SetOption(plugName string, plugParams []string) {
} }
//Load External Plug //Load External Plug
plug, err := plugin.Open("./plug/"+ plugName) plug, err := plugin.Open("./plug/" + plugName)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -174,5 +173,5 @@ func (p *Plug) SetOption(plugName string, plugParams []string) {
} }
p.ResolveStream = resolvePacket.(func(net gopacket.Flow, transport gopacket.Flow, r io.Reader)) p.ResolveStream = resolvePacket.(func(net gopacket.Flow, transport gopacket.Flow, r io.Reader))
setFlag.(func([]string))(plugParams) setFlag.(func([]string))(plugParams)
p.BPF = BPFFilter.(func()string)() p.BPF = BPFFilter.(func() string)()
} }

View File

@ -1,14 +1,14 @@
package build package build
import ( import (
"bufio"
"fmt"
"github.com/google/gopacket" "github.com/google/gopacket"
"io" "io"
"log" "log"
"strconv"
"fmt"
"os"
"bufio"
"net/http" "net/http"
"os"
"strconv"
) )
const ( const (
@ -30,8 +30,8 @@ var hp *H
func NewInstance() *H { func NewInstance() *H {
if hp == nil { if hp == nil {
hp = &H{ hp = &H{
port :Port, port: Port,
version:Version, version: Version,
} }
} }
return hp return hp
@ -66,7 +66,7 @@ func (m *H) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
} }
func (m *H) BPFFilter() string { func (m *H) BPFFilter() string {
return "tcp and port "+strconv.Itoa(m.port); return "tcp and port " + strconv.Itoa(m.port)
} }
func (m *H) Version() string { func (m *H) Version() string {
@ -80,17 +80,17 @@ func (m *H) SetFlag(flg []string) {
if c == 0 { if c == 0 {
return return
} }
if c >> 1 == 0 { if c>>1 == 0 {
fmt.Println("ERR : Http Number of parameters") fmt.Println("ERR : Http Number of parameters")
os.Exit(1) os.Exit(1)
} }
for i:=0;i<c;i=i+2 { for i := 0; i < c; i = i + 2 {
key := flg[i] key := flg[i]
val := flg[i+1] val := flg[i+1]
switch key { switch key {
case CmdPort: case CmdPort:
port, err := strconv.Atoi(val); port, err := strconv.Atoi(val)
m.port = port m.port = port
if err != nil { if err != nil {
panic("ERR : port") panic("ERR : port")

View File

@ -26,7 +26,6 @@ type stream struct {
} }
type packet struct { type packet struct {
isClientFlow bool //client->server isClientFlow bool //client->server
messageLength int messageLength int
@ -42,8 +41,8 @@ var mongodbInstance *Mongodb
func NewInstance() *Mongodb { func NewInstance() *Mongodb {
if mongodbInstance == nil { if mongodbInstance == nil {
mongodbInstance = &Mongodb{ mongodbInstance = &Mongodb{
port :Port, port: Port,
version:Version, version: Version,
source: make(map[string]*stream), source: make(map[string]*stream),
} }
} }
@ -55,16 +54,16 @@ func (m *Mongodb) SetFlag(flg []string) {
if c == 0 { if c == 0 {
return return
} }
if c >> 1 != 1 { if c>>1 != 1 {
panic("ERR : Mongodb Number of parameters") panic("ERR : Mongodb Number of parameters")
} }
for i:=0;i<c;i=i+2 { for i := 0; i < c; i = i + 2 {
key := flg[i] key := flg[i]
val := flg[i+1] val := flg[i+1]
switch key { switch key {
case CmdPort: case CmdPort:
p, err := strconv.Atoi(val); p, err := strconv.Atoi(val)
if err != nil { if err != nil {
panic("ERR : port") panic("ERR : port")
} }
@ -80,7 +79,7 @@ func (m *Mongodb) SetFlag(flg []string) {
} }
func (m *Mongodb) BPFFilter() string { func (m *Mongodb) BPFFilter() string {
return "tcp and port "+strconv.Itoa(m.port); return "tcp and port " + strconv.Itoa(m.port)
} }
func (m *Mongodb) Version() string { func (m *Mongodb) Version() string {
@ -95,8 +94,8 @@ func (m *Mongodb) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
//resolve packet //resolve packet
if _, ok := m.source[uuid]; !ok { if _, ok := m.source[uuid]; !ok {
var newStream = stream { var newStream = stream{
packets:make(chan *packet, 100), packets: make(chan *packet, 100),
} }
m.source[uuid] = &newStream m.source[uuid] = &newStream
@ -135,7 +134,7 @@ func (m *Mongodb) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
//set flow direction //set flow direction
if transport.Src().String() == strconv.Itoa(m.port) { if transport.Src().String() == strconv.Itoa(m.port) {
packet.isClientFlow = false packet.isClientFlow = false
}else{ } else {
packet.isClientFlow = true packet.isClientFlow = true
} }
@ -145,7 +144,7 @@ func (m *Mongodb) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
func (stm *stream) resolve() { func (stm *stream) resolve() {
for { for {
select { select {
case packet := <- stm.packets: case packet := <-stm.packets:
if packet.isClientFlow { if packet.isClientFlow {
stm.resolveClientPacket(packet) stm.resolveClientPacket(packet)
} else { } else {
@ -266,7 +265,7 @@ func readStream(r io.Reader) (*packet, error) {
//header //header
header := make([]byte, 16) header := make([]byte, 16)
if _, err := io.ReadFull(r, header); err != nil { if _, err := io.ReadFull(r, header); err != nil {
return nil,err return nil, err
} }
// message length // message length

View File

@ -773,7 +773,7 @@ func (d *decodeState) isNull(off int) bool {
// name consumes a const or function from d.data[d.off-1:], decoding into the value v. // name consumes a const or function from d.data[d.off-1:], decoding into the value v.
// the first byte of the function name has been read already. // the first byte of the function name has been read already.
func (d *decodeState) name(v reflect.Value) { func (d *decodeState) name(v reflect.Value) {
if d.isNull(d.off-1) { if d.isNull(d.off - 1) {
d.literal(v) d.literal(v)
return return
} }

View File

@ -4,9 +4,9 @@ import (
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
"fmt" "fmt"
"time"
"io"
"github.com/40t/go-sniffer/plugSrc/mongodb/build/bson" "github.com/40t/go-sniffer/plugSrc/mongodb/build/bson"
"io"
"time"
) )
func GetNowStr(isClient bool) string { func GetNowStr(isClient bool) string {
@ -15,7 +15,7 @@ func GetNowStr(isClient bool) string {
msg += time.Now().Format(layout) msg += time.Now().Format(layout)
if isClient { if isClient {
msg += "| cli -> ser |" msg += "| cli -> ser |"
}else{ } else {
msg += "| ser -> cli |" msg += "| ser -> cli |"
} }
return msg return msg
@ -54,7 +54,7 @@ func ReadString(r io.Reader) string {
return string(result) return string(result)
} }
func ReadBson2Json(r io.Reader) (string) { func ReadBson2Json(r io.Reader) string {
//read len //read len
docLen := ReadInt32(r) docLen := ReadInt32(r)
@ -83,4 +83,3 @@ func ReadBson2Json(r io.Reader) (string) {
} }
return string(jsonStr) return string(jsonStr)
} }

View File

@ -1,18 +1,18 @@
package build package build
import ( import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/google/gopacket" "github.com/google/gopacket"
"io" "io"
"bytes"
"errors"
"log" "log"
"os"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
"fmt"
"encoding/binary"
"strings"
"os"
) )
const ( const (
@ -41,12 +41,13 @@ type packet struct {
var mysql *Mysql var mysql *Mysql
var once sync.Once var once sync.Once
func NewInstance() *Mysql { func NewInstance() *Mysql {
once.Do(func() { once.Do(func() {
mysql = &Mysql{ mysql = &Mysql{
port :Port, port: Port,
version:Version, version: Version,
source: make(map[string]*stream), source: make(map[string]*stream),
} }
}) })
@ -63,8 +64,8 @@ func (m *Mysql) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
if _, ok := m.source[uuid]; !ok { if _, ok := m.source[uuid]; !ok {
var newStream = stream{ var newStream = stream{
packets:make(chan *packet, 100), packets: make(chan *packet, 100),
stmtMap:make(map[uint32]*Stmt), stmtMap: make(map[uint32]*Stmt),
} }
m.source[uuid] = &newStream m.source[uuid] = &newStream
@ -86,7 +87,7 @@ func (m *Mysql) ResolveStream(net, transport gopacket.Flow, buf io.Reader) {
} }
func (m *Mysql) BPFFilter() string { func (m *Mysql) BPFFilter() string {
return "tcp and port "+strconv.Itoa(m.port); return "tcp and port " + strconv.Itoa(m.port)
} }
func (m *Mysql) Version() string { func (m *Mysql) Version() string {
@ -100,17 +101,17 @@ func (m *Mysql) SetFlag(flg []string) {
if c == 0 { if c == 0 {
return return
} }
if c >> 1 == 0 { if c>>1 == 0 {
fmt.Println("ERR : Mysql Number of parameters") fmt.Println("ERR : Mysql Number of parameters")
os.Exit(1) os.Exit(1)
} }
for i:=0;i<c;i=i+2 { for i := 0; i < c; i = i + 2 {
key := flg[i] key := flg[i]
val := flg[i+1] val := flg[i+1]
switch key { switch key {
case CmdPort: case CmdPort:
port, err := strconv.Atoi(val); port, err := strconv.Atoi(val)
m.port = port m.port = port
if err != nil { if err != nil {
panic("ERR : port") panic("ERR : port")
@ -146,12 +147,12 @@ func (m *Mysql) newPacket(net, transport gopacket.Flow, r io.Reader) *packet {
//generate new packet //generate new packet
var pk = packet{ var pk = packet{
seq: int(seq), seq: int(seq),
length:payload.Len(), length: payload.Len(),
payload:payload.Bytes(), payload: payload.Bytes(),
} }
if transport.Src().String() == strconv.Itoa(Port) { if transport.Src().String() == strconv.Itoa(Port) {
pk.isClientFlow = false pk.isClientFlow = false
}else{ } else {
pk.isClientFlow = true pk.isClientFlow = true
} }
@ -187,7 +188,7 @@ func (m *Mysql) resolvePacketTo(r io.Reader, w io.Writer) (uint8, error) {
func (stm *stream) resolve() { func (stm *stream) resolve() {
for { for {
select { select {
case packet := <- stm.packets: case packet := <-stm.packets:
if packet.isClientFlow { if packet.isClientFlow {
stm.resolveClientPacket(packet.payload, packet.seq) stm.resolveClientPacket(packet.payload, packet.seq)
} else { } else {
@ -197,10 +198,10 @@ func (stm *stream) resolve() {
} }
} }
func (stm *stream) findStmtPacket (srv chan *packet, seq int) *packet { func (stm *stream) findStmtPacket(srv chan *packet, seq int) *packet {
for { for {
select { select {
case packet, ok := <- stm.packets: case packet, ok := <-stm.packets:
if !ok { if !ok {
return nil return nil
} }
@ -220,17 +221,17 @@ func (stm *stream) resolveServerPacket(payload []byte, seq int) {
case 0xff: case 0xff:
errorCode := int(binary.LittleEndian.Uint16(payload[1:3])) errorCode := int(binary.LittleEndian.Uint16(payload[1:3]))
errorMsg,_ := ReadStringFromByte(payload[4:]) errorMsg, _ := ReadStringFromByte(payload[4:])
msg = GetNowStr(false)+"%s Err code:%s,Err msg:%s" msg = GetNowStr(false) + "%s Err code:%s,Err msg:%s"
msg = fmt.Sprintf(msg, ErrorPacket, strconv.Itoa(errorCode), strings.TrimSpace(errorMsg)) msg = fmt.Sprintf(msg, ErrorPacket, strconv.Itoa(errorCode), strings.TrimSpace(errorMsg))
case 0x00: case 0x00:
var pos = 1 var pos = 1
l,_ := LengthBinary(payload[pos:]) l, _ := LengthBinary(payload[pos:])
affectedRows := int(l) affectedRows := int(l)
msg += GetNowStr(false)+"%s Effect Row:%s" msg += GetNowStr(false) + "%s Effect Row:%s"
msg = fmt.Sprintf(msg, OkPacket, strconv.Itoa(affectedRows)) msg = fmt.Sprintf(msg, OkPacket, strconv.Itoa(affectedRows))
default: default:
@ -276,7 +277,7 @@ func (stm *stream) resolveClientPacket(payload []byte, seq int) {
stmt.ParamCount = binary.LittleEndian.Uint16(serverPacket.payload[7:9]) stmt.ParamCount = binary.LittleEndian.Uint16(serverPacket.payload[7:9])
stmt.Args = make([]interface{}, stmt.ParamCount) stmt.Args = make([]interface{}, stmt.ParamCount)
msg = PreparePacket+stmt.Query msg = PreparePacket + stmt.Query
case COM_STMT_SEND_LONG_DATA: case COM_STMT_SEND_LONG_DATA:
stmtID := binary.LittleEndian.Uint32(payload[1:5]) stmtID := binary.LittleEndian.Uint32(payload[1:5])
@ -295,7 +296,7 @@ func (stm *stream) resolveClientPacket(payload []byte, seq int) {
case COM_STMT_RESET: case COM_STMT_RESET:
stmtID := binary.LittleEndian.Uint32(payload[1:5]) stmtID := binary.LittleEndian.Uint32(payload[1:5])
stmt, _:= stm.stmtMap[stmtID] stmt, _ := stm.stmtMap[stmtID]
stmt.Args = make([]interface{}, stmt.ParamCount) stmt.Args = make([]interface{}, stmt.ParamCount)
return return
case COM_STMT_EXECUTE: case COM_STMT_EXECUTE:
@ -348,4 +349,3 @@ func (stm *stream) resolveClientPacket(payload []byte, seq int) {
fmt.Println(GetNowStr(true) + msg) fmt.Println(GetNowStr(true) + msg)
} }

View File

@ -3,10 +3,10 @@ package build
import ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"errors"
"fmt" "fmt"
"math" "math"
"strings" "strings"
"errors"
) )
type Stmt struct { type Stmt struct {

View File

@ -12,13 +12,13 @@ func GetNowStr(isClient bool) string {
msg += time.Now().Format("2006-01-02 15:04:05") msg += time.Now().Format("2006-01-02 15:04:05")
if isClient { if isClient {
msg += "| cli -> ser |" msg += "| cli -> ser |"
}else{ } else {
msg += "| ser -> cli |" msg += "| ser -> cli |"
} }
return msg return msg
} }
func ReadStringFromByte(b []byte) (string,int) { func ReadStringFromByte(b []byte) (string, int) {
var l int var l int
l = bytes.IndexByte(b, 0x00) l = bytes.IndexByte(b, 0x00)
@ -35,18 +35,18 @@ func LengthBinary(b []byte) (uint32, int) {
return uint32(first), 1 return uint32(first), 1
} }
if first == 251 { if first == 251 {
return 0,1 return 0, 1
} }
if first == 252 { if first == 252 {
return binary.LittleEndian.Uint32(b[1:2]),1 return binary.LittleEndian.Uint32(b[1:2]), 1
} }
if first == 253 { if first == 253 {
return binary.LittleEndian.Uint32(b[1:4]),3 return binary.LittleEndian.Uint32(b[1:4]), 3
} }
if first == 254 { if first == 254 {
return binary.LittleEndian.Uint32(b[1:9]),8 return binary.LittleEndian.Uint32(b[1:9]), 8
} }
return 0,0 return 0, 0
} }
func LengthEncodedInt(input []byte) (num uint64, isNull bool, n int) { func LengthEncodedInt(input []byte) (num uint64, isNull bool, n int) {

View File

@ -1,12 +1,12 @@
package build package build
import ( import (
"bufio"
"fmt"
"github.com/google/gopacket" "github.com/google/gopacket"
"io" "io"
"strings"
"fmt"
"strconv" "strconv"
"bufio" "strings"
) )
type Redis struct { type Redis struct {
@ -22,12 +22,12 @@ const (
CmdPort string = "-p" CmdPort string = "-p"
) )
var redis = &Redis { var redis = &Redis{
port:Port, port: Port,
version:Version, version: Version,
} }
func NewInstance() *Redis{ func NewInstance() *Redis {
return redis return redis
} }
@ -63,9 +63,9 @@ func (red Redis) ResolveStream(net, transport gopacket.Flow, r io.Reader) {
l := string(line[1]) l := string(line[1])
cmdCount, _ = strconv.Atoi(l) cmdCount, _ = strconv.Atoi(l)
cmd = "" cmd = ""
for j := 0; j < cmdCount * 2; j++ { for j := 0; j < cmdCount*2; j++ {
c, _, _ := buf.ReadLine() c, _, _ := buf.ReadLine()
if j & 1 == 0 { if j&1 == 0 {
continue continue
} }
cmd += " " + string(c) cmd += " " + string(c)
@ -75,23 +75,23 @@ func (red Redis) ResolveStream(net, transport gopacket.Flow, r io.Reader) {
} }
/** /**
SetOption SetOption
*/ */
func (red *Redis) SetFlag(flg []string) { func (red *Redis) SetFlag(flg []string) {
c := len(flg) c := len(flg)
if c == 0 { if c == 0 {
return return
} }
if c >> 1 != 1 { if c>>1 != 1 {
panic("ERR : Redis num of params") panic("ERR : Redis num of params")
} }
for i:=0;i<c;i=i+2 { for i := 0; i < c; i = i + 2 {
key := flg[i] key := flg[i]
val := flg[i+1] val := flg[i+1]
switch key { switch key {
case CmdPort: case CmdPort:
port, err := strconv.Atoi(val); port, err := strconv.Atoi(val)
redis.port = port redis.port = port
if err != nil { if err != nil {
panic("ERR : Port error") panic("ERR : Port error")
@ -107,16 +107,15 @@ func (red *Redis) SetFlag(flg []string) {
} }
/** /**
BPFFilter BPFFilter
*/ */
func (red *Redis) BPFFilter() string { func (red *Redis) BPFFilter() string {
return "tcp and port "+strconv.Itoa(redis.port) return "tcp and port " + strconv.Itoa(redis.port)
} }
/** /**
Version Version
*/ */
func (red *Redis) Version() string { func (red *Redis) Version() string {
return red.version return red.version
} }