add communicator api

This commit is contained in:
hebo
2019-09-05 20:27:49 +08:00
parent d06ca1131e
commit c9f514feed
18 changed files with 1490 additions and 42 deletions

View File

@@ -1,16 +1,17 @@
package capture
import (
sd "github.com/zr-hebo/sniffer-agent/session-dealer"
log "github.com/sirupsen/logrus"
"sync"
sd "github.com/zr-hebo/sniffer-agent/session-dealer"
"math/rand"
"time"
)
var (
localIPAddr *string
sessionPool = make(map[string]sd.ConnSession)
sessionPoolLock sync.Mutex
// sessionPoolLock sync.Mutex
)
func init() {
@@ -21,4 +22,6 @@ func init() {
localIPAddr = &ipAddr
log.Infof("parsed local ip address:%s", *localIPAddr)
rand.Seed(time.Now().UnixNano())
}

View File

@@ -6,7 +6,9 @@ import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/zr-hebo/sniffer-agent/communicator"
"golang.org/x/net/bpf"
"math/rand"
"time"
"github.com/google/gopacket/pcapgo"
@@ -114,6 +116,20 @@ func (nc *networkCard) listenNormal() {
continue
}
// throw packets according to a certain probability
throwPacketRate := communicator.GetConfig(communicator.THROW_PACKET_RATE).(float64)
if throwPacketRate >= 1.0 {
time.Sleep(time.Second*3)
continue
} else if 0 < throwPacketRate && throwPacketRate < 1.0 {
// fall into throw range
rn := rand.Float64()
if rn <= throwPacketRate {
continue
}
}
packet := gopacket.NewPacket(data, layers.LayerTypeEthernet, gopacket.NoCopy)
m := packet.Metadata()
m.CaptureInfo = ci