forked from electricbubble/gidevice
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
pcapd_test.go
75 lines (70 loc) · 1.59 KB
/
pcapd_test.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
package giDevice
import (
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"log"
"testing"
)
type NetworkInfo struct {
Mac string
IPv4 string
IPv6 string
}
func TestPacd(t *testing.T) {
setupLockdownSrv(t)
GetNetworkIP()
}
func GetNetworkIP() error {
mac, err := dev.GetValue("", "WiFiAddress")
if err != nil {
fmt.Println("not key")
}
macStr, _ := mac.(string)
info := NetworkInfo{}
info.Mac = macStr
resultBytes, err := dev.Pcap()
if err != nil {
return err
}
for {
select {
case data, ok := <-resultBytes:
if ok {
err = findIP(data, &info)
if err != nil {
return err
}
if info.Mac != "" && info.IPv6 != "" && info.IPv4 != "" {
return nil
}
}
log.Println(&info)
}
}
}
// findIP @link https://github.com/danielpaulus/go-ios/blob/main/ios/pcap/ipfinder.go
func findIP(p []byte, info *NetworkInfo) error {
packet := gopacket.NewPacket(p, layers.LayerTypeEthernet, gopacket.Default)
// Get the TCP layer from this packet
if tcpLayer := packet.Layer(layers.LayerTypeEthernet); tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.Ethernet)
if tcp.SrcMAC.String() == info.Mac {
if ipv4Layer := packet.Layer(layers.LayerTypeIPv4); ipv4Layer != nil {
ipv4, ok := ipv4Layer.(*layers.IPv4)
if ok {
info.IPv4 = ipv4.SrcIP.String()
log.Printf("ip4 found:%s", info.IPv4)
}
}
if ipv6Layer := packet.Layer(layers.LayerTypeIPv6); ipv6Layer != nil {
ipv6, ok := ipv6Layer.(*layers.IPv6)
if ok {
info.IPv6 = ipv6.SrcIP.String()
log.Printf("ip6 found:%s", info.IPv6)
}
}
}
}
return nil
}