-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcapture.go
69 lines (60 loc) · 1.49 KB
/
capture.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
package main
import (
"fmt"
"io"
"log"
"runtime/debug"
"time"
"github.com/cxfksword/httpcap/common"
"github.com/cxfksword/httpcap/config"
"github.com/cxfksword/httpcap/reader"
"github.com/cxfksword/httpcap/writer"
)
func startCapture() {
input := reader.NewRAWInput(config.Setting.InterfaceName, config.Setting.Port)
go CopyMulty(input)
for {
select {
// case <-stop:
// return
case <-time.After(1 * time.Second):
}
}
}
func CopyMulty(src reader.InputReader) (err error) {
// Don't exit on panic
defer func() {
if r := recover(); r != nil {
if _, ok := r.(error); !ok {
fmt.Printf("PANIC: pkg: %v %s \n", r, debug.Stack())
} else {
fmt.Printf("PANIC: pkg: %s \n", debug.Stack())
}
log.Fatal(r.(error))
}
}()
http := writer.NewHttpOutput("")
memcache := writer.NewMemcacheOutput("")
buf := make([]byte, 5*1024*1024)
for {
nr, raw, er := src.Read(buf)
if nr > 0 && len(buf) > nr {
common.Debug("[DEBUG]", raw.SrcAddr, ":", raw.SrcPort, "->", raw.DestAddr, ":", raw.DestPort, "size:", nr)
if config.Setting.Service == "memcache" {
memcache.Write(buf[0:nr], int(raw.SrcPort), int(raw.DestPort), raw.SrcAddr, raw.DestAddr, raw.Seq)
} else {
if !(config.Setting.Service != "" && config.Setting.Service != "http") {
http.Write(buf[0:nr], int(raw.SrcPort), int(raw.DestPort), raw.SrcAddr, raw.DestAddr, raw.Seq)
}
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return err
}