Files
Reims-Go/internal/packetbridge/adapters_windows.go
T

168 lines
4.9 KiB
Go

//go:build windows
package packetbridge
import (
"context"
"fmt"
"sort"
"syscall"
"unsafe"
"github.com/Nwflower/reims-go/internal/networkmsg"
"golang.org/x/sys/windows"
)
var (
iphlpapiDLL = windows.NewLazySystemDLL("iphlpapi.dll")
getIfTable2 = iphlpapiDLL.NewProc("GetIfTable2")
getIfEntry2 = iphlpapiDLL.NewProc("GetIfEntry2")
freeMibTable = iphlpapiDLL.NewProc("FreeMibTable")
)
const (
ifMaxStringSize = 256
ifMaxPhysAddrLen = 32
ifTypeIEEE80211 = 71
ndisPhysMedWirelessLan = 1
ndisPhysMedNative80211 = 9
ifOperStatusUp = 1
ifOperStatusNotPresent = 6
ifFlagHardware = 0x01
ifFlagConnectorPresent = 0x04
)
// mibIfRow2 matches Windows SDK MIB_IF_ROW2 (1352 bytes on amd64).
type mibIfRow2 struct {
InterfaceLuid uint64
InterfaceIndex uint32
InterfaceGuid windows.GUID
Alias [ifMaxStringSize + 1]uint16
Description [ifMaxStringSize + 1]uint16
PhysicalAddressLength uint32
PhysicalAddress [ifMaxPhysAddrLen]byte
PermanentPhysicalAddress [ifMaxPhysAddrLen]byte
Mtu uint32
Type uint32
TunnelType uint32
MediaType uint32
PhysicalMediumType uint32
AccessType uint32
DirectionType uint32
InterfaceAndOperStatusFlags byte
_ [3]byte
OperStatus uint32
AdminStatus uint32
MediaConnectState uint32
NetworkGuid windows.GUID
ConnectionType uint32
_ [4]byte
TransmitLinkSpeed uint64
ReceiveLinkSpeed uint64
InOctets uint64
InUcastPkts uint64
InNUcastPkts uint64
InDiscards uint64
InErrors uint64
InUnknownProtos uint64
InUcastOctets uint64
InMulticastOctets uint64
InBroadcastOctets uint64
OutOctets uint64
OutUcastPkts uint64
OutNUcastPkts uint64
OutDiscards uint64
OutErrors uint64
OutUcastOctets uint64
OutMulticastOctets uint64
OutBroadcastOctets uint64
OutQLen uint64
}
func isWirelessIfRow(row *mibIfRow2) bool {
return row.Type == ifTypeIEEE80211 ||
row.PhysicalMediumType == ndisPhysMedNative80211 ||
row.PhysicalMediumType == ndisPhysMedWirelessLan
}
func formatLinkSpeed(bitsPerSec uint64) string {
if bitsPerSec == 0 || bitsPerSec == ^uint64(0) {
return "0 Mbps"
}
speed := float64(bitsPerSec)
if speed >= 1e9 {
return fmt.Sprintf("%.0f Gbps", speed/1e9)
}
return fmt.Sprintf("%.0f Mbps", speed/1e6)
}
type mibIfTable2 struct {
NumEntries uint32
_ uint32
Table [1]mibIfRow2
}
// ListAdapters 直接通过 Win32 IP Helper API (GetIfTable2) 枚举硬件网卡,不启动 COM/WMI,不修改宿主配置。
func ListAdapters(ctx context.Context) ([]Adapter, error) {
if err := ctx.Err(); err != nil {
return nil, err
}
var table *mibIfTable2
code, _, _ := getIfTable2.Call(uintptr(unsafe.Pointer(&table)))
if code != 0 {
return nil, networkmsg.Error("wmi_query", syscall.Errno(code), "name", "GetIfTable2")
}
defer freeMibTable.Call(uintptr(unsafe.Pointer(table)))
rows := unsafe.Slice(&table.Table[0], table.NumEntries)
result := make([]Adapter, 0, 8)
for i := range rows {
if err := ctx.Err(); err != nil {
return nil, err
}
row := &rows[i]
if (row.InterfaceAndOperStatusFlags&ifFlagHardware) == 0 ||
(row.InterfaceAndOperStatusFlags&ifFlagConnectorPresent) == 0 ||
row.OperStatus == ifOperStatusNotPresent {
continue
}
if row.InterfaceIndex == 0 {
continue
}
guid := row.InterfaceGuid.String()
if guid == "" {
continue
}
status := "Disconnected"
if row.OperStatus == ifOperStatusUp {
status = "Up"
}
result = append(result, Adapter{
Index: int(row.InterfaceIndex),
GUID: guid,
Name: windows.UTF16ToString(row.Alias[:]),
Description: windows.UTF16ToString(row.Description[:]),
Status: status,
Speed: formatLinkSpeed(row.TransmitLinkSpeed),
Wireless: isWirelessIfRow(row),
})
}
sort.Slice(result, func(i, j int) bool { return result[i].Index < result[j].Index })
return result, ctx.Err()
}
// adapterIsWireless 通过 GetIfEntry2 单次系统调用判断指定接口是否为 WLAN 无线网卡。
func adapterIsWireless(index int) (bool, error) {
if index <= 0 {
return false, networkmsg.Error("adapter_missing", nil)
}
var row mibIfRow2
row.InterfaceIndex = uint32(index)
code, _, _ := getIfEntry2.Call(uintptr(unsafe.Pointer(&row)))
if code != 0 {
return false, networkmsg.Error("adapter_missing", syscall.Errno(code))
}
return isWirelessIfRow(&row), nil
}