forked from anastop/latte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesspresso.go
146 lines (124 loc) · 3.71 KB
/
esspresso.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"os/exec"
"flag"
"fmt"
"io/ioutil"
"net/http"
"log"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
type Metric struct {
Rate int
CurrentValue int
PrevValue int
}
func getHandleFunc(metric *int, metricDescription string) func(w http.ResponseWriter, r *http.Request){
return func(w http.ResponseWriter, r *http.Request) {
if _, err := w.Write([]byte(fmt.Sprintf("%d\n", *metric))); err != nil {
log.Fatal("Can't write ", metricDescription)
}
}
}
func createPacketSource(device string, filter string) (*gopacket.PacketSource, *pcap.Handle) {
var (
snapshotLen int32 = 1024
promiscuous bool = true
timeout time.Duration = 100 * time.Millisecond
err error
handler *pcap.Handle
packetSource *gopacket.PacketSource
)
handler, err = pcap.OpenLive(device, snapshotLen, promiscuous, timeout)
if err != nil {
log.Fatal(err)
}
if err := handler.SetBPFFilter(filter); err != nil {
log.Fatal(err)
}
packetSource = gopacket.NewPacketSource(handler, handler.LinkType())
return packetSource, handler
}
// main
func main() {
// cmd line options
var (
device = flag.String("device", "docker0", "device to sniff packets from")
clientIP = flag.String("client-ip", "0.0.0.0", "The client container IP")
gorbIP = flag.String("gorb-ip", "0.0.0.0", "The IP gorb listens to")
dolog = flag.Bool("log", true, "Enable logging")
)
flag.Parse()
fmt.Println("device: ", *device)
if !*dolog {
log.SetFlags(0)
log.SetOutput(ioutil.Discard)
}
requests := &Metric{
Rate: 0,
CurrentValue: 0,
PrevValue: 0,
}
responses := &Metric{
Rate: 0,
CurrentValue: 0,
PrevValue: 0,
}
ticker := time.NewTicker(1 * time.Second)
//requests2 := 0
go func() {
command := exec.Command("/bin/sleep", "100")
command.Start()
for {
std, err := command.Output()
time.Sleep(2 * time.Second)
if (err != nil) {
log.Fatal(err)
}
fmt.Println(std)
fmt.Println("foo")
}
//requests2 = os.Run("")
}()
// requestFilter := fmt.Sprintf("src %s and dst %s and (tcp[0xd] & tcp-syn) != 0", *clientIP, *gorbIP)
requestFilter := fmt.Sprintf("tcp and dst %s and tcp[(tcp[0xc] >> 4) << 2 : 4] = 0x47455420", *gorbIP)
fmt.Printf("Request filter: %s\n", requestFilter)
requestPacketSource, requestHandler := createPacketSource(*device, requestFilter)
defer requestHandler.Close()
responseFilter := fmt.Sprintf("dst %s and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)", *clientIP)
fmt.Printf("Response filter: %s\n", responseFilter)
responsePacketSource, responseHandler := createPacketSource(*device, responseFilter)
defer responseHandler.Close()
go func() {
http.HandleFunc("/request/count", getHandleFunc(&requests.CurrentValue, "request count"))
http.HandleFunc("/response/count", getHandleFunc(&responses.CurrentValue, "response count"))
http.HandleFunc("/request/rate", getHandleFunc(&requests.Rate, "request rate"))
http.HandleFunc("/response/rate", getHandleFunc(&responses.Rate, "response rate"))
log.Fatal(http.ListenAndServe(":6666", nil))
}()
for {
select {
case <-ticker.C:
go func() {
requests.Rate = requests.CurrentValue - requests.PrevValue
requests.PrevValue = requests.CurrentValue
responses.Rate = responses.CurrentValue - responses.PrevValue
responses.PrevValue = responses.CurrentValue
}()
case request := <-requestPacketSource.Packets():
go func() {
log.Print(request)
requests.CurrentValue += 1
log.Print("Requests: ", requests.CurrentValue)
}()
case response := <-responsePacketSource.Packets():
go func() {
log.Print(response)
responses.CurrentValue += 1
log.Print("Responses: ", responses.CurrentValue)
}()
}
}
}