Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Go Modules #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

44 changes: 22 additions & 22 deletions core/assembly.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,28 +191,28 @@ func (p *StreamPool) connections() []*connection {
return conns
}

//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
//and skip the data they're waiting for).
//
//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
//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
//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
//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
//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)
//come in.
//
//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 will be closed.
//
//Returns the number of connections flushed, and of those, the number closed
//because of the flush.
// 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
// and skip the data they're waiting for).
//
// 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
// 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
// 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
// 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
// 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)
// come in.
//
// 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 will be closed.
//
// Returns the number of connections flushed, and of those, the number closed
// because of the flush.
func (a *Assembler) FlushOlderThan(t time.Time) (flushed, closed int) {
conns := a.connPool.connections()
closes := 0
Expand Down
78 changes: 37 additions & 41 deletions core/cmd.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package core

import (
"os"
"strings"
"fmt"
"net"
"os"
"strconv"
"strings"
)

const InternalCmdPrefix = "--"
Expand All @@ -18,27 +18,27 @@ const (
)

type Cmd struct {
Device string
Device string
plugHandle *Plug
}

func NewCmd(p *Plug) *Cmd {

return &Cmd{
plugHandle:p,
plugHandle: p,
}
}

//start
// start
func (cm *Cmd) Run() {

//print help
// print help
if len(os.Args) <= 1 {
cm.printHelpMessage()
os.Exit(1)
}

//parse command
// parse command
firstArg := string(os.Args[1])
if strings.HasPrefix(firstArg, InternalCmdPrefix) {
cm.parseInternalCmd()
Expand All @@ -47,35 +47,35 @@ func (cm *Cmd) Run() {
}
}

//parse internal commend
//like --help, --env, --device
// parse internal commend
// like --help, --env, --device
func (cm *Cmd) parseInternalCmd() {

arg := string(os.Args[1])
cmd := strings.Trim(arg, InternalCmdPrefix)

switch cmd {
case InternalCmdHelp:
cm.printHelpMessage()
break
case InternalCmdEnv:
fmt.Println("External plug-in path : "+cm.plugHandle.dir)
break
case InternalCmdList:
cm.plugHandle.PrintList()
break
case InternalCmdVer:
fmt.Println(cxt.Version)
break
case InternalDevice:
cm.printDevice()
break
case InternalCmdHelp:
cm.printHelpMessage()
break
case InternalCmdEnv:
fmt.Println("External plug-in path : " + cm.plugHandle.dir)
break
case InternalCmdList:
cm.plugHandle.PrintList()
break
case InternalCmdVer:
fmt.Println(cxt.Version)
break
case InternalDevice:
cm.printDevice()
break
}
os.Exit(1)
}

//usage
func (cm *Cmd) printHelpMessage() {
// usage
func (cm *Cmd) printHelpMessage() {

fmt.Println("==================================================================================")
fmt.Println("[Usage]")
Expand All @@ -100,46 +100,42 @@ func (cm *Cmd) printHelpMessage() {
fmt.Println("==================================================================================")
}

//print plug-in list
// print plug-in list
func (cm *Cmd) printPlugList() {
l := len(cm.plugHandle.InternalPlugList)
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() {
ifaces, err:= net.Interfaces()
ifaces, err := net.Interfaces()
if err != nil {
panic(err)
}
for _, iface := range ifaces {
addrs, _ := iface.Addrs()
for _,a:=range addrs {
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok {
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())
}
}
}
}
}

//Parameters needed for plug-ins
func (cm *Cmd) parsePlugCmd() {
// Parameters needed for plug-ins
func (cm *Cmd) parsePlugCmd() {

if len(os.Args) < 3 {
fmt.Println("not found [Plug-in name]")
fmt.Println("go-sniffer [device] [plug] [plug's params(optional)]")
os.Exit(1)
}

cm.Device = os.Args[1]
plugName := os.Args[2]
plugParams:= os.Args[3:]
cm.Device = os.Args[1]
plugName := os.Args[2]
plugParams := os.Args[3:]
cm.plugHandle.SetOption(plugName, plugParams)
}




12 changes: 6 additions & 6 deletions core/core.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core

type Core struct{
type Core struct {
Version string
}

Expand All @@ -13,15 +13,15 @@ func New() Core {
return cxt
}

func (c *Core) Run() {
func (c *Core) Run() {

//new plugin
// new plugin
plug := NewPlug()

//parse commend
// parse commend
cmd := NewCmd(plug)
cmd.Run()

//dispatch
// dispatch
NewDispatch(plug, cmd).Capture()
}
}
43 changes: 22 additions & 21 deletions core/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,58 @@ package core

import (
"fmt"
"log"
"time"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
"github.com/google/gopacket/tcpassembly/tcpreader"
"log"
"time"
)

type Dispatch struct {
device string
device string
payload []byte
Plug *Plug
Plug *Plug
}

func NewDispatch(plug *Plug, cmd *Cmd) *Dispatch {
return &Dispatch {
Plug: plug,
device:cmd.Device,
return &Dispatch{
Plug: plug,
device: cmd.Device,
}
}

func (d *Dispatch) Capture() {

//init device
// init device
handle, err := pcap.OpenLive(d.device, 65535, false, pcap.BlockForever)
if err != nil {
log.Fatal(err)
return
}

//set filter
// set filter
fmt.Println(d.Plug.BPF)
err = handle.SetBPFFilter(d.Plug.BPF)
if err != nil {
log.Fatal(err)
}

//capture
src := gopacket.NewPacketSource(handle, handle.LinkType())
// capture
src := gopacket.NewPacketSource(handle, handle.LinkType())
packets := src.Packets()

//set up assembly
// set up assembly
streamFactory := &ProtocolStreamFactory{
dispatch:d,
dispatch: d,
}
streamPool := NewStreamPool(streamFactory)
assembler := NewAssembler(streamPool)
ticker := time.Tick(time.Minute)
assembler := NewAssembler(streamPool)
ticker := time.Tick(time.Minute)

//loop until ctrl+z
// loop until ctrl+z
for {
select {
case packet := <-packets:
Expand Down Expand Up @@ -84,18 +85,18 @@ type ProtocolStream struct {

func (m *ProtocolStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream {

//init stream struct
stm := &ProtocolStream {
// init stream struct
stm := &ProtocolStream{
net: net,
transport: transport,
r: tcpreader.NewReaderStream(),
}

//new stream
// new stream
fmt.Println("# Start new stream:", net, transport)

//decode packet
// decode packet
go m.dispatch.Plug.ResolveStream(net, transport, &(stm.r))

return &(stm.r)
}
}
Loading