|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + crand "crypto/rand" |
| 6 | + "encoding/binary" |
| 7 | + "fmt" |
| 8 | + "github.com/mostlygeek/arp" |
| 9 | + "log" |
| 10 | + "math/big" |
| 11 | + "net" |
| 12 | + "os/exec" |
| 13 | + "runtime" |
| 14 | + "strings" |
| 15 | +) |
| 16 | + |
| 17 | +func CryptoRandomNumber(max int64) int64 { |
| 18 | + n, err := crand.Int(crand.Reader, big.NewInt(max)) |
| 19 | + if err != nil { |
| 20 | + panic(fmt.Errorf("crypto number failed to read bytes %v", err)) |
| 21 | + } |
| 22 | + return n.Int64() |
| 23 | +} |
| 24 | + |
| 25 | +// RandomNum Generates a random number between the given min and max |
| 26 | +func RandomNum(min, max int) int { |
| 27 | + return int(CryptoRandomNumber(int64(max-min))) + min |
| 28 | +} |
| 29 | + |
| 30 | +func BinaryReader(reader *bytes.Reader, dests ...interface{}) error { |
| 31 | + for _, dest := range dests { |
| 32 | + err := binary.Read(reader, binary.BigEndian, dest) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + } |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func LookupMACAddr(ipAddr net.IP) (macAddr net.HardwareAddr, e error) { |
| 41 | + // check if ipAddr is local |
| 42 | + // find all devices |
| 43 | + devices, err := net.Interfaces() |
| 44 | + if err != nil { |
| 45 | + return nil, err |
| 46 | + } |
| 47 | + |
| 48 | + for _, device := range devices { |
| 49 | + //DEBUG |
| 50 | + //fmt.Printf("device: %s mac: %s\n", device.Name, device.HardwareAddr) |
| 51 | + addresses, _ := device.Addrs() |
| 52 | + for _, address := range addresses { |
| 53 | + //DEBUG |
| 54 | + //fmt.Printf("\taddress: %s\n", address) |
| 55 | + deviceIPAddr, _, _ := net.ParseCIDR(address.String()) |
| 56 | + if deviceIPAddr != nil { |
| 57 | + if ipAddr.Equal(deviceIPAddr) { |
| 58 | + // found IP locally, grab the interface and return it |
| 59 | + return device.HardwareAddr, nil |
| 60 | + } |
| 61 | + } else { |
| 62 | + deviceIPAddr = net.ParseIP(address.String()) |
| 63 | + if ipAddr.Equal(deviceIPAddr) { |
| 64 | + return device.HardwareAddr, nil |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + // give ipAddr was not found locally, so lets move on to ARP |
| 70 | + // ping first to force the OS to resolve mac address |
| 71 | + cmd := exec.Command("ping", "-c", "1", ipAddr.String()) |
| 72 | + err = cmd.Run() |
| 73 | + if err != nil { |
| 74 | + log.Printf("Failed to ping %s: %s", ipAddr, err) |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + mac := arp.Search(ipAddr.String()) |
| 78 | + if runtime.GOOS == "darwin" { |
| 79 | + mac = DarwinMACFormat(mac) |
| 80 | + } |
| 81 | + macParsed, err := net.ParseMAC(mac) |
| 82 | + if err != nil { |
| 83 | + fmt.Printf("Error parsing destination MAC: %s - %s\n", mac, err) |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return macParsed, nil |
| 87 | +} |
| 88 | + |
| 89 | +// DarwinMACFormat fixes the issue that macOS returns oddly formatted arp -a results |
| 90 | +func DarwinMACFormat(macString string) string { |
| 91 | + var group int |
| 92 | + var builder strings.Builder |
| 93 | + for i := 0; i < len(macString); i++ { |
| 94 | + r := macString[i] |
| 95 | + if r == ':' { |
| 96 | + for chars := group; chars < 2; chars++ { |
| 97 | + builder.WriteString("0") |
| 98 | + } |
| 99 | + builder.WriteString(macString[i-group : i]) |
| 100 | + builder.WriteString(":") |
| 101 | + group = 0 |
| 102 | + continue |
| 103 | + } |
| 104 | + group++ |
| 105 | + } |
| 106 | + for chars := group; chars < 2; chars++ { |
| 107 | + builder.WriteString("0") |
| 108 | + } |
| 109 | + builder.WriteString(macString[len(macString)-group:]) |
| 110 | + return builder.String() |
| 111 | +} |
0 commit comments