This repository has been archived by the owner on May 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from Ultraporing/with_mac_filter
Rewrote interface filtering for windows and split files into win,
- Loading branch information
Showing
4 changed files
with
155 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// +build linux darwin | ||
|
||
package client | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/regner/albionmarket-client/log" | ||
) | ||
|
||
// Gets the first physical interface based on filter results, ignoring all VM, Loopback and Tunnel interfaces | ||
func GetFirstPhysicalInterface() string { | ||
ifaces, err := net.Interfaces() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
return "" | ||
} | ||
|
||
for _, element := range ifaces { | ||
if element.Flags&net.FlagLoopback == 0 && element.Flags&net.FlagUp == 1 && isPhysicalInterface(element.HardwareAddr.String()) { | ||
return element.Name | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// Gets all physical interfaces based on filter results, ignoring all VM, Loopback and Tunnel interfaces. | ||
func GetAllPhysicalInterface() []string { | ||
ifaces, err := net.Interfaces() | ||
|
||
if err != nil { | ||
log.Fatal(err) | ||
return nil | ||
} | ||
|
||
var outInterfaces []string | ||
|
||
for _, element := range ifaces { | ||
if element.Flags&net.FlagLoopback == 0 && element.Flags&net.FlagUp == 1 && isPhysicalInterface(element.HardwareAddr.String()) { | ||
outInterfaces = append(outInterfaces, element.Name) | ||
} | ||
} | ||
|
||
return outInterfaces | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// +build windows | ||
|
||
package client | ||
|
||
import ( | ||
"os" | ||
"syscall" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
const ( | ||
IfOperStatusUp = 1 | ||
IF_TYPE_SOFTWARE_LOOPBACK = 24 | ||
) | ||
|
||
const hexDigit = "0123456789abcdef" | ||
|
||
func adapterAddresses() ([]*windows.IpAdapterAddresses, error) { | ||
var b []byte | ||
l := uint32(15000) // recommended initial size | ||
for { | ||
b = make([]byte, l) | ||
err := windows.GetAdaptersAddresses(syscall.AF_UNSPEC, windows.GAA_FLAG_INCLUDE_PREFIX, 0, (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])), &l) | ||
if err == nil { | ||
if l == 0 { | ||
return nil, nil | ||
} | ||
break | ||
} | ||
if err.(syscall.Errno) != syscall.ERROR_BUFFER_OVERFLOW { | ||
return nil, os.NewSyscallError("getadaptersaddresses", err) | ||
} | ||
if l <= uint32(len(b)) { | ||
return nil, os.NewSyscallError("getadaptersaddresses", err) | ||
} | ||
} | ||
var aas []*windows.IpAdapterAddresses | ||
for aa := (*windows.IpAdapterAddresses)(unsafe.Pointer(&b[0])); aa != nil; aa = aa.Next { | ||
aas = append(aas, aa) | ||
} | ||
return aas, nil | ||
} | ||
|
||
func bytePtrToString(p *uint8) string { | ||
a := (*[10000]uint8)(unsafe.Pointer(p)) | ||
i := 0 | ||
for a[i] != 0 { | ||
i++ | ||
} | ||
return string(a[:i]) | ||
} | ||
|
||
func physicalAddrToString(physAddr [8]byte) string { | ||
if len(physAddr) == 0 { | ||
return "" | ||
} | ||
buf := make([]byte, 0, len(physAddr)*3-1) | ||
for i, b := range physAddr { | ||
if i > 0 { | ||
buf = append(buf, ':') | ||
} | ||
buf = append(buf, hexDigit[b>>4]) | ||
buf = append(buf, hexDigit[b&0xF]) | ||
} | ||
return string(buf) | ||
} | ||
|
||
// Gets the first physical interface based on filter results, ignoring all VM, Loopback and Tunnel interfaces | ||
func GetFirstPhysicalInterface() string { | ||
aa, _ := adapterAddresses() | ||
|
||
for _, pa := range aa { | ||
mac := physicalAddrToString(pa.PhysicalAddress) | ||
name := "\\Device\\NPF_" + bytePtrToString(pa.AdapterName) | ||
var flags uint32 = pa.Flags | ||
|
||
if flags&uint32(IF_TYPE_SOFTWARE_LOOPBACK) == 0 && flags&uint32(IfOperStatusUp) == 1 && isPhysicalInterface(mac) { | ||
return name | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
// Gets all physical interfaces based on filter results, ignoring all VM, Loopback and Tunnel interfaces. | ||
func GetAllPhysicalInterface() []string { | ||
aa, _ := adapterAddresses() | ||
|
||
var outInterfaces []string | ||
|
||
for _, pa := range aa { | ||
mac := physicalAddrToString(pa.PhysicalAddress) | ||
name := "\\Device\\NPF_" + bytePtrToString(pa.AdapterName) | ||
var flags uint32 = pa.Flags | ||
|
||
if flags&uint32(IF_TYPE_SOFTWARE_LOOPBACK) == 0 && flags&uint32(IfOperStatusUp) == 1 && isPhysicalInterface(mac) { | ||
outInterfaces = append(outInterfaces, name) | ||
} | ||
} | ||
|
||
return outInterfaces | ||
} |