-
Notifications
You must be signed in to change notification settings - Fork 69
/
pcapd.go
60 lines (54 loc) · 997 Bytes
/
pcapd.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
package giDevice
import (
"log"
"github.com/electricbubble/gidevice/pkg/libimobiledevice"
)
type pcapdClient struct {
stop chan struct{}
c *libimobiledevice.PcapdClient
}
func newPcapdClient(c *libimobiledevice.PcapdClient) *pcapdClient {
return &pcapdClient{
stop: make(chan struct{}),
c: c,
}
}
func (c *pcapdClient) Packet() <-chan []byte {
packetCh := make(chan []byte, 10)
go func() {
for {
select {
case <-c.stop:
return
default:
pkt, err := c.c.ReceivePacket()
if err != nil {
close(packetCh)
return
}
var payload []byte
_ = pkt.Unmarshal(&payload)
raw, err := c.c.GetPacket(payload)
if err != nil {
close(packetCh)
return
}
res, err := c.c.CreatePacket(raw)
if err != nil {
log.Println("failed to create packet")
return
}
packetCh <- res
}
}
}()
return packetCh
}
func (c *pcapdClient) Stop() {
select {
case <-c.stop:
default:
close(c.stop)
}
c.c.Close()
}