-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhpfeeds.go
51 lines (41 loc) · 1.24 KB
/
hpfeeds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"time"
log "github.com/Sirupsen/logrus"
hpfeeds "github.com/fw42/go-hpfeeds"
)
type HpFeedsEvent struct {
Packet []byte `json:"packet"`
SourceIP string `json:"source_ip"`
SourcePort string `json:"source_port"`
DestIP string `json:"dest_ip"`
DestPort string `json:"dest_port"`
}
type HpFeedsConfig struct {
Host string `json:"host"`
Port int `json:"port"`
Channel string `json:"channel"`
Ident string `json:"ident"`
Secret string `json:"secret"`
Enabled bool `json:"enabled"`
}
func hpfeedsConnect(hpfeedsConfig *HpFeedsConfig, hpfeedsChannel chan []byte) {
backoff := 1
hp := hpfeeds.NewHpfeeds(hpfeedsConfig.Host, hpfeedsConfig.Port, hpfeedsConfig.Ident, hpfeedsConfig.Secret)
hp.Log = true
log.Infof("Connecting to hpfeed at %s:%d", hpfeedsConfig.Host, hpfeedsConfig.Port)
for {
err := hp.Connect()
if err == nil {
log.Info("Connected to Hpfeeds server.")
hp.Publish(hpfeedsConfig.Channel, hpfeedsChannel)
<-hp.Disconnected
log.Info("Lost connection to hpfeed.")
}
log.Infof("Reconnecting to hpfeed at %s:%d in %d seconds", hpfeedsConfig.Host, hpfeedsConfig.Port, backoff)
time.Sleep(time.Duration(backoff) * time.Second)
if backoff <= 20 {
backoff++
}
}
}